|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager; |
| 5 | + |
| 6 | +import com.azure.core.management.Region; |
| 7 | +import com.azure.resourcemanager.containerservice.models.Code; |
| 8 | +import com.azure.resourcemanager.containerservice.models.ContainerServiceVMSizeTypes; |
| 9 | +import com.azure.resourcemanager.containerservice.models.KubernetesCluster; |
| 10 | +import com.azure.resourcemanager.containerservice.models.KubernetesClusterAgentPool; |
| 11 | +import com.azure.resourcemanager.containerservice.models.KubernetesClusters; |
| 12 | +import com.azure.resourcemanager.containerservice.models.PowerState; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.mockito.Mockito; |
| 16 | + |
| 17 | +public class MockSdkSamples { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void mockResponse() { |
| 21 | + AzureResourceManager mockAzure = Mockito.mock(AzureResourceManager.class); |
| 22 | + KubernetesClusters mockClusters = Mockito.mock(KubernetesClusters.class); |
| 23 | + KubernetesCluster mockCluster = Mockito.mock(KubernetesCluster.class); |
| 24 | + |
| 25 | + PowerState stubPowerState = new PowerState().withCode(Code.RUNNING); |
| 26 | + |
| 27 | + Mockito.when(mockAzure.kubernetesClusters()).thenReturn(mockClusters); |
| 28 | + Mockito.when(mockClusters.getById(Mockito.anyString())).thenReturn(mockCluster); |
| 29 | + Mockito.when(mockCluster.powerState()).thenReturn(stubPowerState); |
| 30 | + |
| 31 | + Assertions.assertEquals(Code.RUNNING, mockAzure.kubernetesClusters().getById("mockId").powerState().code()); |
| 32 | + } |
| 33 | + |
| 34 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 35 | + @Test |
| 36 | + public void mockCreate() { |
| 37 | + AzureResourceManager mockAzure = Mockito.mock(AzureResourceManager.class); |
| 38 | + KubernetesClusters mockClusters = Mockito.mock(KubernetesClusters.class); |
| 39 | + KubernetesCluster mockCluster = Mockito.mock(KubernetesCluster.class); |
| 40 | + |
| 41 | + PowerState stubPowerState = new PowerState().withCode(Code.RUNNING); |
| 42 | + |
| 43 | + /* |
| 44 | + The most desirable approach for mocking Fluent API would be the “Mockito.RETURNS_DEEP_STUBS” provided by Mockito. |
| 45 | +
|
| 46 | + However, some of the interfaces involved in our API uses generic, and due to type erasure Mockito will not able to handle them. |
| 47 | + Hence, we still need to use the full declaration on every step of the Fluent API. |
| 48 | + */ |
| 49 | + KubernetesCluster.DefinitionStages.Blank mockStage1 = Mockito.mock(KubernetesCluster.DefinitionStages.Blank.class); |
| 50 | + KubernetesCluster.DefinitionStages.WithGroup mockStage2 = Mockito.mock(KubernetesCluster.DefinitionStages.WithGroup.class); |
| 51 | + KubernetesCluster.DefinitionStages.WithVersion mockStage3 = Mockito.mock(KubernetesCluster.DefinitionStages.WithVersion.class); |
| 52 | + KubernetesCluster.DefinitionStages.WithLinuxRootUsername mockStage4 = Mockito.mock(KubernetesCluster.DefinitionStages.WithLinuxRootUsername.class); |
| 53 | + KubernetesCluster.DefinitionStages.WithLinuxSshKey mockStage5 = Mockito.mock(KubernetesCluster.DefinitionStages.WithLinuxSshKey.class); |
| 54 | + KubernetesCluster.DefinitionStages.WithServicePrincipalClientId mockStage6 = Mockito.mock(KubernetesCluster.DefinitionStages.WithServicePrincipalClientId.class); |
| 55 | + KubernetesCluster.DefinitionStages.WithCreate mockStage7 = Mockito.mock(KubernetesCluster.DefinitionStages.WithCreate.class); |
| 56 | + KubernetesClusterAgentPool.DefinitionStages.Blank mockStage8 = Mockito.mock(KubernetesClusterAgentPool.DefinitionStages.Blank.class); |
| 57 | + KubernetesClusterAgentPool.DefinitionStages.WithAgentPoolVirtualMachineCount mockStage9 = Mockito.mock(KubernetesClusterAgentPool.DefinitionStages.WithAgentPoolVirtualMachineCount.class); |
| 58 | + KubernetesClusterAgentPool.DefinitionStages.WithAttach mockStage10 = Mockito.mock(KubernetesClusterAgentPool.DefinitionStages.WithAttach.class); |
| 59 | + |
| 60 | + Mockito.when(mockAzure.kubernetesClusters()).thenReturn(mockClusters); |
| 61 | + |
| 62 | + Mockito.when(mockClusters.define(Mockito.anyString())).thenReturn(mockStage1); |
| 63 | + Mockito.when(mockStage1.withRegion(Mockito.any(Region.class))).thenReturn(mockStage2); |
| 64 | + Mockito.when(mockStage2.withExistingResourceGroup(Mockito.anyString())).thenReturn(mockStage3); |
| 65 | + Mockito.when(mockStage3.withDefaultVersion()).thenReturn(mockStage4); |
| 66 | + Mockito.when(mockStage4.withRootUsername(Mockito.anyString())).thenReturn(mockStage5); |
| 67 | + Mockito.when(mockStage5.withSshKey(Mockito.anyString())).thenReturn(mockStage6); |
| 68 | + Mockito.when(mockStage6.withSystemAssignedManagedServiceIdentity()).thenReturn(mockStage7); |
| 69 | + Mockito.when(mockStage7.create()).thenReturn(mockCluster); |
| 70 | + Mockito.when(mockStage7.defineAgentPool(Mockito.anyString())).thenReturn(mockStage8); |
| 71 | + Mockito.when(mockStage8.withVirtualMachineSize(Mockito.any())).thenReturn(mockStage9); |
| 72 | + Mockito.when(mockStage9.withAgentPoolVirtualMachineCount(Mockito.anyInt())).thenReturn(mockStage10); |
| 73 | + Mockito.when(mockStage10.attach()).thenReturn(mockStage7); |
| 74 | + |
| 75 | + Mockito.when(mockCluster.powerState()).thenReturn(stubPowerState); |
| 76 | + |
| 77 | + KubernetesCluster cluster = mockAzure.kubernetesClusters().define("aks1") |
| 78 | + .withRegion(Region.US_EAST2) |
| 79 | + .withExistingResourceGroup("rg1") |
| 80 | + .withDefaultVersion() |
| 81 | + .withRootUsername("user") |
| 82 | + .withSshKey("key") |
| 83 | + .withSystemAssignedManagedServiceIdentity() |
| 84 | + .defineAgentPool("ap1") |
| 85 | + .withVirtualMachineSize(ContainerServiceVMSizeTypes.STANDARD_D1_V2) |
| 86 | + .withAgentPoolVirtualMachineCount(3) |
| 87 | + .attach() |
| 88 | + .create(); |
| 89 | + |
| 90 | + Assertions.assertEquals(Code.RUNNING, cluster.powerState().code()); |
| 91 | + } |
| 92 | +} |
0 commit comments