use onap logging 1.6.1 with needed workarounds
[vid.git] / vid-app-common / src / test / java / org / onap / vid / logging / ApacheClientMetricInterceptorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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.vid.logging;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertNull;
25
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;
31 import org.slf4j.MDC;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.Test;
34
35 public class ApacheClientMetricInterceptorTest {
36
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;
41
42     @BeforeMethod
43     public void before() {
44         interceptor = new ApacheClientMetricInterceptor() {};
45         request = new HttpGet(path);
46         response = new BasicHttpResponse(new ProtocolVersion("a",1,2), 200, "ok");
47         MDC.clear();
48     }
49
50     @Test
51     public void testAddHeader() {
52         interceptor.addHeader(request, "key", "value");
53         assertEquals(request.getFirstHeader("key").getValue(), "value");
54     }
55
56     @Test
57     public void testGetTargetServiceName() {
58         assertEquals(interceptor.getTargetServiceName(request), path);
59     }
60
61     @Test
62     public void testGetServiceName() {
63         assertEquals(interceptor.getTargetServiceName(request), path);
64     }
65
66     @Test
67     public void testGetHttpStatusCode() {
68         assertEquals(interceptor.getHttpStatusCode(response), 200);
69     }
70
71     @Test
72     public void testGetResponseCode() {
73         assertEquals(interceptor.getResponseCode(response), "200");
74     }
75
76     @Test
77     public void testGetTargetEntity() {
78         assertNull(interceptor.getTargetEntity(request));
79     }
80
81     @Test
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");
86     }
87
88     @Test
89     protected void whenThereIsNoInvocationIdHeader_thenMdcValueIsNull() {
90         interceptor.additionalPre(request, request);
91         assertNull(MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
92     }
93 }