Skip to content

Commit 5717071

Browse files
authored
Add unit tests to improve SonarScan tests code covarage (#1836)
Add unit tests to improve SonarScan tests code covarage LMCROSSITXSADEPLOY-3258
1 parent dfb29de commit 5717071

59 files changed

Lines changed: 4861 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.cloudfoundry.multiapps.controller.client;
2+
3+
import org.cloudfoundry.multiapps.controller.client.facade.oauth2.OAuth2AccessTokenWithAdditionalInfo;
4+
import org.cloudfoundry.multiapps.controller.client.facade.oauth2.OAuthClient;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.mockito.Mock;
9+
import org.mockito.Mockito;
10+
import org.mockito.MockitoAnnotations;
11+
12+
class CloudFoundryTokenProviderTest {
13+
14+
@Mock
15+
private OAuthClient oAuthClient;
16+
@Mock
17+
private OAuth2AccessTokenWithAdditionalInfo token;
18+
19+
private CloudFoundryTokenProvider tokenProvider;
20+
21+
@BeforeEach
22+
void setUp() throws Exception {
23+
MockitoAnnotations.openMocks(this)
24+
.close();
25+
tokenProvider = new CloudFoundryTokenProvider(oAuthClient);
26+
}
27+
28+
@Test
29+
void testGetTokenDelegatesToOAuthClient() {
30+
Mockito.when(oAuthClient.getToken())
31+
.thenReturn(token);
32+
33+
OAuth2AccessTokenWithAdditionalInfo result = tokenProvider.getToken();
34+
35+
Assertions.assertSame(token, result);
36+
Mockito.verify(oAuthClient)
37+
.getToken();
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
package org.cloudfoundry.multiapps.controller.client;
2+
3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
5+
import java.time.Duration;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import java.util.UUID;
9+
10+
import org.cloudfoundry.multiapps.controller.client.facade.CloudOperationException;
11+
import org.cloudfoundry.multiapps.controller.client.facade.UploadStatusCallback;
12+
import org.cloudfoundry.multiapps.controller.client.facade.domain.CloudApplication;
13+
import org.cloudfoundry.multiapps.controller.client.facade.domain.CloudDomain;
14+
import org.cloudfoundry.multiapps.controller.client.facade.domain.CloudPackage;
15+
import org.cloudfoundry.multiapps.controller.client.facade.domain.CloudSpace;
16+
import org.cloudfoundry.multiapps.controller.client.facade.domain.CloudStack;
17+
import org.cloudfoundry.multiapps.controller.client.facade.rest.CloudControllerRestClient;
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.mockito.ArgumentMatchers;
22+
import org.mockito.Mockito;
23+
import org.springframework.http.HttpStatus;
24+
25+
class ResilientCloudControllerClientTest {
26+
27+
private CloudControllerRestClient restClient;
28+
private ResilientCloudControllerClient client;
29+
30+
@BeforeEach
31+
void setUp() {
32+
restClient = Mockito.mock(CloudControllerRestClient.class);
33+
client = new ResilientCloudControllerClient(restClient);
34+
}
35+
36+
@Test
37+
void testGetTargetIsExecutedWithoutRetryWrapping() {
38+
CloudSpace space = Mockito.mock(CloudSpace.class);
39+
Mockito.when(restClient.getTarget())
40+
.thenReturn(space);
41+
42+
Assertions.assertSame(space, client.getTarget());
43+
Mockito.verify(restClient)
44+
.getTarget();
45+
}
46+
47+
@Test
48+
void testVoidMethodRetriesOnTransientBadGateway() {
49+
Mockito.doThrow(new CloudOperationException(HttpStatus.BAD_GATEWAY))
50+
.doNothing()
51+
.when(restClient)
52+
.addDomain("example.com");
53+
54+
client.addDomain("example.com");
55+
56+
Mockito.verify(restClient, Mockito.times(2))
57+
.addDomain("example.com");
58+
}
59+
60+
@Test
61+
void testValueReturningMethodRetriesOnTransientBadGateway() {
62+
CloudApplication app = Mockito.mock(CloudApplication.class);
63+
Mockito.when(restClient.getApplication("my-app"))
64+
.thenThrow(new CloudOperationException(HttpStatus.BAD_GATEWAY))
65+
.thenReturn(app);
66+
67+
Assertions.assertSame(app, client.getApplication("my-app"));
68+
Mockito.verify(restClient, Mockito.times(2))
69+
.getApplication("my-app");
70+
}
71+
72+
@Test
73+
void testNonRetryableStatusPropagatesImmediately() {
74+
Mockito.when(restClient.getApplication("missing"))
75+
.thenThrow(new CloudOperationException(HttpStatus.UNAUTHORIZED));
76+
77+
CloudOperationException thrown = Assertions.assertThrows(CloudOperationException.class, () -> client.getApplication("missing"));
78+
79+
Assertions.assertEquals(HttpStatus.UNAUTHORIZED, thrown.getStatusCode());
80+
Mockito.verify(restClient, Mockito.times(1))
81+
.getApplication("missing");
82+
}
83+
84+
@Test
85+
void testGetApplicationsIgnoresNotFound() {
86+
Mockito.when(restClient.getApplications())
87+
.thenThrow(new CloudOperationException(HttpStatus.NOT_FOUND))
88+
.thenReturn(List.of());
89+
90+
Assertions.assertTrue(client.getApplications()
91+
.isEmpty());
92+
Mockito.verify(restClient, Mockito.times(2))
93+
.getApplications();
94+
}
95+
96+
@Test
97+
void testGetDomainsIgnoresNotFound() {
98+
Mockito.when(restClient.getDomains())
99+
.thenThrow(new CloudOperationException(HttpStatus.NOT_FOUND))
100+
.thenReturn(List.<CloudDomain> of());
101+
102+
Assertions.assertTrue(client.getDomains()
103+
.isEmpty());
104+
Mockito.verify(restClient, Mockito.times(2))
105+
.getDomains();
106+
}
107+
108+
@Test
109+
void testRoutesByDomainIgnoresNotFound() {
110+
Mockito.when(restClient.getRoutes("example.com"))
111+
.thenThrow(new CloudOperationException(HttpStatus.NOT_FOUND))
112+
.thenReturn(List.of());
113+
114+
Assertions.assertTrue(client.getRoutes("example.com")
115+
.isEmpty());
116+
Mockito.verify(restClient, Mockito.times(2))
117+
.getRoutes("example.com");
118+
}
119+
120+
@Test
121+
void testGetStackByNameDelegates() {
122+
CloudStack stack = Mockito.mock(CloudStack.class);
123+
Mockito.when(restClient.getStack("cflinuxfs4"))
124+
.thenReturn(stack);
125+
126+
Assertions.assertSame(stack, client.getStack("cflinuxfs4"));
127+
}
128+
129+
@Test
130+
void testGetStackByNameAndRequiredFlagDelegates() {
131+
CloudStack stack = Mockito.mock(CloudStack.class);
132+
Mockito.when(restClient.getStack("cflinuxfs4", true))
133+
.thenReturn(stack);
134+
135+
Assertions.assertSame(stack, client.getStack("cflinuxfs4", true));
136+
}
137+
138+
@Test
139+
void testRunTaskDelegatesAndRetries() {
140+
Mockito.when(restClient.runTask(ArgumentMatchers.eq("my-app"), ArgumentMatchers.any()))
141+
.thenThrow(new CloudOperationException(HttpStatus.BAD_GATEWAY))
142+
.thenReturn(null);
143+
144+
client.runTask("my-app", null);
145+
146+
Mockito.verify(restClient, Mockito.times(2))
147+
.runTask(ArgumentMatchers.eq("my-app"), ArgumentMatchers.any());
148+
}
149+
150+
@Test
151+
void testCancelTaskDelegates() {
152+
UUID taskGuid = UUID.randomUUID();
153+
154+
client.cancelTask(taskGuid);
155+
156+
Mockito.verify(restClient)
157+
.cancelTask(taskGuid);
158+
}
159+
160+
@Test
161+
void testRenameDelegates() {
162+
client.rename("old", "new");
163+
164+
Mockito.verify(restClient)
165+
.rename("old", "new");
166+
}
167+
168+
@Test
169+
void testStartApplicationDelegates() {
170+
client.startApplication("my-app");
171+
172+
Mockito.verify(restClient)
173+
.startApplication("my-app");
174+
}
175+
176+
@Test
177+
void testStopApplicationDelegates() {
178+
client.stopApplication("my-app");
179+
180+
Mockito.verify(restClient)
181+
.stopApplication("my-app");
182+
}
183+
184+
@Test
185+
void testRestartApplicationDelegates() {
186+
client.restartApplication("my-app");
187+
188+
Mockito.verify(restClient)
189+
.restartApplication("my-app");
190+
}
191+
192+
@Test
193+
void testBindServiceInstanceReturnsOptional() {
194+
Mockito.when(restClient.bindServiceInstance("binding", "app", "service"))
195+
.thenReturn(Optional.of("job-1"));
196+
197+
Assertions.assertEquals(Optional.of("job-1"), client.bindServiceInstance("binding", "app", "service"));
198+
}
199+
200+
@Test
201+
void testGetApplicationSshEnabledRetriesAndReturnsBoolean() {
202+
UUID guid = UUID.randomUUID();
203+
Mockito.when(restClient.getApplicationSshEnabled(guid))
204+
.thenThrow(new CloudOperationException(HttpStatus.BAD_GATEWAY))
205+
.thenReturn(true);
206+
207+
Assertions.assertTrue(client.getApplicationSshEnabled(guid));
208+
Mockito.verify(restClient, Mockito.times(2))
209+
.getApplicationSshEnabled(guid);
210+
}
211+
212+
@Test
213+
void testAsyncUploadWithOverrideTimeoutUsesPlainRetry() {
214+
Path file = Paths.get("/tmp/some.mtar");
215+
UploadStatusCallback callback = Mockito.mock(UploadStatusCallback.class);
216+
Duration override = Duration.ofMinutes(2);
217+
CloudPackage pkg = Mockito.mock(CloudPackage.class);
218+
Mockito.when(restClient.asyncUploadApplication("my-app", file, callback, override))
219+
.thenReturn(pkg);
220+
221+
Assertions.assertSame(pkg, client.asyncUploadApplicationWithExponentialBackoff("my-app", file, callback, override));
222+
Mockito.verify(restClient)
223+
.asyncUploadApplication("my-app", file, callback, override);
224+
}
225+
226+
@Test
227+
void testAsyncUploadWithoutOverrideTimeoutUsesExponentialBackoff() {
228+
Path file = Paths.get("/tmp/some.mtar");
229+
UploadStatusCallback callback = Mockito.mock(UploadStatusCallback.class);
230+
CloudPackage pkg = Mockito.mock(CloudPackage.class);
231+
Mockito.when(restClient.asyncUploadApplication(ArgumentMatchers.eq("my-app"),
232+
ArgumentMatchers.eq(file),
233+
ArgumentMatchers.eq(callback),
234+
ArgumentMatchers.any(Duration.class)))
235+
.thenReturn(pkg);
236+
237+
Assertions.assertSame(pkg, client.asyncUploadApplicationWithExponentialBackoff("my-app", file, callback, null));
238+
Mockito.verify(restClient)
239+
.asyncUploadApplication(ArgumentMatchers.eq("my-app"),
240+
ArgumentMatchers.eq(file),
241+
ArgumentMatchers.eq(callback),
242+
ArgumentMatchers.any(Duration.class));
243+
}
244+
245+
@Test
246+
void testGetUploadStatusDelegates() {
247+
UUID packageGuid = UUID.randomUUID();
248+
249+
client.getUploadStatus(packageGuid);
250+
251+
Mockito.verify(restClient)
252+
.getUploadStatus(packageGuid);
253+
}
254+
255+
@Test
256+
void testDeleteOrphanedRoutesIgnoresNotFound() {
257+
Mockito.doThrow(new CloudOperationException(HttpStatus.NOT_FOUND))
258+
.doNothing()
259+
.when(restClient)
260+
.deleteOrphanedRoutes();
261+
262+
client.deleteOrphanedRoutes();
263+
264+
Mockito.verify(restClient, Mockito.times(2))
265+
.deleteOrphanedRoutes();
266+
}
267+
268+
@Test
269+
void testGetServiceBrokersIgnoresNotFound() {
270+
Mockito.when(restClient.getServiceBrokers())
271+
.thenThrow(new CloudOperationException(HttpStatus.NOT_FOUND))
272+
.thenReturn(List.of());
273+
274+
Assertions.assertTrue(client.getServiceBrokers()
275+
.isEmpty());
276+
}
277+
278+
@Test
279+
void testGetEventsIgnoresNotFound() {
280+
Mockito.when(restClient.getEvents())
281+
.thenThrow(new CloudOperationException(HttpStatus.NOT_FOUND))
282+
.thenReturn(List.of());
283+
284+
Assertions.assertTrue(client.getEvents()
285+
.isEmpty());
286+
}
287+
288+
@Test
289+
void testGetServiceOfferingsIgnoresNotFound() {
290+
Mockito.when(restClient.getServiceOfferings())
291+
.thenThrow(new CloudOperationException(HttpStatus.NOT_FOUND))
292+
.thenReturn(List.of());
293+
294+
Assertions.assertTrue(client.getServiceOfferings()
295+
.isEmpty());
296+
}
297+
298+
@Test
299+
void testGetStacksIgnoresNotFound() {
300+
Mockito.when(restClient.getStacks())
301+
.thenThrow(new CloudOperationException(HttpStatus.NOT_FOUND))
302+
.thenReturn(List.of());
303+
304+
Assertions.assertTrue(client.getStacks()
305+
.isEmpty());
306+
}
307+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.cloudfoundry.multiapps.controller.client.facade;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.http.HttpStatus;
6+
7+
class CloudControllerExceptionTest {
8+
9+
@Test
10+
void testStatusOnlyConstructorDecoratesMessage() {
11+
CloudControllerException e = new CloudControllerException(HttpStatus.NOT_FOUND);
12+
13+
Assertions.assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
14+
Assertions.assertEquals("Controller operation failed: 404 Not Found", e.getMessage());
15+
}
16+
17+
@Test
18+
void testStatusAndStatusTextConstructor() {
19+
CloudControllerException e = new CloudControllerException(HttpStatus.BAD_REQUEST, "Bad Request");
20+
21+
Assertions.assertEquals("Bad Request", e.getStatusText());
22+
Assertions.assertEquals("Controller operation failed: 400 Bad Request", e.getMessage());
23+
}
24+
25+
@Test
26+
void testStatusStatusTextDescriptionConstructor() {
27+
CloudControllerException e = new CloudControllerException(HttpStatus.UNAUTHORIZED, "Unauthorized", "expired");
28+
29+
Assertions.assertEquals("expired", e.getDescription());
30+
Assertions.assertEquals("Controller operation failed: 401 Unauthorized: expired", e.getMessage());
31+
}
32+
33+
@Test
34+
void testWrappingConstructorPreservesFieldsAndCause() {
35+
CloudOperationException source = new CloudOperationException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", "boom");
36+
37+
CloudControllerException e = new CloudControllerException(source);
38+
39+
Assertions.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, e.getStatusCode());
40+
Assertions.assertEquals("Internal Server Error", e.getStatusText());
41+
Assertions.assertEquals("boom", e.getDescription());
42+
Assertions.assertSame(source, e.getCause());
43+
Assertions.assertEquals("Controller operation failed: 500 Internal Server Error: boom", e.getMessage());
44+
}
45+
}

0 commit comments

Comments
 (0)