fix powermock issue
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / consulextend / ConsulTest.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.wrapper.consulextend;
15
16 import java.util.List;
17
18 import org.apache.http.HttpEntity;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mockito;
23 import org.mockito.invocation.InvocationOnMock;
24 import org.mockito.stubbing.Answer;
25 import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
26 import org.onap.msb.apiroute.wrapper.consulextend.async.OriginalConsulResponse;
27 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
28 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
29 import org.powermock.api.mockito.PowerMockito;
30 import org.powermock.core.classloader.annotations.PowerMockIgnore;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.fasterxml.jackson.core.type.TypeReference;
37 import com.orbitz.consul.model.ConsulResponse;
38 import com.orbitz.consul.option.CatalogOptions;
39 import com.orbitz.consul.option.QueryOptions;
40
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest({Http.class})
43 @PowerMockIgnore({"javax.net.ssl.*", "jdk.internal.reflect.*"})
44 public class ConsulTest {
45     private static Consul consul10081;
46     private static Consul consul8500;
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ConsulTest.class);
49
50     @SuppressWarnings({"unchecked", "rawtypes"})
51     @BeforeClass
52     public static void setUpBeforeClass() throws Exception {
53
54         Http http = PowerMockito.mock(Http.class);
55
56         PowerMockito.doAnswer(new Answer() {
57             @Override
58             public Object answer(InvocationOnMock invocation) throws Throwable {
59                 Object[] args = invocation.getArguments();
60                 ((ConsulResponseCallback) args[2]).onComplete(null);
61                 return null;
62             }
63         }).when(http).asyncGet(Mockito.anyString(), Mockito.any(TypeReference.class),
64                         Mockito.any(ConsulResponseCallback.class));
65
66         //
67         PowerMockito.spy(Http.class);
68         PowerMockito.when(Http.getInstance()).thenReturn(http);
69
70         //
71         consul10081 = Consul.builder().withHostAndPort("10.74.148.111", 10081).build();
72         consul8500 = Consul.builder().withHostAndPort("10.74.148.111", 8500).build();
73     }
74
75     @Test
76     public void testcatalogClient() {
77
78         ConsulResponseCallback<HttpEntity> callback = new ConsulResponseCallback<HttpEntity>() {
79
80             @Override
81             public void onComplete(ConsulResponse<HttpEntity> consulResponse) {
82                 LOGGER.info("service list complete!");
83             }
84
85             @Override
86             public void onFailure(Throwable throwable) {
87                 LOGGER.info("service list failure!");
88             }
89
90             @Override
91             public void onDelayComplete(OriginalConsulResponse<HttpEntity> originalConsulResponse) {
92                 // TODO Auto-generated method stub
93                 LOGGER.info("service list complete!");
94             }
95
96         };
97
98         // 10081
99         CatalogClient catalogclient10081 = consul10081.catalogClient();
100         catalogclient10081.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
101
102         // 8500
103         CatalogClient catalogclient8500 = consul8500.catalogClient();
104         catalogclient8500.getServices(CatalogOptions.BLANK, QueryOptions.BLANK, callback);
105     }
106
107     @Test
108     public void testhealthClient() {
109
110         ConsulResponseCallback<List<ServiceHealth>> callback = new ConsulResponseCallback<List<ServiceHealth>>() {
111
112             @Override
113             public void onComplete(ConsulResponse<List<ServiceHealth>> consulResponse) {
114                 LOGGER.info("health service complete!");
115
116             }
117
118             @Override
119             public void onFailure(Throwable throwable) {
120                 LOGGER.info("health service failure!");
121             }
122
123             @Override
124             public void onDelayComplete(OriginalConsulResponse<List<ServiceHealth>> originalConsulResponse) {
125                 // TODO Auto-generated method stub
126                 LOGGER.info("health service complete!");
127             }
128
129         };
130
131         // 10081
132         HealthClient healthClient10081 = consul10081.healthClient();
133         healthClient10081.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK, QueryOptions.BLANK,
134                         callback);
135
136         healthClient10081.getHealthyServiceInstances("apigateway-default", CatalogOptions.BLANK, QueryOptions.BLANK,
137                         callback);
138
139         // 8500
140         HealthClient healthClient8500 = consul8500.healthClient();
141         healthClient8500.getAllServiceInstances("apigateway-default", CatalogOptions.BLANK, QueryOptions.BLANK,
142                         callback);
143         healthClient8500.getHealthyServiceInstances("apigateway-default", CatalogOptions.BLANK, QueryOptions.BLANK,
144                         callback);
145
146     }
147 }