Add license header for java files
[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");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at 
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.health;
17
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mockito;
21 import org.onap.msb.apiroute.health.OpenRestyHealthCheck;
22 import org.onap.msb.apiroute.wrapper.util.HttpClientUtil;
23 import org.powermock.api.mockito.PowerMockito;
24 import org.powermock.core.classloader.annotations.PrepareForTest;
25 import org.powermock.modules.junit4.PowerMockRunner;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.codahale.metrics.health.HealthCheck.Result;
30
31 @RunWith(PowerMockRunner.class)
32 @PrepareForTest({ HttpClientUtil.class,OpenRestyHealthCheck.class })
33 public class OpenRestyHealthCheckTest {
34         private static final Logger LOGGER = LoggerFactory
35                         .getLogger(OpenRestyHealthCheckTest.class);
36         
37         @Test
38         public void testchecksuccess()
39         {
40                 PowerMockito.mockStatic(HttpClientUtil.class);
41                 try {
42                         
43                         PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(200);
44                 } catch (Exception e) {
45                         // TODO Auto-generated catch block
46                         e.printStackTrace();
47                 }
48                 
49                 PowerMockito.mockStatic(System.class);
50                 PowerMockito.when(System.getenv("HTTP_OVERWRITE_PORT")).thenReturn("10080");
51                 
52                 OpenRestyHealthCheck check = new OpenRestyHealthCheck();
53                 
54                 Result rst = check.execute();
55                 
56                 if (!rst.isHealthy()) {
57                         LOGGER.warn("testchecksuccess health check failed:"+rst.getMessage());
58                 }
59                 else
60                 {
61                         LOGGER.debug(" testchecksuccess health");
62                 }
63         }
64         
65         @Test
66         public void testcheckfailed()
67         {
68                 PowerMockito.mockStatic(HttpClientUtil.class);
69                 try {
70                         
71                         PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(400);
72                 } catch (Exception e) {
73                         // TODO Auto-generated catch block
74                         e.printStackTrace();
75                 }
76                 
77                 PowerMockito.mockStatic(System.class);
78                 PowerMockito.when(System.getenv("HTTP_OVERWRITE_PORT")).thenReturn("");
79                 
80                 OpenRestyHealthCheck check = new OpenRestyHealthCheck();
81                 
82                 Result rst = check.execute();
83                 
84                 if (!rst.isHealthy()) {
85                         LOGGER.warn("testchecksuccess health check failed:"+rst.getMessage());
86                 }
87                 else
88                 {
89                         LOGGER.debug(" testchecksuccess health");
90                 }
91         }
92 }