// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.costmanagement.generated;

import com.azure.core.util.BinaryData;
import com.azure.resourcemanager.costmanagement.fluent.models.ReportConfigDefinition;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.ReportConfigAggregation;
import com.azure.resourcemanager.costmanagement.models.ReportConfigDataset;
import com.azure.resourcemanager.costmanagement.models.ReportConfigDatasetConfiguration;
import com.azure.resourcemanager.costmanagement.models.ReportConfigFilter;
import com.azure.resourcemanager.costmanagement.models.ReportConfigGrouping;
import com.azure.resourcemanager.costmanagement.models.ReportConfigSorting;
import com.azure.resourcemanager.costmanagement.models.ReportConfigSortingType;
import com.azure.resourcemanager.costmanagement.models.ReportConfigTimePeriod;
import com.azure.resourcemanager.costmanagement.models.ReportGranularityType;
import com.azure.resourcemanager.costmanagement.models.ReportTimeframeType;
import com.azure.resourcemanager.costmanagement.models.ReportType;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;

public final class ReportConfigDefinitionTests {
    @org.junit.jupiter.api.Test
    public void testDeserialize() throws Exception {
        ReportConfigDefinition model =
            BinaryData
                .fromString(
                    "{\"type\":\"Usage\",\"timeframe\":\"Custom\",\"timePeriod\":{\"from\":\"2021-08-28T07:34:48Z\",\"to\":\"2021-08-27T18:55:51Z\"},\"dataSet\":{\"granularity\":\"Monthly\",\"configuration\":{\"columns\":[\"uertumk\",\"o\"]},\"aggregation\":{\"bjf\":{\"name\":\"whbmd\",\"function\":\"Sum\"}},\"grouping\":[{\"type\":\"Dimension\",\"name\":\"bmbexppbhtqqro\"},{\"type\":\"Dimension\",\"name\":\"p\"},{\"type\":\"Dimension\",\"name\":\"s\"}],\"sorting\":[{\"direction\":\"Ascending\",\"name\":\"uxig\"},{\"direction\":\"Descending\",\"name\":\"gzjaoyfhrtxilne\"}],\"filter\":{\"and\":[],\"or\":[]}},\"includeMonetaryCommitment\":false}")
                .toObject(ReportConfigDefinition.class);
        Assertions.assertEquals(ReportType.USAGE, model.type());
        Assertions.assertEquals(ReportTimeframeType.CUSTOM, model.timeframe());
        Assertions.assertEquals(OffsetDateTime.parse("2021-08-28T07:34:48Z"), model.timePeriod().from());
        Assertions.assertEquals(OffsetDateTime.parse("2021-08-27T18:55:51Z"), model.timePeriod().to());
        Assertions.assertEquals(ReportGranularityType.MONTHLY, model.dataSet().granularity());
        Assertions.assertEquals("uertumk", model.dataSet().configuration().columns().get(0));
        Assertions.assertEquals("whbmd", model.dataSet().aggregation().get("bjf").name());
        Assertions.assertEquals(FunctionType.SUM, model.dataSet().aggregation().get("bjf").function());
        Assertions.assertEquals(QueryColumnType.DIMENSION, model.dataSet().grouping().get(0).type());
        Assertions.assertEquals("bmbexppbhtqqro", model.dataSet().grouping().get(0).name());
        Assertions.assertEquals(ReportConfigSortingType.ASCENDING, model.dataSet().sorting().get(0).direction());
        Assertions.assertEquals("uxig", model.dataSet().sorting().get(0).name());
        Assertions.assertEquals(false, model.includeMonetaryCommitment());
    }

    @org.junit.jupiter.api.Test
    public void testSerialize() throws Exception {
        ReportConfigDefinition model =
            new ReportConfigDefinition()
                .withType(ReportType.USAGE)
                .withTimeframe(ReportTimeframeType.CUSTOM)
                .withTimePeriod(
                    new ReportConfigTimePeriod()
                        .withFrom(OffsetDateTime.parse("2021-08-28T07:34:48Z"))
                        .withTo(OffsetDateTime.parse("2021-08-27T18:55:51Z")))
                .withDataSet(
                    new ReportConfigDataset()
                        .withGranularity(ReportGranularityType.MONTHLY)
                        .withConfiguration(
                            new ReportConfigDatasetConfiguration().withColumns(Arrays.asList("uertumk", "o")))
                        .withAggregation(
                            mapOf(
                                "bjf", new ReportConfigAggregation().withName("whbmd").withFunction(FunctionType.SUM)))
                        .withGrouping(
                            Arrays
                                .asList(
                                    new ReportConfigGrouping()
                                        .withType(QueryColumnType.DIMENSION)
                                        .withName("bmbexppbhtqqro"),
                                    new ReportConfigGrouping().withType(QueryColumnType.DIMENSION).withName("p"),
                                    new ReportConfigGrouping().withType(QueryColumnType.DIMENSION).withName("s")))
                        .withSorting(
                            Arrays
                                .asList(
                                    new ReportConfigSorting()
                                        .withDirection(ReportConfigSortingType.ASCENDING)
                                        .withName("uxig"),
                                    new ReportConfigSorting()
                                        .withDirection(ReportConfigSortingType.DESCENDING)
                                        .withName("gzjaoyfhrtxilne")))
                        .withFilter(new ReportConfigFilter().withAnd(Arrays.asList()).withOr(Arrays.asList())))
                .withIncludeMonetaryCommitment(false);
        model = BinaryData.fromObject(model).toObject(ReportConfigDefinition.class);
        Assertions.assertEquals(ReportType.USAGE, model.type());
        Assertions.assertEquals(ReportTimeframeType.CUSTOM, model.timeframe());
        Assertions.assertEquals(OffsetDateTime.parse("2021-08-28T07:34:48Z"), model.timePeriod().from());
        Assertions.assertEquals(OffsetDateTime.parse("2021-08-27T18:55:51Z"), model.timePeriod().to());
        Assertions.assertEquals(ReportGranularityType.MONTHLY, model.dataSet().granularity());
        Assertions.assertEquals("uertumk", model.dataSet().configuration().columns().get(0));
        Assertions.assertEquals("whbmd", model.dataSet().aggregation().get("bjf").name());
        Assertions.assertEquals(FunctionType.SUM, model.dataSet().aggregation().get("bjf").function());
        Assertions.assertEquals(QueryColumnType.DIMENSION, model.dataSet().grouping().get(0).type());
        Assertions.assertEquals("bmbexppbhtqqro", model.dataSet().grouping().get(0).name());
        Assertions.assertEquals(ReportConfigSortingType.ASCENDING, model.dataSet().sorting().get(0).direction());
        Assertions.assertEquals("uxig", model.dataSet().sorting().get(0).name());
        Assertions.assertEquals(false, model.includeMonetaryCommitment());
    }

    @SuppressWarnings("unchecked")
    private static <T> Map<String, T> mapOf(Object... inputs) {
        Map<String, T> map = new HashMap<>();
        for (int i = 0; i < inputs.length; i += 2) {
            String key = (String) inputs[i];
            T value = (T) inputs[i + 1];
            map.put(key, value);
        }
        return map;
    }
}
