*/
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;
}
/**
* limitations under the License.
* ============LICENSE_END=========================================================
*/
-package org.onap.crud.service;
+package org.onap.crud.dao;
import java.util.ArrayList;
import java.util.HashSet;
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;
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 {
--- /dev/null
+/**
+ * ============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));
+ }
+
+}
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;
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);
* 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;
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
* 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;
* 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;
--- /dev/null
+/**
+ * ============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());
+ }
+
+}
* 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;