CSIT Fix for SDC-2585
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / log / elements / LoggerErrorTest.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 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.openecomp.sdc.common.config.EcompErrorCode;
30 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
31 import org.openecomp.sdc.common.log.enums.LogLevel;
32 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
33 import org.slf4j.Logger;
34 import org.slf4j.MDC;
35
36 import static org.junit.Assert.assertEquals;
37 import static org.openecomp.sdc.common.log.api.ILogConfiguration.*;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class LoggerErrorTest {
41     private LoggerError errorLog;
42
43     @Mock
44     private Logger logger;
45     @Before
46     public void init() {
47        errorLog = LoggerFactory.getMdcLogger(LoggerError.class, logger);
48        MDC.clear();
49     }
50
51     @Test
52     public void allFieldsArePresentTest() {
53         ThreadLocalsHolder.setUuid("uuid");
54         errorLog.log(LogLevel.ERROR, EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR, "service", "entity", "server error");
55
56         Assert.assertEquals(MDC.get(MDC_ERROR_CODE), String.valueOf(EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR.getErrorCode()));
57         Assert.assertEquals("uuid", MDC.get(MDC_KEY_REQUEST_ID));
58         Assert.assertEquals("entity", MDC.get(MDC_TARGET_ENTITY));
59         Assert.assertEquals("service", MDC.get(MDC_SERVICE_NAME));
60     }
61
62     @Test
63     public void missingFieldsTest() {
64         errorLog.clear()
65                 .log(LogLevel.ERROR,"some message");
66     }
67
68     @Test
69     public void convertEcompErrorForLogging_correctName() {
70         assertEquals(EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR, EcompLoggerErrorCode.getByValue(EcompErrorCode.E_210.name()));
71     }
72
73    @Test
74     public void convertEcompErrorForLogging_correctName_2() {
75         assertEquals(EcompLoggerErrorCode.DATA_ERROR, EcompLoggerErrorCode.getByValue(EcompErrorCode.E_399.name()));
76     }
77
78     @Test
79     public void convertEcompErrorForLogging_NotConvertable() {
80         assertEquals(EcompLoggerErrorCode.UNKNOWN_ERROR, EcompLoggerErrorCode.getByValue("ABC"));
81     }
82
83     @Test
84     public void convertEcompErrorForLogging_NotConvertable_2() {
85         assertEquals(EcompLoggerErrorCode.UNKNOWN_ERROR, EcompLoggerErrorCode.getByValue("E_ABC"));
86     }
87
88     @Test
89     public void convertEcompErrorForLogging_Success() {
90         assertEquals(EcompLoggerErrorCode.SUCCESS, EcompLoggerErrorCode.getByValue("E_0"));
91     }
92 }