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.google.common.util.concurrent.ListenableFuture;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.contrib.java.lang.system.EnvironmentVariables;
28 import org.onap.ccsdk.sli.core.dblib.DBLibConnection;
29 import org.onap.ccsdk.sli.core.dblib.DbLibService;
30 import org.onap.ccsdk.sli.plugins.grtoolkit.data.ClusterActor;
31 import org.opendaylight.controller.cluster.access.concepts.MemberName;
32 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
33 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
36 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
37 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.AdminHealthOutput;
38 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.ClusterHealthOutput;
39 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.DatabaseHealthOutput;
40 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.FailoverOutput;
41 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.FailoverOutputBuilder;
42 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.SiteHealthOutput;
43 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.SiteIdentifierOutput;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import java.lang.reflect.Field;
47 import java.lang.reflect.InvocationTargetException;
48 import java.lang.reflect.Method;
49 import java.sql.SQLException;
50 import java.util.ArrayList;
51 import java.util.HashMap;
53 import java.util.concurrent.ExecutionException;
55 import static org.junit.Assert.assertEquals;
56 import static org.junit.Assert.fail;
57 import static org.mockito.Mockito.mock;
58 import static org.mockito.Mockito.spy;
59 import static org.mockito.Mockito.when;
61 public class GrToolkitProviderTest {
62 GrToolkitProvider provider;
63 GrToolkitProvider providerSpy;
64 DataBroker dataBroker;
65 NotificationPublishService notificationProviderService;
66 RpcProviderRegistry rpcProviderRegistry;
67 DistributedDataStoreInterface configDatastore;
68 DbLibService dbLibService;
69 DBLibConnection connection;
72 public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
76 environmentVariables.set("SDNC_CONFIG_DIR","src/test/resources/");
77 dataBroker = mock(DataBroker.class);
78 notificationProviderService = mock(NotificationPublishService.class);
79 rpcProviderRegistry = mock(RpcProviderRegistry.class);
80 configDatastore = mock(DistributedDataStoreInterface.class);
81 dbLibService = mock(DbLibService.class);
82 connection = mock(DBLibConnection.class);
84 ActorContext actorContext = mock(ActorContext.class);
85 MemberName memberName = MemberName.forName("Test");
87 when(actorContext.getCurrentMemberName()).thenReturn(memberName);
88 when(configDatastore.getActorContext()).thenReturn(actorContext);
91 when(connection.isReadOnly()).thenReturn(false);
92 when(connection.isClosed()).thenReturn(false);
93 when(dbLibService.isActive()).thenReturn(true);
94 when(dbLibService.getConnection()).thenReturn(connection);
95 } catch(SQLException e) {
99 provider = new GrToolkitProvider(dataBroker, notificationProviderService,
100 rpcProviderRegistry, configDatastore, dbLibService);
101 providerSpy = spy(provider);
105 public void closeTest() {
110 // Exception expected
115 public void onDataTreeChangedTest() {
116 provider.onDataTreeChanged(new ArrayList());
117 // onDataTreeChanged is an empty stub
121 public void clusterHealthTest() {
122 ListenableFuture<RpcResult<ClusterHealthOutput>> result = provider.clusterHealth(null);
124 assertEquals("200", result.get().getResult().getStatus());
125 } catch(InterruptedException | ExecutionException e) {
131 public void siteHealthTest() {
132 ListenableFuture<RpcResult<SiteHealthOutput>> result = provider.siteHealth(null);
134 assertEquals("200", result.get().getResult().getStatus());
135 } catch(InterruptedException | ExecutionException e) {
141 public void siteHealth6NodeTest() {
142 Map<String, ClusterActor> memberMap = new HashMap<>();
144 for(int ndx = 0; ndx < 6; ndx++) {
145 actor = new ClusterActor();
146 actor.setNode("member-" + (ndx + 1));
148 actor.setUnreachable(false);
150 memberMap.put(actor.getNode(), actor);
154 Field field = provider.getClass().getDeclaredField("siteConfiguration");
155 field.setAccessible(true);
156 field.set(provider, GrToolkitProvider.SiteConfiguration.GEO);
158 field = provider.getClass().getDeclaredField("memberMap");
159 field.setAccessible(true);
160 field.set(provider, memberMap);
163 actor = new ClusterActor();
164 actor.setNode("member-1");
165 field = provider.getClass().getDeclaredField("self");
166 field.setAccessible(true);
167 field.set(provider, actor);
169 field = provider.getClass().getDeclaredField("member");
170 field.setAccessible(true);
171 field.set(provider, actor.getNode());
173 catch(IllegalAccessException | NoSuchFieldException e) {
177 ListenableFuture<RpcResult<SiteHealthOutput>> result = provider.siteHealth(null);
179 assertEquals("200", result.get().getResult().getStatus());
180 } catch(InterruptedException | ExecutionException e) {
186 public void databaseHealthTest() {
187 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
189 assertEquals("200", result.get().getResult().getStatus());
190 } catch(InterruptedException | ExecutionException e) {
196 public void databaseHealthWhenROTest() {
198 when(connection.isReadOnly()).thenReturn(true);
199 } catch(SQLException e) {
202 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
204 assertEquals("200", result.get().getResult().getStatus());
205 } catch(InterruptedException | ExecutionException e) {
211 public void databaseHealthWhenExceptionTest() {
213 when(connection.isReadOnly()).thenThrow(new SQLException());
214 } catch(SQLException e) {
217 ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
219 assertEquals("200", result.get().getResult().getStatus());
220 } catch(InterruptedException | ExecutionException e) {
226 public void adminHealthTest() {
227 ListenableFuture<RpcResult<AdminHealthOutput>> result = provider.adminHealth(null);
229 assertEquals("200", result.get().getResult().getStatus());
230 } catch(InterruptedException | ExecutionException e) {
236 public void siteIdentifierTest() {
237 ListenableFuture<RpcResult<SiteIdentifierOutput>> result = provider.siteIdentifier(null);
239 assertEquals("200", result.get().getResult().getStatus());
240 } catch(InterruptedException | ExecutionException e) {
246 public void failoverTest() {
247 ListenableFuture<RpcResult<FailoverOutput>> result = provider.failover(null);
249 assertEquals("400", result.get().getResult().getStatus());
250 } catch(InterruptedException | ExecutionException e) {
256 public void executeCommandTest() {
258 Method method = provider.getClass().getDeclaredMethod("executeCommand", String.class);
259 method.setAccessible(true);
260 method.invoke(provider, "ls");
262 catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
268 public void isolateSiteFromClusterTest() {
270 ClusterActor actor = new ClusterActor();
271 actor.setNode("some-node");
272 actor.setAkkaPort("2550");
273 ArrayList<ClusterActor> activeList = new ArrayList<>();
274 activeList.add(actor);
275 ArrayList<ClusterActor> standbyList = new ArrayList<>();
276 standbyList.add(actor);
277 Method method = provider.getClass().getDeclaredMethod("isolateSiteFromCluster", ArrayList.class, ArrayList.class, String.class);
278 method.setAccessible(true);
279 method.invoke(provider, activeList, standbyList, "80");
281 catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
287 public void downUnreachableNodesTest() {
289 ClusterActor actor = new ClusterActor();
290 actor.setNode("some-node");
291 actor.setAkkaPort("2550");
292 ArrayList<ClusterActor> activeList = new ArrayList<>();
293 activeList.add(actor);
294 ArrayList<ClusterActor> standbyList = new ArrayList<>();
295 standbyList.add(actor);
296 Method method = provider.getClass().getDeclaredMethod("downUnreachableNodes", ArrayList.class, ArrayList.class, String.class);
297 method.setAccessible(true);
298 method.invoke(provider, activeList, standbyList, "80");
300 catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
306 public void changeClusterVotingTest() {
308 ClusterActor actor = new ClusterActor();
309 actor.setMember("some-member");
310 actor.setNode("some-Node");
311 ArrayList<ClusterActor> activeList = new ArrayList<>();
312 activeList.add(actor);
313 ArrayList<ClusterActor> standbyList = new ArrayList<>();
314 standbyList.add(actor);
315 Field field = provider.getClass().getDeclaredField("self");
316 field.setAccessible(true);
317 field.set(provider, actor);
318 Method method = provider.getClass().getDeclaredMethod("changeClusterVoting", FailoverOutputBuilder.class, ArrayList.class, ArrayList.class, String.class);
319 method.setAccessible(true);
320 method.invoke(provider, new FailoverOutputBuilder(), activeList, standbyList, "80");
322 catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
328 public void backupMdSalTest() {
330 ClusterActor actor = new ClusterActor();
331 actor.setNode("some-Node");
332 actor.setAkkaPort("2550");
333 ArrayList<ClusterActor> activeList = new ArrayList<>();
334 activeList.add(actor);
335 Method method = provider.getClass().getDeclaredMethod("backupMdSal", ArrayList.class, String.class);
336 method.setAccessible(true);
337 method.invoke(provider, activeList, "80");
339 catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {