Add RequestId and InvocationId to DR
[dmaap/datarouter.git] / datarouter-node / src / test / java / org / onap / dmaap / datarouter / node / NodeUtilsTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23 package org.onap.dmaap.datarouter.node;
24
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.powermock.core.classloader.annotations.PrepareForTest;
30 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
31 import org.powermock.modules.junit4.PowerMockRunner;
32 import org.slf4j.MDC;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import java.io.IOException;
37 import java.net.InetAddress;
38 import java.util.UUID;
39
40 import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
41 import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;
42 import static org.mockito.Mockito.when;
43 import static org.powermock.api.mockito.PowerMockito.mockStatic;
44
45 @RunWith(PowerMockRunner.class)
46 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeUtils")
47 @PrepareForTest({ UUID.class, InetAddress.class })
48 public class NodeUtilsTest {
49
50     @Mock
51     private HttpServletRequest request;
52
53     @Test
54     public void Given_Uri_With_Params_Then_Get_Feed_And_File_Id_Returns_Correct_Values() {
55         String uri = "prov.datarouternew.com:8443/feed/12/fileName";
56         String[] uriParams = NodeUtils.getFeedAndFileID(uri);
57         Assert.assertEquals("12", uriParams[0]);
58         Assert.assertEquals("fileName", uriParams[1]);
59     }
60
61     @Test
62     public void Given_Uri_With_Illegal_Params_Then_Get_Feed_And_File_Id_Returns_Null() {
63         String uri = "prov.datarouternew.com:8443/feed";
64         String[] uriParams = NodeUtils.getFeedAndFileID(uri);
65         Assert.assertNull(uriParams);
66     }
67
68     @Test
69     public void Given_String_With_Escape_Fields_Then_Loge_Returns_Special_Chars() {
70         String s = NodeUtils.loge("\\search|pub|12\n");
71         Assert.assertEquals("\\esearch\\ppub\\p12\\n", s);
72     }
73
74     @Test
75     public void Given_String_With_Special_Chars_Then_Loge_Returns_String_With_Escape_Fields() {
76         String s = NodeUtils.unloge("\\esearch\\ppub\\p12\\n");
77         Assert.assertEquals("\\search|pub|12\n", s);
78     }
79
80     @Test
81     public void Given_Request_Has_RequestId_And_InvocationId_Headers_Set_MDC_Values() {
82         when(request.getHeader("X-ONAP-RequestID")).thenReturn("123");
83         when(request.getHeader("X-InvocationID")).thenReturn("456");
84         NodeUtils.setRequestIdAndInvocationId(request);
85         Assert.assertEquals("123", MDC.get("RequestId"));
86         Assert.assertEquals("456", MDC.get("InvocationId"));
87     }
88
89     @Test
90     public void Given_setIpAndFqdnForEelf_Called_Set_MDC_Values() throws IOException {
91         mockStatic(InetAddress.class);
92         when(InetAddress.getLocalHost().getHostName()).thenReturn("testHostName");
93         when(InetAddress.getLocalHost().getHostAddress()).thenReturn("testHostAddress");
94         NodeUtils.setIpAndFqdnForEelf("doGet");
95         Assert.assertEquals("testHostName", MDC.get(MDC_SERVER_FQDN));
96         Assert.assertEquals("testHostAddress", MDC.get(MDC_SERVER_IP_ADDRESS));
97     }
98
99     @Test
100     public void Given_Request_Has_Empty_RequestId_And_InvocationId_Headers_Generate_MDC_Values() {
101         when(request.getHeader("X-ONAP-RequestID")).thenReturn("");
102         when(request.getHeader("X-InvocationID")).thenReturn("");
103         mockStatic(UUID.class);
104         when(UUID.randomUUID().toString()).thenReturn("123", "456");
105         NodeUtils.setRequestIdAndInvocationId(request);
106         Assert.assertEquals("123", MDC.get("RequestId"));
107         Assert.assertEquals("456", MDC.get("InvocationId"));
108     }
109 }