2 * ============LICENSE_START=======================================================
4 * Copyright (C) 2019 Samsung. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END============================================
18 * ===================================================================
22 package org.onap.clamp.clds.util;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
32 import java.time.ZoneOffset;
33 import java.time.ZonedDateTime;
34 import java.time.format.DateTimeFormatter;
35 import java.util.Arrays;
38 import javax.net.ssl.HttpsURLConnection;
39 import javax.servlet.http.HttpServletRequest;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mockito;
45 import org.mockito.runners.MockitoJUnitRunner;
47 import org.slf4j.event.Level;
48 import org.springframework.security.core.Authentication;
49 import org.springframework.security.core.context.SecurityContext;
50 import org.springframework.security.core.context.SecurityContextHolder;
51 import org.springframework.security.core.userdetails.UserDetails;
56 @RunWith(MockitoJUnitRunner.class)
57 public class LoggingUtilsTest {
59 private static final EELFLogger logger = EELFManager.getInstance().getLogger(LoggingUtilsTest.class);
61 private static final String SERVICE_NAME = "LogginUtilsTest: Test Entering method";
63 private LoggingUtils util;
67 this.util = new LoggingUtils(logger);
71 public void testEnteringLoggingUtils() {
73 final String userName = "test";
75 UserDetails userDetails = Mockito.mock(UserDetails.class);
76 Mockito.when(userDetails.getUsername()).thenReturn(userName);
78 Authentication localAuth = Mockito.mock(Authentication.class);
79 Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
81 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
82 Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
83 SecurityContextHolder.setContext(securityContext);
85 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
87 util.entering(request, SERVICE_NAME);
90 String[] keys = { OnapLogConstants.Mdcs.PARTNER_NAME, OnapLogConstants.Mdcs.ENTRY_TIMESTAMP,
91 OnapLogConstants.Mdcs.REQUEST_ID, OnapLogConstants.Mdcs.INVOCATION_ID,
92 OnapLogConstants.Mdcs.CLIENT_IP_ADDRESS, OnapLogConstants.Mdcs.SERVER_FQDN,
93 OnapLogConstants.Mdcs.INSTANCE_UUID, OnapLogConstants.Mdcs.SERVICE_NAME };
94 Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
96 assertTrue(checkMapKeys(mdc, keys));
97 assertEquals(userName, mdc.get(OnapLogConstants.Mdcs.PARTNER_NAME));
101 public void testExistingLoggingUtils() {
103 MDC.put(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP,
104 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
107 util.exiting("200", SERVICE_NAME, Level.INFO, OnapLogConstants.ResponseStatus.COMPLETED);
110 Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
115 public void testInvokeTestUtils() {
117 final String targetEntity = "LoggingUtilsTest";
118 final String targetServiceName = "testInvokeTestUtils";
119 HttpsURLConnection secureConnection = Mockito.mock(HttpsURLConnection.class);
122 secureConnection = util.invokeHttps(secureConnection, targetEntity, targetServiceName);
125 assertNotNull(secureConnection);
126 String[] keys = { OnapLogConstants.Mdcs.TARGET_ENTITY, OnapLogConstants.Mdcs.TARGET_SERVICE_NAME,
127 OnapLogConstants.Mdcs.INVOCATIONID_OUT, OnapLogConstants.Mdcs.INVOKE_TIMESTAMP };
128 Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
130 assertTrue(checkMapKeys(mdc, keys));
131 assertEquals(targetEntity, mdc.get(OnapLogConstants.Mdcs.TARGET_ENTITY));
132 assertEquals(targetServiceName, mdc.get(OnapLogConstants.Mdcs.TARGET_SERVICE_NAME));
135 private boolean checkMapKeys(Map map, String[] keys) {
136 return Arrays.stream(keys).allMatch(key -> map.get(key) != null);