diff options
-rw-r--r-- | prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java | 8 | ||||
-rw-r--r-- | prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java | 29 | ||||
-rw-r--r-- | prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilder.java (renamed from prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilderImpl.java) | 15 | ||||
-rw-r--r-- | prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilderTest.java | 24 |
4 files changed, 36 insertions, 40 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java index 9cec7779..1a528180 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java @@ -20,10 +20,9 @@ package org.onap.dcaegen2.services.prh.tasks; -import com.google.gson.JsonPrimitive; import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.prh.model.PnfReadyJsonBodyBuilderImpl; +import org.onap.dcaegen2.services.prh.model.PnfReadyJsonBodyBuilder; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterPublisher; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse; @@ -42,7 +41,7 @@ public class DmaapPublisherTaskImpl implements DmaapPublisherTask { private final Supplier<MessageRouterPublishRequest> config; private final MessageRouterPublisherResolver messageRouterPublisherClientResolver; - private final PnfReadyJsonBodyBuilderImpl pnfReadyJsonBodyBuilder = new PnfReadyJsonBodyBuilderImpl(); + private final PnfReadyJsonBodyBuilder pnfReadyJsonBodyBuilder = new PnfReadyJsonBodyBuilder(); public DmaapPublisherTaskImpl(Supplier<MessageRouterPublishRequest> config, MessageRouterPublisherResolver messageRouterPublisherClientResolver) { @@ -57,9 +56,8 @@ public class DmaapPublisherTaskImpl implements DmaapPublisherTask { } MessageRouterPublisher messageRouterPublisher = messageRouterPublisherClientResolver.resolveClient(); LOGGER.info("Method called with arg {}", consumerDmaapModel); - String json = pnfReadyJsonBodyBuilder.createJsonBody(consumerDmaapModel); return messageRouterPublisher.put( config.get(), - Flux.just(json).map(JsonPrimitive::new)); + Flux.just(pnfReadyJsonBodyBuilder.createJsonBody(consumerDmaapModel))); } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java index 6347ad3d..6f38d87d 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java @@ -20,7 +20,8 @@ package org.onap.dcaegen2.services.prh.tasks; -import com.google.gson.JsonPrimitive; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -38,10 +39,8 @@ import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRout import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishRequest; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest; import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; -import java.util.function.Supplier; - -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; @@ -53,30 +52,27 @@ import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class DmaapPublisherTaskImplTest { - private static DmaapPublisherTaskImpl dmaapPublisherTask; + private DmaapPublisherTaskImpl dmaapPublisherTask; + + private MessageRouterPublishRequest mrRequest = createMRRequest(); @Mock private static MessageRouterPublisherResolver messageRouterPublisherClientResolver; @Mock private static MessageRouterPublisher messageRouterPublisher; - private Supplier<MessageRouterPublishRequest> configSupplier; - - @Captor - private ArgumentCaptor<Flux<JsonPrimitive>> fluxCaptor; + private ArgumentCaptor<Flux<JsonElement>> fluxCaptor; @BeforeEach void beforeEach() { when(messageRouterPublisherClientResolver.resolveClient()).thenReturn(messageRouterPublisher); - MessageRouterPublishRequest mrRequest = createMRRequest(); - configSupplier = () -> mrRequest; } @Test void execute_whenPassedObjectDoesntFit_ThrowsPrhTaskException() { //given - dmaapPublisherTask = new DmaapPublisherTaskImpl(configSupplier, messageRouterPublisherClientResolver); + dmaapPublisherTask = new DmaapPublisherTaskImpl(() -> mrRequest, messageRouterPublisherClientResolver); //when Executable executableFunction = () -> dmaapPublisherTask.execute(null); //then @@ -86,12 +82,15 @@ class DmaapPublisherTaskImplTest { @Test void execute_whenPassedObjectFits_ReturnsCorrectStatus() throws DmaapNotFoundException { //given - dmaapPublisherTask = new DmaapPublisherTaskImpl(configSupplier, messageRouterPublisherClientResolver); + dmaapPublisherTask = new DmaapPublisherTaskImpl(() -> mrRequest, messageRouterPublisherClientResolver); //when dmaapPublisherTask.execute(createConsumerDmaapModel()); //then - verify(messageRouterPublisher).put(eq(configSupplier.get()), fluxCaptor.capture()); - assertEquals(new JsonPrimitive("{\"correlationId\":\"NOKQTFCOC540002E\"}"), fluxCaptor.getValue().blockFirst()); + verify(messageRouterPublisher).put(eq(mrRequest), fluxCaptor.capture()); + + StepVerifier.create(fluxCaptor.getValue()) + .expectNext(new JsonParser().parse("{\"correlationId\":\"NOKQTFCOC540002E\"}")) + .verifyComplete(); } diff --git a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilderImpl.java b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilder.java index 8584f01f..b39c4690 100644 --- a/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilderImpl.java +++ b/prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilder.java @@ -20,16 +20,13 @@ package org.onap.dcaegen2.services.prh.model; -import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.TypeAdapterFactory; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel.Builder; -import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder; +import org.onap.dcaegen2.services.prh.model.utils.PrhModelAwareGsonBuilder; -import java.util.ServiceLoader; - -public class PnfReadyJsonBodyBuilderImpl implements JsonBodyBuilder<ConsumerDmaapModel> { +public class PnfReadyJsonBodyBuilder { /** * Method for serialization object by GSON. @@ -37,9 +34,7 @@ public class PnfReadyJsonBodyBuilderImpl implements JsonBodyBuilder<ConsumerDmaa * @param consumerDmaapModel - object which will be serialized * @return string from serialization */ - public String createJsonBody(ConsumerDmaapModel consumerDmaapModel) { - GsonBuilder gsonBuilder = new GsonBuilder(); - ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory); + public JsonElement createJsonBody(ConsumerDmaapModel consumerDmaapModel) { Builder builder = ImmutableConsumerDmaapModel.builder() .correlationId(consumerDmaapModel.getCorrelationId()); @@ -47,6 +42,6 @@ public class PnfReadyJsonBodyBuilderImpl implements JsonBodyBuilder<ConsumerDmaa if(additionalFields != null && !additionalFields.equals(new JsonObject())) { builder.additionalFields(additionalFields); } - return gsonBuilder.create().toJson(builder.build()); + return PrhModelAwareGsonBuilder.createGson().toJsonTree(builder.build()); } }
\ No newline at end of file diff --git a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilderTest.java b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilderTest.java index e104f54e..769e1673 100644 --- a/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilderTest.java +++ b/prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/PnfReadyJsonBodyBuilderTest.java @@ -31,23 +31,23 @@ class PnfReadyJsonBodyBuilderTest { @Test void createJsonBody_shouldReturnJsonInString() { - JsonObject jsonObject = new JsonParser().parse("{\n" + JsonObject jsonObject = parse("{\n" + " \"attachmentPoint\": \"bla-bla-30-3\",\n" + " \"cvlan\": \"678\",\n" + " \"svlan\": \"1005\"\n" - + " }").getAsJsonObject(); + + " }"); ConsumerDmaapModel model = ImmutableConsumerDmaapModel.builder() .correlationId("NOKnhfsadhff") .additionalFields(jsonObject) .build(); - String expectedResult = "{" + JsonObject expectedResult = parse("{" + "\"correlationId\":\"NOKnhfsadhff\"," + "\"additionalFields\":{\"attachmentPoint\":\"bla-bla-30-3\",\"cvlan\":\"678\",\"svlan\":\"1005\"}" - + "}"; + + "}"); - assertEquals(expectedResult, new PnfReadyJsonBodyBuilderImpl().createJsonBody(model)); + assertEquals(expectedResult, new PnfReadyJsonBodyBuilder().createJsonBody(model)); } @Test @@ -57,22 +57,26 @@ class PnfReadyJsonBodyBuilderTest { .correlationId("NOKnhfsadhff") .build(); - String expectedResult = "{\"correlationId\":\"NOKnhfsadhff\"}"; + JsonObject expectedResult = parse("{\"correlationId\":\"NOKnhfsadhff\"}"); - assertEquals(expectedResult, new PnfReadyJsonBodyBuilderImpl().createJsonBody(model)); + assertEquals(expectedResult, new PnfReadyJsonBodyBuilder().createJsonBody(model)); } @Test void createJsonBodyWithEmptyOptionalPnfRegistrationFields_shouldReturnJsonInString() { - JsonObject jsonObject = new JsonParser().parse("{}").getAsJsonObject(); + JsonObject jsonObject = new JsonObject(); ConsumerDmaapModel model = ImmutableConsumerDmaapModel.builder() .correlationId("NOKnhfsadhff") .additionalFields(jsonObject) .build(); - String expectedResult = "{\"correlationId\":\"NOKnhfsadhff\"}"; + JsonObject expectedResult = parse("{\"correlationId\":\"NOKnhfsadhff\"}"); - assertEquals(expectedResult, new PnfReadyJsonBodyBuilderImpl().createJsonBody(model)); + assertEquals(expectedResult, new PnfReadyJsonBodyBuilder().createJsonBody(model)); + } + + private static JsonObject parse(String jsonString) { + return new JsonParser().parse(jsonString).getAsJsonObject(); } } |