New unit tests and sonar fixes
[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.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.assertEquals;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31
32 import java.time.ZoneOffset;
33 import java.time.ZonedDateTime;
34 import java.time.format.DateTimeFormatter;
35 import java.util.Arrays;
36 import java.util.Map;
37
38 import javax.net.ssl.HttpsURLConnection;
39 import javax.servlet.http.HttpServletRequest;
40
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;
46 import org.slf4j.MDC;
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;
52
53 /**
54  * Test Logging Utils.
55  */
56 @RunWith(MockitoJUnitRunner.class)
57 public class LoggingUtilsTest {
58
59     private static final EELFLogger logger = EELFManager.getInstance().getLogger(LoggingUtilsTest.class);
60
61     private static final String SERVICE_NAME =  "LogginUtilsTest: Test Entering method";
62
63     private LoggingUtils util;
64
65     @Before
66     public void setup() {
67         this.util = new LoggingUtils(logger);
68     }
69
70     @Test
71     public void testEnteringLoggingUtils() {
72         // given
73         final String userName = "test";
74
75         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
76
77         UserDetails userDetails = Mockito.mock(UserDetails.class);
78         Mockito.when(userDetails.getUsername()).thenReturn(userName);
79
80         Authentication localAuth = Mockito.mock(Authentication.class);
81         Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
82
83         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
84         Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
85         SecurityContextHolder.setContext(securityContext);
86
87         // when
88         util.entering(request, SERVICE_NAME);
89
90         // then
91         String[] keys = { ONAPLogConstants.MDCs.PARTNER_NAME,
92                 ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
93                 ONAPLogConstants.MDCs.REQUEST_ID,
94                 ONAPLogConstants.MDCs.INVOCATION_ID,
95                 ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS,
96                 ONAPLogConstants.MDCs.SERVER_FQDN,
97                 ONAPLogConstants.MDCs.INSTANCE_UUID,
98                 ONAPLogConstants.MDCs.SERVICE_NAME
99         };
100         Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
101
102         assertTrue(checkMapKeys(mdc, keys));
103         assertEquals(userName,
104                mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
105     }
106
107     @Test
108     public void testExistingLoggingUtils() {
109         // given
110         MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
111                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
112
113         // when
114         util.exiting("200", SERVICE_NAME, Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED);
115
116         // then
117         Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
118         assertNull(mdc);
119     }
120
121     @Test
122     public void testInvokeTestUtils() {
123         // given
124         final String targetEntity = "LoggingUtilsTest";
125         final String targetServiceName = "testInvokeTestUtils";
126         HttpsURLConnection secureConnection = Mockito.mock(HttpsURLConnection.class);
127
128         // when
129         secureConnection = util.invokeHttps(secureConnection, targetEntity, targetServiceName);
130
131         // then
132         assertNotNull(secureConnection);
133         String[] keys = {ONAPLogConstants.MDCs.TARGET_ENTITY,
134                 ONAPLogConstants.MDCs.TARGET_SERVICE_NAME,
135                 ONAPLogConstants.MDCs.INVOCATIONID_OUT,
136                 ONAPLogConstants.MDCs.INVOKE_TIMESTAMP
137         };
138         Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap();
139
140         assertTrue(checkMapKeys(mdc, keys));
141         assertEquals(targetEntity, mdc.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
142         assertEquals(targetServiceName, mdc.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
143     }
144
145     private boolean checkMapKeys(Map map, String[] keys) {
146         return Arrays.stream(keys).allMatch(key -> map.get(key) != null);
147     }
148 }