Fix logging date format to avoid parse error
[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 static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_CLASS_NAME;
27 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_END_TIMESTAMP;
28 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_OPT_FIELD1;
29
30 import java.time.ZonedDateTime;
31 import java.time.format.DateTimeFormatter;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.logging.ref.slf4j.ONAPLogConstants;
35 import org.slf4j.MDC;
36
37 public class LogFieldsMdcHandlerTest {
38
39         private LogFieldsMdcHandler ecompMdcWrapper;
40
41         @Before
42         public void init(){
43                 ecompMdcWrapper = new LogFieldsMdcHandler();
44                 ecompMdcWrapper.clear();
45                 MDC.clear();
46         }
47
48         @Test
49         public void isMDCParamEmpty_shouldReturnTrue_onNonNullValueInMDC(){
50                 MDC.put("Key","value1");
51                 assertFalse(ecompMdcWrapper.isMDCParamEmpty("Key"));
52         }
53         @Test
54         public void isMDCParamEmpty_shouldReturnFalse_onEmptyStringInMDC(){
55                 MDC.put("Key","");
56                 assertTrue(ecompMdcWrapper.isMDCParamEmpty("Key"));
57         }
58
59         @Test
60         public void isMDCParamEmpty_shouldReturnFalse_onNullValueInMDC(){
61                 MDC.put("Key",null);
62                 assertTrue(ecompMdcWrapper.isMDCParamEmpty("Key"));
63         }
64
65         @Test
66         public void startTimer_shouldFilecompMdcWrappereginTimestampField(){
67                 ecompMdcWrapper.startMetricTimer();
68                 assertFalse(ecompMdcWrapper.isMDCParamEmpty(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
69         }
70
71         @Test
72         public void stopTimer_shouldFillEndTimestampField_ifStartTimerWasCalledPreviously(){
73                 ecompMdcWrapper.startAuditTimer();
74                 ecompMdcWrapper.stopAuditTimer();
75                 assertFalse(ecompMdcWrapper.isMDCParamEmpty(MDC_END_TIMESTAMP));
76         }
77
78         @Test
79         public void stopTimer_shouldTimestampsBeIsoFormat() {
80                 ecompMdcWrapper.startAuditTimer();
81                 ecompMdcWrapper.stopAuditTimer();
82                 // Expect no exceptions thrown
83                 ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP), DateTimeFormatter.ISO_ZONED_DATE_TIME);
84                 ZonedDateTime.parse(MDC.get(MDC_END_TIMESTAMP), DateTimeFormatter.ISO_ZONED_DATE_TIME);
85         }
86
87         @Test
88         public void clear_shouldRemoveAllMandatoryAndOptionalFields_And_OnlyThem(){
89                 ecompMdcWrapper.setClassName("class1");
90                 ecompMdcWrapper.setPartnerName("partner1");
91                 ecompMdcWrapper.setOptCustomField1("of1");
92                 ecompMdcWrapper.clear();
93                 assertNull(MDC.get(MDC_CLASS_NAME));
94                 assertNull(MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
95                 assertNull(MDC.get(MDC_OPT_FIELD1));
96         }
97
98         @Test
99         public void clear_shouldNotThrowAnException_WhenNoFieldWasAssignedAsMandatoryOrOptional(){
100                 ecompMdcWrapper.setClassName("class1");
101                 ecompMdcWrapper.setPartnerName("partner1");
102                 ecompMdcWrapper.setOptCustomField1("of1");
103                 Exception exp = null;
104                 try {
105                         ecompMdcWrapper.clear();
106                 }
107                 catch (Exception e)
108                 {
109                         exp =e;
110                 }
111                 assertNull(exp);
112         }
113
114 }
115