Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed 4 unnecessary stubbings from KubernetesEngineBuilderTest.java #351

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void testDoFillProjectIdItemsErrorMessageWithIOException() throws IOExcep

@Test
public void testDoFillProjectIdItemsEmptyWithEmptyCredentialsId() throws IOException {
DescriptorImpl descriptor = setUpProjectDescriptor(ImmutableList.of(), "", null, null);
DescriptorImpl descriptor = setUpProjectDescriptor3(ImmutableList.of(), "", null, null);
ListBoxModel expected =
initExpected(ImmutableList.of(EMPTY_NAME), ImmutableList.of(EMPTY_VALUE));
ListBoxModel result = descriptor.doFillProjectIdItems(jenkins, null, null);
Expand Down Expand Up @@ -171,15 +171,15 @@ public void testDoFillProjectIdItemsWithValidCredentialsAndEmptyProject() throws

@Test
public void testDoCheckProjectIdMessageWithEmptyProjectID() throws IOException {
DescriptorImpl descriptor = setUpProjectDescriptor(ImmutableList.of(), "", null, null);
DescriptorImpl descriptor = setUpProjectDescriptor2(ImmutableList.of(), "", null, null);
FormValidation result = descriptor.doCheckProjectId(jenkins, null, TEST_CREDENTIALS_ID);
assertNotNull(result);
assertEquals(Messages.KubernetesEngineBuilder_ProjectIDRequired(), result.getMessage());
}

@Test
public void testDoCheckProjectIdMessageWithEmptyCredentialsID() throws IOException {
DescriptorImpl descriptor = setUpProjectDescriptor(ImmutableList.of(), "", null, null);
DescriptorImpl descriptor = setUpProjectDescriptor3(ImmutableList.of(), "", null, null);
FormValidation result = descriptor.doCheckProjectId(jenkins, TEST_PROJECT_ID, null);
assertNotNull(result);
assertEquals(
Expand All @@ -199,7 +199,7 @@ public void testDoCheckProjectIdMessageWithAbortException() throws IOException {
@Test
public void testDoCheckProjectIdMessageWithIOException() throws IOException {
DescriptorImpl descriptor =
setUpProjectDescriptor(ImmutableList.of(), "", null, new IOException());
setUpProjectDescriptor2(ImmutableList.of(), "", null, new IOException());
FormValidation result =
descriptor.doCheckProjectId(jenkins, TEST_PROJECT_ID, TEST_CREDENTIALS_ID);
assertNotNull(result);
Expand All @@ -219,7 +219,7 @@ public void testDoCheckProjectIdMessageWithAbortExceptionAndEmptyProjectId() thr
@Test
public void testDoCheckProjectIdMessageWithIOExceptionAndEmptyProjectId() throws IOException {
DescriptorImpl descriptor =
setUpProjectDescriptor(ImmutableList.of(), "", null, new IOException());
setUpProjectDescriptor2(ImmutableList.of(), "", null, new IOException());
FormValidation result = descriptor.doCheckProjectId(jenkins, null, TEST_CREDENTIALS_ID);
assertNotNull(result);
assertEquals(
Expand All @@ -228,7 +228,7 @@ public void testDoCheckProjectIdMessageWithIOExceptionAndEmptyProjectId() throws

@Test
public void testDoCheckProjectIdMessageWithNoProjects() throws IOException {
DescriptorImpl descriptor = setUpProjectDescriptor(ImmutableList.of(), "", null, null);
DescriptorImpl descriptor = setUpProjectDescriptor2(ImmutableList.of(), "", null, null);
FormValidation result =
descriptor.doCheckProjectId(jenkins, TEST_PROJECT_ID, TEST_CREDENTIALS_ID);
assertNotNull(result);
Expand All @@ -239,7 +239,7 @@ public void testDoCheckProjectIdMessageWithNoProjects() throws IOException {
@Test
public void testDoCheckProjectIdMessageWithWrongProjects() throws IOException {
DescriptorImpl descriptor =
setUpProjectDescriptor(ImmutableList.of(OTHER_PROJECT_ID), "", null, null);
setUpProjectDescriptor2(ImmutableList.of(OTHER_PROJECT_ID), "", null, null);
FormValidation result =
descriptor.doCheckProjectId(jenkins, TEST_PROJECT_ID, TEST_CREDENTIALS_ID);
assertNotNull(result);
Expand All @@ -250,7 +250,8 @@ public void testDoCheckProjectIdMessageWithWrongProjects() throws IOException {
@Test
public void testDoCheckProjectIdMessageWithValidProject() throws IOException {
DescriptorImpl descriptor =
setUpProjectDescriptor(ImmutableList.of(OTHER_PROJECT_ID, TEST_PROJECT_ID), "", null, null);
setUpProjectDescriptor2(
ImmutableList.of(OTHER_PROJECT_ID, TEST_PROJECT_ID), "", null, null);
FormValidation result =
descriptor.doCheckProjectId(jenkins, TEST_PROJECT_ID, TEST_CREDENTIALS_ID);
assertNotNull(result);
Expand Down Expand Up @@ -344,4 +345,60 @@ static void assertValueSelected(ListBoxModel result, String expectedValue) {
result.stream().filter(i -> expectedValue.equals(i.value)).findFirst();
expectedOption.ifPresent(option -> assertTrue(option.selected));
}

private DescriptorImpl setUpProjectDescriptor2(
List<String> initialProjects,
String defaultProjectId,
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());
CloudResourceManagerClient cloudResourceManagerClient =
Mockito.mock(CloudResourceManagerClient.class);
Mockito.when(clientFactory.cloudResourceManagerClient()).thenReturn(cloudResourceManagerClient);
if (ioException != null) {
Mockito.when(cloudResourceManagerClient.listProjects()).thenThrow(ioException);
return descriptor;
}
List<Project> projects = new ArrayList<>();
initialProjects.forEach(p -> projects.add(new Project().setProjectId(p)));
Mockito.when(cloudResourceManagerClient.listProjects())
.thenReturn(ImmutableList.copyOf(projects));
return descriptor;
}

private DescriptorImpl setUpProjectDescriptor3(
List<String> initialProjects,
String defaultProjectId,
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);
CloudResourceManagerClient cloudResourceManagerClient =
Mockito.mock(CloudResourceManagerClient.class);
if (ioException != null) {
Mockito.when(cloudResourceManagerClient.listProjects()).thenThrow(ioException);
return descriptor;
}
List<Project> projects = new ArrayList<>();
initialProjects.forEach(p -> projects.add(new Project().setProjectId(p)));
return descriptor;
}
}
Loading