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