Fix DataRouter Subscriber NPE on No Metadata
Issue-ID: DCAEGEN2-1232
Change-Id: I692d65c460f2974535d5b9addc4d889c530cf951
Signed-off-by: JoeOLeary <joseph.o.leary@est.tech>
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java
index fc623bd..40efde5 100644
--- a/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java
+++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java
@@ -155,6 +155,14 @@
}
}
+ private EventMetadata getMetadata(HttpServerExchange httpServerExchange) throws NoMetadataException {
+ String metadata = Optional.ofNullable(httpServerExchange.getRequestHeaders()
+ .get(METADATA_HEADER))
+ .map((HeaderValues headerValues) -> headerValues.get(0))
+ .orElseThrow(() -> new NoMetadataException("Metadata Not found"));
+ return metadataBuilder.fromJson(metadata, EventMetadata.class);
+ }
+
/**
* Receives inbound requests, verifies that required headers are valid
* and passes an Event onto the eventReceiver.
@@ -172,16 +180,13 @@
.send(StatusCodes.SERVICE_UNAVAILABLE_STRING);
} else {
try {
- String metadataAsString = Optional.of(httpServerExchange.getRequestHeaders()
- .get(METADATA_HEADER))
- .map((HeaderValues headerValues) -> headerValues.get(0))
- .orElseThrow(() -> new NoMetadataException("Metadata Not found"));
+
Map<String,String> mdc = MDC.getCopyOfContextMap();
- EventMetadata metadata = metadataBuilder.fromJson(metadataAsString, EventMetadata.class);
+ EventMetadata metadata = getMetadata(httpServerExchange);
httpServerExchange.getRequestReceiver()
- .receiveFullString((callbackExchange, body) -> {
- httpServerExchange.dispatch(() -> eventReceiver.receive(new Event(callbackExchange, body, metadata, mdc)));
- });
+ .receiveFullString((callbackExchange, body) ->
+ httpServerExchange.dispatch(() -> eventReceiver.receive(new Event(callbackExchange, body, metadata, mdc)))
+ );
} catch (NoMetadataException exception) {
logger.unwrap().info("Bad Request: no metadata found under '{}' header.", METADATA_HEADER, exception);
httpServerExchange.setStatusCode(StatusCodes.BAD_REQUEST)
diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java
index a40e28b..ad73b63 100644
--- a/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java
+++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java
@@ -39,6 +39,7 @@
import io.undertow.io.Receiver;
import io.undertow.io.Sender;
import io.undertow.server.HttpServerExchange;
+import io.undertow.util.HeaderMap;
import io.undertow.util.StatusCodes;
import utils.LoggingUtils;
@@ -186,8 +187,11 @@
public void testRequestInboundNoMetadata() throws Exception {
HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS);
Receiver receiver = mock(Receiver.class);
+ HeaderMap headers = mock(HeaderMap.class);
when(httpServerExchange.getRequestReceiver()).thenReturn(receiver);
when(httpServerExchange.setStatusCode(anyInt())).thenReturn(httpServerExchange);
+ when(httpServerExchange.getRequestHeaders()).thenReturn(headers);
+ when(headers.get(any(String.class))).thenReturn(null);
doAnswer((Answer<Void>) invocationOnMock -> {
Receiver.FullStringCallback callback = invocationOnMock.getArgument(0);