### Logs
-Clamp uses logback framework to generate logs. The logback.xml file cand be found under the [src/main/resources/ folder](src/main/resources).
+Clamp uses logback framework to generate logs. The logback.xml file can be found under the [src/main/resources/ folder](src/main/resources).
With the default log settings, all logs will be generated into console and into root.log file under the Clamp root folder. The root.log file is not allowed to be appended, thus restarting the clamp will result in cleaning of the old log files.
--spring.config.name=application-noaaf
-to the jvm parameters. This file is available by default in the java classpath resource folder.
\ No newline at end of file
+to the jvm parameters. This file is available by default in the java classpath resource folder.
import java.net.HttpURLConnection;
import java.net.InetAddress;
+import java.net.URLConnection;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
*/
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 : "");
}
return dateFormat;
}
-
-
/*********************************************************************************************
* Method for ONAP Application Logging Specification v1.2
********************************************************************************************/
public String getProperties(String name) {
return MDC.get(name);
}
-
+
/**
* Report pending invocation with <tt>INVOKE</tt> marker,
* setting standard ONAP logging headers automatically.
* @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);
}
/**
* @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);
}
/**
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;
+ }
}
*/
@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"));
}
//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
//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
@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());
}
}
--- /dev/null
+/*-
+* ============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);
+ }
+}
--- /dev/null
+<!-- Empty Configuration to prevent creating log files by Units Tests (e.g LoggingUtilsTest) ! -->
+<configuration />