16b874e788c651fc4989f999a2bfbf34007ba5f1
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / health / RedisHealthCheckTest.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.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
20 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
21 import org.powermock.api.mockito.PowerMockito;
22 import org.powermock.core.classloader.annotations.PowerMockIgnore;
23 import org.powermock.core.classloader.annotations.PrepareForTest;
24 import org.powermock.modules.junit4.PowerMockRunner;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.codahale.metrics.health.HealthCheck.Result;
29 import com.fiftyonred.mock_jedis.MockJedisPool;
30
31 import redis.clients.jedis.JedisPool;
32 import redis.clients.jedis.JedisPoolConfig;
33
34 @RunWith(PowerMockRunner.class)
35 @PrepareForTest({JedisUtil.class, RedisAccessWrapper.class})
36 @PowerMockIgnore({"javax.management.*"})
37 public class RedisHealthCheckTest {
38     private static final Logger LOGGER = LoggerFactory.getLogger(RedisHealthCheckTest.class);
39
40     @Before
41     public void setUpBeforeTest() throws Exception {
42
43     }
44
45     @SuppressWarnings("static-access")
46     @Test
47     public void testchecksuccess() {
48
49         try {
50             final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
51             PowerMockito.mockStatic(JedisUtil.class);
52             JedisUtil jedisUtil = PowerMockito.mock(JedisUtil.class);
53             PowerMockito.when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
54
55             RedisHealthCheck check = new RedisHealthCheck();
56             Result rst = check.execute();
57
58             if (!rst.isHealthy()) {
59                 LOGGER.warn("testchecksuccess health check failed:" + rst.getMessage());
60             } else {
61                 LOGGER.debug(" testchecksuccess health");
62             }
63
64         } catch (Exception e) {
65             // TODO Auto-generated catch block
66             e.printStackTrace();
67         }
68     }
69
70     @Test
71     public void testcheckfailed() {
72         RedisHealthCheck check = new RedisHealthCheck();
73         Result rst = check.execute();
74
75         if (!rst.isHealthy()) {
76             LOGGER.warn("testcheckfailed health check failed:" + rst.getMessage());
77         } else {
78             LOGGER.debug("testcheckfailed health");
79         }
80
81     }
82 }