b4645a3c1ebb6b3b581892a961172abd8878ea28
[dmaap/messagerouter/msgrtr.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
30 import javax.servlet.http.HttpServletRequest;
31
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;
37
38 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
39 import org.onap.dmaap.dmf.mr.utils.Utils;
40
41 public class UtilsTest {
42
43         private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
44
45         @Before
46         public void setUp() throws Exception {
47         }
48
49         @After
50         public void tearDown() throws Exception {
51         }
52
53         @Test
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));
63         }
64         
65         @Test
66         public void testgetUserApiKey(){
67                 MockHttpServletRequest request = new MockHttpServletRequest();
68                 request.addHeader(Utils.CAMBRIA_AUTH_HEADER, "User:Password");
69                 assertEquals("User", Utils.getUserApiKey(request));
70                 
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));
76                 
77                 MockHttpServletRequest request3 = new MockHttpServletRequest();
78                 assertNull(Utils.getUserApiKey(request3));
79         }
80         
81         @Test
82         public void testgetFromattedBatchSequenceId(){
83                 Long x = new Long(1234);
84                 String str = Utils.getFromattedBatchSequenceId(x);
85                 assertEquals("001234", str);            
86         }
87         
88         @Test
89         public void testmessageLengthInBytes(){
90                 String str = "TestString";
91                 long length = Utils.messageLengthInBytes(str);
92                 assertEquals(10, length);
93                 assertEquals(0, Utils.messageLengthInBytes(null));
94         }
95
96         @Test
97         public void testgetResponseTransactionId(){
98                 String transactionId = "test123::sampleResponseMessage";
99                 assertEquals("test123",Utils.getResponseTransactionId(transactionId));
100                 assertNull(Utils.getResponseTransactionId(null));
101                 assertNull(Utils.getResponseTransactionId(""));
102         }
103         
104         @Test
105         public void testgetSleepMsForRate(){
106                 long x = Utils.getSleepMsForRate(1024.124);
107                 assertEquals(1000, x);
108                 assertEquals(0, Utils.getSleepMsForRate(-1));
109         }
110         
111         @Test
112         public void testgetRemoteAddress(){
113                 DMaaPContext dMaapContext = new DMaaPContext();
114                 MockHttpServletRequest request = new MockHttpServletRequest();
115                 
116                 dMaapContext.setRequest(request);
117                 
118                 assertEquals(request.getRemoteAddr(), Utils.getRemoteAddress(dMaapContext));
119                 
120                 request.addHeader("X-Forwarded-For", "XForward");
121                 assertEquals("XForward", Utils.getRemoteAddress(dMaapContext));
122                 
123                 
124         }
125 }