Add license header for java files
[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");
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.ConsulLinkHealthCheck;
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,ConsulLinkHealthCheck.class })
33 public class ConsulLinkHealthCheckTest {
34         private static final Logger LOGGER = LoggerFactory
35                         .getLogger(ConsulLinkHealthCheckTest.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("CONSUL_IP")).thenReturn("192.168.0.1");
51                 
52                 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
53                 Result rst = check.execute();
54                 
55                 if (!rst.isHealthy()) {
56                         LOGGER.warn("testchecksuccess health check failed:"+rst.getMessage());
57                 }
58                 else
59                 {
60                         LOGGER.debug(" testchecksuccess health");
61                 }
62         }
63         
64         @Test
65         public void testcheckfailed()
66         {
67                 PowerMockito.mockStatic(HttpClientUtil.class);
68                 try {
69                         
70                         PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(400);
71                 } catch (Exception e) {
72                         // TODO Auto-generated catch block
73                         e.printStackTrace();
74                 }
75                 
76                 PowerMockito.mockStatic(System.class);
77                 PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("192.168.0.1");
78                 
79                 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
80                 Result rst = check.execute();
81                 
82                 if (!rst.isHealthy()) {
83                         LOGGER.warn("testcheckfailed health check failed:"+rst.getMessage());
84                 }
85                 else
86                 {
87                         LOGGER.debug("testcheckfailed health");
88                 }
89         }
90         
91         
92         @Test
93         public void testcheckNoENV()
94         {
95                 PowerMockito.mockStatic(HttpClientUtil.class);
96                 try {
97                         
98                         PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(400);
99                 } catch (Exception e) {
100                         // TODO Auto-generated catch block
101                         e.printStackTrace();
102                 }
103                 
104                 PowerMockito.mockStatic(System.class);
105                 PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("");
106                 
107                 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
108                 Result rst = check.execute();
109                 
110                 if (!rst.isHealthy()) {
111                         LOGGER.warn("testcheckNoENV health check failed:"+rst.getMessage());
112                 }
113                 else
114                 {
115                         LOGGER.debug("testcheckNoENV health");
116                 }
117         }
118 }