diff options
11 files changed, 145 insertions, 16 deletions
diff --git a/src/main/java/org/onap/crud/logging/LoggingUtil.java b/src/main/java/org/onap/crud/logging/LoggingUtil.java index f9cb905..31c0c1f 100644 --- a/src/main/java/org/onap/crud/logging/LoggingUtil.java +++ b/src/main/java/org/onap/crud/logging/LoggingUtil.java @@ -37,21 +37,29 @@ public class LoggingUtil { */ public static void initMdcContext(HttpServletRequest httpReq, HttpHeaders headers) { String fromIp = httpReq.getRemoteAddr(); - String fromAppId = ""; - String transId = null; - if (headers.getRequestHeaders().getFirst("X-FromAppId") != null) { + MdcContext.initialize(getTransactionId(headers), CrudServiceConstants.CRD_SERVICE_NAME, "", getAppId(headers), fromIp); + } + + public static String getAppId(HttpHeaders headers) { + String fromAppId = ""; + if (headers.getRequestHeaders().getFirst("X-FromMsId") != null) { + fromAppId = headers.getRequestHeaders().getFirst("X-FromMsId"); + } else if (headers.getRequestHeaders().getFirst("X-FromAppId") != null) { fromAppId = headers.getRequestHeaders().getFirst("X-FromAppId"); } - + return fromAppId; + } + + public static String getTransactionId(HttpHeaders headers) { + String transId = null; if ((headers.getRequestHeaders().getFirst("X-TransactionId") == null) - || headers.getRequestHeaders().getFirst("X-TransactionId").isEmpty()) { + || headers.getRequestHeaders().getFirst("X-TransactionId").isEmpty()) { transId = java.util.UUID.randomUUID().toString(); } else { transId = headers.getRequestHeaders().getFirst("X-TransactionId"); } - - MdcContext.initialize(transId, CrudServiceConstants.CRD_SERVICE_NAME, "", fromAppId, fromIp); + return transId; } /** diff --git a/src/test/java/org/onap/crud/service/TestDao.java b/src/test/java/org/onap/crud/dao/TestDao.java index 5af85f0..69ce4a3 100644 --- a/src/test/java/org/onap/crud/service/TestDao.java +++ b/src/test/java/org/onap/crud/dao/TestDao.java @@ -18,7 +18,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.crud.service; +package org.onap.crud.dao; import java.util.ArrayList; import java.util.HashSet; diff --git a/src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java b/src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java index e280c95..de56992 100644 --- a/src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java +++ b/src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java @@ -26,7 +26,7 @@ import org.junit.Test; import org.onap.crud.entity.Vertex; import org.onap.crud.event.GraphEvent.GraphEventOperation; import org.onap.crud.event.envelope.GraphEventEnvelope; -import org.onap.crud.test.util.TestUtil; +import org.onap.crud.util.TestUtil; import org.skyscreamer.jsonassert.Customization; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; diff --git a/src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java b/src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java index 9330556..1829496 100644 --- a/src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java +++ b/src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java @@ -24,7 +24,7 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import org.junit.Test; import org.onap.crud.event.envelope.GraphEventEnvelope; -import org.onap.crud.test.util.TestUtil; +import org.onap.crud.util.TestUtil; import com.google.gson.Gson; public class GraphEventResponseHandlerTest { diff --git a/src/test/java/org/onap/crud/logging/LoggingUtilTest.java b/src/test/java/org/onap/crud/logging/LoggingUtilTest.java new file mode 100644 index 0000000..bc1c9bd --- /dev/null +++ b/src/test/java/org/onap/crud/logging/LoggingUtilTest.java @@ -0,0 +1,52 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.logging; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.crud.service.util.TestHeaders; + +public class LoggingUtilTest { + + @Test + public void testGetAppId() throws Exception { + // When both MsId and AppId headers populated, return MsId header to log + TestHeaders headers = new TestHeaders(); + Assert.assertEquals("sending-service", LoggingUtil.getAppId(headers)); + + // When AppId header populated, return AppId header to log + headers = new TestHeaders(); + headers.clearRequestHeader("X-FromMsId"); + Assert.assertEquals("source-of-truth", LoggingUtil.getAppId(headers)); + + // When no headers populated, return empty string + headers = new TestHeaders(); + headers.clearRequestHeader("X-FromMsId", "X-FromAppId"); + Assert.assertEquals("", LoggingUtil.getAppId(headers)); + } + + @Test + public void testGetTransactionId() throws Exception { + TestHeaders headers = new TestHeaders(); + Assert.assertEquals("1234567890", LoggingUtil.getTransactionId(headers)); + } + +} diff --git a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java index b61f234..3d1ce12 100644 --- a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java +++ b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java @@ -34,7 +34,11 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mockito; +import org.onap.crud.dao.TestDao; import org.onap.crud.exception.CrudException; +import org.onap.crud.service.util.TestHeaders; +import org.onap.crud.service.util.TestRequest; +import org.onap.crud.service.util.TestUriInfo; import org.onap.schema.RelationshipSchemaLoader; @@ -232,6 +236,16 @@ public class CrudRestServiceTest { } @Test + public void testRequestHeaderWithMsId() throws CrudException { + thrown.expect(CrudException.class); + thrown.expectMessage("Invalid request, Missing X-FromAppId header"); + + TestHeaders testHeaders = new TestHeaders(); + testHeaders.clearRequestHeader("X-TransactionId", "X-FromAppId"); + mockService.validateRequestHeader(testHeaders); + } + + @Test public void testEmptyRequestHeader() throws CrudException { thrown.expect(CrudException.class); thrown.expectMessage("Invalid request, Missing X-FromAppId header"); diff --git a/src/test/java/org/onap/crud/service/TestHeaders.java b/src/test/java/org/onap/crud/service/util/TestHeaders.java index 835840f..aca276c 100644 --- a/src/test/java/org/onap/crud/service/TestHeaders.java +++ b/src/test/java/org/onap/crud/service/util/TestHeaders.java @@ -18,7 +18,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.crud.service; +package org.onap.crud.service.util; import java.util.Date; import java.util.List; @@ -37,8 +37,9 @@ public class TestHeaders implements HttpHeaders { public TestHeaders() { headers = new MultivaluedHashMap<String, String>(); - headers.add("X-FromAppId", "test-app"); - headers.add("X-TransactionId", "65f7e29c-57fd-45b2-bfd5-19e25c59110e"); + headers.add("X-FromMsId", "sending-service"); + headers.add("X-FromAppId", "source-of-truth"); + headers.add("X-TransactionId", "1234567890"); } @Override diff --git a/src/test/java/org/onap/crud/service/TestRequest.java b/src/test/java/org/onap/crud/service/util/TestRequest.java index f5ee7d6..a0c9a2e 100644 --- a/src/test/java/org/onap/crud/service/TestRequest.java +++ b/src/test/java/org/onap/crud/service/util/TestRequest.java @@ -18,7 +18,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.crud.service; +package org.onap.crud.service.util; import java.io.BufferedReader; import java.io.IOException; diff --git a/src/test/java/org/onap/crud/service/TestUriInfo.java b/src/test/java/org/onap/crud/service/util/TestUriInfo.java index 980f0da..f416d8b 100644 --- a/src/test/java/org/onap/crud/service/TestUriInfo.java +++ b/src/test/java/org/onap/crud/service/util/TestUriInfo.java @@ -18,7 +18,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.crud.service; +package org.onap.crud.service.util; import java.net.URI; import java.util.List; diff --git a/src/test/java/org/onap/crud/util/CrudServiceUtilTest.java b/src/test/java/org/onap/crud/util/CrudServiceUtilTest.java new file mode 100644 index 0000000..872586e --- /dev/null +++ b/src/test/java/org/onap/crud/util/CrudServiceUtilTest.java @@ -0,0 +1,54 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.util; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.crud.service.VertexPayload; +import org.onap.crud.service.util.TestHeaders; +import com.google.gson.JsonElement; + +public class CrudServiceUtilTest { + + private final String putVertexPayload = "{" + "\"id\": \"test-uuid\"," + "\"type\": \"pserver\"," + + "\"properties\": {" + "fqdn: myhost.onap.com," + "hostname: myhost } }"; + + @Test + public void testMergeHeaderInFoToPayload() throws Exception { + TestHeaders headers = new TestHeaders(); + // X-FromAppId is used to set the source of truth + VertexPayload payload = VertexPayload.fromJson(putVertexPayload); + + JsonElement properties = CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false); + Assert.assertEquals("myhost.onap.com", properties.getAsJsonObject().get("fqdn").getAsString()); + Assert.assertEquals("myhost", properties.getAsJsonObject().get("hostname").getAsString()); + Assert.assertEquals("source-of-truth", + properties.getAsJsonObject().get("last-mod-source-of-truth").getAsString()); + + properties = CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true); + Assert.assertEquals("myhost.onap.com", properties.getAsJsonObject().get("fqdn").getAsString()); + Assert.assertEquals("myhost", properties.getAsJsonObject().get("hostname").getAsString()); + Assert.assertEquals("source-of-truth", + properties.getAsJsonObject().get("last-mod-source-of-truth").getAsString()); + Assert.assertEquals("source-of-truth", properties.getAsJsonObject().get("source-of-truth").getAsString()); + } + +} diff --git a/src/test/java/org/onap/crud/test/util/TestUtil.java b/src/test/java/org/onap/crud/util/TestUtil.java index 1fcb46e..bfbc91b 100644 --- a/src/test/java/org/onap/crud/test/util/TestUtil.java +++ b/src/test/java/org/onap/crud/util/TestUtil.java @@ -18,7 +18,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.crud.test.util; +package org.onap.crud.util; import java.io.File; import java.io.IOException; |