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.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.onap.msb.apiroute.health.RedisHealthCheck;
22 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
23 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
24 import org.powermock.api.mockito.PowerMockito;
25 import org.powermock.core.classloader.annotations.PowerMockIgnore;
26 import org.powermock.core.classloader.annotations.PrepareForTest;
27 import org.powermock.modules.junit4.PowerMockRunner;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import redis.clients.jedis.JedisPool;
32 import redis.clients.jedis.JedisPoolConfig;
34 import com.codahale.metrics.health.HealthCheck.Result;
35 import com.fiftyonred.mock_jedis.MockJedisPool;
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest({JedisUtil.class,RedisAccessWrapper.class})
39 @PowerMockIgnore( {"javax.management.*"})
40 public class RedisHealthCheckTest {
41 private static final Logger LOGGER = LoggerFactory
42 .getLogger(RedisHealthCheckTest.class);
45 public void setUpBeforeTest() throws Exception {
49 @SuppressWarnings("static-access")
51 public void testchecksuccess()
55 final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
56 PowerMockito.mockStatic(JedisUtil.class);
57 JedisUtil jedisUtil=PowerMockito.mock(JedisUtil.class);
58 PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
60 RedisHealthCheck check = new RedisHealthCheck();
61 Result rst = check.execute();
63 if (!rst.isHealthy()) {
64 LOGGER.warn("testchecksuccess health check failed:"+rst.getMessage());
68 LOGGER.debug(" testchecksuccess health");
71 } catch (Exception e) {
72 // TODO Auto-generated catch block
78 public void testcheckfailed()
80 RedisHealthCheck check = new RedisHealthCheck();
81 Result rst = check.execute();
83 if (!rst.isHealthy()) {
84 LOGGER.warn("testcheckfailed health check failed:"+rst.getMessage());
88 LOGGER.debug("testcheckfailed health");