Remove Sonar warnings
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / dmaap / DmaapMessageHandlerTest.java
index 67d00a2..f1ae7fb 100644 (file)
@@ -22,6 +22,7 @@ package org.oransc.policyagent.dmaap;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
@@ -52,13 +53,17 @@ import org.oransc.policyagent.dmaap.DmaapRequestMessage.Operation;
 import org.oransc.policyagent.repository.ImmutablePolicyType;
 import org.oransc.policyagent.repository.PolicyType;
 import org.oransc.policyagent.utils.LoggingUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
 
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
-public class DmaapMessageHandlerTest {
-
+class DmaapMessageHandlerTest {
+    private static final Logger logger = LoggerFactory.getLogger(DmaapMessageHandlerTest.class);
     private static final String URL = "url";
 
     private final MRBatchingPublisher dmaapClient = mock(MRBatchingPublisher.class);
@@ -85,7 +90,8 @@ public class DmaapMessageHandlerTest {
         Optional<JsonObject> payload =
             ((operation == Operation.PUT || operation == Operation.POST) ? Optional.of(payloadAsJson())
                 : Optional.empty());
-        return ImmutableDmaapRequestMessage.builder().apiVersion("apiVersion") //
+        return ImmutableDmaapRequestMessage.builder() //
+            .apiVersion("apiVersion") //
             .correlationId("correlationId") //
             .operation(operation) //
             .originatorId("originatorId") //
@@ -93,7 +99,6 @@ public class DmaapMessageHandlerTest {
             .requestId("requestId") //
             .target("target") //
             .timestamp("timestamp") //
-            .type("type") //
             .url(URL) //
             .build();
     }
@@ -102,23 +107,28 @@ public class DmaapMessageHandlerTest {
         return gson.toJson(dmaapRequestMessage(operation));
     }
 
+    private Mono<ResponseEntity<String>> okResponse() {
+        ResponseEntity<String> entity = new ResponseEntity<>("OK", HttpStatus.OK);
+        return Mono.just(entity);
+    }
+
     @Test
-    public void testMessageParsing() {
+    void testMessageParsing() {
         String message = dmaapInputMessage(Operation.DELETE);
-        System.out.println(message);
+        logger.info(message);
         DmaapRequestMessage parsedMessage = gson.fromJson(message, ImmutableDmaapRequestMessage.class);
-        assertTrue(parsedMessage != null);
+        assertNotNull(parsedMessage);
         assertFalse(parsedMessage.payload().isPresent());
 
         message = dmaapInputMessage(Operation.PUT);
-        System.out.println(message);
+        logger.info(message);
         parsedMessage = gson.fromJson(message, ImmutableDmaapRequestMessage.class);
-        assertTrue(parsedMessage != null);
+        assertNotNull(parsedMessage);
         assertTrue(parsedMessage.payload().isPresent());
     }
 
     @Test
-    public void unparseableMessage_thenWarning() {
+    void unparseableMessage_thenWarning() {
         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapMessageHandler.class);
 
         testedObject.handleDmaapMsg("bad message");
@@ -128,8 +138,8 @@ public class DmaapMessageHandlerTest {
     }
 
     @Test
-    public void successfulDelete() throws IOException {
-        doReturn(Mono.just("OK")).when(agentClient).delete(anyString());
+    void successfulDelete() throws IOException {
+        doReturn(okResponse()).when(agentClient).deleteForEntity(anyString());
         doReturn(1).when(dmaapClient).send(anyString());
         doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
 
@@ -141,7 +151,7 @@ public class DmaapMessageHandlerTest {
             .expectNext("OK") //
             .verifyComplete(); //
 
-        verify(agentClient).delete(URL);
+        verify(agentClient).deleteForEntity(URL);
         verifyNoMoreInteractions(agentClient);
 
         verify(dmaapClient).send(anyString());
@@ -150,8 +160,8 @@ public class DmaapMessageHandlerTest {
     }
 
     @Test
-    public void successfulGet() throws IOException {
-        doReturn(Mono.just("OK")).when(agentClient).get(anyString());
+    void successfulGet() throws IOException {
+        doReturn(okResponse()).when(agentClient).getForEntity(anyString());
         doReturn(1).when(dmaapClient).send(anyString());
         doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
 
@@ -161,7 +171,7 @@ public class DmaapMessageHandlerTest {
             .expectNext("OK") //
             .verifyComplete(); //
 
-        verify(agentClient).get(URL);
+        verify(agentClient).getForEntity(URL);
         verifyNoMoreInteractions(agentClient);
 
         verify(dmaapClient).send(anyString());
@@ -170,8 +180,8 @@ public class DmaapMessageHandlerTest {
     }
 
     @Test
-    public void successfulPut() throws IOException {
-        doReturn(Mono.just("OK")).when(agentClient).put(anyString(), anyString());
+    void successfulPut() throws IOException {
+        doReturn(okResponse()).when(agentClient).putForEntity(anyString(), anyString());
         doReturn(1).when(dmaapClient).send(anyString());
         doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
 
@@ -181,7 +191,7 @@ public class DmaapMessageHandlerTest {
             .expectNext("OK") //
             .verifyComplete(); //
 
-        verify(agentClient).put(URL, payloadAsString());
+        verify(agentClient).putForEntity(URL, payloadAsString());
         verifyNoMoreInteractions(agentClient);
 
         verify(dmaapClient).send(anyString());
@@ -190,8 +200,8 @@ public class DmaapMessageHandlerTest {
     }
 
     @Test
-    public void successfulPost() throws IOException {
-        doReturn(Mono.just("OK")).when(agentClient).post(anyString(), anyString());
+    void successfulPost() throws IOException {
+        doReturn(okResponse()).when(agentClient).postForEntity(anyString(), anyString());
         doReturn(1).when(dmaapClient).send(anyString());
         doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
 
@@ -201,7 +211,7 @@ public class DmaapMessageHandlerTest {
             .expectNext("OK") //
             .verifyComplete(); //
 
-        verify(agentClient).post(URL, payloadAsString());
+        verify(agentClient).postForEntity(URL, payloadAsString());
         verifyNoMoreInteractions(agentClient);
 
         verify(dmaapClient).send(anyString());
@@ -210,9 +220,9 @@ public class DmaapMessageHandlerTest {
     }
 
     @Test
-    public void exceptionWhenCallingPolicyAgent_thenNotFoundResponse() throws IOException {
-        String errorCause = "Refused";
-        doReturn(Mono.error(new Exception(errorCause))).when(agentClient).put(anyString(), any());
+    void exceptionWhenCallingPolicyAgent_thenNotFoundResponse() throws IOException {
+        WebClientResponseException except = new WebClientResponseException(400, "Refused", null, null, null, null);
+        doReturn(Mono.error(except)).when(agentClient).putForEntity(anyString(), any());
         doReturn(1).when(dmaapClient).send(anyString());
         doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse();
 
@@ -221,21 +231,20 @@ public class DmaapMessageHandlerTest {
             .expectSubscription() //
             .verifyComplete(); //
 
-        verify(agentClient).put(anyString(), anyString());
+        verify(agentClient).putForEntity(anyString(), anyString());
         verifyNoMoreInteractions(agentClient);
 
         ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
         verify(dmaapClient).send(captor.capture());
         String actualMessage = captor.getValue();
-        assertThat(actualMessage.contains(HttpStatus.NOT_FOUND + "\",\"message\":\"java.lang.Exception: " + errorCause))
-            .isTrue();
+        assertThat(actualMessage.contains(HttpStatus.BAD_REQUEST.toString())).isTrue();
 
         verify(dmaapClient).sendBatchWithResponse();
         verifyNoMoreInteractions(dmaapClient);
     }
 
     @Test
-    public void unsupportedOperationInMessage_thenNotFoundResponseWithNotImplementedOperation() throws Exception {
+    void unsupportedOperationInMessage_thenNotFoundResponseWithNotImplementedOperation() throws Exception {
         String message = dmaapInputMessage(Operation.PUT).toString();
         String badOperation = "BAD";
         message = message.replace(Operation.PUT.toString(), badOperation);
@@ -246,14 +255,14 @@ public class DmaapMessageHandlerTest {
         verify(dmaapClient).send(captor.capture());
         String actualMessage = captor.getValue();
         assertThat(actualMessage
-            .contains(HttpStatus.NOT_FOUND + "\",\"message\":\"Not implemented operation: " + badOperation)).isTrue();
+            .contains(HttpStatus.BAD_REQUEST + "\",\"message\":\"Not implemented operation: " + badOperation)).isTrue();
 
         verify(dmaapClient).sendBatchWithResponse();
         verifyNoMoreInteractions(dmaapClient);
     }
 
     @Test
-    public void putWithoutPayload_thenNotFoundResponseWithWarning() throws Exception {
+    void putWithoutPayload_thenNotFoundResponseWithWarning() throws Exception {
         String message = dmaapInputMessage(Operation.PUT).toString();
         message = message.replace(",\"payload\":{\"name\":\"name\",\"schema\":\"schema\"}", "");