Add license header for java files
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / util / MicroServiceUtilTest.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at 
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.util;
17
18 import javax.servlet.http.HttpServletRequest;
19
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil;
24 import org.powermock.api.mockito.PowerMockito;
25 import org.powermock.core.classloader.annotations.PrepareForTest;
26 import org.powermock.modules.junit4.PowerMockRunner;
27
28 import redis.clients.jedis.Jedis;
29
30 @RunWith(PowerMockRunner.class)
31 @PrepareForTest({HttpServletRequest.class})
32 public class MicroServiceUtilTest {
33
34   @Test
35   public void test_getPrefixedKey(){
36     Assert.assertEquals("discover:microservices:test:v1",MicroServiceUtil.getPrefixedKey("test","v1"));
37   }
38   
39   @Test
40   public void test_getServiceKey(){
41     Assert.assertEquals("discover:microservices:test:v1",MicroServiceUtil.getServiceKey("test","v1"));
42   }
43   
44   @Test
45   public void test_getRealIp(){
46     HttpServletRequest request=PowerMockito.mock(HttpServletRequest.class);  
47     PowerMockito.when(request.getHeader("X-Forwarded-For")).thenReturn("127.0.0.1");
48     Assert.assertEquals("127.0.0.1",MicroServiceUtil.getRealIp(request));
49   
50     PowerMockito.when(request.getHeader("X-Forwarded-For")).thenReturn("");
51     PowerMockito.when(request.getHeader("X-Real-IP")).thenReturn("127.0.0.2");
52     Assert.assertEquals("127.0.0.2",MicroServiceUtil.getRealIp(request));
53     
54     PowerMockito.when(request.getHeader("X-Forwarded-For")).thenReturn("");
55     PowerMockito.when(request.getHeader("X-Real-IP")).thenReturn("");
56     PowerMockito.when(request.getRemoteAddr()).thenReturn("127.0.0.3");
57     Assert.assertEquals("127.0.0.3",MicroServiceUtil.getRealIp(request));
58     
59   }
60 }