Skip to content

Commit 8fdfab1

Browse files
committed
introduce groovy updates for microprofile re-structure plus mphealth testcase
1 parent 3f0cf6f commit 8fdfab1

27 files changed

+1240
-5
lines changed

boost-gradle/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ dependencies {
3333
testCompile("javax.ws.rs:javax.ws.rs-api:2.1")
3434
testCompile("com.github.docker-java:docker-java:3.1.0-rc-4")
3535
testCompile "commons-httpclient:commons-httpclient:3.1"
36+
testCompile group: 'org.glassfish', name: 'javax.json', version: '1.0.4'
37+
testCompile group: 'org.apache.cxf', name: 'cxf-rt-rs-client', version: '3.1.11'
38+
testCompile group: 'org.apache.cxf', name: 'cxf-rt-rs-extension-providers', version: '3.1.11'
39+
testCompile group: 'junit', name: 'junit', version: '4.+'
3640
}
3741

3842
test {

boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostPackageTask.groovy

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import io.openliberty.boost.gradle.utils.GradleProjectUtil
3636
import io.openliberty.boost.common.utils.BoostUtil
3737
import io.openliberty.boost.common.utils.SpringBootUtil
3838
import io.openliberty.boost.common.utils.LibertyBoosterUtil
39+
import io.openliberty.boost.common.config.BoosterDependencyInfo
3940

4041
import net.wasdev.wlp.gradle.plugins.extensions.PackageAndDumpExtension
4142

@@ -146,7 +147,11 @@ public class BoostPackageTask extends AbstractBoostTask {
146147

147148
} else if (project.plugins.hasPlugin('war')) {
148149
// Get booster dependencies from project
149-
Map<String, String> boosterDependencies = GradleProjectUtil.getBoosterDependencies(project)
150+
// Get booster dependencies from project
151+
List<BoosterDependencyInfo> boosterDependencies = GradleProjectUtil.getBoosterDependencies(project,
152+
BoostLogger.getInstance());
153+
154+
//Map<String, String> boosterDependencies = GradleProjectUtil.getBoosterDependencies(project)
150155
boosterUtil = new LibertyBoosterUtil(libertyServerPath, boosterDependencies, BoostLogger.getInstance())
151156

152157
copyBoosterDependencies()

boost-gradle/src/main/groovy/io/openliberty/boost/gradle/utils/GradleProjectUtil.groovy

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import org.gradle.api.artifacts.ModuleVersionIdentifier
1818

1919
import groovy.lang.MissingPropertyException
2020

21+
import io.openliberty.boost.common.config.BoosterDependencyInfo
22+
2123
public class GradleProjectUtil {
2224

2325
/**
@@ -56,14 +58,16 @@ public class GradleProjectUtil {
5658
return springBootDependencies
5759
}
5860

59-
public static Map<String, String> getBoosterDependencies(Project project) {
60-
Map<String, String> boosterDependencies = new HashMap<String, String>()
61+
public static List<BoosterDependencyInfo> getBoosterDependencies(Project project) {
62+
List<BoosterDependencyInfo> boosterDependencies = new ArrayList<BoosterDependencyInfo>()
63+
//Map<String, String> boosterDependencies = new HashMap<String, String>()
6164

6265
project.configurations.compile.resolvedConfiguration.resolvedArtifacts.collect { it.moduleVersion.id }.each { ModuleVersionIdentifier id ->
6366

6467
if ( id.group.equals("io.openliberty.boosters") ) {
65-
66-
boosterDependencies.put(id.name.toString(), id.version.toString())
68+
BoosterDependencyInfo currBooster = new BoosterDependencyInfo(id.group.toString, id.name.toString, id.version.toString);
69+
boosterDependencies.add(currBooster);
70+
//boosterDependencies.put(id.name.toString(), id.version.toString())
6771
}
6872
}
6973

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* IBM Corporation - initial API and implementation
10+
*******************************************************************************/
11+
import org.gradle.testkit.runner.BuildResult
12+
import org.gradle.testkit.runner.GradleRunner
13+
import static org.gradle.testkit.runner.TaskOutcome.*
14+
15+
import static org.junit.Assert.assertEquals
16+
import org.junit.After
17+
import org.junit.Before
18+
import org.junit.Test
19+
import org.junit.BeforeClass
20+
import org.junit.AfterClass
21+
22+
import java.io.BufferedReader
23+
import java.io.File
24+
import java.io.FileReader
25+
import java.io.FileWriter
26+
27+
import javax.ws.rs.client.Client
28+
import javax.ws.rs.client.ClientBuilder
29+
import javax.ws.rs.core.Response
30+
import javax.json.JsonArray
31+
import javax.json.JsonObject
32+
33+
import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider
34+
35+
public class BoostPackageMPHealth10Test extends AbstractBoostTest {
36+
37+
static BuildResult result
38+
39+
static File resourceDir = new File("build/resources/test/test-mpHealth-1.0")
40+
static File testProjectDir = new File(integTestDir, "BoostPackageMPHealth10Test")
41+
static String buildFilename = "testMPHealth10.gradle"
42+
43+
private JsonArray servicesStates
44+
private static HashMap<String, String> dataWhenServicesUP
45+
private static HashMap<String, String> dataWhenInventoryDown
46+
47+
static {
48+
dataWhenServicesUP = new HashMap<String, String>()
49+
dataWhenInventoryDown = new HashMap<String, String>()
50+
51+
dataWhenServicesUP.put("SystemResource", "UP")
52+
dataWhenServicesUP.put("InventoryResource", "UP")
53+
54+
dataWhenInventoryDown.put("SystemResource", "UP")
55+
dataWhenInventoryDown.put("InventoryResource", "DOWN")
56+
}
57+
58+
@BeforeClass
59+
public static void setup() {
60+
createDir(testProjectDir)
61+
createTestProject(testProjectDir, resourceDir, buildFilename)
62+
63+
result = GradleRunner.create()
64+
.withProjectDir(testProjectDir)
65+
.forwardOutput()
66+
.withArguments("boostPackage", "boostStart", "-i", "-s")
67+
.build()
68+
}
69+
70+
@AfterClass
71+
public static void teardown() {
72+
73+
HealthTestUtil.cleanUp()
74+
75+
result = GradleRunner.create()
76+
.withProjectDir(testProjectDir)
77+
.forwardOutput()
78+
.withArguments("boostStop", "-i", "-s")
79+
.build()
80+
81+
assertEquals(SUCCESS, result.task(":boostStop").getOutcome())
82+
83+
}
84+
85+
@Test
86+
public void testIfServicesAreUp() {
87+
servicesStates = HealthTestUtil.connectToHealthEnpoint(200)
88+
checkStates(dataWhenServicesUP, servicesStates)
89+
}
90+
91+
@Test
92+
public void testIfInventoryServiceIsDown() {
93+
servicesStates = HealthTestUtil.connectToHealthEnpoint(200)
94+
checkStates(dataWhenServicesUP, servicesStates)
95+
HealthTestUtil.changeInventoryProperty(HealthTestUtil.INV_MAINTENANCE_FALSE,
96+
HealthTestUtil.INV_MAINTENANCE_TRUE)
97+
servicesStates = HealthTestUtil.connectToHealthEnpoint(503)
98+
checkStates(dataWhenInventoryDown, servicesStates)
99+
}
100+
101+
private void checkStates(HashMap<String, String> testData, JsonArray servStates) {
102+
testData.forEach { service, expectedState ->
103+
assertEquals("The state of " + service + " service is not matching.",
104+
expectedState,
105+
HealthTestUtil.getActualState(service, servStates));
106+
}
107+
}
108+
109+
110+
private class HealthTestUtil {
111+
112+
private static String port
113+
private static String baseUrl
114+
private final static String HEALTH_ENDPOINT = "health"
115+
public static final String INV_MAINTENANCE_FALSE = "io_openliberty_guides_inventory_inMaintenance\":false"
116+
public static final String INV_MAINTENANCE_TRUE = "io_openliberty_guides_inventory_inMaintenance\":true"
117+
118+
static {
119+
//port = System.getProperty("liberty.test.port");
120+
port = "9080"
121+
baseUrl = "http://localhost:" + port + "/"
122+
}
123+
124+
public static JsonArray connectToHealthEnpoint(int expectedResponseCode) {
125+
String healthURL = baseUrl + HEALTH_ENDPOINT
126+
Client client = ClientBuilder.newClient().register(JsrJsonpProvider.class)
127+
Response response = client.target(healthURL).request().get()
128+
assertEquals("Response code is not matching " + healthURL,
129+
expectedResponseCode, response.getStatus())
130+
JsonArray servicesStates = response.readEntity(JsonObject.class)
131+
.getJsonArray("checks")
132+
response.close()
133+
client.close()
134+
return servicesStates
135+
}
136+
137+
public static String getActualState(String service,
138+
JsonArray servicesStates) {
139+
String state = ""
140+
for (Object obj : servicesStates) {
141+
if (obj instanceof JsonObject) {
142+
if (service.equals(((JsonObject) obj).getString("name"))) {
143+
state = ((JsonObject) obj).getString("state")
144+
}
145+
}
146+
}
147+
return state
148+
}
149+
150+
public static void changeInventoryProperty(String oldValue, String newValue) {
151+
try {
152+
String fileName = System.getProperty("user.dir").split("target")[0]
153+
+ "target/it/test-mpHealth-1.0/src/main/resources/CustomConfigSource.json"
154+
BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)))
155+
String line = ""
156+
String oldContent = "", newContent = ""
157+
while ((line = reader.readLine()) != null) {
158+
oldContent += line + "\r\n"
159+
}
160+
reader.close()
161+
newContent = oldContent.replaceAll(oldValue, newValue)
162+
FileWriter writer = new FileWriter(fileName)
163+
writer.write(newContent)
164+
writer.close()
165+
Thread.sleep(600)
166+
} catch (Exception e) {
167+
e.printStackTrace()
168+
}
169+
}
170+
171+
public static void cleanUp() {
172+
changeInventoryProperty(INV_MAINTENANCE_TRUE, INV_MAINTENANCE_FALSE)
173+
}
174+
175+
}
176+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
mavenLocal()
5+
maven {
6+
url 'https://oss.sonatype.org/content/repositories/snapshots/'
7+
}
8+
maven { url 'https://repo.spring.io/plugins-snapshot' }
9+
}
10+
dependencies {
11+
classpath("io.openliberty.boost:boost-gradle-plugin:$boostVersion")
12+
classpath 'io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE'
13+
}
14+
}
15+
16+
apply plugin: 'war'
17+
apply plugin: 'boost'
18+
apply plugin: 'io.spring.dependency-management'
19+
20+
war {
21+
baseName = 'testWar'
22+
}
23+
24+
repositories {
25+
mavenCentral()
26+
mavenLocal()
27+
}
28+
29+
sourceCompatibility = 1.8
30+
targetCompatibility = 1.8
31+
32+
dependencyManagement {
33+
imports {
34+
mavenBom "io.openliberty.boosters:mp20-bom:$boosterVersion"
35+
}
36+
}
37+
38+
dependencies {
39+
compile "io.openliberty.boosters:jaxrs"
40+
compile "io.openliberty.boosters:cdi"
41+
compile "io.openliberty.boosters:mpConfig"
42+
compile "io.openliberty.boosters:jsonp"
43+
compile "io.openliberty.boosters:mpRestClient"
44+
compile "io.openliberty.boosters:mpHealth"
45+
compile("org.apache.httpcomponents:httpclient:4.5.6")
46+
compile("org.apache.httpcomponents:httpcore:4.4.10")
47+
compile("org.apache.commons:commons-lang3:3.0")
48+
compile "javax.servlet:javax.servlet-api:3.1.0"
49+
50+
libertyRuntime "$runtimeGroup:$runtimeArtifactId:$runtimeVersion"
51+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// tag::copyright[]
2+
/*******************************************************************************
3+
* Copyright (c) 2017 IBM Corporation and others.
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/legal/epl-v10.html
8+
*
9+
* Contributors:
10+
* IBM Corporation - Initial implementation
11+
*******************************************************************************/
12+
// end::copyright[]
13+
14+
// tag::customConfig[]
15+
package io.openliberty.guides.config;
16+
17+
import javax.json.stream.JsonParser;
18+
import javax.json.stream.JsonParser.Event;
19+
import javax.json.Json;
20+
import java.math.BigDecimal;
21+
import java.util.*;
22+
import java.io.StringReader;
23+
import java.io.BufferedReader;
24+
import java.io.FileReader;
25+
import org.eclipse.microprofile.config.spi.ConfigSource;
26+
27+
public class CustomConfigSource implements ConfigSource {
28+
29+
String fileLocation = System.getProperty("user.dir").split("target")[0]
30+
+ "target/it/test-mpHealth-1.0/src/main/resources/CustomConfigSource.json";
31+
32+
@Override
33+
public int getOrdinal() {
34+
return Integer.parseInt(getProperties().get("config_ordinal"));
35+
}
36+
37+
@Override
38+
public Set<String> getPropertyNames() {
39+
return getProperties().keySet();
40+
}
41+
42+
@Override
43+
public String getValue(String key) {
44+
return getProperties().get(key);
45+
}
46+
47+
@Override
48+
public String getName() {
49+
return "Custom Config Source: file:" + this.fileLocation;
50+
}
51+
52+
public Map<String, String> getProperties() {
53+
Map<String, String> m = new HashMap<String, String>();
54+
String jsonData = this.readFile(this.fileLocation);
55+
JsonParser parser = Json.createParser(new StringReader(jsonData));
56+
String key = null;
57+
while (parser.hasNext()) {
58+
final Event event = parser.next();
59+
switch (event) {
60+
case KEY_NAME:
61+
key = parser.getString();
62+
break;
63+
case VALUE_STRING:
64+
String string = parser.getString();
65+
m.put(key, string);
66+
break;
67+
case VALUE_NUMBER:
68+
BigDecimal number = parser.getBigDecimal();
69+
m.put(key, number.toString());
70+
break;
71+
case VALUE_TRUE:
72+
m.put(key, "true");
73+
break;
74+
case VALUE_FALSE:
75+
m.put(key, "false");
76+
break;
77+
default:
78+
break;
79+
}
80+
}
81+
parser.close();
82+
return m;
83+
}
84+
85+
public String readFile(String fileName) {
86+
String result = "";
87+
try {
88+
BufferedReader br = new BufferedReader(new FileReader(fileName));
89+
StringBuilder sb = new StringBuilder();
90+
String line = br.readLine();
91+
while (line != null) {
92+
sb.append(line);
93+
line = br.readLine();
94+
}
95+
result = sb.toString();
96+
br.close();
97+
} catch (Exception e) {
98+
e.printStackTrace();
99+
}
100+
return result;
101+
}
102+
}
103+
// end::customConfig[]

0 commit comments

Comments
 (0)