diff options
Diffstat (limited to 'prh-commons/src')
-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 |
2 files changed, 19 insertions, 20 deletions
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(); } } |