6495c89cfbe5a7927d38eb040a9efb3a7d7304b6
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.plugins.grtoolkit;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.fail;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.when;
32 import com.github.tomakehurst.wiremock.junit.WireMockRule;
33 import com.google.common.util.concurrent.ListenableFuture;
34 import java.io.IOException;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37 import java.nio.file.Files;
38 import java.nio.file.Paths;
39 import java.sql.SQLException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Properties;
43 import java.util.concurrent.ExecutionException;
44 import java.util.stream.Collectors;
45 import java.util.stream.Stream;
46 import org.junit.Before;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.contrib.java.lang.system.EnvironmentVariables;
50 import org.onap.ccsdk.sli.core.dblib.DBLibConnection;
51 import org.onap.ccsdk.sli.core.dblib.DbLibService;
52 import org.onap.ccsdk.sli.plugins.grtoolkit.data.ClusterActor;
53 import org.opendaylight.controller.cluster.access.concepts.MemberName;
54 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
55 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
56 import org.opendaylight.mdsal.binding.api.DataBroker;
57 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
58 import org.opendaylight.mdsal.binding.api.RpcProviderService;
59 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.AdminHealthOutput;
60 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.ClusterHealthOutput;
61 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.DatabaseHealthOutput;
62 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.FailoverOutput;
63 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.HaltAkkaTrafficInputBuilder;
64 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.HaltAkkaTrafficOutput;
65 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.ResumeAkkaTrafficInputBuilder;
66 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.ResumeAkkaTrafficOutput;
67 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.SiteHealthOutput;
68 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.SiteIdentifierOutput;
69 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.resume.akka.traffic.input.NodeInfoBuilder;
70 import org.opendaylight.yangtools.yang.common.RpcResult;
71
72 public class GrToolkitProviderTest {
73     GrToolkitProvider provider;
74     GrToolkitProvider providerSpy;
75     DataBroker dataBroker;
76     NotificationPublishService notificationProviderService;
77     RpcProviderService rpcProviderRegistry;
78     DistributedDataStoreInterface configDatastore;
79     DbLibService dbLibService;
80     DBLibConnection connection;
81     Properties properties;
82
83     @Rule
84     public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
85     @Rule
86     public WireMockRule wireMockRule = new WireMockRule(9999);
87
88     @Before
89     public void setup() {
90         environmentVariables.set("SDNC_CONFIG_DIR","src/test/resources");
91         dataBroker = mock(DataBroker.class);
92         notificationProviderService = mock(NotificationPublishService.class);
93         rpcProviderRegistry = mock(RpcProviderService.class);
94         configDatastore = mock(DistributedDataStoreInterface.class);
95         dbLibService = mock(DbLibService.class);
96         connection = mock(DBLibConnection.class);
97
98         ActorUtils actorContext = mock(ActorUtils.class);
99         MemberName memberName = MemberName.forName("Test");
100
101         when(actorContext.getCurrentMemberName()).thenReturn(memberName);
102         when(configDatastore.getActorUtils()).thenReturn(actorContext);
103
104         try {
105             when(connection.isReadOnly()).thenReturn(false);
106             when(connection.isClosed()).thenReturn(false);
107             when(dbLibService.isActive()).thenReturn(true);
108             when(dbLibService.getConnection()).thenReturn(connection);
109         } catch(SQLException e) {
110             fail();
111         }
112
113         provider = new GrToolkitProvider(dataBroker, notificationProviderService,
114                 rpcProviderRegistry, configDatastore, dbLibService);
115         providerSpy = spy(provider);
116         stubController();
117     }
118
119     @Test
120     public void closeTest() {
121         try {
122             provider.close();
123         } catch(Exception e) {
124             // Exception expected
125         }
126     }
127
128     @Test
129     public void onDataTreeChangedTest() {
130         provider.onDataTreeChanged(new ArrayList());
131         // onDataTreeChanged is an empty stub
132     }
133
134     private void stubController() {
135         String clusterBody = null;
136         String shardManagerBody = null;
137         String shardDefaultBody = null;
138         String shardOperationalBody = null;
139         try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/cluster.json"))) {
140             clusterBody = stream.collect(Collectors.joining());
141         } catch(IOException e) {
142             fail();
143         }
144         try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/shard-manager.json"))) {
145             shardManagerBody = stream.collect(Collectors.joining());
146         } catch(IOException e) {
147             fail();
148         }
149         try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/default-config.json"))) {
150             shardDefaultBody = stream.collect(Collectors.joining());
151         } catch(IOException e) {
152             fail();
153         }
154         try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/default-operational.json"))) {
155             shardOperationalBody = stream.collect(Collectors.joining());
156         } catch(IOException e) {
157             fail();
158         }
159
160         if(clusterBody == null || shardManagerBody == null || shardDefaultBody == null || shardOperationalBody == null) {
161             fail();
162         }
163         stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
164         stubFor(get(urlEqualTo("/jolokia/read/akka:type=Cluster")).willReturn(aResponse().withStatus(200).withBody(clusterBody)));
165         stubFor(get(urlEqualTo("/jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-config,type=DistributedConfigDatastore")).inScenario("testing").willReturn(aResponse().withStatus(200).withBody(shardManagerBody)).willSetStateTo("next"));
166         stubFor(get(urlEqualTo("/jolokia/read/org.opendaylight.controller:Category=Shards,name=member-1-shard-default-config,type=DistributedConfigDatastore")).inScenario("testing").willReturn(aResponse().withStatus(200).withBody(shardDefaultBody)).willSetStateTo("next"));
167         stubFor(get(urlEqualTo("/jolokia/read/org.opendaylight.controller:Category=Shards,name=member-1-shard-default-operational,type=DistributedOperationalDatastore")).willReturn(aResponse().withStatus(200).withBody(shardOperationalBody)));
168     }
169
170     @Test
171     public void clusterHealthTest() {
172         ListenableFuture<RpcResult<ClusterHealthOutput>> result = provider.clusterHealth(null);
173         try {
174             assertEquals("0", result.get().getResult().getStatus());
175         } catch(InterruptedException | ExecutionException e) {
176             fail();
177         }
178     }
179
180     @Test
181     public void siteHealthTest() {
182         ListenableFuture<RpcResult<SiteHealthOutput>> result = provider.siteHealth(null);
183         try {
184             assertEquals("200", result.get().getResult().getStatus());
185         } catch(InterruptedException | ExecutionException e) {
186             fail();
187         }
188     }
189
190     @Test
191     public void databaseHealthTest() {
192         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
193         try {
194             assertEquals("200", result.get().getResult().getStatus());
195         } catch(InterruptedException | ExecutionException e) {
196             fail();
197         }
198     }
199
200     @Test
201     public void databaseHealthWhenROTest() {
202         try {
203             when(connection.isReadOnly()).thenReturn(true);
204         } catch(SQLException e) {
205             fail();
206         }
207         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
208         try {
209             assertEquals("500", result.get().getResult().getStatus());
210         } catch(InterruptedException | ExecutionException e) {
211             fail();
212         }
213     }
214
215     @Test
216     public void databaseHealthWhenExceptionTest() {
217         try {
218             when(connection.isReadOnly()).thenThrow(new SQLException());
219         } catch(SQLException e) {
220             //expected
221         }
222         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
223         try {
224             assertEquals("500", result.get().getResult().getStatus());
225         } catch(InterruptedException | ExecutionException e) {
226             fail();
227         }
228     }
229
230     @Test
231     public void adminHealthTest() {
232         ListenableFuture<RpcResult<AdminHealthOutput>> result = provider.adminHealth(null);
233         try {
234             assertEquals("200", result.get().getResult().getStatus());
235         } catch(InterruptedException | ExecutionException e) {
236             fail();
237         }
238     }
239
240     @Test
241     public void siteIdentifierTest() {
242         ListenableFuture<RpcResult<SiteIdentifierOutput>> result = provider.siteIdentifier(null);
243         try {
244             assertEquals("200", result.get().getResult().getStatus());
245         } catch(InterruptedException | ExecutionException e) {
246             fail();
247         }
248     }
249
250     @Test
251     public void failoverTest() {
252         ListenableFuture<RpcResult<FailoverOutput>> result = provider.failover(null);
253         try {
254             assertEquals("400", result.get().getResult().getStatus());
255         } catch(InterruptedException | ExecutionException e) {
256             fail();
257         }
258     }
259
260     @Test
261     public void haltTrafficTest() {
262         HaltAkkaTrafficInputBuilder builder = new HaltAkkaTrafficInputBuilder();
263         builder.setNodeInfo(Arrays.asList(
264                 new org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.halt.akka.traffic.
265                 input.NodeInfoBuilder().build()));
266         ListenableFuture<RpcResult<HaltAkkaTrafficOutput>> result = provider.haltAkkaTraffic(builder.build());
267         try {
268             assertEquals("200", result.get().getResult().getStatus());
269         } catch (InterruptedException | ExecutionException e) {
270             fail();
271         }
272     }
273
274     @Test
275     public void resumeTrafficTest() {
276         ResumeAkkaTrafficInputBuilder builder = new ResumeAkkaTrafficInputBuilder();
277         builder.setNodeInfo(Arrays.asList(new NodeInfoBuilder().build()));
278         ListenableFuture<RpcResult<ResumeAkkaTrafficOutput>> result = provider.resumeAkkaTraffic(builder.build());
279         try {
280             assertEquals("200", result.get().getResult().getStatus());
281         } catch(InterruptedException | ExecutionException e) {
282             fail();
283         }
284     }
285
286     @Test
287     public void executeCommandTest() {
288         try {
289             Method method = provider.getClass().getDeclaredMethod("executeCommand", String.class);
290             method.setAccessible(true);
291             method.invoke(provider, "ls");
292         } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
293             fail();
294         }
295     }
296
297     @Test
298     public void isolateSiteFromClusterTest() {
299         try {
300             ClusterActor actor = new ClusterActor();
301             actor.setNode("some-node");
302             actor.setAkkaPort("2550");
303             ArrayList<ClusterActor> activeList = new ArrayList<>();
304             activeList.add(actor);
305             ArrayList<ClusterActor> standbyList = new ArrayList<>();
306             standbyList.add(actor);
307             Method method = provider.getClass().getDeclaredMethod("isolateSiteFromCluster", ArrayList.class, ArrayList.class, String.class);
308             method.setAccessible(true);
309             method.invoke(provider, activeList, standbyList, "80");
310         } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
311             fail();
312         }
313     }
314
315     @Test
316     public void downUnreachableNodesTest() {
317         try {
318             ClusterActor actor = new ClusterActor();
319             actor.setNode("some-node");
320             actor.setAkkaPort("2550");
321             ArrayList<ClusterActor> activeList = new ArrayList<>();
322             activeList.add(actor);
323             ArrayList<ClusterActor> standbyList = new ArrayList<>();
324             standbyList.add(actor);
325             Method method = provider.getClass().getDeclaredMethod("downUnreachableNodes", ArrayList.class, ArrayList.class, String.class);
326             method.setAccessible(true);
327             method.invoke(provider, activeList, standbyList, "80");
328         } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
329             fail();
330         }
331     }
332
333     @Test
334     public void backupMdSalTest() {
335         try {
336             ClusterActor actor = new ClusterActor();
337             actor.setNode("some-Node");
338             actor.setAkkaPort("2550");
339             ArrayList<ClusterActor> activeList = new ArrayList<>();
340             activeList.add(actor);
341             Method method = provider.getClass().getDeclaredMethod("backupMdSal", ArrayList.class, String.class);
342             method.setAccessible(true);
343             method.invoke(provider, activeList, "80");
344         } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
345             fail();
346         }
347     }
348 }