2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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;
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;
84 public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
86 public WireMockRule wireMockRule = new WireMockRule(9999);
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);
98 ActorUtils actorContext = mock(ActorUtils.class);
99 MemberName memberName = MemberName.forName("Test");
101 when(actorContext.getCurrentMemberName()).thenReturn(memberName);
102 when(configDatastore.getActorUtils()).thenReturn(actorContext);
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) {
113 provider = new GrToolkitProvider(dataBroker, notificationProviderService,
114 rpcProviderRegistry, configDatastore, dbLibService);
115 providerSpy = spy(provider);
120 public void closeTest() {
123 } catch(Exception e) {
124 // Exception expected
129 public void onDataTreeChangedTest() {
130 provider.onDataTreeChanged(new ArrayList());
131 // onDataTreeChanged is an empty stub
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) {
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) {
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) {
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) {
160 if(clusterBody == null || shardManagerBody == null || shardDefaultBody == null || shardOperationalBody == null) {
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)));
171 public void clusterHealthTest() {
172 ListenableFuture<RpcResult<ClusterHealthOutput>> result = provider.clusterHealth(null);
174 assertEquals("0", result.get().getResult().getStatus());
175 } catch(InterruptedException | ExecutionException e) {
181 public void siteHealthTest() {
182 ListenableFuture<RpcResult<SiteHealthOutput>> result = provider.siteHealth(null);
184 assertEquals("200", result.get().getResult().getStatus());
185 } catch(InterruptedException | ExecutionException e) {
191 public void databaseHealthTest() {
192 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
194 assertEquals("200", result.get().getResult().getStatus());
195 } catch(InterruptedException | ExecutionException e) {
201 public void databaseHealthWhenROTest() {
203 when(connection.isReadOnly()).thenReturn(true);
204 } catch(SQLException e) {
207 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
209 assertEquals("500", result.get().getResult().getStatus());
210 } catch(InterruptedException | ExecutionException e) {
216 public void databaseHealthWhenExceptionTest() {
218 when(connection.isReadOnly()).thenThrow(new SQLException());
219 } catch(SQLException e) {
222 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
224 assertEquals("500", result.get().getResult().getStatus());
225 } catch(InterruptedException | ExecutionException e) {
231 public void adminHealthTest() {
232 ListenableFuture<RpcResult<AdminHealthOutput>> result = provider.adminHealth(null);
234 assertEquals("200", result.get().getResult().getStatus());
235 } catch(InterruptedException | ExecutionException e) {
241 public void siteIdentifierTest() {
242 ListenableFuture<RpcResult<SiteIdentifierOutput>> result = provider.siteIdentifier(null);
244 assertEquals("200", result.get().getResult().getStatus());
245 } catch(InterruptedException | ExecutionException e) {
251 public void failoverTest() {
252 ListenableFuture<RpcResult<FailoverOutput>> result = provider.failover(null);
254 assertEquals("400", result.get().getResult().getStatus());
255 } catch(InterruptedException | ExecutionException e) {
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());
268 assertEquals("200", result.get().getResult().getStatus());
269 } catch (InterruptedException | ExecutionException e) {
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());
280 assertEquals("200", result.get().getResult().getStatus());
281 } catch(InterruptedException | ExecutionException e) {
287 public void executeCommandTest() {
289 Method method = provider.getClass().getDeclaredMethod("executeCommand", String.class);
290 method.setAccessible(true);
291 method.invoke(provider, "ls");
292 } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
298 public void isolateSiteFromClusterTest() {
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) {
316 public void downUnreachableNodesTest() {
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) {
334 public void backupMdSalTest() {
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) {