2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.resolver;
24 import com.github.tomakehurst.wiremock.junit.WireMockRule;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
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;
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;
48 import java.util.Properties;
49 import java.util.stream.Collectors;
50 import java.util.stream.Stream;
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;
57 import static org.junit.Assert.*;
59 import static org.mockito.Mockito.mock;
60 import static org.mockito.Mockito.when;
62 public class ThreeNodeHealthResolverTest {
63 private Map<String, ClusterActor> memberMap;
64 private DbLibService dbLibService;
65 private DBLibConnection connection;
66 private ThreeNodeHealthResolver resolver;
69 public WireMockRule wireMockRule = new WireMockRule(9999);
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) {
81 dbLibService = mock(DbLibService.class);
82 connection = mock(DBLibConnection.class);
83 resolver = new ThreeNodeHealthResolver(memberMap, properties, dbLibService);
86 private Map<String, ClusterActor> generateMemberMap(int memberCount) {
87 Map<String, ClusterActor> map = new HashMap<>();
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));
95 actor.setUnreachable(false);
97 map.put(actor.getNode(), actor);
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());
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());
121 public void getDatabaseHealth() {
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) {
130 DatabaseHealth health = resolver.getDatabaseHealth();
131 assertEquals(Health.HEALTHY, health.getHealth());
135 public void getDatabaseHealthFaulty() {
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) {
144 DatabaseHealth health = resolver.getDatabaseHealth();
145 assertEquals(Health.FAULTY, health.getHealth());
149 public void getDatabaseHealthException() {
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) {
158 DatabaseHealth health = resolver.getDatabaseHealth();
159 assertEquals(Health.FAULTY, health.getHealth());
163 public void siteIdentifier() {
164 assertEquals("TestODL", resolver.getSiteIdentifier());
165 resolver.setSiteIdentifier("NewTestODL");
166 assertEquals("NewTestODL", resolver.getSiteIdentifier());
170 public void getClusterHealth() {
172 ClusterHealth health = resolver.getClusterHealth();
173 assertEquals(Health.HEALTHY, health.getHealth());
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) {
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) {
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) {
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) {
202 if(clusterBody == null || shardManagerBody == null || shardDefaultBody == null || shardOperationalBody == null) {
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)));
212 public void getSiteHealth() {
214 stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
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) {
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());
231 public void getSiteHealthFaulty() {
233 stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
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) {
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());
251 public void getSiteHealthFaultyShard() {
253 stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
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) {
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());
271 public void getSiteHealthFaultyCluster() {
273 stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(200)));
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) {
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());
291 public void getSiteHealthFaultyAdmin() {
293 stubFor(get(urlEqualTo("/adm/healthcheck")).willReturn(aResponse().withStatus(400)));
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) {
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());
310 public void tryFailover() {
311 FailoverStatus status = resolver.tryFailover(null);
312 assertEquals(400, status.getStatusCode());