Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / health / ConsulLinkHealthCheckTest.java
1 package org.onap.msb.apiroute.health;
2
3 import org.junit.Test;
4 import org.junit.runner.RunWith;
5 import org.mockito.Mockito;
6 import org.onap.msb.apiroute.health.ConsulLinkHealthCheck;
7 import org.onap.msb.apiroute.wrapper.util.HttpClientUtil;
8 import org.powermock.api.mockito.PowerMockito;
9 import org.powermock.core.classloader.annotations.PrepareForTest;
10 import org.powermock.modules.junit4.PowerMockRunner;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import com.codahale.metrics.health.HealthCheck.Result;
15
16 @RunWith(PowerMockRunner.class)
17 @PrepareForTest({ HttpClientUtil.class,ConsulLinkHealthCheck.class })
18 public class ConsulLinkHealthCheckTest {
19         private static final Logger LOGGER = LoggerFactory
20                         .getLogger(ConsulLinkHealthCheckTest.class);
21         
22         @Test
23         public void testchecksuccess()
24         {
25                 PowerMockito.mockStatic(HttpClientUtil.class);
26                 try {
27                         
28                         PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(200);
29                 } catch (Exception e) {
30                         // TODO Auto-generated catch block
31                         e.printStackTrace();
32                 }
33                 
34                 PowerMockito.mockStatic(System.class);
35                 PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("192.168.0.1");
36                 
37                 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
38                 Result rst = check.execute();
39                 
40                 if (!rst.isHealthy()) {
41                         LOGGER.warn("testchecksuccess health check failed:"+rst.getMessage());
42                 }
43                 else
44                 {
45                         LOGGER.debug(" testchecksuccess health");
46                 }
47         }
48         
49         @Test
50         public void testcheckfailed()
51         {
52                 PowerMockito.mockStatic(HttpClientUtil.class);
53                 try {
54                         
55                         PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(400);
56                 } catch (Exception e) {
57                         // TODO Auto-generated catch block
58                         e.printStackTrace();
59                 }
60                 
61                 PowerMockito.mockStatic(System.class);
62                 PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("192.168.0.1");
63                 
64                 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
65                 Result rst = check.execute();
66                 
67                 if (!rst.isHealthy()) {
68                         LOGGER.warn("testcheckfailed health check failed:"+rst.getMessage());
69                 }
70                 else
71                 {
72                         LOGGER.debug("testcheckfailed health");
73                 }
74         }
75         
76         
77         @Test
78         public void testcheckNoENV()
79         {
80                 PowerMockito.mockStatic(HttpClientUtil.class);
81                 try {
82                         
83                         PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(400);
84                 } catch (Exception e) {
85                         // TODO Auto-generated catch block
86                         e.printStackTrace();
87                 }
88                 
89                 PowerMockito.mockStatic(System.class);
90                 PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("");
91                 
92                 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
93                 Result rst = check.execute();
94                 
95                 if (!rst.isHealthy()) {
96                         LOGGER.warn("testcheckNoENV health check failed:"+rst.getMessage());
97                 }
98                 else
99                 {
100                         LOGGER.debug("testcheckNoENV health");
101                 }
102         }
103 }