Skip to content

Commit

Permalink
Merge pull request #10023 from murdos/feat/custom-jhlite-test-scripts
Browse files Browse the repository at this point in the history
feat(custom-jhlite): add shell scripts for helping testing generated apps in ci
  • Loading branch information
murdos authored Jun 10, 2024
2 parents 866aaf4 + 0de3d6a commit bbd5f10
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import tech.jhipster.lite.module.domain.JHipsterProjectFilePath;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.*;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency.JavaDependencyOptionalValueBuilder;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyType;
import tech.jhipster.lite.module.domain.javaproperties.PropertyKey;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;
Expand Down Expand Up @@ -68,6 +66,12 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
.add(SOURCE.template("CucumberConfiguration.java"), cucumberDestination.append("CucumberConfiguration.java"))
.add(CUCUMBER_SOURCE.append("rest").template("CucumberRestTemplate.java"), cucumberDestination.append("rest").append("CucumberRestTemplate.java"))
.add(CUCUMBER_SOURCE.file("gitkeep"), to("src/test/features/.gitkeep"))
.batch(SOURCE.append("tests-ci"),to("tests-ci"))
.addExecutableTemplate("generate.sh")
.addTemplate("modulePayload.json")
.addExecutableTemplate("start.sh")
.addExecutable("stop.sh")
.and()
.and()
.build();
//@formatter:on
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package tech.jhipster.lite.module.domain.file;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;
import tech.jhipster.lite.module.domain.JHipsterModule.JHipsterModuleBuilder;
import tech.jhipster.lite.module.domain.JHipsterModuleUpgrade;
import tech.jhipster.lite.module.domain.JHipsterProjectFilePath;
Expand Down Expand Up @@ -125,6 +123,18 @@ public JHipsterModuleFileBatchBuilder addFile(String file) {
return add(source.file(file), destination.append(file));
}

public JHipsterModuleFileBatchBuilder addExecutable(String file) {
files.addExecutable(source.file(file), destination.append(file));

return this;
}

public JHipsterModuleFileBatchBuilder addExecutableTemplate(String file) {
files.addExecutable(source.template(file), destination.append(file));

return this;
}

private JHipsterModuleFileBatchBuilder add(JHipsterSource source, JHipsterDestination destination) {
files.add(source, destination);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

show_syntax() {
echo "Usage: $0 <application> <java-build-tool> <spring-configuration-format>" >&2
exit 1
}

if [ "$#" -ne 3 ]; then
show_syntax
fi

if test -f "modulePayload.json"; then
payloadFile="modulePayload.json"
elif test -f tests-ci/modulePayload.json; then
payloadFile=tests-ci/modulePayload.json
fi

application=$1
java_build_tool=$2
configuration_format=$3

applyModules() {
for module in $@
do
local payload="$(sed -e "s/APP_NAME/$application/g;s/SPRING_CONFIG_FORMAT/$configuration_format/g" $payloadFile)"
local api="/api/modules/$module/apply-patch"

echo "curl -o /dev/null -s -w "%{http_code}\n" \
-X POST \
-H "accept: */*" \
-H "Content-Type: application/json" \
-d "$payload" \
"http://localhost:{{serverPort}}""$api""

local status_code=$(curl -o /dev/null -s -w "%{http_code}\n" \
-X POST \
-H "accept: */*" \
-H "Content-Type: application/json" \
-d "$payload" \
"http://localhost:{{serverPort}}""$api")

if [[ $status_code == '40'* || $status_code == '50'* ]]; then
echo "Error when calling API:" "$status_code" "$api"
exit 1
fi;
done
}

init_server() {
applyModules \
"init" \
"${java_build_tool}-wrapper" \
"${java_build_tool}-java"
}

if [[ $application == 'fullapp' ]]; then
init_server
# apply here your custom modules

else
echo "*** Unknown configuration..."
exit 1
fi

echo ""
cat "$payloadFile"
echo ""
sleep 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"projectFolder": "/tmp/jhlite/APP_NAME",
"commit": true,
"parameters": {
"projectName": "Chips Project",
"serverPort": 8081,
"baseName": "APP_NAME",
"packageName": "{{packageName}}.APP_NAME",
"jwtBase64Secret": "NmE2NDhmMmQ5OWQzYzk4NmE4NjNlMmQzNmU3ZTU4ZDY2MmUxOWEwZGI4YTk4NDY0MTk4MmQyMWZlNDNlM2FjYg==",
"date": "2021-12-03T10:15:30.00Z",
"kafkaClusterId": "7870e9ec-dd1f-4562-86a2-df2199d5db1a",
"springConfigurationFormat": "SPRING_CONFIG_FORMAT"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

PORT=$1
if [[ $PORT == '' ]]; then
echo "*** Using default port {{serverPort}}"
PORT='{{serverPort}}'
fi

echo "*** Waiting 5sec to be sure the Jar is here"
sleep 5

echo "*** List folder"
ls -al

if test -f "mvnw"; then
JAR_DIRECTORY="target"
elif test -f "gradlew"; then
JAR_DIRECTORY="build/libs/"
fi

echo "*** Identifying application executable..."
export EXEC_JAR=$(\
find ${JAR_DIRECTORY} -maxdepth 1 -name "*-exec.jar" | grep . \
|| find ${JAR_DIRECTORY} -maxdepth 1 -name "*.jar" | grep -v "\-javadoc" | grep -v "\-sources" | grep -v "\-tests" | grep -v "\-plain" \
)

echo "*** Starting application using ${EXEC_JAR}..."
java \
-jar ${EXEC_JAR} \
--logging.level.ROOT=OFF & > /dev/null
echo $! > .pid-jhlite

retryCount=1
maxRetry=30
httpUrl="http://localhost:"$PORT"/management/health"

rep=$(curl -v "$httpUrl")
status=$?
while [ "$status" -ne 0 ] && [ "$retryCount" -le "$maxRetry" ]; do
echo "*** [$(date)] Application not reachable yet. Sleep and retry - retryCount =" $retryCount "/" $maxRetry
retryCount=$((retryCount+1))
sleep 5
rep=$(curl -v "$httpUrl")
status=$?
done

if [ "$status" -ne 0 ]; then
echo "*** [$(date)] Not connected after" $retryCount " retries."
exit 1
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

echo "*** Stopping JHipster Lite in 5sec..."
sleep 5
kill $(cat .pid-jhlite)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ void shouldBuildModule() {

//@formatter:off
assertThatModuleWithFiles(module, pomFile(), mainAppFile())
.hasExecutableFiles("tests-ci/generate.sh", "tests-ci/start.sh", "tests-ci/stop.sh")
.hasFile("tests-ci/generate.sh")
.containing("http://localhost:9000")
.and()
.hasFile("tests-ci/start.sh")
.containing("9000")
.and()
.hasFile("tests-ci/modulePayload.json")
.containing("""
"packageName": "com.jhipster.test.APP_NAME",
""")
.and()
.hasFile("pom.xml")
.containing("<artifactId>cucumber-junit-platform-engine</artifactId>")
.containing("<artifactId>cucumber-java</artifactId>")
Expand Down

0 comments on commit bbd5f10

Please sign in to comment.