50% Code Coverage-MSB Discovery
[msb/discovery.git] / sdclient / discovery-service / src / test / java / org / onap / msb / sdclient / wrapper / util / ConfigUtilTest.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.msb.sdclient.wrapper.util;
15
16 import org.junit.Assert;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.onap.msb.sdclient.DiscoverAppConfig;
21 import org.powermock.api.mockito.PowerMockito;
22 import org.powermock.core.classloader.annotations.PowerMockIgnore;
23 import org.powermock.core.classloader.annotations.PrepareForTest;
24 import org.powermock.modules.junit4.PowerMockRunner;
25
26 @RunWith(PowerMockRunner.class)
27 @PrepareForTest({DiscoverAppConfig.class,ConfigUtil.class})
28 @PowerMockIgnore( {"javax.management.*"})
29 public class ConfigUtilTest {
30
31          private static ConfigUtil configUtil =  ConfigUtil.getInstance();
32          private static DiscoverAppConfig discoverConfig;
33          
34          @BeforeClass
35             public static void setUpBeforeClass() throws Exception {
36                  discoverConfig=PowerMockito.mock(DiscoverAppConfig.class);
37              PowerMockito.when(discoverConfig.getConsulAdderss()).thenReturn("127.0.0.1:8500");
38              PowerMockito.when(discoverConfig.getConsulRegisterMode()).thenReturn("catalog");
39
40          }
41          
42          @Test
43             public void testinitConsulClientInfo() {
44                  configUtil.initConsulClientInfo(discoverConfig);
45                  Assert.assertEquals("127.0.0.1:8500",configUtil.getConsulAddress());
46                  
47                  PowerMockito.mockStatic(System.class);        
48              PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("10.74.151.26");
49              configUtil.initConsulClientInfo(discoverConfig);
50                  Assert.assertEquals("10.74.151.26:8500",configUtil.getConsulAddress());
51                  
52          }
53          
54          @Test
55             public void testinitTCP_UDP_portRange() {
56                   PowerMockito.mockStatic(System.class);        
57               PowerMockito.when(System.getenv("TCP_UDP_PORT_RANGE_START")).thenReturn("8500");
58               PowerMockito.when(System.getenv("TCP_UDP_PORT_RANGE_END")).thenReturn("8600");  
59               
60               configUtil.initTCP_UDP_portRange();
61               
62               Assert.assertEquals("8500",configUtil.getTcpudpPortRangeStart());
63               Assert.assertEquals("8600",configUtil.getTcpudpPortRangeEnd());
64          }
65          
66          @Test
67             public void testinitConsulRegisterMode() {
68                  
69                  configUtil.initConsulRegisterMode(discoverConfig);
70                  Assert.assertEquals("catalog",configUtil.getConsulRegisterMode());
71                  
72                   PowerMockito.mockStatic(System.class);        
73               PowerMockito.when(System.getenv("CONSUL_REGISTER_MODE")).thenReturn("agent");
74               configUtil.initConsulRegisterMode(discoverConfig);
75               Assert.assertEquals("agent",configUtil.getConsulRegisterMode());
76          }
77 }