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

package com.azure.resourcemanager.servicefabric.generated;

import com.azure.core.util.BinaryData;
import com.azure.resourcemanager.servicefabric.models.ApplicationUpgradePolicy;
import com.azure.resourcemanager.servicefabric.models.ArmApplicationHealthPolicy;
import com.azure.resourcemanager.servicefabric.models.ArmRollingUpgradeMonitoringPolicy;
import com.azure.resourcemanager.servicefabric.models.ArmServiceTypeHealthPolicy;
import com.azure.resourcemanager.servicefabric.models.ArmUpgradeFailureAction;
import com.azure.resourcemanager.servicefabric.models.RollingUpgradeMode;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;

public final class ApplicationUpgradePolicyTests {
    @org.junit.jupiter.api.Test
    public void testDeserialize() throws Exception {
        ApplicationUpgradePolicy model =
            BinaryData
                .fromString(
                    "{\"upgradeReplicaSetCheckTimeout\":\"iplrbpbewtghfgb\",\"forceRestart\":false,\"rollingUpgradeMonitoringPolicy\":{\"failureAction\":\"Manual\",\"healthCheckWaitDuration\":\"lvqhjkbegibtnmx\",\"healthCheckStableDuration\":\"bwwaloa\",\"healthCheckRetryTimeout\":\"cgwrtzjuz\",\"upgradeTimeout\":\"yzm\",\"upgradeDomainTimeout\":\"xongmtsavjcbpwxq\"},\"applicationHealthPolicy\":{\"considerWarningAsError\":true,\"maxPercentUnhealthyDeployedApplications\":1220568259,\"defaultServiceTypeHealthPolicy\":{\"maxPercentUnhealthyServices\":487735132,\"maxPercentUnhealthyPartitionsPerService\":1935483255,\"maxPercentUnhealthyReplicasPerPartition\":987025721},\"serviceTypeHealthPolicyMap\":{\"xqtayriwwro\":{\"maxPercentUnhealthyServices\":499230067,\"maxPercentUnhealthyPartitionsPerService\":1059396624,\"maxPercentUnhealthyReplicasPerPartition\":1883973857},\"cqibycnojv\":{\"maxPercentUnhealthyServices\":1731999756,\"maxPercentUnhealthyPartitionsPerService\":1603221085,\"maxPercentUnhealthyReplicasPerPartition\":591654502},\"gzva\":{\"maxPercentUnhealthyServices\":2121055666,\"maxPercentUnhealthyPartitionsPerService\":841332893,\"maxPercentUnhealthyReplicasPerPartition\":1391032405}}},\"upgradeMode\":\"UnmonitoredAuto\",\"recreateApplication\":false}")
                .toObject(ApplicationUpgradePolicy.class);
        Assertions.assertEquals("iplrbpbewtghfgb", model.upgradeReplicaSetCheckTimeout());
        Assertions.assertEquals(false, model.forceRestart());
        Assertions.assertEquals(ArmUpgradeFailureAction.MANUAL, model.rollingUpgradeMonitoringPolicy().failureAction());
        Assertions.assertEquals("lvqhjkbegibtnmx", model.rollingUpgradeMonitoringPolicy().healthCheckWaitDuration());
        Assertions.assertEquals("bwwaloa", model.rollingUpgradeMonitoringPolicy().healthCheckStableDuration());
        Assertions.assertEquals("cgwrtzjuz", model.rollingUpgradeMonitoringPolicy().healthCheckRetryTimeout());
        Assertions.assertEquals("yzm", model.rollingUpgradeMonitoringPolicy().upgradeTimeout());
        Assertions.assertEquals("xongmtsavjcbpwxq", model.rollingUpgradeMonitoringPolicy().upgradeDomainTimeout());
        Assertions.assertEquals(true, model.applicationHealthPolicy().considerWarningAsError());
        Assertions.assertEquals(1220568259, model.applicationHealthPolicy().maxPercentUnhealthyDeployedApplications());
        Assertions
            .assertEquals(
                487735132,
                model.applicationHealthPolicy().defaultServiceTypeHealthPolicy().maxPercentUnhealthyServices());
        Assertions
            .assertEquals(
                1935483255,
                model
                    .applicationHealthPolicy()
                    .defaultServiceTypeHealthPolicy()
                    .maxPercentUnhealthyPartitionsPerService());
        Assertions
            .assertEquals(
                987025721,
                model
                    .applicationHealthPolicy()
                    .defaultServiceTypeHealthPolicy()
                    .maxPercentUnhealthyReplicasPerPartition());
        Assertions
            .assertEquals(
                499230067,
                model
                    .applicationHealthPolicy()
                    .serviceTypeHealthPolicyMap()
                    .get("xqtayriwwro")
                    .maxPercentUnhealthyServices());
        Assertions
            .assertEquals(
                1059396624,
                model
                    .applicationHealthPolicy()
                    .serviceTypeHealthPolicyMap()
                    .get("xqtayriwwro")
                    .maxPercentUnhealthyPartitionsPerService());
        Assertions
            .assertEquals(
                1883973857,
                model
                    .applicationHealthPolicy()
                    .serviceTypeHealthPolicyMap()
                    .get("xqtayriwwro")
                    .maxPercentUnhealthyReplicasPerPartition());
        Assertions.assertEquals(RollingUpgradeMode.UNMONITORED_AUTO, model.upgradeMode());
        Assertions.assertEquals(false, model.recreateApplication());
    }

