Add license header for java files
[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");
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.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;
30
31 import redis.clients.jedis.JedisPool;
32 import redis.clients.jedis.JedisPoolConfig;
33
34 import com.codahale.metrics.health.HealthCheck.Result;
35 import com.fiftyonred.mock_jedis.MockJedisPool;
36
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);
43         
44         @Before
45     public void setUpBeforeTest() throws Exception {
46
47     }
48         
49         @SuppressWarnings("static-access")
50         @Test
51         public void testchecksuccess()
52         {
53
54         try {
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());
59                         
60                         RedisHealthCheck check = new RedisHealthCheck();
61                         Result rst = check.execute();
62                         
63                         if (!rst.isHealthy()) {
64                                 LOGGER.warn("testchecksuccess health check failed:"+rst.getMessage());
65                         }
66                         else
67                         {
68                                 LOGGER.debug(" testchecksuccess health");
69                         }
70                         
71                 } catch (Exception e) {
72                         // TODO Auto-generated catch block
73                         e.printStackTrace();
74                 }
75         }
76         
77         @Test
78         public void testcheckfailed()
79         {
80                 RedisHealthCheck check = new RedisHealthCheck();
81                 Result rst = check.execute();
82                 
83                 if (!rst.isHealthy()) {
84                         LOGGER.warn("testcheckfailed health check failed:"+rst.getMessage());
85                 }
86                 else
87                 {
88                         LOGGER.debug("testcheckfailed health");
89                 }
90                 
91         }
92 }