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