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