3be1595989d285fdbecc480e9a80a325680f5820
[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.github.tomakehurst.wiremock.junit.WireMockRule;
24
25 import com.google.common.util.concurrent.ListenableFuture;
26
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.contrib.java.lang.system.EnvironmentVariables;
31
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;
35
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;
53
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;
65
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;
70
71 import static org.junit.Assert.assertEquals;
72 import static org.junit.Assert.fail;
73
74 import static org.mockito.Mockito.mock;
75 import static org.mockito.Mockito.spy;
76 import static org.mockito.Mockito.when;
77
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;
88
89     @Rule
90     public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
91     @Rule
92     public WireMockRule wireMockRule = new WireMockRule(9999);
93
94     @Before
95     public void setup() {
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);
103
104         ActorContext actorContext = mock(ActorContext.class);
105         MemberName memberName = MemberName.forName("Test");
106
107         when(actorContext.getCurrentMemberName()).thenReturn(memberName);
108         when(configDatastore.getActorContext()).thenReturn(actorContext);
109
110         try {
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) {
116             fail();
117         }
118
119         provider = new GrToolkitProvider(dataBroker, notificationProviderService,
120                 rpcProviderRegistry, configDatastore, dbLibService);
121         providerSpy = spy(provider);
122         stubController();
123     }
124
125     @Test
126     public void closeTest() {
127         try {
128             provider.close();
129         } catch(Exception e) {
130             // Exception expected
131         }
132     }
133
134     @Test
135     public void onDataTreeChangedTest() {
136         provider.onDataTreeChanged(new ArrayList());
137         // onDataTreeChanged is an empty stub
138     }
139
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) {
148             fail();
149         }
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) {
153             fail();
154         }
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) {
158             fail();
159         }
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) {
163             fail();
164         }
165
166         if(clusterBody == null || shardManagerBody == null || shardDefaultBody == null || shardOperationalBody == null) {
167             fail();
168         }
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)));
174     }
175
176     @Test
177     public void clusterHealthTest() {
178         ListenableFuture<RpcResult<ClusterHealthOutput>> result = provider.clusterHealth(null);
179         try {
180             assertEquals("0", result.get().getResult().getStatus());
181         } catch(InterruptedException | ExecutionException e) {
182             fail();
183         }
184     }
185
186     @Test
187     public void siteHealthTest() {
188         ListenableFuture<RpcResult<SiteHealthOutput>> result = provider.siteHealth(null);
189         try {
190             assertEquals("200", result.get().getResult().getStatus());
191         } catch(InterruptedException | ExecutionException e) {
192             fail();
193         }
194     }
195
196     @Test
197     public void databaseHealthTest() {
198         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
199         try {
200             assertEquals("200", result.get().getResult().getStatus());
201         } catch(InterruptedException | ExecutionException e) {
202             fail();
203         }
204     }
205
206     @Test
207     public void databaseHealthWhenROTest() {
208         try {
209             when(connection.isReadOnly()).thenReturn(true);
210         } catch(SQLException e) {
211             fail();
212         }
213         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
214         try {
215             assertEquals("500", result.get().getResult().getStatus());
216         } catch(InterruptedException | ExecutionException e) {
217             fail();
218         }
219     }
220
221     @Test
222     public void databaseHealthWhenExceptionTest() {
223         try {
224             when(connection.isReadOnly()).thenThrow(new SQLException());
225         } catch(SQLException e) {
226             //expected
227         }
228         ListenableFuture<RpcResult<DatabaseHealthOutput>> result = provider.databaseHealth(null);
229         try {
230             assertEquals("500", result.get().getResult().getStatus());
231         } catch(InterruptedException | ExecutionException e) {
232             fail();
233         }
234     }
235
236     @Test
237     public void adminHealthTest() {
238         ListenableFuture<RpcResult<AdminHealthOutput>> result = provider.adminHealth(null);
239         try {
240             assertEquals("200", result.get().getResult().getStatus());
241         } catch(InterruptedException | ExecutionException e) {
242             fail();
243         }
244     }
245
246     @Test
247     public void siteIdentifierTest() {
248         ListenableFuture<RpcResult<SiteIdentifierOutput>> result = provider.siteIdentifier(null);
249         try {
250             assertEquals("200", result.get().getResult().getStatus());
251         } catch(InterruptedException | ExecutionException e) {
252             fail();
253         }
254     }
255
256     @Test
257     public void failoverTest() {
258         ListenableFuture<RpcResult<FailoverOutput>> result = provider.failover(null);
259         try {
260             assertEquals("400", result.get().getResult().getStatus());
261         } catch(InterruptedException | ExecutionException e) {
262             fail();
263         }
264     }
265
266     @Test
267     public void haltTrafficTest() {
268         HaltAkkaTrafficInputBuilder builder = new HaltAkkaTrafficInputBuilder();
269         builder.setNodeInfo(new ArrayList<>());
270         ListenableFuture<RpcResult<HaltAkkaTrafficOutput>> result = provider.haltAkkaTraffic(builder.build());
271         try {
272             assertEquals("200", result.get().getResult().getStatus());
273         } catch(InterruptedException | ExecutionException e) {
274             fail();
275         }
276     }
277
278     @Test
279     public void resumeTrafficTest() {
280         ResumeAkkaTrafficInputBuilder builder = new ResumeAkkaTrafficInputBuilder();
281         builder.setNodeInfo(new ArrayList<>());
282         ListenableFuture<RpcResult<ResumeAkkaTrafficOutput>> result = provider.resumeAkkaTraffic(builder.build());
283         try {
284             assertEquals("200", result.get().getResult().getStatus());
285         } catch(InterruptedException | ExecutionException e) {
286             fail();
287         }
288     }
289
290     @Test
291     public void executeCommandTest() {
292         try {
293             Method method = provider.getClass().getDeclaredMethod("executeCommand", String.class);
294             method.setAccessible(true);
295             method.invoke(provider, "ls");
296         } catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
297             fail();
298         }
299     }
300
301     @Test
302     public void isolateSiteFromClusterTest() {
303         try {
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) {
315             fail();
316         }
317     }
318
319     @Test
320     public void downUnreachableNodesTest() {
321         try {
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) {
333             fail();
334         }
335     }
336
337     @Test
338     public void backupMdSalTest() {
339         try {
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) {
349             fail();
350         }
351     }
352 }