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=f0efddcfaba646255eec3779bee64e26bdbcc655;hb=964a97cbe18f38e8417366dfdf871fcfae908fc8;hp=5ba538e83c0ce15c4072babc2042779652c64947;hpb=889b38fd05bc9143647827742e3a8e0f10783bc8;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 5ba538e8..f0efddcf 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,10 +20,11 @@ package org.oransc.policyagent.dmaap; +import static ch.qos.logback.classic.Level.WARN; 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; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -31,7 +32,6 @@ import static org.mockito.Mockito.spy; 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; @@ -40,18 +40,20 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import java.io.IOException; +import java.nio.charset.Charset; 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.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.reactive.function.client.WebClientResponseException; @@ -59,11 +61,11 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti 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); + private final AsyncRestClient dmaapClient = mock(AsyncRestClient.class); private final AsyncRestClient agentClient = mock(AsyncRestClient.class); private DmaapMessageHandler testedObject; private static Gson gson = new GsonBuilder() // @@ -87,7 +89,8 @@ public class DmaapMessageHandlerTest { Optional 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") // @@ -95,7 +98,6 @@ public class DmaapMessageHandlerTest { .requestId("requestId") // .target("target") // .timestamp("timestamp") // - .type("type") // .url(URL) // .build(); } @@ -109,36 +111,43 @@ public class DmaapMessageHandlerTest { return Mono.just(entity); } + private Mono> notOkResponse() { + ResponseEntity entity = new ResponseEntity<>("NOK", HttpStatus.BAD_GATEWAY); + 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() { - final ListAppender logAppender = LoggingUtils.getLogListAppender(DmaapMessageHandler.class); + void unparseableMessage_thenWarning() { + final ListAppender logAppender = + LoggingUtils.getLogListAppender(DmaapMessageHandler.class, WARN); - testedObject.handleDmaapMsg("bad message"); + String msg = "bad message"; + testedObject.handleDmaapMsg(msg); - assertThat(logAppender.list.get(0).getLevel()).isEqualTo(Level.WARN); - assertThat(logAppender.list.toString().contains("handleDmaapMsg failure ")).isTrue(); + assertThat(logAppender.list.get(0).getFormattedMessage()).startsWith( + "handleDmaapMsg failure org.oransc.policyagent.exceptions.ServiceException: Received unparsable " + + "message from DMAAP: \"" + msg + "\", reason: "); } @Test - public void successfulDelete() throws IOException { + void successfulDelete() throws IOException { doReturn(okResponse()).when(agentClient).deleteForEntity(anyString()); - doReturn(1).when(dmaapClient).send(anyString()); - doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse(); + doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString()); String message = dmaapInputMessage(Operation.DELETE); @@ -151,16 +160,15 @@ public class DmaapMessageHandlerTest { verify(agentClient).deleteForEntity(URL); verifyNoMoreInteractions(agentClient); - verify(dmaapClient).send(anyString()); - verify(dmaapClient).sendBatchWithResponse(); + verify(dmaapClient).post(anyString(), anyString()); + verifyNoMoreInteractions(dmaapClient); } @Test - public void successfulGet() throws IOException { + void successfulGet() throws IOException { doReturn(okResponse()).when(agentClient).getForEntity(anyString()); - doReturn(1).when(dmaapClient).send(anyString()); - doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse(); + doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString()); StepVerifier // .create(testedObject.createTask(dmaapInputMessage(Operation.GET))) // @@ -171,16 +179,34 @@ public class DmaapMessageHandlerTest { verify(agentClient).getForEntity(URL); verifyNoMoreInteractions(agentClient); - verify(dmaapClient).send(anyString()); - verify(dmaapClient).sendBatchWithResponse(); + verify(dmaapClient).post(anyString(), anyString()); verifyNoMoreInteractions(dmaapClient); } @Test - public void successfulPut() throws IOException { + void exceptionFromAgentWhenGet_thenPostError() throws IOException { + String errorBody = "Unavailable"; + WebClientResponseException webClientResponseException = new WebClientResponseException( + HttpStatus.SERVICE_UNAVAILABLE.value(), "", (HttpHeaders) null, errorBody.getBytes(), (Charset) null); + doReturn(Mono.error(webClientResponseException)).when(agentClient).getForEntity(anyString()); + doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString()); + + StepVerifier // + .create(testedObject.createTask(dmaapInputMessage(Operation.GET))) // + .expectSubscription() // + .verifyComplete(); // + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(dmaapClient).post(anyString(), captor.capture()); + String actualMessage = captor.getValue(); + assertThat(actualMessage).contains(HttpStatus.SERVICE_UNAVAILABLE.toString()) // + .contains(errorBody); + } + + @Test + void successfulPut() throws IOException { doReturn(okResponse()).when(agentClient).putForEntity(anyString(), anyString()); - doReturn(1).when(dmaapClient).send(anyString()); - doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse(); + doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString()); StepVerifier // .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) // @@ -191,16 +217,14 @@ public class DmaapMessageHandlerTest { verify(agentClient).putForEntity(URL, payloadAsString()); verifyNoMoreInteractions(agentClient); - verify(dmaapClient).send(anyString()); - verify(dmaapClient).sendBatchWithResponse(); + verify(dmaapClient).post(anyString(), anyString()); verifyNoMoreInteractions(dmaapClient); } @Test - public void successfulPost() throws IOException { + void successfulPost() throws IOException { doReturn(okResponse()).when(agentClient).postForEntity(anyString(), anyString()); - doReturn(1).when(dmaapClient).send(anyString()); - doReturn(new MRPublisherResponse()).when(dmaapClient).sendBatchWithResponse(); + doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString()); StepVerifier // .create(testedObject.createTask(dmaapInputMessage(Operation.POST))) // @@ -211,37 +235,32 @@ public class DmaapMessageHandlerTest { verify(agentClient).postForEntity(URL, payloadAsString()); verifyNoMoreInteractions(agentClient); - verify(dmaapClient).send(anyString()); - verify(dmaapClient).sendBatchWithResponse(); + verify(dmaapClient).post(anyString(), anyString()); verifyNoMoreInteractions(dmaapClient); } @Test - 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(); + void exceptionWhenCallingPolicyAgent_thenNotFoundResponse() throws IOException { - StepVerifier // - .create(testedObject.createTask(dmaapInputMessage(Operation.PUT))) // - .expectSubscription() // - .verifyComplete(); // + doReturn(notOkResponse()).when(agentClient).putForEntity(anyString(), anyString()); + doReturn(Mono.just("OK")).when(dmaapClient).post(anyString(), anyString()); + + testedObject.createTask(dmaapInputMessage(Operation.PUT)).block(); verify(agentClient).putForEntity(anyString(), anyString()); verifyNoMoreInteractions(agentClient); ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(dmaapClient).send(captor.capture()); + verify(dmaapClient).post(anyString(), captor.capture()); String actualMessage = captor.getValue(); - assertThat(actualMessage.contains(HttpStatus.BAD_REQUEST.toString())).isTrue(); + assertThat(actualMessage).as("Message \"%s\" sent to DMaaP contains %s", actualMessage, HttpStatus.BAD_GATEWAY) + .contains(HttpStatus.BAD_GATEWAY.toString()); - 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); @@ -249,25 +268,23 @@ public class DmaapMessageHandlerTest { testedObject.handleDmaapMsg(message); ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); - verify(dmaapClient).send(captor.capture()); + verify(dmaapClient).post(anyString(), captor.capture()); String actualMessage = captor.getValue(); - assertThat(actualMessage.contains(HttpStatus.NOT_FOUND + "\",\"message\":\"Not implemented operation:")) - .isTrue(); - - verify(dmaapClient).sendBatchWithResponse(); - verifyNoMoreInteractions(dmaapClient); + assertThat(actualMessage).contains("Not implemented operation") // + .contains("BAD_REQUEST"); } @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\"}", ""); - final ListAppender logAppender = LoggingUtils.getLogListAppender(DmaapMessageHandler.class); + final ListAppender logAppender = + LoggingUtils.getLogListAppender(DmaapMessageHandler.class, WARN); testedObject.handleDmaapMsg(message); - assertThat(logAppender.list.get(0).getLevel()).isEqualTo(Level.WARN); - assertThat(logAppender.list.toString().contains("Expected payload in message from DMAAP: ")).isTrue(); + assertThat(logAppender.list.get(0).getFormattedMessage()) + .startsWith("Expected payload in message from DMAAP: "); } }