Add tests for X-FromMsId 99/50699/1
authorSotiropoulos, Ioannis (is948x) <Ioannis.Sotiropoulos@amdocs.com>
Wed, 6 Jun 2018 14:37:35 +0000 (15:37 +0100)
committerSotiropoulos, Ioannis (is948x) <Ioannis.Sotiropoulos@amdocs.com>
Wed, 6 Jun 2018 14:37:35 +0000 (15:37 +0100)
Add tests for X-FromMsId header parameter changes

Issue-ID: AAI-1198

Change-Id: I6342cf95e5c635b500245bfa90040376ffa1b992
Signed-off-by: Sotiropoulos, Ioannis (is948x) <Ioannis.Sotiropoulos@amdocs.com>
src/main/java/org/onap/crud/logging/LoggingUtil.java
src/test/java/org/onap/crud/dao/TestDao.java [moved from src/test/java/org/onap/crud/service/TestDao.java with 99% similarity]
src/test/java/org/onap/crud/event/GraphEventEnvelopeTest.java
src/test/java/org/onap/crud/event/response/GraphEventResponseHandlerTest.java
src/test/java/org/onap/crud/logging/LoggingUtilTest.java [new file with mode: 0644]
src/test/java/org/onap/crud/service/CrudRestServiceTest.java
src/test/java/org/onap/crud/service/util/TestHeaders.java [moved from src/test/java/org/onap/crud/service/TestHeaders.java with 92% similarity]
src/test/java/org/onap/crud/service/util/TestRequest.java [moved from src/test/java/org/onap/crud/service/TestRequest.java with 99% similarity]
src/test/java/org/onap/crud/service/util/TestUriInfo.java [moved from src/test/java/org/onap/crud/service/TestUriInfo.java with 99% similarity]
src/test/java/org/onap/crud/util/CrudServiceUtilTest.java [new file with mode: 0644]
src/test/java/org/onap/crud/util/TestUtil.java [moved from src/test/java/org/onap/crud/test/util/TestUtil.java with 98% similarity]

index f9cb905..31c0c1f 100644 (file)
@@ -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;
   }
 
   /**
@@ -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;
index e280c95..de56992 100644 (file)
@@ -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;
index 9330556..1829496 100644 (file)
@@ -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 (file)
index 0000000..bc1c9bd
--- /dev/null
@@ -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));
+    }
+
+}
index b61f234..3d1ce12 100644 (file)
@@ -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;
 
 
@@ -231,6 +235,16 @@ public class CrudRestServiceTest {
     mockService.validateRequestHeader(testHeaders);
   }
 
+  @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);
@@ -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
@@ -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;
@@ -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 (file)
index 0000000..872586e
--- /dev/null
@@ -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());
+    }
+
+}
@@ -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;