1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
23 package org.onap.dmaap.datarouter.node;
25 import static org.mockito.Mockito.when;
27 import javax.servlet.http.HttpServletRequest;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.powermock.core.classloader.annotations.PowerMockIgnore;
33 import org.powermock.modules.junit4.PowerMockRunner;
36 @RunWith(PowerMockRunner.class)
37 @PowerMockIgnore({"java.net.ssl", "javax.security.auth.x500.X500Principal"})
38 public class NodeUtilsTest {
41 private HttpServletRequest request;
44 public void Given_Uri_With_Params_Then_Get_Feed_And_File_Id_Returns_Correct_Values() {
45 String uri = "prov.datarouternew.com:8443/feed/12/fileName";
46 String[] uriParams = NodeUtils.getFeedAndFileID(uri);
47 assert uriParams != null;
48 Assert.assertEquals("12", uriParams[0]);
49 Assert.assertEquals("fileName", uriParams[1]);
53 public void Given_Uri_With_Illegal_Params_Then_Get_Feed_And_File_Id_Returns_Null() {
54 String uri = "prov.datarouternew.com:8443/feed";
55 String[] uriParams = NodeUtils.getFeedAndFileID(uri);
56 Assert.assertNull(uriParams);
60 public void Given_String_With_Escape_Fields_Then_Loge_Returns_Special_Chars() {
61 String s = NodeUtils.loge("\\search|pub|12\n");
62 Assert.assertEquals("\\esearch\\ppub\\p12\\n", s);
66 public void Given_String_With_Special_Chars_Then_Loge_Returns_String_With_Escape_Fields() {
67 String s = NodeUtils.unloge("\\esearch\\ppub\\p12\\n");
68 Assert.assertEquals("\\search|pub|12\n", s);
72 public void Given_Request_Has_RequestId_And_InvocationId_Headers_Set_MDC_Values() {
73 when(request.getHeader("X-ONAP-RequestID")).thenReturn("123");
74 when(request.getHeader("X-InvocationID")).thenReturn("456");
75 NodeUtils.setRequestIdAndInvocationId(request);
76 Assert.assertEquals("123", MDC.get("RequestId"));
77 Assert.assertEquals("456", MDC.get("InvocationId"));
81 public void Given_Get_CanonicalName_Called_Valid_CN_Returned_From_JKS() {
82 String canonicalName = NodeUtils.getCanonicalName("jks", "src/test/resources/org.onap.dmaap-dr-test-cert.jks", "WGxd2P6MDo*Bi4+UdzWs{?$8");
83 Assert.assertEquals("dmaap-dr-node", canonicalName);
87 public void Given_Get_CanonicalName_Called_Valid_CN_Returned_From_P12() {
88 String canonicalName = NodeUtils.getCanonicalName("PKCS12", "src/test/resources/aaf/org.onap.dmaap-dr.p12", "V+b}aGuWxHI+BPSNMVXqD*bx");
89 Assert.assertEquals("dmaap-dr-node", canonicalName);