Catalog alignment
[sdc.git] / common-app-logging / src / test / java / org / openecomp / sdc / common / log / elements / LogFieldsMdcHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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 import org.junit.Before;
24 import org.junit.Test;
25 import org.onap.logging.ref.slf4j.ONAPLogConstants;
26 import org.slf4j.MDC;
27
28 import static org.junit.Assert.*;
29 import static org.openecomp.sdc.common.log.api.ILogConfiguration.*;
30
31 public class LogFieldsMdcHandlerTest {
32
33         private LogFieldsMdcHandler ecompMdcWrapper;
34
35         @Before
36         public void init(){
37                 ecompMdcWrapper = new LogFieldsMdcHandler();
38                 ecompMdcWrapper.clear();
39                 MDC.clear();
40         }
41
42         @Test
43         public void isMDCParamEmpty_shouldReturnTrue_onNonNullValueInMDC(){
44                 MDC.put("Key","value1");
45                 assertFalse(ecompMdcWrapper.isMDCParamEmpty("Key"));
46         }
47         @Test
48         public void isMDCParamEmpty_shouldReturnFalse_onEmptyStringInMDC(){
49                 MDC.put("Key","");
50                 assertTrue(ecompMdcWrapper.isMDCParamEmpty("Key"));
51         }
52
53         @Test
54         public void isMDCParamEmpty_shouldReturnFalse_onNullValueInMDC(){
55                 MDC.put("Key",null);
56                 assertTrue(ecompMdcWrapper.isMDCParamEmpty("Key"));
57         }
58
59         @Test
60         public void startTimer_shouldFilecompMdcWrappereginTimestampField(){
61                 ecompMdcWrapper.startMetricTimer();
62                 assertFalse(ecompMdcWrapper.isMDCParamEmpty(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
63         }
64
65         @Test
66         public void stopTimer_shouldFillEndTimestampField_ifStartTimerWasCalledPreviously(){
67                 ecompMdcWrapper.startAuditTimer();
68                 ecompMdcWrapper.stopAuditTimer();
69                 assertFalse(ecompMdcWrapper.isMDCParamEmpty(MDC_END_TIMESTAMP));
70         }
71
72         @Test
73         public void clear_shouldRemoveAllMandatoryAndOptionalFields_And_OnlyThem(){
74                 ecompMdcWrapper.setClassName("class1");
75                 ecompMdcWrapper.setPartnerName("partner1");
76                 ecompMdcWrapper.setOptCustomField1("of1");
77                 ecompMdcWrapper.clear();
78                 assertNull(MDC.get(MDC_CLASS_NAME));
79                 assertNull(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
80                 assertNull(MDC.get(MDC_OPT_FIELD1));
81         }
82
83         @Test
84         public void clear_shouldNotThrowAnException_WhenNoFieldWasAssignedAsMandatoryOrOptional(){
85                 ecompMdcWrapper.setClassName("class1");
86                 ecompMdcWrapper.setPartnerName("partner1");
87                 ecompMdcWrapper.setOptCustomField1("of1");
88                 Exception exp = null;
89                 try {
90                         ecompMdcWrapper.clear();
91                 }
92                 catch (Exception e)
93                 {
94                         exp =e;
95                 }
96                 assertNull(exp);
97         }
98
99 }
100