2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.log.elements;
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;
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;
37 public class LogFieldsMdcHandlerTest {
39 private LogFieldsMdcHandler ecompMdcWrapper;
43 ecompMdcWrapper = new LogFieldsMdcHandler();
44 ecompMdcWrapper.clear();
49 public void isMDCParamEmpty_shouldReturnTrue_onNonNullValueInMDC(){
50 MDC.put("Key","value1");
51 assertFalse(ecompMdcWrapper.isMDCParamEmpty("Key"));
54 public void isMDCParamEmpty_shouldReturnFalse_onEmptyStringInMDC(){
56 assertTrue(ecompMdcWrapper.isMDCParamEmpty("Key"));
60 public void isMDCParamEmpty_shouldReturnFalse_onNullValueInMDC(){
62 assertTrue(ecompMdcWrapper.isMDCParamEmpty("Key"));
66 public void startTimer_shouldFilecompMdcWrappereginTimestampField(){
67 ecompMdcWrapper.startMetricTimer();
68 assertFalse(ecompMdcWrapper.isMDCParamEmpty(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP));
72 public void stopTimer_shouldFillEndTimestampField_ifStartTimerWasCalledPreviously(){
73 ecompMdcWrapper.startAuditTimer();
74 ecompMdcWrapper.stopAuditTimer();
75 assertFalse(ecompMdcWrapper.isMDCParamEmpty(MDC_END_TIMESTAMP));
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);
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));
99 public void clear_shouldNotThrowAnException_WhenNoFieldWasAssignedAsMandatoryOrOptional(){
100 ecompMdcWrapper.setClassName("class1");
101 ecompMdcWrapper.setPartnerName("partner1");
102 ecompMdcWrapper.setOptCustomField1("of1");
103 Exception exp = null;
105 ecompMdcWrapper.clear();