[MR] Add support for configuring jaas.sasl.config at runtime
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / mr / cambria / utils / UtilsTest.java
1 /*******************************************************************************
2  /*-
3  * ============LICENSE_START=======================================================
4  * ONAP Policy Engine
5  * ================================================================================
6  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dmaap.mr.cambria.utils;
23
24 import static org.junit.Assert.*;
25
26 import java.security.Principal;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.Properties;
30
31
32 import org.apache.http.auth.BasicUserPrincipal;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.contrib.java.lang.system.EnvironmentVariables;
38 import org.springframework.mock.web.MockHttpServletRequest;
39
40
41 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
42 import org.onap.dmaap.dmf.mr.utils.Utils;
43
44 public class UtilsTest {
45
46         private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
47
48         @Rule
49         public EnvironmentVariables environmentVariables = new EnvironmentVariables();
50
51         @Before
52         public void setUp() throws Exception {
53         }
54
55         @After
56         public void tearDown() throws Exception {
57         }
58
59         @Test
60         public void testGetFormattedDate() {
61                 Date now = new Date();
62                 String dateStr = Utils.getFormattedDate(now);
63                 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
64                 String expectedStr = sdf.format(now);
65                 assertNotNull(dateStr);
66                 assertTrue("Formatted date does not match - expected [" + expectedStr
67                                                 + "] received [" + dateStr + "]",
68                                 dateStr.equalsIgnoreCase(expectedStr));
69         }
70
71         @Test
72         public void testgetUserApiKey(){
73                 MockHttpServletRequest request = new MockHttpServletRequest();
74                 request.addHeader(Utils.CAMBRIA_AUTH_HEADER, "User:Password");
75                 assertEquals("User", Utils.getUserApiKey(request));
76
77                 MockHttpServletRequest request2 = new MockHttpServletRequest();
78                 Principal principal = new BasicUserPrincipal("User@Test");
79                 request2.setUserPrincipal(principal);
80                 request2.addHeader("Authorization", "test");
81                 assertEquals("User", Utils.getUserApiKey(request2));
82
83                 MockHttpServletRequest request3 = new MockHttpServletRequest();
84                 assertNull(Utils.getUserApiKey(request3));
85         }
86
87         @Test
88         public void testgetFromattedBatchSequenceId(){
89                 Long x = new Long(1234);
90                 String str = Utils.getFromattedBatchSequenceId(x);
91                 assertEquals("001234", str);
92         }
93
94         @Test
95         public void testmessageLengthInBytes(){
96                 String str = "TestString";
97                 long length = Utils.messageLengthInBytes(str);
98                 assertEquals(10, length);
99                 assertEquals(0, Utils.messageLengthInBytes(null));
100         }
101
102         @Test
103         public void testgetResponseTransactionId(){
104                 String transactionId = "test123::sampleResponseMessage";
105                 assertEquals("test123",Utils.getResponseTransactionId(transactionId));
106                 assertNull(Utils.getResponseTransactionId(null));
107                 assertNull(Utils.getResponseTransactionId(""));
108         }
109
110         @Test
111         public void testgetSleepMsForRate(){
112                 long x = Utils.getSleepMsForRate(1024.124);
113                 assertEquals(1000, x);
114                 assertEquals(0, Utils.getSleepMsForRate(-1));
115         }
116
117         @Test
118         public void testgetRemoteAddress(){
119                 DMaaPContext dMaapContext = new DMaaPContext();
120                 MockHttpServletRequest request = new MockHttpServletRequest();
121
122                 dMaapContext.setRequest(request);
123
124                 assertEquals(request.getRemoteAddr(), Utils.getRemoteAddress(dMaapContext));
125
126                 request.addHeader("X-Forwarded-For", "XForward");
127                 assertEquals("XForward", Utils.getRemoteAddress(dMaapContext));
128
129
130         }
131
132         @Test
133         public void testGetKey(){
134                 assertNotNull(Utils.getKafkaproperty());
135
136         }
137
138         @Test
139         public void testCadiEnable(){
140                 assertFalse(Utils.isCadiEnabled());
141
142         }
143
144         @Test
145         public void testaddSaslPropsPlain() {
146                 Properties props = new Properties();
147                 props.put("security.protocol", "SASL_PLAINTEXT");
148                 props.put(Utils.SASL_MECH, "PLAIN");
149                 props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username='admin' password='admin_secret';");
150                 assertEquals(props, Utils.addSaslProps());
151         }
152
153         @Test
154         public void testaddSaslPropsScram(){
155                 Properties props = new Properties();
156                 environmentVariables.set("SASLMECH", "scram-sha-512");
157                 environmentVariables.set("JAASLOGIN", "org.apache.kafka.common.security.scram.ScramLoginModule required username='onap-dmaap-strimzi-kafka-admin' password='qul6A3TLvidY';");
158                 props.put("security.protocol", "SASL_PLAINTEXT");
159                 props.put(Utils.SASL_MECH, "SCRAM-SHA-512");
160                 props.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username='onap-dmaap-strimzi-kafka-admin' password='qul6A3TLvidY';");
161                 assertEquals(props, Utils.addSaslProps());
162         }
163 }