From 26ca48097d89f2dde55cfa9b2b47cabaf2995ec3 Mon Sep 17 00:00:00 2001 From: arus2023 Date: Mon, 25 Sep 2023 19:14:02 -0500 Subject: [PATCH] Removed 3 unnecessary stubbings from KubernetesEngineBuilderClusterTest.java --- .../KubernetesEngineBuilderClusterTest.java | 53 +++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/google/jenkins/plugins/k8sengine/KubernetesEngineBuilderClusterTest.java b/src/test/java/com/google/jenkins/plugins/k8sengine/KubernetesEngineBuilderClusterTest.java index a5711834..a432468f 100644 --- a/src/test/java/com/google/jenkins/plugins/k8sengine/KubernetesEngineBuilderClusterTest.java +++ b/src/test/java/com/google/jenkins/plugins/k8sengine/KubernetesEngineBuilderClusterTest.java @@ -58,7 +58,7 @@ public static void init() { @Test public void testDoFillClusterItemsEmptyWithEmptyCredentialsId() throws IOException { - DescriptorImpl descriptor = setUpClusterDescriptor(ImmutableList.of(), null, null); + DescriptorImpl descriptor = setUpClusterDescriptor3(ImmutableList.of(), null, null); ListBoxModel expected = initExpected(ImmutableList.of(EMPTY_NAME), ImmutableList.of(EMPTY_VALUE)); ListBoxModel result = descriptor.doFillClusterItems(jenkins, null, null, TEST_PROJECT_ID); @@ -68,7 +68,7 @@ public void testDoFillClusterItemsEmptyWithEmptyCredentialsId() throws IOExcepti @Test public void testDoFillClusterItemsEmptyWithEmptyProjectId() throws IOException { - DescriptorImpl descriptor = setUpClusterDescriptor(ImmutableList.of(), null, null); + DescriptorImpl descriptor = setUpClusterDescriptor3(ImmutableList.of(), null, null); ListBoxModel expected = initExpected(ImmutableList.of(EMPTY_NAME), ImmutableList.of(EMPTY_VALUE)); ListBoxModel result = descriptor.doFillClusterItems(jenkins, null, TEST_CREDENTIALS_ID, null); @@ -184,7 +184,7 @@ public void testDoCheckClusterMessageWithEmptyCluster() throws IOException { @Test public void testDoCheckClusterMessageWithEmptyCredentialsId() throws IOException { - DescriptorImpl descriptor = setUpClusterDescriptor(ImmutableList.of(), null, null); + DescriptorImpl descriptor = setUpClusterDescriptor3(ImmutableList.of(), null, null); FormValidation result = descriptor.doCheckCluster(jenkins, TEST_CLUSTER, null, TEST_PROJECT_ID); assertNotNull(result); assertEquals( @@ -193,7 +193,7 @@ public void testDoCheckClusterMessageWithEmptyCredentialsId() throws IOException @Test public void testDoCheckClusterMessageWithEmptyProjectId() throws IOException { - DescriptorImpl descriptor = setUpClusterDescriptor(ImmutableList.of(), null, null); + DescriptorImpl descriptor = setUpClusterDescriptor2(ImmutableList.of(), null, null); FormValidation result = descriptor.doCheckCluster(jenkins, TEST_CLUSTER, TEST_CREDENTIALS_ID, null); assertNotNull(result); @@ -289,4 +289,49 @@ private DescriptorImpl setUpClusterDescriptor( .thenReturn(ImmutableList.copyOf(clusters)); return descriptor; } + + private DescriptorImpl setUpClusterDescriptor2( + List initialClusters, AbortException abortException, IOException ioException) + throws IOException { + DescriptorImpl descriptor = Mockito.spy(DescriptorImpl.class); + if (abortException != null) { + Mockito.doThrow(abortException) + .when(descriptor) + .getClientFactory(any(Jenkins.class), anyString()); + return descriptor; + } + ClientFactory clientFactory = Mockito.mock(ClientFactory.class); + Mockito.doReturn(clientFactory) + .when(descriptor) + .getClientFactory(any(Jenkins.class), anyString()); + ContainerClient containerClient = Mockito.mock(ContainerClient.class); + if (ioException != null) { + Mockito.when(containerClient.listAllClusters(anyString())).thenThrow(ioException); + return descriptor; + } + List clusters = new ArrayList<>(); + initialClusters.forEach(c -> clusters.add(ClusterUtil.fromNameAndLocation(c))); + return descriptor; + } + + private DescriptorImpl setUpClusterDescriptor3( + List initialClusters, AbortException abortException, IOException ioException) + throws IOException { + DescriptorImpl descriptor = Mockito.spy(DescriptorImpl.class); + if (abortException != null) { + Mockito.doThrow(abortException) + .when(descriptor) + .getClientFactory(any(Jenkins.class), anyString()); + return descriptor; + } + ClientFactory clientFactory = Mockito.mock(ClientFactory.class); + ContainerClient containerClient = Mockito.mock(ContainerClient.class); + if (ioException != null) { + Mockito.when(containerClient.listAllClusters(anyString())).thenThrow(ioException); + return descriptor; + } + List clusters = new ArrayList<>(); + initialClusters.forEach(c -> clusters.add(ClusterUtil.fromNameAndLocation(c))); + return descriptor; + } }