    @org.junit.jupiter.api.Test
    public void testSerialize() throws Exception {
        ApplicationUpgradePolicy model =
            new ApplicationUpgradePolicy()
                .withUpgradeReplicaSetCheckTimeout("iplrbpbewtghfgb")
                .withForceRestart(false)
                .withRollingUpgradeMonitoringPolicy(
                    new ArmRollingUpgradeMonitoringPolicy()
                        .withFailureAction(ArmUpgradeFailureAction.MANUAL)
                        .withHealthCheckWaitDuration("lvqhjkbegibtnmx")
                        .withHealthCheckStableDuration("bwwaloa")
                        .withHealthCheckRetryTimeout("cgwrtzjuz")
                        .withUpgradeTimeout("yzm")
                        .withUpgradeDomainTimeout("xongmtsavjcbpwxq"))
                .withApplicationHealthPolicy(
                    new ArmApplicationHealthPolicy()
                        .withConsiderWarningAsError(true)
                        .withMaxPercentUnhealthyDeployedApplications(1220568259)
                        .withDefaultServiceTypeHealthPolicy(
                            new ArmServiceTypeHealthPolicy()
                                .withMaxPercentUnhealthyServices(487735132)
                                .withMaxPercentUnhealthyPartitionsPerService(1935483255)
                                .withMaxPercentUnhealthyReplicasPerPartition(987025721))
                        .withServiceTypeHealthPolicyMap(
                            mapOf(
                                "xqtayriwwro",
                                new ArmServiceTypeHealthPolicy()
                                    .withMaxPercentUnhealthyServices(499230067)
                                    .withMaxPercentUnhealthyPartitionsPerService(1059396624)
                                    .withMaxPercentUnhealthyReplicasPerPartition(1883973857),
                                "cqibycnojv",
                                new ArmServiceTypeHealthPolicy()
                                    .withMaxPercentUnhealthyServices(1731999756)
                                    .withMaxPercentUnhealthyPartitionsPerService(1603221085)
                                    .withMaxPercentUnhealthyReplicasPerPartition(591654502),
                                "gzva",
                                new ArmServiceTypeHealthPolicy()
                                    .withMaxPercentUnhealthyServices(2121055666)
                                    .withMaxPercentUnhealthyPartitionsPerService(841332893)
                                    .withMaxPercentUnhealthyReplicasPerPartition(1391032405))))
                .withUpgradeMode(RollingUpgradeMode.UNMONITORED_AUTO)
                .withRecreateApplication(false);
        model = BinaryData.fromObject(model).toObject(ApplicationUpgradePolicy.class);
        Assertions.assertEquals("iplrbpbewtghfgb", model.upgradeReplicaSetCheckTimeout());
        Assertions.assertEquals(false, model.forceRestart());
        Assertions.assertEquals(ArmUpgradeFailureAction.MANUAL, model.rollingUpgradeMonitoringPolicy().failureAction());
        Assertions.assertEquals("lvqhjkbegibtnmx", model.rollingUpgradeMonitoringPolicy().healthCheckWaitDuration());
        Assertions.assertEquals("bwwaloa", model.rollingUpgradeMonitoringPolicy().healthCheckStableDuration());
        Assertions.assertEquals("cgwrtzjuz", model.rollingUpgradeMonitoringPolicy().healthCheckRetryTimeout());
        Assertions.assertEquals("yzm", model.rollingUpgradeMonitoringPolicy().upgradeTimeout());
        Assertions.assertEquals("xongmtsavjcbpwxq", model.rollingUpgradeMonitoringPolicy().upgradeDomainTimeout());
        Assertions.assertEquals(true, model.applicationHealthPolicy().considerWarningAsError());
        Assertions.assertEquals(1220568259, model.applicationHealthPolicy().maxPercentUnhealthyDeployedApplications());
        Assertions
            .assertEquals(
                487735132,
                model.applicationHealthPolicy().defaultServiceTypeHealthPolicy().maxPercentUnhealthyServices());
        Assertions
            .assertEquals(
                1935483255,
                model
                    .applicationHealthPolicy()
                    .defaultServiceTypeHealthPolicy()
                    .maxPercentUnhealthyPartitionsPerService());
        Assertions
            .assertEquals(
                987025721,
                model
                    .applicationHealthPolicy()
                    .defaultServiceTypeHealthPolicy()
                    .maxPercentUnhealthyReplicasPerPartition());
        Assertions
            .assertEquals(
                499230067,
                model
                    .applicationHealthPolicy()
                    .serviceTypeHealthPolicyMap()
                    .get("xqtayriwwro")
                    .maxPercentUnhealthyServices());
        Assertions
            .assertEquals(
                1059396624,
                model
                    .applicationHealthPolicy()
                    .serviceTypeHealthPolicyMap()
                    .get("xqtayriwwro")
                    .maxPercentUnhealthyPartitionsPerService());
        Assertions
            .assertEquals(
                1883973857,
                model
                    .applicationHealthPolicy()
                    .serviceTypeHealthPolicyMap()
                    .get("xqtayriwwro")
                    .maxPercentUnhealthyReplicasPerPartition());
        Assertions.assertEquals(RollingUpgradeMode.UNMONITORED_AUTO, model.upgradeMode());
        Assertions.assertEquals(false, model.recreateApplication());
    }

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