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