forked from OpenLiberty/sample-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HealthUtilIT.java
89 lines (77 loc) · 3.13 KB
/
HealthUtilIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************
* Copyright (c) 2018, 2020 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - Initial implementation
*******************************************************************************/
package it.io.openliberty.sample.health;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.Response;
public class HealthUtilIT {
private static String port;
private static String contextRoot;
private static String baseUrl;
private final static String HEALTH_ENDPOINT = "health";
public static final String INV_MAINTENANCE_FALSE = "io_openliberty_sample_system_inMaintenance\":false";
public static final String INV_MAINTENANCE_TRUE = "io_openliberty_sample_system_inMaintenance\":true";
static {
port = System.getProperty("http.port");
contextRoot = System.getProperty("app.context.root");
baseUrl = "http://localhost:" + port + contextRoot;
}
public static JsonArray connectToHealthEnpoint(int expectedResponseCode) {
String healthURL = baseUrl + HEALTH_ENDPOINT;
Client client = ClientBuilder.newClient();
Response response = client.target(healthURL).request().get();
assertEquals(expectedResponseCode, response.getStatus(), "Response code is not matching " + healthURL);
JsonArray servicesstatus = response.readEntity(JsonObject.class).getJsonArray("checks");
response.close();
client.close();
return servicesstatus;
}
public static String getActualState(String service, JsonArray servicesstatus) {
String state = "";
for (Object obj : servicesstatus) {
if (obj instanceof JsonObject) {
if (service.equals(((JsonObject) obj).getString("name"))) {
state = ((JsonObject) obj).getString("status");
}
}
}
return state;
}
public static void changeProperty(String oldValue, String newValue) {
try {
String fileName = System.getProperty("user.dir").split("target")[0] + "/resources/CustomConfigSource.json";
BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
String line = "";
String oldContent = "", newContent = "";
while ((line = reader.readLine()) != null) {
oldContent += line + "\r\n";
}
reader.close();
newContent = oldContent.replaceAll(oldValue, newValue);
FileWriter writer = new FileWriter(fileName);
writer.write(newContent);
writer.close();
Thread.sleep(600);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void cleanUp() {
changeProperty(INV_MAINTENANCE_TRUE, INV_MAINTENANCE_FALSE);
}
}