diff options
author | pwielebs <piotr.wielebski@nokia.com> | 2018-06-04 15:43:27 +0200 |
---|---|---|
committer | pwielebs <piotr.wielebski@nokia.com> | 2018-06-04 15:43:27 +0200 |
commit | 53e33ec68a6eb0a06ea130215e03a7725da9d22d (patch) | |
tree | b39721025dbb39a5fb1487c72dd498ecd410b1fe /prh-app-server/src | |
parent | 04fdbd0e18a891241ed068044093a257ff506400 (diff) |
2nd fix for DmaapConsumerJsonParser
Change-Id: I0b5fa7e2a1e8472f72e636a84ef6d2285580b494
Issue-ID: DCAEGEN2-451
Signed-off-by: pwielebs <piotr.wielebski@nokia.com>
Diffstat (limited to 'prh-app-server/src')
2 files changed, 20 insertions, 24 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java index 04956f8d..ba0ec0e8 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java @@ -26,6 +26,7 @@ import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; +import java.util.Optional; import java.util.stream.StreamSupport; /** @@ -41,17 +42,16 @@ public class DmaapConsumerJsonParser { private static final String PNF_SERIAL_NUMBER = "pnfSerialNumber"; - private DmaapConsumerJsonParser() { - } + private DmaapConsumerJsonParser() {} public static ConsumerDmaapModel getJsonObject(String message) throws DmaapNotFoundException { JsonElement jsonElement = new JsonParser().parse(message); - JsonObject jsonObject; - jsonObject = jsonElement.isJsonObject() ? jsonElement.getAsJsonObject() : + JsonObject jsonObject = jsonElement.isJsonObject() ? jsonElement.getAsJsonObject() : StreamSupport.stream(jsonElement.getAsJsonArray().spliterator(), false).findFirst() - .orElseThrow(() -> new DmaapNotFoundException("Json object not found in json array")) + .flatMap(element -> Optional.of(new JsonParser().parse(element.toString()).getAsJsonObject())) + .orElseThrow(() -> new DmaapNotFoundException("Json object not found in json array")) .getAsJsonObject(); return create(jsonObject); diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParserTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParserTest.java index 067a528d..da293a45 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParserTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParserTest.java @@ -59,7 +59,7 @@ class DmaapConsumerJsonParserTest { + "\"pnfVendorName\":\"Nokia\"}}}]"; @Test - public void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException { + void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException { //given String message = "[{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\"" @@ -84,7 +84,7 @@ class DmaapConsumerJsonParserTest { @Test - public void whenPassingCorrectJsonWihoutIPV4_validationNotThrowingAnException() throws DmaapNotFoundException { + void whenPassingCorrectJsonWithoutIPV4_validationNotThrowingAnException() throws DmaapNotFoundException { //given String message = "[{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\"" @@ -108,7 +108,7 @@ class DmaapConsumerJsonParserTest { } @Test - public void whenPassingCorrectJsonWihoutIPV6_validationNotThrowingAnException() throws DmaapNotFoundException { + void whenPassingCorrectJsonWihoutIPV6_validationNotThrowingAnException() throws DmaapNotFoundException { //given String message = "[{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\"" @@ -131,7 +131,7 @@ class DmaapConsumerJsonParserTest { } @Test - public void whenPassingCorrectJsonWihoutIPV4andIPV6_validationThrowingAnException() { + void whenPassingCorrectJsonWihoutIPV4andIPV6_validationThrowingAnException() { String message = "[{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\"" + ":\"pnfRegistration_5GDU\",\"eventType\":\"pnfRegistration\",\"internalHeaderFields\":{}," @@ -142,29 +142,25 @@ class DmaapConsumerJsonParserTest { + ":1517206400,\"pnfManufactureDate\":1516406400,\"pnfModelNumber\":\"AJ02\",\"pnfSoftwareVersion\":" + "\"v4.5.0.1\",\"pnfType\":\"AirScale\",\"pnfVendorName\":\"Nokia\"}}}]"; - Assertions.assertThrows(DmaapNotFoundException.class, () -> { - DmaapConsumerJsonParser.getJsonObject(message); - }); + Assertions.assertThrows(DmaapNotFoundException.class, + () -> DmaapConsumerJsonParser.getJsonObject(message)); } @Test - public void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() { - Assertions.assertThrows(DmaapNotFoundException.class, () -> { - DmaapConsumerJsonParser.getJsonObject(incorrectMessage); - }); + void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() { + Assertions.assertThrows(DmaapNotFoundException.class, + () -> DmaapConsumerJsonParser.getJsonObject(incorrectMessage)); } @Test - public void whenPassingJsonWithoutPnfSerialNumberOrPnfVendorName_validationThrowingAnException() { - Assertions.assertThrows(DmaapNotFoundException.class, () -> { - DmaapConsumerJsonParser.getJsonObject(jsonWithoutPnfVendorAndSerialNumber); - }); + void whenPassingJsonWithoutPnfSerialNumberOrPnfVendorName_validationThrowingAnException() { + Assertions.assertThrows(DmaapNotFoundException.class, + () -> DmaapConsumerJsonParser.getJsonObject(jsonWithoutPnfVendorAndSerialNumber)); } @Test - public void whenPassingJsonWithoutIPInformation_validationThrowingAnException() { - Assertions.assertThrows(DmaapNotFoundException.class, () -> { - DmaapConsumerJsonParser.getJsonObject(jsonWithoutIPInformation); - }); + void whenPassingJsonWithoutIPInformation_validationThrowingAnException() { + Assertions.assertThrows(DmaapNotFoundException.class, + () -> DmaapConsumerJsonParser.getJsonObject(jsonWithoutIPInformation)); } }
\ No newline at end of file |