// 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.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDatasetConfiguration;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.QueryTimePeriod;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
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 QueryDefinitionTests {
    @org.junit.jupiter.api.Test
    public void testDeserialize() throws Exception {
        QueryDefinition model =
            BinaryData
                .fromString(
                    "{\"type\":\"Usage\",\"timeframe\":\"TheLastBillingMonth\",\"timePeriod\":{\"from\":\"2021-04-02T08:06:31Z\",\"to\":\"2021-08-30T17:38:27Z\"},\"dataset\":{\"granularity\":\"Daily\",\"configuration\":{\"columns\":[\"hdneuelfph\",\"dyhtozfikdowwquu\"]},\"aggregation\":{\"qzonosggbhcohf\":{\"name\":\"xclvit\",\"function\":\"Sum\"}},\"grouping\":[{\"type\":\"Dimension\",\"name\":\"n\"},{\"type\":\"Dimension\",\"name\":\"ljuti\"}],\"filter\":{\"and\":[],\"or\":[]}}}")
                .toObject(QueryDefinition.class);
        Assertions.assertEquals(ExportType.USAGE, model.type());
        Assertions.assertEquals(TimeframeType.THE_LAST_BILLING_MONTH, model.timeframe());
        Assertions.assertEquals(OffsetDateTime.parse("2021-04-02T08:06:31Z"), model.timePeriod().from());
        Assertions.assertEquals(OffsetDateTime.parse("2021-08-30T17:38:27Z"), model.timePeriod().to());
        Assertions.assertEquals(GranularityType.DAILY, model.dataset().granularity());
        Assertions.assertEquals("hdneuelfph", model.dataset().configuration().columns().get(0));
        Assertions.assertEquals("xclvit", model.dataset().aggregation().get("qzonosggbhcohf").name());
        Assertions.assertEquals(FunctionType.SUM, model.dataset().aggregation().get("qzonosggbhcohf").function());
        Assertions.assertEquals(QueryColumnType.DIMENSION, model.dataset().grouping().get(0).type());
        Assertions.assertEquals("n", model.dataset().grouping().get(0).name());
    }

    @org.junit.jupiter.api.Test
    public void testSerialize() throws Exception {
        QueryDefinition model =
            new QueryDefinition()
                .withType(ExportType.USAGE)
                .withTimeframe(TimeframeType.THE_LAST_BILLING_MONTH)
                .withTimePeriod(
                    new QueryTimePeriod()
                        .withFrom(OffsetDateTime.parse("2021-04-02T08:06:31Z"))
                        .withTo(OffsetDateTime.parse("2021-08-30T17:38:27Z")))
                .withDataset(
                    new QueryDataset()
                        .withGranularity(GranularityType.DAILY)
                        .withConfiguration(
                            new QueryDatasetConfiguration()
                                .withColumns(Arrays.asList("hdneuelfph", "dyhtozfikdowwquu")))
                        .withAggregation(
                            mapOf(
                                "qzonosggbhcohf",
                                new QueryAggregation().withName("xclvit").withFunction(FunctionType.SUM)))
                        .withGrouping(
                            Arrays
                                .asList(
                                    new QueryGrouping().withType(QueryColumnType.DIMENSION).withName("n"),
                                    new QueryGrouping().withType(QueryColumnType.DIMENSION).withName("ljuti")))
                        .withFilter(new QueryFilter().withAnd(Arrays.asList()).withOr(Arrays.asList())));
        model = BinaryData.fromObject(model).toObject(QueryDefinition.class);
        Assertions.assertEquals(ExportType.USAGE, model.type());
        Assertions.assertEquals(TimeframeType.THE_LAST_BILLING_MONTH, model.timeframe());
        Assertions.assertEquals(OffsetDateTime.parse("2021-04-02T08:06:31Z"), model.timePeriod().from());
        Assertions.assertEquals(OffsetDateTime.parse("2021-08-30T17:38:27Z"), model.timePeriod().to());
        Assertions.assertEquals(GranularityType.DAILY, model.dataset().granularity());
        Assertions.assertEquals("hdneuelfph", model.dataset().configuration().columns().get(0));
        Assertions.assertEquals("xclvit", model.dataset().aggregation().get("qzonosggbhcohf").name());
        Assertions.assertEquals(FunctionType.SUM, model.dataset().aggregation().get("qzonosggbhcohf").function());
        Assertions.assertEquals(QueryColumnType.DIMENSION, model.dataset().grouping().get(0).type());
        Assertions.assertEquals("n", model.dataset().grouping().get(0).name());
    }

    @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;
    }
}
