fix powermock issue
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / health / ApiRouteHealthCheckTest.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 import org.powermock.core.classloader.annotations.PowerMockIgnore;
28
29 @RunWith(PowerMockRunner.class)
30 @PrepareForTest({HttpClientUtil.class})
31 @PowerMockIgnore("jdk.internal.reflect.*")
32 public class ApiRouteHealthCheckTest {
33     private static final Logger LOGGER = LoggerFactory.getLogger(ApiRouteHealthCheckTest.class);
34
35     @Test
36     public void testchecksuccess() {
37         PowerMockito.mockStatic(HttpClientUtil.class);
38         try {
39
40             PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(200);
41         } catch (Exception e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         }
45
46         ApiRouteHealthCheck check = new ApiRouteHealthCheck();
47         Result rst = check.execute();
48
49         if (!rst.isHealthy()) {
50             LOGGER.warn("testchecksuccess health check failed:" + rst.getMessage());
51         } else {
52             LOGGER.debug(" testchecksuccess health");
53         }
54     }
55
56     @Test
57     public void testcheckfailed() {
58         PowerMockito.mockStatic(HttpClientUtil.class);
59         try {
60
61             PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(400);
62         } catch (Exception e) {
63             // TODO Auto-generated catch block
64             e.printStackTrace();
65         }
66
67         ApiRouteHealthCheck check = new ApiRouteHealthCheck();
68         Result rst = check.execute();
69
70         if (!rst.isHealthy()) {
71             LOGGER.warn("testcheckfailed health check failed:" + rst.getMessage());
72         } else {
73             LOGGER.debug(" testcheckfailed health");
74         }
75     }
76 }