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;
31 import org.apache.http.auth.BasicUserPrincipal;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.springframework.mock.web.MockHttpServletRequest;
37 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
38 import org.onap.dmaap.dmf.mr.utils.Utils;
40 public class UtilsTest {
42 private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
45 public void setUp() throws Exception {
49 public void tearDown() throws Exception {
53 public void testGetFormattedDate() {
54 Date now = new Date();
55 String dateStr = Utils.getFormattedDate(now);
56 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
57 String expectedStr = sdf.format(now);
58 assertNotNull(dateStr);
59 assertTrue("Formatted date does not match - expected [" + expectedStr
60 + "] received [" + dateStr + "]",
61 dateStr.equalsIgnoreCase(expectedStr));
65 public void testgetUserApiKey(){
66 MockHttpServletRequest request = new MockHttpServletRequest();
67 request.addHeader(Utils.CAMBRIA_AUTH_HEADER, "User:Password");
68 assertEquals("User", Utils.getUserApiKey(request));
70 MockHttpServletRequest request2 = new MockHttpServletRequest();
71 Principal principal = new BasicUserPrincipal("User@Test");
72 request2.setUserPrincipal(principal);
73 request2.addHeader("Authorization", "test");
74 assertEquals("User", Utils.getUserApiKey(request2));
76 MockHttpServletRequest request3 = new MockHttpServletRequest();
77 assertNull(Utils.getUserApiKey(request3));
81 public void testgetFromattedBatchSequenceId(){
82 Long x = new Long(1234);
83 String str = Utils.getFromattedBatchSequenceId(x);
84 assertEquals("001234", str);
88 public void testmessageLengthInBytes(){
89 String str = "TestString";
90 long length = Utils.messageLengthInBytes(str);
91 assertEquals(10, length);
92 assertEquals(0, Utils.messageLengthInBytes(null));
96 public void testgetResponseTransactionId(){
97 String transactionId = "test123::sampleResponseMessage";
98 assertEquals("test123",Utils.getResponseTransactionId(transactionId));
99 assertNull(Utils.getResponseTransactionId(null));
100 assertNull(Utils.getResponseTransactionId(""));
104 public void testgetSleepMsForRate(){
105 long x = Utils.getSleepMsForRate(1024.124);
106 assertEquals(1000, x);
107 assertEquals(0, Utils.getSleepMsForRate(-1));
111 public void testgetRemoteAddress(){
112 DMaaPContext dMaapContext = new DMaaPContext();
113 MockHttpServletRequest request = new MockHttpServletRequest();
115 dMaapContext.setRequest(request);
117 assertEquals(request.getRemoteAddr(), Utils.getRemoteAddress(dMaapContext));
119 request.addHeader("X-Forwarded-For", "XForward");
120 assertEquals("XForward", Utils.getRemoteAddress(dMaapContext));
126 public void testGetKey(){
127 assertNotNull(Utils.getKafkaproperty());
132 public void testCadiEnable(){
133 assertFalse(Utils.isCadiEnabled());