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

package com.azure.resourcemanager.postgresqlflexibleserver.generated;

import com.azure.core.util.BinaryData;
import com.azure.resourcemanager.postgresqlflexibleserver.models.IdentityType;
import com.azure.resourcemanager.postgresqlflexibleserver.models.UserAssignedIdentity;
import com.azure.resourcemanager.postgresqlflexibleserver.models.UserIdentity;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;

public final class UserAssignedIdentityTests {
    @org.junit.jupiter.api.Test
    public void testDeserialize() throws Exception {
        UserAssignedIdentity model =
            BinaryData
                .fromString(
                    "{\"userAssignedIdentities\":{\"poczvyifqrvkdvjs\":{\"principalId\":\"o\",\"clientId\":\"qn\"},\"watkpnpulexxb\":{\"principalId\":\"rm\",\"clientId\":\"d\"},\"hzdobpxjmflbvvnc\":{\"principalId\":\"wtruwiqzbqjvsovm\",\"clientId\":\"kacspkw\"},\"imjm\":{\"principalId\":\"kcciwwzjuqkhr\",\"clientId\":\"jiwkuofoskghsau\"}},\"type\":\"UserAssigned\",\"tenantId\":\"eduugi\"}")
                .toObject(UserAssignedIdentity.class);
        Assertions.assertEquals("o", model.userAssignedIdentities().get("poczvyifqrvkdvjs").principalId());
        Assertions.assertEquals("qn", model.userAssignedIdentities().get("poczvyifqrvkdvjs").clientId());
        Assertions.assertEquals(IdentityType.USER_ASSIGNED, model.type());
    }

    @org.junit.jupiter.api.Test
    public void testSerialize() throws Exception {
        UserAssignedIdentity model =
            new UserAssignedIdentity()
                .withUserAssignedIdentities(
                    mapOf(
                        "poczvyifqrvkdvjs",
                        new UserIdentity().withPrincipalId("o").withClientId("qn"),
                        "watkpnpulexxb",
                        new UserIdentity().withPrincipalId("rm").withClientId("d"),
                        "hzdobpxjmflbvvnc",
                        new UserIdentity().withPrincipalId("wtruwiqzbqjvsovm").withClientId("kacspkw"),
                        "imjm",
                        new UserIdentity().withPrincipalId("kcciwwzjuqkhr").withClientId("jiwkuofoskghsau")))
                .withType(IdentityType.USER_ASSIGNED);
        model = BinaryData.fromObject(model).toObject(UserAssignedIdentity.class);
        Assertions.assertEquals("o", model.userAssignedIdentities().get("poczvyifqrvkdvjs").principalId());
        Assertions.assertEquals("qn", model.userAssignedIdentities().get("poczvyifqrvkdvjs").clientId());
        Assertions.assertEquals(IdentityType.USER_ASSIGNED, model.type());
    }

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