2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 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.onap.vid.logging;
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertNull;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.ProtocolVersion;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.message.BasicHttpResponse;
30 import org.onap.logging.ref.slf4j.ONAPLogConstants;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.Test;
35 public class ApacheClientMetricInterceptorTest {
37 private ApacheClientMetricInterceptor interceptor;
38 private final String path = "https://gerrit.onap.org/r/projects/vid/branches?n=16&S=0&m=master";
39 private HttpGet request;
40 private HttpResponse response;
43 public void before() {
44 interceptor = new ApacheClientMetricInterceptor() {};
45 request = new HttpGet(path);
46 response = new BasicHttpResponse(new ProtocolVersion("a",1,2), 200, "ok");
51 public void testAddHeader() {
52 interceptor.addHeader(request, "key", "value");
53 assertEquals(request.getFirstHeader("key").getValue(), "value");
57 public void testGetTargetServiceName() {
58 assertEquals(interceptor.getTargetServiceName(request), path);
62 public void testGetServiceName() {
63 assertEquals(interceptor.getTargetServiceName(request), path);
67 public void testGetHttpStatusCode() {
68 assertEquals(interceptor.getHttpStatusCode(response), 200);
72 public void testGetResponseCode() {
73 assertEquals(interceptor.getResponseCode(response), "200");
77 public void testGetTargetEntity() {
78 assertNull(interceptor.getTargetEntity(request));
82 protected void testAdditionalPre() {
83 request.addHeader(ONAPLogConstants.Headers.INVOCATION_ID, "123");
84 interceptor.additionalPre(request, request);
85 assertEquals(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID), "123");
89 protected void whenThereIsNoInvocationIdHeader_thenMdcValueIsNull() {
90 interceptor.additionalPre(request, request);
91 assertNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));