Added oparent to sdc main
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / log / elements / LoggerMetricTest.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.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.sdc.common.log.enums.LogLevel;
31 import org.openecomp.sdc.common.log.enums.Severity;
32 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
33 import org.slf4j.Logger;
34 import org.slf4j.MDC;
35
36 import javax.ws.rs.core.Response;
37 import java.net.InetAddress;
38 import java.net.UnknownHostException;
39 import java.util.UUID;
40
41 import static org.openecomp.sdc.common.log.api.ILogConfiguration.*;
42 import static org.openecomp.sdc.common.log.elements.LogFieldsMdcHandler.hostAddress;
43
44 /**
45  * Created by dd4296 on 12/31/2017.
46  */
47 @RunWith(MockitoJUnitRunner.class)
48 public class LoggerMetricTest {
49     @Mock
50     private Logger logger;
51
52     @Mock
53     Response.StatusType statusType;
54
55     private LoggerMetric metricLog;
56
57     @Before
58     public void init() {
59         metricLog = new LoggerMetric(LogFieldsMdcHandler.getInstance(), logger);
60     }
61
62     @After
63     public void tearDown() {
64         MDC.clear();
65         ThreadLocalsHolder.setUuid(null);
66     }
67
68     @Test
69     public void whenNoMetricFieldsArePopulated_ShouldReturnassertEquals_onMdcMap() {
70         metricLog.clear()
71                 .log(LogLevel.DEBUG, "some error code");
72         Assert.assertNotNull(MDC.get(MDC_SERVER_FQDN));
73         Assert.assertNotNull(MDC.get(MDC_SERVER_IP_ADDRESS));
74     }
75
76     @Test
77     public void whenAllMetricFieldsArePopulated_ShouldReturnassertEquals_onEachMACField() throws UnknownHostException {
78         String uuid = UUID.randomUUID().toString();
79         ThreadLocalsHolder.setUuid(uuid);
80
81         String hostName = InetAddress.getByName(hostAddress).getCanonicalHostName();
82         String hostAddress = InetAddress.getLocalHost().getHostAddress();
83
84         metricLog.clear()
85                 .startTimer()
86                 .stopTimer()
87                 .setInstanceUUID(MDC_INSTANCE_UUID)
88                 .setRemoteHost(MDC_REMOTE_HOST)
89                 .setServiceName(MDC_SERVICE_NAME)
90                 .setResponseCode(500)
91                 .setStatusCode("201")
92                 .setResponseDesc(MDC_RESPONSE_DESC)
93                 .setPartnerName(MDC_PARTNER_NAME)
94
95                 .setOptClassName(LoggerMetricTest.class.toString())
96                 .setOptAlertSeverity(Severity.CRITICAL)
97                 .setOptProcessKey(MDC_PROCESS_KEY)
98                 .setOptServiceInstanceId(MDC_SERVICE_INSTANCE_ID)
99
100                 .setTargetEntity(MDC_TARGET_ENTITY)
101                 .setTargetServiceName(MDC_TARGET_SERVICE_NAME)
102                 .setTargetVirtualEntity(MDC_TARGET_VIRTUAL_ENTITY)
103
104                 .log(LogLevel.DEBUG, "");
105
106
107         Assert.assertFalse(LogFieldsMdcHandler.getInstance().isMDCParamEmpty(MDC_METRIC_BEGIN_TIMESTAMP));
108         Assert.assertFalse(LogFieldsMdcHandler.getInstance().isMDCParamEmpty(MDC_END_TIMESTAMP));
109         Assert.assertFalse(LogFieldsMdcHandler.getInstance().isMDCParamEmpty(MDC_ELAPSED_TIME));
110
111         Assert.assertEquals(MDC.get(MDC_SERVER_IP_ADDRESS),hostAddress);
112         Assert.assertEquals(MDC.get(MDC_SERVER_FQDN), hostName);
113         Assert.assertEquals(MDC.get(MDC_REMOTE_HOST), MDC_REMOTE_HOST);
114         Assert.assertEquals(MDC.get(MDC_STATUS_CODE) ,"201");
115
116         Assert.assertEquals(MDC.get(MDC_KEY_REQUEST_ID), uuid);
117         Assert.assertEquals(MDC.get(MDC_SERVICE_NAME) ,MDC_SERVICE_NAME);
118         Assert.assertEquals(MDC.get(MDC_PARTNER_NAME) ,MDC_PARTNER_NAME);
119         Assert.assertEquals(MDC.get(MDC_RESPONSE_CODE) ,"500");
120         Assert.assertEquals(MDC.get(MDC_RESPONSE_DESC) ,MDC_RESPONSE_DESC);
121         Assert.assertEquals(MDC.get(MDC_INSTANCE_UUID) ,MDC_INSTANCE_UUID);
122         Assert.assertEquals(MDC.get(MDC_CLASS_NAME) ,LoggerMetricTest.class.toString());
123
124         Assert.assertEquals(MDC.get(MDC_ALERT_SEVERITY) ,String.valueOf(Severity.CRITICAL.getSeverityType()));
125         Assert.assertEquals(MDC.get(MDC_PROCESS_KEY) ,MDC_PROCESS_KEY);
126         Assert.assertEquals(MDC.get(MDC_SERVICE_INSTANCE_ID) ,MDC_SERVICE_INSTANCE_ID);
127
128         Assert.assertEquals(MDC.get(MDC_TARGET_ENTITY) ,MDC_TARGET_ENTITY);
129         Assert.assertEquals(MDC.get(MDC_TARGET_SERVICE_NAME) ,MDC_TARGET_SERVICE_NAME);
130         Assert.assertEquals(MDC.get(MDC_TARGET_VIRTUAL_ENTITY) ,MDC_TARGET_VIRTUAL_ENTITY);
131     }
132 }