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

package com.azure.resourcemanager.apimanagement.generated;

import com.azure.core.util.BinaryData;
import com.azure.resourcemanager.apimanagement.models.ApiManagementServiceIdentity;
import com.azure.resourcemanager.apimanagement.models.ApimIdentityType;
import com.azure.resourcemanager.apimanagement.models.UserIdentityProperties;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;

public final class ApiManagementServiceIdentityTests {
    @org.junit.jupiter.api.Test
    public void testDeserialize() throws Exception {
        ApiManagementServiceIdentity model =
            BinaryData
                .fromString(
                    "{\"type\":\"SystemAssigned\",\"principalId\":\"208f6187-b145-48d7-8864-011b1f19d199\",\"tenantId\":\"87c391a7-17ad-49fb-b6da-847d0523748e\",\"userAssignedIdentities\":{\"uoswkjmdih\":{\"principalId\":\"qseusrfjbdxzfxnx\",\"clientId\":\"b\"},\"gmdq\":{\"principalId\":\"yyyzlwhbw\",\"clientId\":\"nufzr\"},\"jvavdpwwo\":{\"principalId\":\"nbzrtfte\",\"clientId\":\"uubjtvgjsxmty\"},\"ofw\":{\"principalId\":\"d\",\"clientId\":\"titsf\"}}}")
                .toObject(ApiManagementServiceIdentity.class);
        Assertions.assertEquals(ApimIdentityType.SYSTEM_ASSIGNED, model.type());
        Assertions.assertEquals("qseusrfjbdxzfxnx", model.userAssignedIdentities().get("uoswkjmdih").principalId());
        Assertions.assertEquals("b", model.userAssignedIdentities().get("uoswkjmdih").clientId());
    }

    @org.junit.jupiter.api.Test
    public void testSerialize() throws Exception {
        ApiManagementServiceIdentity model =
            new ApiManagementServiceIdentity()
                .withType(ApimIdentityType.SYSTEM_ASSIGNED)
                .withUserAssignedIdentities(
                    mapOf(
                        "uoswkjmdih",
                        new UserIdentityProperties().withPrincipalId("qseusrfjbdxzfxnx").withClientId("b"),
                        "gmdq",
                        new UserIdentityProperties().withPrincipalId("yyyzlwhbw").withClientId("nufzr"),
                        "jvavdpwwo",
                        new UserIdentityProperties().withPrincipalId("nbzrtfte").withClientId("uubjtvgjsxmty"),
                        "ofw",
                        new UserIdentityProperties().withPrincipalId("d").withClientId("titsf")));
        model = BinaryData.fromObject(model).toObject(ApiManagementServiceIdentity.class);
        Assertions.assertEquals(ApimIdentityType.SYSTEM_ASSIGNED, model.type());
        Assertions.assertEquals("qseusrfjbdxzfxnx", model.userAssignedIdentities().get("uoswkjmdih").principalId());
        Assertions.assertEquals("b", model.userAssignedIdentities().get("uoswkjmdih").clientId());
    }

    // Use "Map.of" if available
    @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;
    }
}
