|
4 | 4 | import static org.hamcrest.Matchers.is; |
5 | 5 | import static org.mockito.ArgumentMatchers.any; |
6 | 6 | import static org.mockito.Mockito.doReturn; |
| 7 | +import static org.mockito.Mockito.reset; |
7 | 8 | import static org.mockito.Mockito.spy; |
8 | 9 | import static org.mockito.Mockito.times; |
9 | 10 | import static org.mockito.Mockito.verify; |
@@ -90,24 +91,41 @@ void createMocks() { |
90 | 91 | void callsJiraWithSpecifiedParameters() throws InterruptedException, IOException { |
91 | 92 | when(build.getEnvironment(listener)).thenReturn(env); |
92 | 93 | when(site.getSession(any())).thenReturn(session); |
93 | | - when(session.getVersions(JIRA_PRJ)).thenReturn(Arrays.asList(existingVersion)); |
94 | 94 |
|
| 95 | + // for new version, verify the addVersion method is called |
| 96 | + when(session.getVersions(JIRA_PRJ)).thenReturn(null); |
95 | 97 | versionCreator.perform(project, JIRA_VER, JIRA_PRJ, build, listener); |
96 | | - verify(session).addVersion(versionCaptor.capture(), projectCaptor.capture()); |
| 98 | + verify(session, times(1)).addVersion(versionCaptor.capture(), projectCaptor.capture()); |
97 | 99 | assertThat(projectCaptor.getValue(), is(JIRA_PRJ)); |
98 | 100 | assertThat(versionCaptor.getValue(), is(JIRA_VER)); |
| 101 | + verify(logger, times(1)).println(Messages.JiraVersionCreator_CreatingVersion(JIRA_VER, JIRA_PRJ)); |
| 102 | + |
| 103 | + // for existing version, verify the addVersion method is not called |
| 104 | + reset(session); |
| 105 | + when(session.getVersions(JIRA_PRJ)).thenReturn(Arrays.asList(existingVersion)); |
| 106 | + versionCreator.perform(project, JIRA_VER, JIRA_PRJ, build, listener); |
| 107 | + verify(session, times(0)).addVersion(versionCaptor.capture(), projectCaptor.capture()); |
| 108 | + verify(logger, times(1)).println(Messages.JiraVersionCreator_VersionExists(JIRA_VER, JIRA_PRJ)); |
99 | 109 | } |
100 | 110 |
|
101 | 111 | @Test |
102 | 112 | void expandsEnvParameters() throws InterruptedException, IOException { |
103 | 113 | when(build.getEnvironment(listener)).thenReturn(env); |
104 | 114 | when(site.getSession(any())).thenReturn(session); |
105 | | - when(session.getVersions(JIRA_PRJ)).thenReturn(Arrays.asList(existingVersion)); |
106 | 115 |
|
| 116 | + // for new version, verify the addVersion method is called |
| 117 | + when(session.getVersions(JIRA_PRJ)).thenReturn(null); |
107 | 118 | versionCreator.perform(project, JIRA_VER_PARAM, JIRA_PRJ_PARAM, build, listener); |
108 | | - verify(session).addVersion(versionCaptor.capture(), projectCaptor.capture()); |
| 119 | + verify(session, times(1)).addVersion(versionCaptor.capture(), projectCaptor.capture()); |
109 | 120 | assertThat(projectCaptor.getValue(), is(JIRA_PRJ)); |
110 | 121 | assertThat(versionCaptor.getValue(), is(JIRA_VER)); |
| 122 | + |
| 123 | + // for existing version, verify the addVersion method is called |
| 124 | + reset(session); |
| 125 | + when(session.getVersions(JIRA_PRJ)).thenReturn(Arrays.asList(existingVersion)); |
| 126 | + versionCreator.perform(project, JIRA_VER_PARAM, JIRA_PRJ_PARAM, build, listener); |
| 127 | + verify(session, times(0)).addVersion(versionCaptor.capture(), projectCaptor.capture()); |
| 128 | + verify(logger, times(1)).println(Messages.JiraVersionCreator_VersionExists(JIRA_VER, JIRA_PRJ)); |
111 | 129 | } |
112 | 130 |
|
113 | 131 | @Test |
|
0 commit comments