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 com.github.tomakehurst.wiremock.junit.WireMockRule;
25 import com.google.common.util.concurrent.ListenableFuture;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.contrib.java.lang.system.EnvironmentVariables;
32 import org.onap.ccsdk.sli.core.dblib.DBLibConnection;
33 import org.onap.ccsdk.sli.core.dblib.DbLibService;
34 import org.onap.ccsdk.sli.plugins.grtoolkit.data.ClusterActor;
36 import org.opendaylight.controller.cluster.access.concepts.MemberName;
37 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
38 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
39 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
40 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
41 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
42 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.AdminHealthOutput;
43 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.ClusterHealthOutput;
44 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.DatabaseHealthOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.FailoverOutput;
46 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.HaltAkkaTrafficInputBuilder;
47 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.HaltAkkaTrafficOutput;
48 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.ResumeAkkaTrafficInputBuilder;
49 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.ResumeAkkaTrafficOutput;
50 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.SiteHealthOutput;
51 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.SiteIdentifierOutput;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import java.io.IOException;
55 import java.lang.reflect.InvocationTargetException;
56 import java.lang.reflect.Method;
57 import java.nio.file.Files;
58 import java.nio.file.Paths;
59 import java.sql.SQLException;
60 import java.util.ArrayList;
61 import java.util.Properties;
62 import java.util.concurrent.ExecutionException;
63 import java.util.stream.Collectors;
64 import java.util.stream.Stream;
66 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
67 import static com.github.tomakehurst.wiremock.client.WireMock.get;
68 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
69 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
71 import static org.junit.Assert.assertEquals;
72 import static org.junit.Assert.fail;
74 import static org.mockito.Mockito.mock;
75 import static org.mockito.Mockito.spy;
76 import static org.mockito.Mockito.when;
78 public class GrToolkitProviderTest {
79 GrToolkitProvider provider;
80 GrToolkitProvider providerSpy;
81 DataBroker dataBroker;
82 NotificationPublishService notificationProviderService;
83 RpcProviderRegistry rpcProviderRegistry;
84 DistributedDataStoreInterface configDatastore;
85 DbLibService dbLibService;
86 DBLibConnection connection;
87 Properties properties;
90 public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
92 public WireMockRule wireMockRule = new WireMockRule(9999);
96 environmentVariables.set("SDNC_CONFIG_DIR","src/test/resources");
97 dataBroker = mock(DataBroker.class);
98 notificationProviderService = mock(NotificationPublishService.class);
99 rpcProviderRegistry = mock(RpcProviderRegistry.class);
100 configDatastore = mock(DistributedDataStoreInterface.class);
101 dbLibService = mock(DbLibService.class);
102 connection = mock(DBLibConnection.class);
104 ActorContext actorContext = mock(ActorContext.class);
105 MemberName memberName = MemberName.forName("Test");
107 when(actorContext.getCurrentMemberName()).thenReturn(memberName);
108 when(configDatastore.getActorContext()).thenReturn(actorContext);
111 when(connection.isReadOnly()).thenReturn(false);
112 when(connection.isClosed()).thenReturn(false);
113 when(dbLibService.isActive()).thenReturn(true);
114 when(dbLibService.getConnection()).thenReturn(connection);
115 } catch(SQLException e) {
119 provider = new GrToolkitProvider(dataBroker, notificationProviderService,
120 rpcProviderRegistry, configDatastore, dbLibService);
121 providerSpy = spy(provider);
126 public void closeTest() {
129 } catch(Exception e) {
130 // Exception expected
135 public void onDataTreeChangedTest() {
136 provider.onDataTreeChanged(new ArrayList());
137 // onDataTreeChanged is an empty stub
140 private void stubController() {
141 String clusterBody = null;
142 String shardManagerBody = null;
143 String shardDefaultBody = null;
144 String shardOperationalBody = null;
145 try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/cluster.json"))) {
146 clusterBody = stream.collect(Collectors.joining());
147 } catch(IOException e) {
150 try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/shard-manager.json"))) {
151 shardManagerBody = stream.collect(Collectors.joining());
152 } catch(IOException e) {
155 try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/default-config.json"))) {
156 shardDefaultBody = stream.collect(Collectors.joining());
157 } catch(IOException e) {
160 try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/default-operational.json"))) {
161 shardOperationalBody = stream.collect(Collectors.joining());
162 } catch(IOException e) {
166 if(clusterBody == null || shardManagerBody == null || shardDefaultBody == null || shardOperationalBody == null) {
169 stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
170 stubFor(get(urlEqualTo("/jolokia/read/akka:type=Cluster")).willReturn(aResponse().withStatus(200).withBody(clusterBody)));
171 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"));
172 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"));
173 stubFor(get(urlEqualTo("/jolokia/read/org.opendaylight.controller:Category=Shards,name=member-1-shard-default-operational,type=DistributedOperationalDatastore")).willReturn(aResponse().withStatus(200).withBody(shardOperationalBody)));
177 public void clusterHealthTest() {
178 ListenableFuture<RpcResult<ClusterHealthOutput>> result = provider.clusterHealth(null);
180 assertEquals("0", result.get().getResult().getStatus());
181 } catch(InterruptedException | ExecutionException e) {
187 public void siteHealthTest() {
188 ListenableFuture<RpcResult<SiteHealthOutput>> result = provider.siteHealth(null);
190 assertEquals("200", result.get().getResult().getStatus());
191 } catch(InterruptedException | ExecutionException e) {
197 public void databaseHealthTest() {
198 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
200 assertEquals("200", result.get().getResult().getStatus());
201 } catch(InterruptedException | ExecutionException e) {
207 public void databaseHealthWhenROTest() {
209 when(connection.isReadOnly()).thenReturn(true);
210 } catch(SQLException e) {
213 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
215 assertEquals("500", result.get().getResult().getStatus());
216 } catch(InterruptedException | ExecutionException e) {
222 public void databaseHealthWhenExceptionTest() {
224 when(connection.isReadOnly()).thenThrow(new SQLException());
225 } catch(SQLException e) {
228 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
230 assertEquals("500", result.get().getResult().getStatus());
231 } catch(InterruptedException | ExecutionException e) {
237 public void adminHealthTest() {
238 ListenableFuture<RpcResult<AdminHealthOutput>> result = provider.adminHealth(null);
240 assertEquals("200", result.get().getResult().getStatus());
241 } catch(InterruptedException | ExecutionException e) {
247 public void siteIdentifierTest() {
248 ListenableFuture<RpcResult<SiteIdentifierOutput>> result = provider.siteIdentifier(null);
250 assertEquals("200", result.get().getResult().getStatus());
251 } catch(InterruptedException | ExecutionException e) {
257 public void failoverTest() {
258 ListenableFuture<RpcResult<FailoverOutput>> result = provider.failover(null);
260 assertEquals("400", result.get().getResult().getStatus());
261 } catch(InterruptedException | ExecutionException e) {
267 public void haltTrafficTest() {
268 HaltAkkaTrafficInputBuilder builder = new HaltAkkaTrafficInputBuilder();
269 builder.setNodeInfo(new ArrayList<>());
270 ListenableFuture<RpcResult<HaltAkkaTrafficOutput>> result = provider.haltAkkaTraffic(builder.build());
272 assertEquals("200", result.get().getResult().getStatus());
273 } catch(InterruptedException | ExecutionException e) {
279 public void resumeTrafficTest() {
280 ResumeAkkaTrafficInputBuilder builder = new ResumeAkkaTrafficInputBuilder();
281 builder.setNodeInfo(new ArrayList<>());
282 ListenableFuture<RpcResult<ResumeAkkaTrafficOutput>> result = provider.resumeAkkaTraffic(builder.build());
284 assertEquals("200", result.get().getResult().getStatus());
285 } catch(InterruptedException | ExecutionException e) {
291 public void executeCommandTest() {
293 Method method = provider.getClass().getDeclaredMethod("executeCommand", String.class);
294 method.setAccessible(true);
295 method.invoke(provider, "ls");
296 } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
302 public void isolateSiteFromClusterTest() {
304 ClusterActor actor = new ClusterActor();
305 actor.setNode("some-node");
306 actor.setAkkaPort("2550");
307 ArrayList<ClusterActor> activeList = new ArrayList<>();
308 activeList.add(actor);
309 ArrayList<ClusterActor> standbyList = new ArrayList<>();
310 standbyList.add(actor);
311 Method method = provider.getClass().getDeclaredMethod("isolateSiteFromCluster", ArrayList.class, ArrayList.class, String.class);
312 method.setAccessible(true);
313 method.invoke(provider, activeList, standbyList, "80");
314 } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
320 public void downUnreachableNodesTest() {
322 ClusterActor actor = new ClusterActor();
323 actor.setNode("some-node");
324 actor.setAkkaPort("2550");
325 ArrayList<ClusterActor> activeList = new ArrayList<>();
326 activeList.add(actor);
327 ArrayList<ClusterActor> standbyList = new ArrayList<>();
328 standbyList.add(actor);
329 Method method = provider.getClass().getDeclaredMethod("downUnreachableNodes", ArrayList.class, ArrayList.class, String.class);
330 method.setAccessible(true);
331 method.invoke(provider, activeList, standbyList, "80");
332 } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
338 public void backupMdSalTest() {
340 ClusterActor actor = new ClusterActor();
341 actor.setNode("some-Node");
342 actor.setAkkaPort("2550");
343 ArrayList<ClusterActor> activeList = new ArrayList<>();
344 activeList.add(actor);
345 Method method = provider.getClass().getDeclaredMethod("backupMdSal", ArrayList.class, String.class);
346 method.setAccessible(true);
347 method.invoke(provider, activeList, "80");
348 } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {