Skip to content

Commit 16468fc

Browse files
committed
Fix sonarqube findings
1 parent 19fb139 commit 16468fc

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

external-service-aap/src/main/java/org/opendevstack/apiservice/externalservice/aap/service/impl/AnsibleAutomationPlatformService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.http.MediaType;
1111
import org.springframework.scheduling.annotation.Async;
1212
import org.springframework.stereotype.Service;
13+
import org.springframework.core.ParameterizedTypeReference;
1314
import org.springframework.web.client.RestClient;
1415
import org.springframework.web.client.RestClientException;
1516
import org.springframework.web.util.UriUtils;
@@ -55,13 +56,13 @@ public AutomationExecutionResult executeWorkflow(String workflowName, Map<String
5556
String encodedWorkflowName = UriUtils.encodePath(workflowName, StandardCharsets.UTF_8);
5657
String url = baseUrl + "/workflow_job_templates/" + encodedWorkflowName + "/launch/";
5758

58-
Map responseBody = restClient.post()
59+
Map<String, Object> responseBody = restClient.post()
5960
.uri(url)
6061
.headers(this::applyAuthHeaders)
6162
.contentType(MediaType.APPLICATION_JSON)
6263
.body(requestBody)
6364
.retrieve()
64-
.body(Map.class);
65+
.body(new ParameterizedTypeReference<Map<String, Object>>() {});
6566

6667
if (responseBody != null) {
6768
String jobId = String.valueOf(responseBody.get("id"));
@@ -119,11 +120,11 @@ public AutomationJobStatus getWorkflowJobStatus(String workflowId) throws Automa
119120

120121
private AutomationJobStatus fetchJobStatus(String jobId, String url) throws AutomationPlatformException {
121122
try {
122-
Map responseBody = restClient.get()
123+
Map<String, Object> responseBody = restClient.get()
123124
.uri(url)
124125
.headers(this::applyAuthHeaders)
125126
.retrieve()
126-
.body(Map.class);
127+
.body(new ParameterizedTypeReference<Map<String, Object>>() {});
127128

128129
if (responseBody != null) {
129130
AutomationJobStatus status = new AutomationJobStatus();

external-service-aap/src/test/java/org/opendevstack/apiservice/externalservice/aap/service/impl/AnsibleAutomationPlatformServiceTest.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.opendevstack.apiservice.externalservice.aap.exception.AutomationPlatformException;
99
import org.opendevstack.apiservice.externalservice.aap.model.AutomationExecutionResult;
1010
import org.opendevstack.apiservice.externalservice.aap.model.AutomationJobStatus;
11+
import org.springframework.core.ParameterizedTypeReference;
1112
import org.springframework.test.util.ReflectionTestUtils;
1213
import org.springframework.web.client.RestClient;
1314
import org.springframework.web.client.RestClientException;
@@ -81,7 +82,7 @@ void executeWorkflow_Success() throws AutomationPlatformException {
8182
responseBody.put("status", "pending");
8283
responseBody.put("url", BASE_URL + "/workflow_jobs/12345/");
8384

84-
when(postResponseSpec.body(Map.class)).thenReturn(responseBody);
85+
doReturn(responseBody).when(postResponseSpec).body(any(ParameterizedTypeReference.class));
8586

8687
AutomationExecutionResult result = service.executeWorkflow("test-workflow", Map.of("env", "dev"));
8788

@@ -99,7 +100,7 @@ void executeWorkflow_WithNullParameters() throws AutomationPlatformException {
99100
responseBody.put("id", "12345");
100101
responseBody.put("status", "pending");
101102

102-
when(postResponseSpec.body(Map.class)).thenReturn(responseBody);
103+
doReturn(responseBody).when(postResponseSpec).body(any(ParameterizedTypeReference.class));
103104

104105
AutomationExecutionResult result = service.executeWorkflow("test-workflow", null);
105106

@@ -122,7 +123,7 @@ void executeWorkflow_RestClientException() {
122123

123124
@Test
124125
void executeWorkflow_NullResponseBody() {
125-
when(postResponseSpec.body(Map.class)).thenReturn(null);
126+
doReturn(null).when(postResponseSpec).body(any(ParameterizedTypeReference.class));
126127

127128
AutomationPlatformException.WorkflowExecutionException ex = assertThrows(
128129
AutomationPlatformException.WorkflowExecutionException.class,
@@ -134,7 +135,7 @@ void executeWorkflow_NullResponseBody() {
134135
@Test
135136
void executeWorkflow_EmptyParameters() throws AutomationPlatformException {
136137
Map<String, Object> responseBody = Map.of("id", "12345", "status", "pending");
137-
when(postResponseSpec.body(Map.class)).thenReturn(responseBody);
138+
doReturn(responseBody).when(postResponseSpec).body(any(ParameterizedTypeReference.class));
138139

139140
AutomationExecutionResult result = service.executeWorkflow("test-workflow", new HashMap<>());
140141
assertNotNull(result);
@@ -144,7 +145,7 @@ void executeWorkflow_EmptyParameters() throws AutomationPlatformException {
144145
@Test
145146
void executeWorkflow_VerifyExtraVarsPassedCorrectly() throws AutomationPlatformException {
146147
Map<String, Object> responseBody = Map.of("id", "99999", "status", "pending");
147-
when(postResponseSpec.body(Map.class)).thenReturn(responseBody);
148+
doReturn(responseBody).when(postResponseSpec).body(any(ParameterizedTypeReference.class));
148149

149150
Map<String, Object> params = new HashMap<>();
150151
params.put("app_name", "my-app");
@@ -163,7 +164,7 @@ void executeWorkflow_VerifyExtraVarsPassedCorrectly() throws AutomationPlatformE
163164
@Test
164165
void executeWorkflowAsync_Success() throws ExecutionException, InterruptedException {
165166
Map<String, Object> responseBody = Map.of("id", "67890", "status", "running");
166-
when(postResponseSpec.body(Map.class)).thenReturn(responseBody);
167+
doReturn(responseBody).when(postResponseSpec).body(any(ParameterizedTypeReference.class));
167168

168169
CompletableFuture<AutomationExecutionResult> future =
169170
service.executeWorkflowAsync("test-workflow", Map.of("env", "prod"));
@@ -199,7 +200,7 @@ void getJobStatus_Success() throws AutomationPlatformException {
199200
responseBody.put("status", "successful");
200201
responseBody.put("result_traceback", "Job completed successfully");
201202

202-
when(getResponseSpec.body(Map.class)).thenReturn(responseBody);
203+
doReturn(responseBody).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
203204

204205
AutomationJobStatus status = service.getJobStatus("12345");
205206

@@ -212,45 +213,45 @@ void getJobStatus_Success() throws AutomationPlatformException {
212213

213214
@Test
214215
void getJobStatus_PendingStatus() throws AutomationPlatformException {
215-
when(getResponseSpec.body(Map.class)).thenReturn(Map.of("status", "pending"));
216+
doReturn(Map.of("status", "pending")).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
216217
assertEquals(AutomationJobStatus.Status.PENDING, service.getJobStatus("1").getStatus());
217218
}
218219

219220
@Test
220221
void getJobStatus_RunningStatus() throws AutomationPlatformException {
221-
when(getResponseSpec.body(Map.class)).thenReturn(Map.of("status", "running"));
222+
doReturn(Map.of("status", "running")).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
222223
assertEquals(AutomationJobStatus.Status.RUNNING, service.getJobStatus("1").getStatus());
223224
}
224225

225226
@Test
226227
void getJobStatus_FailedStatus() throws AutomationPlatformException {
227-
when(getResponseSpec.body(Map.class)).thenReturn(Map.of("status", "failed"));
228+
doReturn(Map.of("status", "failed")).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
228229
assertEquals(AutomationJobStatus.Status.FAILED, service.getJobStatus("1").getStatus());
229230
}
230231

231232
@Test
232233
void getJobStatus_CancelledStatus() throws AutomationPlatformException {
233-
when(getResponseSpec.body(Map.class)).thenReturn(Map.of("status", "canceled"));
234+
doReturn(Map.of("status", "canceled")).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
234235
assertEquals(AutomationJobStatus.Status.CANCELLED, service.getJobStatus("1").getStatus());
235236
}
236237

237238
@Test
238239
void getJobStatus_CancelledAlternativeSpelling() throws AutomationPlatformException {
239-
when(getResponseSpec.body(Map.class)).thenReturn(Map.of("status", "cancelled"));
240+
doReturn(Map.of("status", "cancelled")).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
240241
assertEquals(AutomationJobStatus.Status.CANCELLED, service.getJobStatus("1").getStatus());
241242
}
242243

243244
@Test
244245
void getJobStatus_UnknownStatus() throws AutomationPlatformException {
245-
when(getResponseSpec.body(Map.class)).thenReturn(Map.of("status", "unknown_status"));
246+
doReturn(Map.of("status", "unknown_status")).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
246247
assertEquals(AutomationJobStatus.Status.ERROR, service.getJobStatus("1").getStatus());
247248
}
248249

249250
@Test
250251
void getJobStatus_NullStatus() throws AutomationPlatformException {
251252
Map<String, Object> body = new HashMap<>();
252253
body.put("status", null);
253-
when(getResponseSpec.body(Map.class)).thenReturn(body);
254+
doReturn(body).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
254255
assertEquals(AutomationJobStatus.Status.ERROR, service.getJobStatus("1").getStatus());
255256
}
256257

@@ -268,7 +269,7 @@ void getJobStatus_JobNotFound() {
268269

269270
@Test
270271
void getJobStatus_NullResponseBody() {
271-
when(getResponseSpec.body(Map.class)).thenReturn(null);
272+
doReturn(null).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
272273

273274
assertThrows(
274275
AutomationPlatformException.JobNotFoundException.class,
@@ -286,7 +287,7 @@ void getWorkflowJobStatus_Success() throws AutomationPlatformException {
286287
responseBody.put("status", "successful");
287288
responseBody.put("result_traceback", "Workflow completed");
288289

289-
when(getResponseSpec.body(Map.class)).thenReturn(responseBody);
290+
doReturn(responseBody).when(getResponseSpec).body(any(ParameterizedTypeReference.class));
290291

291292
AutomationJobStatus status = service.getWorkflowJobStatus("67890");
292293

@@ -295,7 +296,7 @@ void getWorkflowJobStatus_Success() throws AutomationPlatformException {
295296
assertEquals(AutomationJobStatus.Status.SUCCESSFUL, status.getStatus());
296297
assertEquals("Workflow completed", status.getStatusMessage());
297298

298-
verify(getUriSpec).uri(eq(BASE_URL + "/workflow_jobs/67890/"));
299+
verify(getUriSpec, times(1)).uri(eq(BASE_URL + "/workflow_jobs/67890/"));
299300
}
300301

301302
@Test
@@ -319,7 +320,7 @@ void validateConnection_Success() {
319320
when(getResponseSpec.toBodilessEntity()).thenReturn(null); // any non-throw = success
320321

321322
assertTrue(service.validateConnection());
322-
verify(getUriSpec).uri(eq(BASE_URL + "/ping/"));
323+
verify(getUriSpec, times(1)).uri(eq(BASE_URL + "/ping/"));
323324
}
324325

325326
@Test

0 commit comments

Comments
 (0)