1 /*******************************************************************************
3 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.dmaap.mr.cambria.utils;
24 import static org.junit.Assert.*;
26 import java.security.Principal;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.Properties;
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;
41 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
42 import org.onap.dmaap.dmf.mr.utils.Utils;
44 public class UtilsTest {
46 private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
49 public EnvironmentVariables environmentVariables = new EnvironmentVariables();
52 public void setUp() throws Exception {
56 public void tearDown() throws Exception {
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));
72 public void testgetUserApiKey(){
73 MockHttpServletRequest request = new MockHttpServletRequest();
74 request.addHeader(Utils.CAMBRIA_AUTH_HEADER, "User:Password");
75 assertEquals("User", Utils.getUserApiKey(request));
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));
83 MockHttpServletRequest request3 = new MockHttpServletRequest();
84 assertNull(Utils.getUserApiKey(request3));
88 public void testgetFromattedBatchSequenceId(){
89 Long x = new Long(1234);
90 String str = Utils.getFromattedBatchSequenceId(x);
91 assertEquals("001234", str);
95 public void testmessageLengthInBytes(){
96 String str = "TestString";
97 long length = Utils.messageLengthInBytes(str);
98 assertEquals(10, length);
99 assertEquals(0, Utils.messageLengthInBytes(null));
103 public void testgetResponseTransactionId(){
104 String transactionId = "test123::sampleResponseMessage";
105 assertEquals("test123",Utils.getResponseTransactionId(transactionId));
106 assertNull(Utils.getResponseTransactionId(null));
107 assertNull(Utils.getResponseTransactionId(""));
111 public void testgetSleepMsForRate(){
112 long x = Utils.getSleepMsForRate(1024.124);
113 assertEquals(1000, x);
114 assertEquals(0, Utils.getSleepMsForRate(-1));
118 public void testgetRemoteAddress(){
119 DMaaPContext dMaapContext = new DMaaPContext();
120 MockHttpServletRequest request = new MockHttpServletRequest();
122 dMaapContext.setRequest(request);
124 assertEquals(request.getRemoteAddr(), Utils.getRemoteAddress(dMaapContext));
126 request.addHeader("X-Forwarded-For", "XForward");
127 assertEquals("XForward", Utils.getRemoteAddress(dMaapContext));
133 public void testGetKey(){
134 assertNotNull(Utils.getKafkaproperty());
139 public void testCadiEnable(){
140 assertFalse(Utils.isCadiEnabled());
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());
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());