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