X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fdmaap%2FDmaapMessageHandlerTest.java;h=52147a85a7c79b703049de71be4f0f8b469912a5;hb=d917ed06c852dbdd282d7aeec6e3f77e83e8c791;hp=6576c699d9147f25299634a06f504e55b64d31a9;hpb=72c98f3b7ef556cf3556c398e12078d9b9eb6d1a;p=nonrtric.git diff --git a/policy-agent/src/test/java/org/oransc/policyagent/dmaap/DmaapMessageHandlerTest.java b/policy-agent/src/test/java/org/oransc/policyagent/dmaap/DmaapMessageHandlerTest.java index 6576c699..52147a85 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/dmaap/DmaapMessageHandlerTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/dmaap/DmaapMessageHandlerTest.java @@ -20,6 +20,7 @@ package org.oransc.policyagent.dmaap; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; @@ -27,10 +28,13 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; @@ -40,18 +44,25 @@ import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import org.onap.dmaap.mr.client.MRBatchingPublisher; import org.onap.dmaap.mr.client.response.MRPublisherResponse; import org.oransc.policyagent.clients.AsyncRestClient; 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 { - + private static final Logger logger = LoggerFactory.getLogger(DmaapMessageHandlerTest.class); private static final String URL = "url"; private final MRBatchingPublisher dmaapClient = mock(MRBatchingPublisher.class); @@ -95,24 +106,39 @@ public class DmaapMessageHandlerTest { return gson.toJson(dmaapRequestMessage(operation)); } + private Mono> okResponse() { + ResponseEntity entity = new ResponseEntity<>("OK", HttpStatus.OK); + return Mono.just(entity); + } + @Test public void testMessageParsing() { String message = dmaapInputMessage(Operation.DELETE); - System.out.println(message); + logger.info(message); DmaapRequestMessage parsedMessage = gson.fromJson(message, ImmutableDmaapRequestMessage.class); assertTrue(parsedMessage != null); assertFalse(parsedMessage.payload().isPresent()); message = dmaapInputMessage(Operation.PUT); - System.out.println(message); + logger.info(message); parsedMessage = gson.fromJson(message, ImmutableDmaapRequestMessage.class); assertTrue(parsedMessage != null); assertTrue(parsedMessage.payload().isPresent()); } + @Test + public void unparseableMessage_thenWarning() { + final ListAppender logAppender = LoggingUtils.getLogListAppender(DmaapMessageHandler.class); + + testedObject.handleDmaapMsg("bad message"); + + assertThat(logAppender.list.get(0).getLevel()).isEqualTo(Level.WARN); + assertThat(logAppender.list.toString().contains("handleDmaapMsg failure ")).isTrue(); + } + @Test public void successfulDelete() throws IOException { - doReturn(Mono.just("OK")).when(agentClient).delete(anyString()); + doReturn(okResponse()).when(agentClient).deleteForEntity(anyString()); doReturn(1).when(dmaapClient).send(anyString()); doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse(); @@ -124,17 +150,17 @@ public class DmaapMessageHandlerTest { .expectNext("OK") // .verifyComplete(); // - verify(agentClient, times(1)).delete(URL); + verify(agentClient).deleteForEntity(URL); verifyNoMoreInteractions(agentClient); - verify(dmaapClient, times(1)).send(anyString()); - verify(dmaapClient, times(1)).sendBatchWithResponse(); + verify(dmaapClient).send(anyString()); + verify(dmaapClient).sendBatchWithResponse(); verifyNoMoreInteractions(dmaapClient); } @Test public void successfulGet() throws IOException { - doReturn(Mono.just("OK")).when(agentClient).get(anyString()); + doReturn(okResponse()).when(agentClient).getForEntity(anyString()); doReturn(1).when(dmaapClient).send(anyString()); doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse(); @@ -144,17 +170,17 @@ public class DmaapMessageHandlerTest { .expectNext("OK") // .verifyComplete(); // - verify(agentClient, times(1)).get(URL); + verify(agentClient).getForEntity(URL); verifyNoMoreInteractions(agentClient); - verify(dmaapClient, times(1)).send(anyString()); - verify(dmaapClient, times(1)).sendBatchWithResponse(); + verify(dmaapClient).send(anyString()); + verify(dmaapClient).sendBatchWithResponse(); verifyNoMoreInteractions(dmaapClient); } @Test public void successfulPut() throws IOException { - doReturn(Mono.just("OK")).when(agentClient).put(anyString(), anyString()); + doReturn(okResponse()).when(agentClient).putForEntity(anyString(), anyString()); doReturn(1).when(dmaapClient).send(anyString()); doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse(); @@ -164,17 +190,17 @@ public class DmaapMessageHandlerTest { .expectNext("OK") // .verifyComplete(); // - verify(agentClient, times(1)).put(URL, payloadAsString()); + verify(agentClient).putForEntity(URL, payloadAsString()); verifyNoMoreInteractions(agentClient); - verify(dmaapClient, times(1)).send(anyString()); - verify(dmaapClient, times(1)).sendBatchWithResponse(); + verify(dmaapClient).send(anyString()); + verify(dmaapClient).sendBatchWithResponse(); verifyNoMoreInteractions(dmaapClient); } @Test public void successfulPost() throws IOException { - doReturn(Mono.just("OK")).when(agentClient).post(anyString(), anyString()); + doReturn(okResponse()).when(agentClient).postForEntity(anyString(), anyString()); doReturn(1).when(dmaapClient).send(anyString()); doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse(); @@ -184,31 +210,66 @@ public class DmaapMessageHandlerTest { .expectNext("OK") // .verifyComplete(); // - verify(agentClient, times(1)).post(URL, payloadAsString()); + verify(agentClient).postForEntity(URL, payloadAsString()); verifyNoMoreInteractions(agentClient); - verify(dmaapClient, times(1)).send(anyString()); - verify(dmaapClient, times(1)).sendBatchWithResponse(); + verify(dmaapClient).send(anyString()); + verify(dmaapClient).sendBatchWithResponse(); verifyNoMoreInteractions(dmaapClient); } @Test - public void errorCase() throws IOException { - doReturn(Mono.error(new Exception("Refused"))).when(agentClient).put(anyString(), any()); + public 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(); + StepVerifier // .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) // .expectSubscription() // .verifyComplete(); // - verify(agentClient, times(1)).put(anyString(), anyString()); + verify(agentClient).putForEntity(anyString(), anyString()); verifyNoMoreInteractions(agentClient); - // Error response - verify(dmaapClient, times(1)).send(anyString()); - verify(dmaapClient, times(1)).sendBatchWithResponse(); + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(dmaapClient).send(captor.capture()); + String actualMessage = captor.getValue(); + assertThat(actualMessage.contains(HttpStatus.BAD_REQUEST.toString())).isTrue(); + + verify(dmaapClient).sendBatchWithResponse(); verifyNoMoreInteractions(dmaapClient); } + @Test + public void unsupportedOperationInMessage_thenNotFoundResponseWithNotImplementedOperation() throws Exception { + String message = dmaapInputMessage(Operation.PUT).toString(); + String badOperation = "BAD"; + message = message.replace(Operation.PUT.toString(), badOperation); + + testedObject.handleDmaapMsg(message); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(dmaapClient).send(captor.capture()); + String actualMessage = captor.getValue(); + assertThat(actualMessage + .contains(HttpStatus.BAD_REQUEST + "\",\"message\":\"Not implemented operation: " + badOperation)).isTrue(); + + verify(dmaapClient).sendBatchWithResponse(); + verifyNoMoreInteractions(dmaapClient); + } + + @Test + public void putWithoutPayload_thenNotFoundResponseWithWarning() throws Exception { + String message = dmaapInputMessage(Operation.PUT).toString(); + message = message.replace(",\"payload\":{\"name\":\"name\",\"schema\":\"schema\"}", ""); + + final ListAppender logAppender = LoggingUtils.getLogListAppender(DmaapMessageHandler.class); + + testedObject.handleDmaapMsg(message); + + assertThat(logAppender.list.get(0).getLevel()).isEqualTo(Level.WARN); + assertThat(logAppender.list.toString().contains("Expected payload in message from DMAAP: ")).isTrue(); + } }