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;
30 import javax.servlet.http.HttpServletRequest;
32 import org.apache.http.auth.BasicUserPrincipal;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.springframework.mock.web.MockHttpServletRequest;
38 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
39 import org.onap.dmaap.dmf.mr.utils.Utils;
41 public class UtilsTest {
43 private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
46 public void setUp() throws Exception {
50 public void tearDown() throws Exception {
54 public void testGetFormattedDate() {
55 Date now = new Date();
56 String dateStr = Utils.getFormattedDate(now);
57 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
58 String expectedStr = sdf.format(now);
59 assertNotNull(dateStr);
60 assertTrue("Formatted date does not match - expected [" + expectedStr
61 + "] received [" + dateStr + "]",
62 dateStr.equalsIgnoreCase(expectedStr));
66 public void testgetUserApiKey(){
67 MockHttpServletRequest request = new MockHttpServletRequest();
68 request.addHeader(Utils.CAMBRIA_AUTH_HEADER, "User:Password");
69 assertEquals("User", Utils.getUserApiKey(request));
71 MockHttpServletRequest request2 = new MockHttpServletRequest();
72 Principal principal = new BasicUserPrincipal("User@Test");
73 request2.setUserPrincipal(principal);
74 request2.addHeader("Authorization", "test");
75 assertEquals("User", Utils.getUserApiKey(request2));
77 MockHttpServletRequest request3 = new MockHttpServletRequest();
78 assertNull(Utils.getUserApiKey(request3));
82 public void testgetFromattedBatchSequenceId(){
83 Long x = new Long(1234);
84 String str = Utils.getFromattedBatchSequenceId(x);
85 assertEquals("001234", str);
89 public void testmessageLengthInBytes(){
90 String str = "TestString";
91 long length = Utils.messageLengthInBytes(str);
92 assertEquals(10, length);
93 assertEquals(0, Utils.messageLengthInBytes(null));
97 public void testgetResponseTransactionId(){
98 String transactionId = "test123::sampleResponseMessage";
99 assertEquals("test123",Utils.getResponseTransactionId(transactionId));
100 assertNull(Utils.getResponseTransactionId(null));
101 assertNull(Utils.getResponseTransactionId(""));
105 public void testgetSleepMsForRate(){
106 long x = Utils.getSleepMsForRate(1024.124);
107 assertEquals(1000, x);
108 assertEquals(0, Utils.getSleepMsForRate(-1));
112 public void testgetRemoteAddress(){
113 DMaaPContext dMaapContext = new DMaaPContext();
114 MockHttpServletRequest request = new MockHttpServletRequest();
116 dMaapContext.setRequest(request);
118 assertEquals(request.getRemoteAddr(), Utils.getRemoteAddress(dMaapContext));
120 request.addHeader("X-Forwarded-For", "XForward");
121 assertEquals("XForward", Utils.getRemoteAddress(dMaapContext));