Fix audit, metric and error logs as per logging specification
[clamp.git] / src / test / java / org / onap / clamp / clds / util / LoggingUtilsTest.java
1 /*-
2 * ============LICENSE_START=======================================================
3 * ONAP CLAMP
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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 * ===================================================================
19 *
20 */
21
22 package org.onap.clamp.clds.util;
23
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;
28
29 import java.time.ZoneOffset;
30 import java.time.ZonedDateTime;
31 import java.time.format.DateTimeFormatter;
32 import java.util.Arrays;
33 import java.util.Map;
34 import javax.net.ssl.HttpsURLConnection;
35 import javax.servlet.http.HttpServletRequest;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.slf4j.MDC;
44 import org.slf4j.event.Level;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.security.core.Authentication;
47 import org.springframework.security.core.context.SecurityContext;
48 import org.springframework.security.core.context.SecurityContextHolder;
49 import org.springframework.security.core.userdetails.UserDetails;
50
51 /**
52  * Test Logging Utils.
53  */
54 @RunWith(MockitoJUnitRunner.class)
55 public class LoggingUtilsTest {
56
57     protected static final Logger logger = LoggerFactory.getLogger(LoggingUtilsTest.class);
58
59     private static final String SERVICE_NAME = "LogginUtilsTest: Test Entering method";
60
61     private LoggingUtils util;
62
63     @Before
64     public void setup() {
65         this.util = new LoggingUtils(logger);
66     }
67
68     @Test
69     public void testEnteringLoggingUtils() {
70         // given
71         final String userName = "test";
72
73         UserDetails userDetails = Mockito.mock(UserDetails.class);
74         Mockito.when(userDetails.getUsername()).thenReturn(userName);
75
76         Authentication localAuth = Mockito.mock(Authentication.class);
77         Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
78
79         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
80         Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
81         SecurityContextHolder.setContext(securityContext);
82
83         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
84         // when
85         util.entering(request, SERVICE_NAME);
86
87         // then
88         String[] keys = {OnapLogConstants.Mdcs.PARTNER_NAME, OnapLogConstants.Mdcs.ENTRY_TIMESTAMP,
89             OnapLogConstants.Mdcs.REQUEST_ID, OnapLogConstants.Mdcs.INVOCATION_ID,
90             OnapLogConstants.Mdcs.CLIENT_IP_ADDRESS, OnapLogConstants.Mdcs.SERVER_FQDN,
91             OnapLogConstants.Mdcs.INSTANCE_UUID, OnapLogConstants.Mdcs.SERVICE_NAME};
92         Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
93
94         assertTrue(checkMapKeys(mdc, keys));
95         assertEquals(userName, mdc.get(OnapLogConstants.Mdcs.PARTNER_NAME));
96     }
97
98     @Test
99     public void testExistingLoggingUtils() {
100         // given
101         MDC.put(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP,
102             ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
103
104         // when
105         util.exiting(HttpStatus.OK.value(), SERVICE_NAME, Level.INFO,
106             OnapLogConstants.ResponseStatus.COMPLETE);
107
108         // then
109         Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
110         assertNull(mdc);
111     }
112
113     @Test
114     public void testInvokeTestUtils() {
115         // given
116         final String targetEntity = "LoggingUtilsTest";
117         final String targetServiceName = "testInvokeTestUtils";
118         HttpsURLConnection secureConnection = Mockito.mock(HttpsURLConnection.class);
119
120         // when
121         secureConnection = util.invokeHttps(secureConnection, targetEntity, targetServiceName);
122
123         // then
124         assertNotNull(secureConnection);
125         String[] keys =
126             {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();
129
130         assertTrue(checkMapKeys(mdc, keys));
131         assertEquals(targetEntity, mdc.get(OnapLogConstants.Mdcs.TARGET_ENTITY));
132         assertEquals(targetServiceName, mdc.get(OnapLogConstants.Mdcs.TARGET_SERVICE_NAME));
133     }
134
135     private boolean checkMapKeys(Map map, String[] keys) {
136         return Arrays.stream(keys).allMatch(key -> map.get(key) != null);
137     }
138 }