[CCSDK-1985]GR Toolkit Refactor
[ccsdk/sli/plugins.git] / grToolkit / provider / src / test / java / org / onap / ccsdk / sli / plugins / grtoolkit / resolver / ThreeNodeHealthResolverTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2019 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.resolver;
23
24 import com.github.tomakehurst.wiremock.junit.WireMockRule;
25
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29
30 import org.onap.ccsdk.sli.core.dblib.DBLibConnection;
31 import org.onap.ccsdk.sli.core.dblib.DbLibService;
32 import org.onap.ccsdk.sli.plugins.grtoolkit.data.AdminHealth;
33 import org.onap.ccsdk.sli.plugins.grtoolkit.data.ClusterActor;
34 import org.onap.ccsdk.sli.plugins.grtoolkit.data.ClusterHealth;
35 import org.onap.ccsdk.sli.plugins.grtoolkit.data.DatabaseHealth;
36 import org.onap.ccsdk.sli.plugins.grtoolkit.data.FailoverStatus;
37 import org.onap.ccsdk.sli.plugins.grtoolkit.data.Health;
38 import org.onap.ccsdk.sli.plugins.grtoolkit.data.SiteHealth;
39
40 import java.io.FileInputStream;
41 import java.io.IOException;
42 import java.nio.file.Files;
43 import java.nio.file.Paths;
44 import java.sql.SQLException;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Properties;
49 import java.util.stream.Collectors;
50 import java.util.stream.Stream;
51
52 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
53 import static com.github.tomakehurst.wiremock.client.WireMock.get;
54 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
55 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
56
57 import static org.junit.Assert.*;
58
59 import static org.mockito.Mockito.mock;
60 import static org.mockito.Mockito.when;
61
62 public class ThreeNodeHealthResolverTest {
63     private Map<String, ClusterActor> memberMap;
64     private DbLibService dbLibService;
65     private DBLibConnection connection;
66     private ThreeNodeHealthResolver resolver;
67
68     @Rule
69     public WireMockRule wireMockRule = new WireMockRule(9999);
70
71     @Before
72     public void setUp() {
73         memberMap = generateMemberMap(3);
74         Properties properties = new Properties();
75         try(FileInputStream fileInputStream = new FileInputStream("src/test/resources/three/gr-toolkit.properties")) {
76             properties.load(fileInputStream);
77         } catch(IOException e) {
78             fail();
79         }
80
81         dbLibService = mock(DbLibService.class);
82         connection = mock(DBLibConnection.class);
83         resolver = new ThreeNodeHealthResolver(memberMap, properties, dbLibService);
84     }
85
86     private Map<String, ClusterActor> generateMemberMap(int memberCount) {
87         Map<String, ClusterActor> map = new HashMap<>();
88         ClusterActor actor;
89         for(int ndx = 0; ndx < memberCount; ndx++) {
90             actor = new ClusterActor();
91             actor.setNode("127.0.1." + (ndx + 1));
92             actor.setAkkaPort("2550");
93             actor.setMember("member-" + (ndx + 1));
94             actor.setUp(true);
95             actor.setUnreachable(false);
96
97             map.put(actor.getNode(),  actor);
98         }
99         return map;
100     }
101
102     @Test
103     public void getAdminHealthFaulty() {
104         stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(500)));
105         AdminHealth health = resolver.getAdminHealth();
106         assertNotNull(health);
107         assertEquals(500, health.getStatusCode());
108         assertEquals(Health.FAULTY, health.getHealth());
109     }
110
111     @Test
112     public void getAdminHealthHealthy() {
113         stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
114         AdminHealth health = resolver.getAdminHealth();
115         assertNotNull(health);
116         assertEquals(200, health.getStatusCode());
117         assertEquals(Health.HEALTHY, health.getHealth());
118     }
119
120     @Test
121     public void getDatabaseHealth() {
122         try {
123             when(connection.isReadOnly()).thenReturn(false);
124             when(connection.isClosed()).thenReturn(false);
125             when(dbLibService.isActive()).thenReturn(true);
126             when(dbLibService.getConnection()).thenReturn(connection);
127         } catch(SQLException e) {
128             fail();
129         }
130         DatabaseHealth health = resolver.getDatabaseHealth();
131         assertEquals(Health.HEALTHY, health.getHealth());
132     }
133
134     @Test
135     public void getDatabaseHealthFaulty() {
136         try {
137             when(connection.isReadOnly()).thenReturn(true);
138             when(connection.isClosed()).thenReturn(true);
139             when(dbLibService.isActive()).thenReturn(false);
140             when(dbLibService.getConnection()).thenReturn(connection);
141         } catch(SQLException e) {
142             fail();
143         }
144         DatabaseHealth health = resolver.getDatabaseHealth();
145         assertEquals(Health.FAULTY, health.getHealth());
146     }
147
148     @Test
149     public void getDatabaseHealthException() {
150         try {
151             when(connection.isReadOnly()).thenThrow(new SQLException());
152             when(connection.isClosed()).thenReturn(true);
153             when(dbLibService.isActive()).thenReturn(false);
154             when(dbLibService.getConnection()).thenReturn(connection);
155         } catch(SQLException e) {
156             fail();
157         }
158         DatabaseHealth health = resolver.getDatabaseHealth();
159         assertEquals(Health.FAULTY, health.getHealth());
160     }
161
162     @Test
163     public void siteIdentifier() {
164         assertEquals("TestODL", resolver.getSiteIdentifier());
165         resolver.setSiteIdentifier("NewTestODL");
166         assertEquals("NewTestODL", resolver.getSiteIdentifier());
167     }
168
169     @Test
170     public void getClusterHealth() {
171         stubController();
172         ClusterHealth health = resolver.getClusterHealth();
173         assertEquals(Health.HEALTHY, health.getHealth());
174     }
175
176     private void stubController() {
177         String clusterBody = null;
178         String shardManagerBody = null;
179         String shardDefaultBody = null;
180         String shardOperationalBody = null;
181         try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/cluster.json"))) {
182             clusterBody = stream.collect(Collectors.joining());
183         } catch(IOException e) {
184             fail();
185         }
186         try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/shard-manager.json"))) {
187             shardManagerBody = stream.collect(Collectors.joining());
188         } catch(IOException e) {
189             fail();
190         }
191         try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/default-config.json"))) {
192             shardDefaultBody = stream.collect(Collectors.joining());
193         } catch(IOException e) {
194             fail();
195         }
196         try(Stream<String> stream = Files.lines(Paths.get("src/test/resources/three/default-operational.json"))) {
197             shardOperationalBody = stream.collect(Collectors.joining());
198         } catch(IOException e) {
199             fail();
200         }
201
202         if(clusterBody == null || shardManagerBody == null || shardDefaultBody == null || shardOperationalBody == null) {
203             fail();
204         }
205         stubFor(get(urlEqualTo("/jolokia/read/akka:type=Cluster")).willReturn(aResponse().withStatus(200).withBody(clusterBody)));
206         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"));
207         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"));
208         stubFor(get(urlEqualTo("/jolokia/read/org.opendaylight.controller:Category=Shards,name=member-1-shard-default-operational,type=DistributedOperationalDatastore")).willReturn(aResponse().withStatus(200).withBody(shardOperationalBody)));
209     }
210
211     @Test
212     public void getSiteHealth() {
213         stubController();
214         stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
215         try {
216             when(connection.isReadOnly()).thenReturn(false);
217             when(connection.isClosed()).thenReturn(false);
218             when(dbLibService.isActive()).thenReturn(true);
219             when(dbLibService.getConnection()).thenReturn(connection);
220         } catch(SQLException e) {
221             fail();
222         }
223         List<SiteHealth> health = resolver.getSiteHealth();
224         assertNotNull(health);
225         assertNotEquals(0, health.size());
226         assertEquals(1, health.size());
227         assertEquals(Health.HEALTHY, health.get(0).getHealth());
228     }
229
230     @Test
231     public void getSiteHealthFaulty() {
232         stubController();
233         stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
234         try {
235             when(connection.isReadOnly()).thenReturn(false);
236             when(connection.isClosed()).thenReturn(false);
237             when(dbLibService.isActive()).thenReturn(true);
238             when(dbLibService.getConnection()).thenReturn(connection);
239         } catch(SQLException e) {
240             fail();
241         }
242         stubFor(get(urlEqualTo("/jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-config,type=DistributedConfigDatastore")).inScenario("testing").whenScenarioStateIs("next").willReturn(aResponse().withBodyFile("nonexistent")));
243         List<SiteHealth> health = resolver.getSiteHealth();
244         assertNotNull(health);
245         assertNotEquals(0, health.size());
246         assertEquals(1, health.size());
247         assertEquals(Health.FAULTY, health.get(0).getHealth());
248     }
249
250     @Test
251     public void getSiteHealthFaultyShard() {
252         stubController();
253         stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
254         try {
255             when(connection.isReadOnly()).thenReturn(false);
256             when(connection.isClosed()).thenReturn(false);
257             when(dbLibService.isActive()).thenReturn(true);
258             when(dbLibService.getConnection()).thenReturn(connection);
259         } catch(SQLException e) {
260             fail();
261         }
262         stubFor(get(urlEqualTo("/jolokia/read/org.opendaylight.controller:Category=Shards,name=member-1-shard-default-config,type=DistributedConfigDatastore")).inScenario("testing").willReturn(aResponse().withBodyFile("nonexistent")).willSetStateTo("next"));
263         List<SiteHealth> health = resolver.getSiteHealth();
264         assertNotNull(health);
265         assertNotEquals(0, health.size());
266         assertEquals(1, health.size());
267         assertEquals(Health.HEALTHY, health.get(0).getHealth());
268     }
269
270     @Test
271     public void getSiteHealthFaultyCluster() {
272         stubController();
273         stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
274         try {
275             when(connection.isReadOnly()).thenReturn(false);
276             when(connection.isClosed()).thenReturn(false);
277             when(dbLibService.isActive()).thenReturn(true);
278             when(dbLibService.getConnection()).thenReturn(connection);
279         } catch(SQLException e) {
280             fail();
281         }
282         stubFor(get(urlEqualTo("/jolokia/read/akka:type=Cluster")).willReturn(aResponse().withStatus(200).withBodyFile("nonexistent")));
283         List<SiteHealth> health = resolver.getSiteHealth();
284         assertNotNull(health);
285         assertNotEquals(0, health.size());
286         assertEquals(1, health.size());
287         assertEquals(Health.FAULTY, health.get(0).getHealth());
288     }
289
290     @Test
291     public void getSiteHealthFaultyAdmin() {
292         stubController();
293         stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(400)));
294         try {
295             when(connection.isReadOnly()).thenReturn(false);
296             when(connection.isClosed()).thenReturn(false);
297             when(dbLibService.isActive()).thenReturn(true);
298             when(dbLibService.getConnection()).thenReturn(connection);
299         } catch(SQLException e) {
300             fail();
301         }
302         List<SiteHealth> health = resolver.getSiteHealth();
303         assertNotNull(health);
304         assertNotEquals(0, health.size());
305         assertEquals(1, health.size());
306         assertEquals(Health.FAULTY, health.get(0).getHealth());
307     }
308
309     @Test
310     public void tryFailover() {
311         FailoverStatus status = resolver.tryFailover(null);
312         assertEquals(400, status.getStatusCode());
313     }
314 }