2 * ============LICENSE_START=======================================================
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
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 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;
36 import javax.ws.rs.core.Response;
37 import java.net.InetAddress;
38 import java.net.UnknownHostException;
39 import java.util.UUID;
41 import static org.openecomp.sdc.common.log.api.ILogConfiguration.*;
42 import static org.openecomp.sdc.common.log.elements.LogFieldsMdcHandler.hostAddress;
45 * Created by dd4296 on 12/31/2017.
47 @RunWith(MockitoJUnitRunner.class)
48 public class LoggerMetricTest {
50 private Logger logger;
53 Response.StatusType statusType;
55 private LoggerMetric metricLog;
59 metricLog = new LoggerMetric(LogFieldsMdcHandler.getInstance(), logger);
63 public void tearDown() {
65 ThreadLocalsHolder.setUuid(null);
69 public void whenNoMetricFieldsArePopulated_ShouldReturnassertEquals_onMdcMap() {
71 .log(LogLevel.DEBUG, "some error code");
72 Assert.assertNotNull(MDC.get(MDC_SERVER_FQDN));
73 Assert.assertNotNull(MDC.get(MDC_SERVER_IP_ADDRESS));
77 public void whenAllMetricFieldsArePopulated_ShouldReturnassertEquals_onEachMACField() throws UnknownHostException {
78 String uuid = UUID.randomUUID().toString();
79 ThreadLocalsHolder.setUuid(uuid);
81 String hostName = InetAddress.getByName(hostAddress).getCanonicalHostName();
82 String hostAddress = InetAddress.getLocalHost().getHostAddress();
87 .setInstanceUUID(MDC_INSTANCE_UUID)
88 .setRemoteHost(MDC_REMOTE_HOST)
89 .setServiceName(MDC_SERVICE_NAME)
92 .setResponseDesc(MDC_RESPONSE_DESC)
93 .setPartnerName(MDC_PARTNER_NAME)
95 .setOptClassName(LoggerMetricTest.class.toString())
96 .setOptAlertSeverity(Severity.CRITICAL)
97 .setOptProcessKey(MDC_PROCESS_KEY)
98 .setOptServiceInstanceId(MDC_SERVICE_INSTANCE_ID)
100 .setTargetEntity(MDC_TARGET_ENTITY)
101 .setTargetServiceName(MDC_TARGET_SERVICE_NAME)
102 .setTargetVirtualEntity(MDC_TARGET_VIRTUAL_ENTITY)
104 .log(LogLevel.DEBUG, "");
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));
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");
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());
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);
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);