Merge "[CCSDK-1241] Increase GRToolkit Unit Test Coverage"
[ccsdk/sli/plugins.git] / grToolkit / provider / src / test / java / org / onap / ccsdk / sli / plugins / grtoolkit / GrToolkitProviderTest.java
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 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;
45
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;
52 import java.util.Map;
53 import java.util.concurrent.ExecutionException;
54
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;
60
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;
70
71     @Rule
72     public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
73
74     @Before
75     public void setup() {
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);
83
84         ActorContext actorContext = mock(ActorContext.class);
85         MemberName memberName = MemberName.forName("Test");
86
87         when(actorContext.getCurrentMemberName()).thenReturn(memberName);
88         when(configDatastore.getActorContext()).thenReturn(actorContext);
89
90         try {
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) {
96             fail();
97         }
98
99         provider = new GrToolkitProvider(dataBroker, notificationProviderService,
100                 rpcProviderRegistry, configDatastore, dbLibService);
101         providerSpy = spy(provider);
102     }
103
104     @Test
105     public void closeTest() {
106         try {
107             provider.close();
108         }
109         catch(Exception e) {
110             // Exception expected
111         }
112     }
113
114     @Test
115     public void onDataTreeChangedTest() {
116         provider.onDataTreeChanged(new ArrayList());
117         // onDataTreeChanged is an empty stub
118     }
119
120     @Test
121     public void clusterHealthTest() {
122         ListenableFuture<RpcResult<ClusterHealthOutput>> result = provider.clusterHealth(null);
123         try {
124             assertEquals("200", result.get().getResult().getStatus());
125         } catch(InterruptedException | ExecutionException e) {
126             fail();
127         }
128     }
129
130     @Test
131     public void siteHealthTest() {
132         ListenableFuture<RpcResult<SiteHealthOutput>> result = provider.siteHealth(null);
133         try {
134             assertEquals("200", result.get().getResult().getStatus());
135         } catch(InterruptedException | ExecutionException e) {
136             fail();
137         }
138     }
139
140     @Test
141     public void siteHealth6NodeTest() {
142         Map<String, ClusterActor> memberMap = new HashMap<>();
143         ClusterActor actor;
144         for(int ndx = 0; ndx < 6; ndx++) {
145             actor = new ClusterActor();
146             actor.setNode("member-" + (ndx + 1));
147             actor.setUp(true);
148             actor.setUnreachable(false);
149
150             memberMap.put(actor.getNode(),  actor);
151         }
152
153         try {
154             Field field = provider.getClass().getDeclaredField("siteConfiguration");
155             field.setAccessible(true);
156             field.set(provider, GrToolkitProvider.SiteConfiguration.GEO);
157
158             field = provider.getClass().getDeclaredField("memberMap");
159             field.setAccessible(true);
160             field.set(provider, memberMap);
161
162
163             actor = new ClusterActor();
164             actor.setNode("member-1");
165             field = provider.getClass().getDeclaredField("self");
166             field.setAccessible(true);
167             field.set(provider, actor);
168
169             field = provider.getClass().getDeclaredField("member");
170             field.setAccessible(true);
171             field.set(provider, actor.getNode());
172         }
173         catch(IllegalAccessException | NoSuchFieldException e) {
174             fail();
175         }
176
177         ListenableFuture<RpcResult<SiteHealthOutput>> result = provider.siteHealth(null);
178         try {
179             assertEquals("200", result.get().getResult().getStatus());
180         } catch(InterruptedException | ExecutionException e) {
181             fail();
182         }
183     }
184
185     @Test
186     public void databaseHealthTest() {
187         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
188         try {
189             assertEquals("200", result.get().getResult().getStatus());
190         } catch(InterruptedException | ExecutionException e) {
191             fail();
192         }
193     }
194
195     @Test
196     public void databaseHealthWhenROTest() {
197         try {
198             when(connection.isReadOnly()).thenReturn(true);
199         } catch(SQLException e) {
200             fail();
201         }
202         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
203         try {
204             assertEquals("200", result.get().getResult().getStatus());
205         } catch(InterruptedException | ExecutionException e) {
206             fail();
207         }
208     }
209
210     @Test
211     public void databaseHealthWhenExceptionTest() {
212         try {
213             when(connection.isReadOnly()).thenThrow(new SQLException());
214         } catch(SQLException e) {
215             //expected
216         }
217         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
218         try {
219             assertEquals("200", result.get().getResult().getStatus());
220         } catch(InterruptedException | ExecutionException e) {
221             fail();
222         }
223     }
224
225     @Test
226     public void adminHealthTest() {
227         ListenableFuture<RpcResult<AdminHealthOutput>> result = provider.adminHealth(null);
228         try {
229             assertEquals("200", result.get().getResult().getStatus());
230         } catch(InterruptedException | ExecutionException e) {
231             fail();
232         }
233     }
234
235     @Test
236     public void siteIdentifierTest() {
237         ListenableFuture<RpcResult<SiteIdentifierOutput>> result = provider.siteIdentifier(null);
238         try {
239             assertEquals("200", result.get().getResult().getStatus());
240         } catch(InterruptedException | ExecutionException e) {
241             fail();
242         }
243     }
244
245     @Test
246     public void failoverTest() {
247         ListenableFuture<RpcResult<FailoverOutput>> result = provider.failover(null);
248         try {
249             assertEquals("400", result.get().getResult().getStatus());
250         } catch(InterruptedException | ExecutionException e) {
251             fail();
252         }
253     }
254
255     @Test
256     public void executeCommandTest() {
257         try {
258             Method method = provider.getClass().getDeclaredMethod("executeCommand", String.class);
259             method.setAccessible(true);
260             method.invoke(provider, "ls");
261         }
262         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
263             fail();
264         }
265     }
266
267     @Test
268     public void isolateSiteFromClusterTest() {
269         try {
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");
280         }
281         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
282             fail();
283         }
284     }
285
286     @Test
287     public void downUnreachableNodesTest() {
288         try {
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");
299         }
300         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
301             fail();
302         }
303     }
304
305     @Test
306     public void changeClusterVotingTest() {
307         try {
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");
321         }
322         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
323             fail();
324         }
325     }
326
327     @Test
328     public void backupMdSalTest() {
329         try {
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");
338         }
339         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
340             fail();
341         }
342     }
343
344 }