summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorXue Gao <xg353y@intl.att.com>2019-06-18 12:10:14 +0000
committerGerrit Code Review <gerrit@onap.org>2019-06-18 12:10:14 +0000
commiteb216ac66f47c2b9d2b69a6e080d3969f75d78af (patch)
tree080b7839e8f755e6542d719bcdb7f6568bfa6a49 /src
parent60b5b25fa4687f8d64f1c5c48515db735a0e5141 (diff)
parent456cf4f6af9c22449dd4a39b1d13900fc38481c1 (diff)
Merge "New unit tests and sonar fixes"
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/onap/clamp/clds/util/LoggingUtils.java66
-rw-r--r--src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java18
-rw-r--r--src/test/java/org/onap/clamp/clds/util/LoggingUtilsTest.java148
-rw-r--r--src/test/resources/logback.xml2
4 files changed, 185 insertions, 49 deletions
diff --git a/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java b/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java
index cbe7eba96..163ab6913 100644
--- a/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java
+++ b/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java
@@ -28,6 +28,7 @@ import com.att.eelf.configuration.EELFManager;
import java.net.HttpURLConnection;
import java.net.InetAddress;
+import java.net.URLConnection;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@@ -114,7 +115,7 @@ public class LoggingUtils {
*/
public static void setResponseContext(String code, String description, String className) {
MDC.put("ResponseCode", code);
- MDC.put("StatusCode", code.equals("0") ? "COMPLETE" : "ERROR");
+ MDC.put("StatusCode", "0".equals(code) ? "COMPLETE" : "ERROR");
MDC.put("ResponseDescription", description != null ? description : "");
MDC.put("ClassName", className != null ? className : "");
}
@@ -167,8 +168,6 @@ public class LoggingUtils {
return dateFormat;
}
-
-
/*********************************************************************************************
* Method for ONAP Application Logging Specification v1.2
********************************************************************************************/
@@ -256,7 +255,7 @@ public class LoggingUtils {
public String getProperties(String name) {
return MDC.get(name);
}
-
+
/**
* Report pending invocation with <tt>INVOKE</tt> marker,
* setting standard ONAP logging headers automatically.
@@ -267,24 +266,7 @@ public class LoggingUtils {
* @return The HTTP url connection
*/
public HttpURLConnection invoke(final HttpURLConnection con, String targetEntity, String targetServiceName) {
- final String invocationId = UUID.randomUUID().toString();
-
- // Set standard HTTP headers on (southbound request) builder.
- con.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID,
- defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)));
- con.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID,
- invocationId);
- con.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME,
- defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)));
-
- invokeContext(targetEntity, targetServiceName, invocationId);
-
- // Log INVOKE*, with the invocationID as the message body.
- // (We didn't really want this kind of behavior in the standard,
- // but is it worse than new, single-message MDC?)
- this.mlogger.info(ONAPLogConstants.Markers.INVOKE);
- this.mlogger.info(ONAPLogConstants.Markers.INVOKE_SYNC + "{" + invocationId + "}");
- return con;
+ return this.invokeGeneric(con, targetEntity, targetServiceName);
}
/**
@@ -316,24 +298,7 @@ public class LoggingUtils {
* @return The HTTPS url connection
*/
public HttpsURLConnection invokeHttps(final HttpsURLConnection con, String targetEntity, String targetServiceName) {
- final String invocationId = UUID.randomUUID().toString();
-
- // Set standard HTTP headers on (southbound request) builder.
- con.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID,
- defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)));
- con.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID,
- invocationId);
- con.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME,
- defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)));
-
- invokeContext(targetEntity, targetServiceName, invocationId);
-
- // Log INVOKE*, with the invocationID as the message body.
- // (We didn't really want this kind of behavior in the standard,
- // but is it worse than new, single-message MDC?)
- this.mlogger.info(ONAPLogConstants.Markers.INVOKE);
- this.mlogger.info(ONAPLogConstants.Markers.INVOKE_SYNC + "{" + invocationId + "}");
- return con;
+ return this.invokeGeneric(con, targetEntity, targetServiceName);
}
/**
@@ -410,4 +375,25 @@ public class LoggingUtils {
MDC.remove(ONAPLogConstants.MDCs.INVOCATIONID_OUT);
MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP);
}
+
+ private <T extends URLConnection> T invokeGeneric(final T con, String targetEntity, String targetServiceName) {
+ final String invocationId = UUID.randomUUID().toString();
+
+ // Set standard HTTP headers on (southbound request) builder.
+ con.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID,
+ defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)));
+ con.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID,
+ invocationId);
+ con.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME,
+ defaultToEmpty(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)));
+
+ invokeContext(targetEntity, targetServiceName, invocationId);
+
+ // Log INVOKE*, with the invocationID as the message body.
+ // (We didn't really want this kind of behavior in the standard,
+ // but is it worse than new, single-message MDC?)
+ this.mlogger.info(ONAPLogConstants.Markers.INVOKE);
+ this.mlogger.info(ONAPLogConstants.Markers.INVOKE_SYNC + "{" + invocationId + "}");
+ return con;
+ }
}
diff --git a/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java b/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java
index 65aa9b44f..0a05d65dc 100644
--- a/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java
+++ b/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java
@@ -55,10 +55,10 @@ public class CldsReferencePropertiesItCase {
*/
@Test
public void testGetStringValue() {
- assertEquals(refProp.getStringValue("policy.onap.name"), "DCAE");
- assertEquals(refProp.getStringValue("policy.ms.policyNamePrefix", ""), "Config_MS_");
- assertEquals(refProp.getStringValue("policy.ms.policyNamePrefix", "testos"), "Config_MS_");
- assertEquals(refProp.getStringValue("policy.ms", "policyNamePrefix"), "Config_MS_");
+ assertEquals("DCAE", refProp.getStringValue("policy.onap.name"));
+ assertEquals("Config_MS_", refProp.getStringValue("policy.ms.policyNamePrefix", ""));
+ assertEquals("Config_MS_", refProp.getStringValue("policy.ms.policyNamePrefix", "testos"));
+ assertEquals("Config_MS_", refProp.getStringValue("policy.ms", "policyNamePrefix"));
assertNull(refProp.getStringValue("does.not.exist"));
}
@@ -70,7 +70,7 @@ public class CldsReferencePropertiesItCase {
//then
assertNotNull(root);
assertTrue(root.isJsonObject());
- assertEquals(root.getAsJsonObject().get("DC1").getAsString(), "Data Center 1");
+ assertEquals("Data Center 1", root.getAsJsonObject().get("DC1").getAsString());
}
@Test
@@ -81,7 +81,7 @@ public class CldsReferencePropertiesItCase {
//then
assertNotNull(root);
assertTrue(root.isJsonObject());
- assertEquals(root.getAsJsonObject().get("DC1").getAsString(), "Data Center 1");
+ assertEquals("Data Center 1", root.getAsJsonObject().get("DC1").getAsString());
}
@Test
@@ -112,9 +112,9 @@ public class CldsReferencePropertiesItCase {
@Test
public void testGetStringList() {
List<String> profileList = refProp.getStringList("policy.pdpUrl1", ",");
- assertTrue(profileList.size() == 3);
+ assertEquals(3, profileList.size());
assertTrue(profileList.get(0).trim().startsWith("http://localhost:"));
- assertTrue(profileList.get(1).trim().equals("testpdp"));
- assertTrue(profileList.get(2).trim().equals("alpha123"));
+ assertEquals("testpdp", profileList.get(1).trim());
+ assertEquals("alpha123", profileList.get(2).trim());
}
}
diff --git a/src/test/java/org/onap/clamp/clds/util/LoggingUtilsTest.java b/src/test/java/org/onap/clamp/clds/util/LoggingUtilsTest.java
new file mode 100644
index 000000000..7e2da4d1e
--- /dev/null
+++ b/src/test/java/org/onap/clamp/clds/util/LoggingUtilsTest.java
@@ -0,0 +1,148 @@
+/*-
+* ============LICENSE_START=======================================================
+* ONAP CLAMP
+* Copyright (C) 2019 Samsung. All rights reserved.
+* ================================================================================
+* 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.clamp.clds.util;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Arrays;
+import java.util.Map;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.servlet.http.HttpServletRequest;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.slf4j.MDC;
+import org.slf4j.event.Level;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.userdetails.UserDetails;
+
+/**
+ * Test Logging Utils.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class LoggingUtilsTest {
+
+ private static final EELFLogger logger = EELFManager.getInstance().getLogger(LoggingUtilsTest.class);
+
+ private static final String SERVICE_NAME = "LogginUtilsTest: Test Entering method";
+
+ private LoggingUtils util;
+
+ @Before
+ public void setup() {
+ this.util = new LoggingUtils(logger);
+ }
+
+ @Test
+ public void testEnteringLoggingUtils() {
+ // given
+ final String userName = "test";
+
+ HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+
+ UserDetails userDetails = Mockito.mock(UserDetails.class);
+ Mockito.when(userDetails.getUsername()).thenReturn(userName);
+
+ Authentication localAuth = Mockito.mock(Authentication.class);
+ Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
+
+ SecurityContext securityContext = Mockito.mock(SecurityContext.class);
+ Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
+ SecurityContextHolder.setContext(securityContext);
+
+ // when
+ util.entering(request, SERVICE_NAME);
+
+ // then
+ String[] keys = { ONAPLogConstants.MDCs.PARTNER_NAME,
+ ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
+ ONAPLogConstants.MDCs.REQUEST_ID,
+ ONAPLogConstants.MDCs.INVOCATION_ID,
+ ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS,
+ ONAPLogConstants.MDCs.SERVER_FQDN,
+ ONAPLogConstants.MDCs.INSTANCE_UUID,
+ ONAPLogConstants.MDCs.SERVICE_NAME
+ };
+ Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
+
+ assertTrue(checkMapKeys(mdc, keys));
+ assertEquals(userName,
+ mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
+ }
+
+ @Test
+ public void testExistingLoggingUtils() {
+ // given
+ MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
+ ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
+
+ // when
+ util.exiting("200", SERVICE_NAME, Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED);
+
+ // then
+ Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
+ assertNull(mdc);
+ }
+
+ @Test
+ public void testInvokeTestUtils() {
+ // given
+ final String targetEntity = "LoggingUtilsTest";
+ final String targetServiceName = "testInvokeTestUtils";
+ HttpsURLConnection secureConnection = Mockito.mock(HttpsURLConnection.class);
+
+ // when
+ secureConnection = util.invokeHttps(secureConnection, targetEntity, targetServiceName);
+
+ // then
+ assertNotNull(secureConnection);
+ String[] keys = {ONAPLogConstants.MDCs.TARGET_ENTITY,
+ ONAPLogConstants.MDCs.TARGET_SERVICE_NAME,
+ ONAPLogConstants.MDCs.INVOCATIONID_OUT,
+ ONAPLogConstants.MDCs.INVOKE_TIMESTAMP
+ };
+ Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
+
+ assertTrue(checkMapKeys(mdc, keys));
+ assertEquals(targetEntity, mdc.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
+ assertEquals(targetServiceName, mdc.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
+ }
+
+ private boolean checkMapKeys(Map map, String[] keys) {
+ return Arrays.stream(keys).allMatch(key -> map.get(key) != null);
+ }
+}
diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml
new file mode 100644
index 000000000..07e587855
--- /dev/null
+++ b/src/test/resources/logback.xml
@@ -0,0 +1,2 @@
+<!-- Empty Configuration to prevent creating log files by Units Tests (e.g LoggingUtilsTest) ! -->
+<configuration />