Added new annotation for ScheduledLogging
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / test / java / org / onap / logging / filter / base / ScheduledTasksMDCSetupAspectTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
4  * ================================================================================
5  * Copyright (C) 2020 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.onap.logging.filter.base;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.UUID;
28
29 import org.junit.After;
30 import org.junit.Test;
31 import org.onap.logging.filter.base.Constants;
32 import org.onap.logging.ref.slf4j.ONAPLogConstants;
33
34 import org.slf4j.MDC;
35
36 public class ScheduledTasksMDCSetupAspectTest {
37
38     private ScheduledTasksMDCSetupAspect tasksMDCSetup = new ScheduledTasksMDCSetupAspect();
39
40     @After
41     public void tearDown() {
42         MDC.clear();
43         System.clearProperty("partnerName");
44     }
45
46     @Test
47     public void mdcSetupTest() {
48         System.setProperty("partnerName", ONAPComponents.SO.toString());
49         tasksMDCSetup.setupMDC("mdcSetupTest");
50
51         assertTrue(isValidUUID(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)));
52         assertTrue(isValidUUID(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)));
53         assertEquals(ONAPComponents.SO.toString(), MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY));
54         assertEquals(ONAPComponents.SO.toString(), MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
55         assertEquals("mdcSetupTest", MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME));
56         assertEquals(Constants.DefaultValues.UNKNOWN, MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME));
57         assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP));
58         assertNotNull(MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME));
59         assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP));
60         assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN));
61     }
62
63     @Test
64     public void errorMDCSetupTest() {
65         tasksMDCSetup.errorMDCSetup(ErrorCode.UnknownError, "Error");
66
67         assertEquals("900", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE));
68         assertEquals("Error", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC));
69     }
70
71     @Test
72     public void setStatusCodeTest() {
73         tasksMDCSetup.setStatusCode();
74
75         assertEquals(ONAPLogConstants.ResponseStatus.COMPLETE.toString(),
76                 MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
77     }
78
79     @Test
80     public void setStatusCodeErrorTest() {
81         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.ERROR.toString());
82         tasksMDCSetup.setStatusCode();
83
84         assertEquals(ONAPLogConstants.ResponseStatus.ERROR.toString(),
85                 MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
86     }
87
88     public static boolean isValidUUID(String id) {
89         try {
90             if (null == id) {
91                 return false;
92             }
93             UUID uuid = UUID.fromString(id);
94             return uuid.toString().equalsIgnoreCase(id);
95         } catch (IllegalArgumentException iae) {
96             return false;
97         }
98     }
99 }