1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
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;
29 import com.codahale.metrics.health.HealthCheck.Result;
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);
38 public void testchecksuccess()
40 PowerMockito.mockStatic(HttpClientUtil.class);
43 PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(200);
44 } catch (Exception e) {
45 // TODO Auto-generated catch block
49 PowerMockito.mockStatic(System.class);
50 PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("192.168.0.1");
52 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
53 Result rst = check.execute();
55 if (!rst.isHealthy()) {
56 LOGGER.warn("testchecksuccess health check failed:"+rst.getMessage());
60 LOGGER.debug(" testchecksuccess health");
65 public void testcheckfailed()
67 PowerMockito.mockStatic(HttpClientUtil.class);
70 PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(400);
71 } catch (Exception e) {
72 // TODO Auto-generated catch block
76 PowerMockito.mockStatic(System.class);
77 PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("192.168.0.1");
79 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
80 Result rst = check.execute();
82 if (!rst.isHealthy()) {
83 LOGGER.warn("testcheckfailed health check failed:"+rst.getMessage());
87 LOGGER.debug("testcheckfailed health");
93 public void testcheckNoENV()
95 PowerMockito.mockStatic(HttpClientUtil.class);
98 PowerMockito.when(HttpClientUtil.httpGetStatus(Mockito.anyString())).thenReturn(400);
99 } catch (Exception e) {
100 // TODO Auto-generated catch block
104 PowerMockito.mockStatic(System.class);
105 PowerMockito.when(System.getenv("CONSUL_IP")).thenReturn("");
107 ConsulLinkHealthCheck check = new ConsulLinkHealthCheck();
108 Result rst = check.execute();
110 if (!rst.isHealthy()) {
111 LOGGER.warn("testcheckNoENV health check failed:"+rst.getMessage());
115 LOGGER.debug("testcheckNoENV health");