Added oparent to sdc main
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / log / elements / LoggerDebugTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.log.elements;
22
23 /**
24  * Created by dd4296 on 12/25/2017.
25  */
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.openecomp.sdc.common.log.enums.LogLevel;
34 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
35 import org.slf4j.Logger;
36 import org.slf4j.MDC;
37
38 import java.net.UnknownHostException;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
42 import static org.openecomp.sdc.common.log.api.ILogConfiguration.*;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class LoggerDebugTest {
46
47     @Mock
48     private Logger logger;
49
50     private LoggerDebug debugLog;
51
52      @Before
53     public void init() {
54         debugLog = new LoggerDebug(LogFieldsMdcHandler.getInstance(), logger);
55         ThreadLocalsHolder.setUuid(null);
56         MDC.clear();
57     }
58
59     @Test
60     public void whenNoFieldsIsPopulated_RequestedMdcFieldsAreEmpty() {
61         debugLog.clear()
62                 .log(LogLevel.DEBUG, "some error code");
63         assertNull(MDC.get(MDC_KEY_REQUEST_ID));
64     }
65
66     @Test
67     public void debugLogCheckValidationValidFieldsTest() {
68         debugLog.clear()
69                 .startTimer()
70                 .setKeyRequestId("uuid")
71                 .log(LogLevel.DEBUG, "some error code");
72
73         Assert.assertEquals(MDC.get(MDC_KEY_REQUEST_ID), "uuid");
74     }
75
76     @Test
77     public void whenOnlyDebugUUIDFieldsIsPopulated_ShouldReturnAssertTrue_onUUIDFieldCheck() {
78         debugLog.clear()
79                 .setKeyRequestId("uuid")
80                 .log(LogLevel.DEBUG, "some error code");
81
82         Assert.assertEquals(MDC.get(MDC_KEY_REQUEST_ID), "uuid");
83     }
84
85     @Test
86     public void whenAllDebugFieldsArePopulated_ShouldReturnAssertTrue_onEachMACFieldCheck() throws UnknownHostException {
87         debugLog.clear()
88                 .startTimer()
89                 .setKeyRequestId(MDC_KEY_REQUEST_ID)
90                 .log(LogLevel.DEBUG, "some message");
91
92         Assert.assertTrue(LogFieldsMdcHandler.getInstance().isMDCParamEmpty(MDC_END_TIMESTAMP));
93         Assert.assertTrue(LogFieldsMdcHandler.getInstance().isMDCParamEmpty(MDC_ELAPSED_TIME));
94         Assert.assertTrue(LogFieldsMdcHandler.getInstance().isMDCParamEmpty(MDC_STATUS_CODE));
95     }
96
97
98     @Test
99     public void validateMandatoryFields(){
100         assertEquals(MDC_KEY_REQUEST_ID, debugLog.checkMandatoryFieldsExistInMDC().trim());
101     }
102
103     @Test
104     public void validateMandatoryFieldsWhenFieldIsSet(){
105         debugLog.clear()
106                 .setKeyRequestId("1234");
107         assertEquals("", debugLog.checkMandatoryFieldsExistInMDC());
108     }
109 }