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