diff --git a/.gitignore b/.gitignore index d0526e77..b7554479 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ target/ !.mvn/wrapper/maven-wrapper.jar audit.log .cache/ +*.log docs/ gh-pages/ diff --git a/customer/java/quarkus/.dockerignore b/customer/java/quarkus/.dockerignore new file mode 100644 index 00000000..b86c7ac3 --- /dev/null +++ b/customer/java/quarkus/.dockerignore @@ -0,0 +1,4 @@ +* +!target/*-runner +!target/*-runner.jar +!target/lib/* \ No newline at end of file diff --git a/customer/java/quarkus/Dockerfile b/customer/java/quarkus/Dockerfile new file mode 100644 index 00000000..7b4a57a0 --- /dev/null +++ b/customer/java/quarkus/Dockerfile @@ -0,0 +1,11 @@ +FROM fabric8/java-jboss-openjdk8-jdk +ENV JAVA_OPTIONS=-Dquarkus.http.host=0.0.0.0 +ENV JAEGER_SERVICE_NAME=customer\ + JAEGER_ENDPOINT=http://jaeger-collector.istio-system.svc:14268/api/traces\ + JAEGER_PROPAGATION=b3\ + JAEGER_SAMPLER_TYPE=const\ + JAEGER_SAMPLER_PARAM=1 +EXPOSE 8080 8778 9779 +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/app.jar +ENTRYPOINT [ "/deployments/run-java.sh" ] \ No newline at end of file diff --git a/customer/java/quarkus/pom.xml b/customer/java/quarkus/pom.xml new file mode 100644 index 00000000..218c0d2e --- /dev/null +++ b/customer/java/quarkus/pom.xml @@ -0,0 +1,128 @@ + + + 4.0.0 + com.redhat.developer.demos.customer.rest + customer + 1.0-SNAPSHOT + + 2.22.0 + 1.8 + 1.8 + 0.12.0 + UTF-8 + + + + + io.quarkus + quarkus-bom + ${quarkus.version} + pom + import + + + + + + io.quarkus + quarkus-resteasy + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + io.quarkus + quarkus-smallrye-rest-client + + + io.quarkus + quarkus-smallrye-opentracing + + + io.quarkus + quarkus-smallrye-health + + + + + + io.quarkus + quarkus-maven-plugin + ${quarkus.version} + + + + build + + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + + + + + + + + native + + + native + + + + + + io.quarkus + quarkus-maven-plugin + ${quarkus.version} + + + + native-image + + + true + + + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + + ${project.build.directory}/${project.build.finalName}-runner + + + + + + + + + + + diff --git a/customer/java/quarkus/src/main/docker/Dockerfile.jvm b/customer/java/quarkus/src/main/docker/Dockerfile.jvm new file mode 100644 index 00000000..57a5b1e8 --- /dev/null +++ b/customer/java/quarkus/src/main/docker/Dockerfile.jvm @@ -0,0 +1,21 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the docker image run: +# +# mvn package +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/customer-jvm . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/customer-jvm +# +### +FROM fabric8/java-jboss-openjdk8-jdk +ENV JAVA_OPTIONS=-Dquarkus.http.host=0.0.0.0 +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/app.jar +ENTRYPOINT [ "/deployments/run-java.sh" ] \ No newline at end of file diff --git a/customer/java/quarkus/src/main/docker/Dockerfile.native b/customer/java/quarkus/src/main/docker/Dockerfile.native new file mode 100644 index 00000000..804c3356 --- /dev/null +++ b/customer/java/quarkus/src/main/docker/Dockerfile.native @@ -0,0 +1,22 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode +# +# Before building the docker image run: +# +# mvn package -Pnative -Dnative-image.docker-build=true +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native -t quarkus/customer . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/customer +# +### +FROM registry.fedoraproject.org/fedora-minimal +WORKDIR /work/ +COPY target/*-runner /work/application +RUN chmod 775 /work +EXPOSE 8080 +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] \ No newline at end of file diff --git a/customer/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/CustomerResource.java b/customer/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/CustomerResource.java new file mode 100644 index 00000000..61e5fedc --- /dev/null +++ b/customer/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/CustomerResource.java @@ -0,0 +1,47 @@ +package com.redhat.developer.demos.customer.rest; + +import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/") +public class CustomerResource { + + private static final String RESPONSE_STRING_FORMAT = "customer => %s\n"; + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Inject + @RestClient + PreferenceService preferenceService; + + @GET + @Produces(MediaType.TEXT_PLAIN) + public Response getCustomer() { + try { + String response = preferenceService.getPreference(); + return Response.ok(String.format(RESPONSE_STRING_FORMAT, response)).build(); + } catch (WebApplicationException ex) { + Response response = ex.getResponse(); + logger.warn("Non HTTP 20x trying to get the response from preference service: " + response.getStatus()); + return Response + .status(Response.Status.SERVICE_UNAVAILABLE) + .entity(String.format(RESPONSE_STRING_FORMAT, + String.format("Error: %d - %s", response.getStatus(), response.readEntity(String.class))) + ) + .build(); + } catch (ProcessingException ex) { + logger.warn("Exception trying to get the response from preference service.", ex); + return Response + .status(Response.Status.SERVICE_UNAVAILABLE) + .entity(String.format(RESPONSE_STRING_FORMAT, ex.getCause().getClass().getSimpleName() + ": " + ex.getCause().getMessage())) + .build(); + } + } + +} \ No newline at end of file diff --git a/customer/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/PreferenceService.java b/customer/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/PreferenceService.java new file mode 100644 index 00000000..b4f956ed --- /dev/null +++ b/customer/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/PreferenceService.java @@ -0,0 +1,21 @@ +package com.redhat.developer.demos.customer.rest; + +import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders; +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; + +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Response; + +@RegisterClientHeaders +@RegisterRestClient +public interface PreferenceService { + + @Path("/") + @GET + @Produces("text/plain") + public String getPreference(); + +} diff --git a/customer/java/quarkus/src/main/resources/application.properties b/customer/java/quarkus/src/main/resources/application.properties new file mode 100644 index 00000000..86b02775 --- /dev/null +++ b/customer/java/quarkus/src/main/resources/application.properties @@ -0,0 +1,2 @@ +org.eclipse.microprofile.rest.client.propagateHeaders=User-Agent +com.redhat.developer.demos.customer.rest.PreferenceService/mp-rest/url=http://preference:8080 \ No newline at end of file diff --git a/customer/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/CustomerResourceTest.java b/customer/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/CustomerResourceTest.java new file mode 100644 index 00000000..6697c4a0 --- /dev/null +++ b/customer/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/CustomerResourceTest.java @@ -0,0 +1,22 @@ +package com.redhat.developer.demos.customer.rest; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; + +@QuarkusTest +public class CustomerResourceTest { + + @Test + public void testHelloEndpoint() { + given() + .when().get("/") + .then() + .statusCode(503) + .body(startsWith("customer =>")); + } + +} \ No newline at end of file diff --git a/customer/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/NativeCustomerResourceIT.java b/customer/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/NativeCustomerResourceIT.java new file mode 100644 index 00000000..705ca1c8 --- /dev/null +++ b/customer/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/NativeCustomerResourceIT.java @@ -0,0 +1,9 @@ +package com.redhat.developer.demos.customer.rest; + +import io.quarkus.test.junit.SubstrateTest; + +@SubstrateTest +public class NativeCustomerResourceIT extends CustomerResourceTest { + + // Execute the same tests but in native mode. +} \ No newline at end of file diff --git a/preference/java/quarkus/.dockerignore b/preference/java/quarkus/.dockerignore new file mode 100644 index 00000000..b86c7ac3 --- /dev/null +++ b/preference/java/quarkus/.dockerignore @@ -0,0 +1,4 @@ +* +!target/*-runner +!target/*-runner.jar +!target/lib/* \ No newline at end of file diff --git a/preference/java/quarkus/Dockerfile b/preference/java/quarkus/Dockerfile new file mode 100644 index 00000000..66f8edb2 --- /dev/null +++ b/preference/java/quarkus/Dockerfile @@ -0,0 +1,11 @@ +FROM fabric8/java-jboss-openjdk8-jdk +ENV JAVA_OPTIONS=-Dquarkus.http.host=0.0.0.0 +ENV JAEGER_SERVICE_NAME=preference \ + JAEGER_ENDPOINT=http://jaeger-collector.istio-system.svc:14268/api/traces \ + JAEGER_PROPAGATION=b3 \ + JAEGER_SAMPLER_TYPE=const \ + JAEGER_SAMPLER_PARAM=1 +EXPOSE 8080 8778 9779 +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/app.jar +ENTRYPOINT [ "/deployments/run-java.sh" ] \ No newline at end of file diff --git a/preference/java/quarkus/pom.xml b/preference/java/quarkus/pom.xml new file mode 100644 index 00000000..6c07ea9d --- /dev/null +++ b/preference/java/quarkus/pom.xml @@ -0,0 +1,128 @@ + + + 4.0.0 + com.redhat.developer.demos.preference.rest + preference + 1.0-SNAPSHOT + + 2.22.0 + 1.8 + 1.8 + 0.12.0 + UTF-8 + + + + + io.quarkus + quarkus-bom + ${quarkus.version} + pom + import + + + + + + io.quarkus + quarkus-resteasy + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + io.quarkus + quarkus-smallrye-rest-client + + + io.quarkus + quarkus-smallrye-opentracing + + + io.quarkus + quarkus-smallrye-health + + + + + + io.quarkus + quarkus-maven-plugin + ${quarkus.version} + + + + build + + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + + + + + + + + native + + + native + + + + + + io.quarkus + quarkus-maven-plugin + ${quarkus.version} + + + + native-image + + + true + + + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + + ${project.build.directory}/${project.build.finalName}-runner + + + + + + + + + + + diff --git a/preference/java/quarkus/src/main/docker/Dockerfile.jvm b/preference/java/quarkus/src/main/docker/Dockerfile.jvm new file mode 100644 index 00000000..17ddf2ed --- /dev/null +++ b/preference/java/quarkus/src/main/docker/Dockerfile.jvm @@ -0,0 +1,21 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the docker image run: +# +# mvn package +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/preference-jvm . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/preference-jvm +# +### +FROM fabric8/java-jboss-openjdk8-jdk +ENV JAVA_OPTIONS=-Dquarkus.http.host=0.0.0.0 +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/app.jar +ENTRYPOINT [ "/deployments/run-java.sh" ] \ No newline at end of file diff --git a/preference/java/quarkus/src/main/docker/Dockerfile.native b/preference/java/quarkus/src/main/docker/Dockerfile.native new file mode 100644 index 00000000..9661e121 --- /dev/null +++ b/preference/java/quarkus/src/main/docker/Dockerfile.native @@ -0,0 +1,22 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode +# +# Before building the docker image run: +# +# mvn package -Pnative -Dnative-image.docker-build=true +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native -t quarkus/preference . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/preference +# +### +FROM registry.fedoraproject.org/fedora-minimal +WORKDIR /work/ +COPY target/*-runner /work/application +RUN chmod 775 /work +EXPOSE 8080 +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] \ No newline at end of file diff --git a/preference/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/PreferenceResource.java b/preference/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/PreferenceResource.java new file mode 100644 index 00000000..4b82b364 --- /dev/null +++ b/preference/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/PreferenceResource.java @@ -0,0 +1,48 @@ +package com.redhat.developer.demos.customer.rest; + +import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/") +public class PreferenceResource { + + private static final String RESPONSE_STRING_FORMAT = "preference => %s\n"; + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Inject + @RestClient + RecommendationService recommendationService; + + @GET + @Produces(MediaType.TEXT_PLAIN) + public Response getCustomer(@HeaderParam("User-Agent") String user) { + System.out.println(user); + try { + String response = recommendationService.getPreference(); + return Response.ok(String.format(RESPONSE_STRING_FORMAT, response)).build(); + } catch (WebApplicationException ex) { + Response response = ex.getResponse(); + logger.warn("Non HTTP 20x trying to get the response from recommendation service: " + response.getStatus()); + return Response + .status(Response.Status.SERVICE_UNAVAILABLE) + .entity(String.format(RESPONSE_STRING_FORMAT, + String.format("Error: %d - %s", response.getStatus(), response.readEntity(String.class))) + ) + .build(); + } catch (ProcessingException ex) { + logger.warn("Exception trying to get the response from recommendation service.", ex); + return Response + .status(Response.Status.SERVICE_UNAVAILABLE) + .entity(String.format(RESPONSE_STRING_FORMAT, ex.getCause().getClass().getSimpleName() + ": " + ex.getCause().getMessage())) + .build(); + } + } + +} \ No newline at end of file diff --git a/preference/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/RecommendationService.java b/preference/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/RecommendationService.java new file mode 100644 index 00000000..da9ff374 --- /dev/null +++ b/preference/java/quarkus/src/main/java/com/redhat/developer/demos/customer/rest/RecommendationService.java @@ -0,0 +1,21 @@ +package com.redhat.developer.demos.customer.rest; + +import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders; +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; + +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Response; + +@RegisterClientHeaders +@RegisterRestClient +public interface RecommendationService { + + @Path("/") + @GET + @Produces("text/plain") + public String getPreference(); + +} diff --git a/preference/java/quarkus/src/main/resources/application.properties b/preference/java/quarkus/src/main/resources/application.properties new file mode 100644 index 00000000..a37b150e --- /dev/null +++ b/preference/java/quarkus/src/main/resources/application.properties @@ -0,0 +1,2 @@ +org.eclipse.microprofile.rest.client.propagateHeaders=User-Agent +com.redhat.developer.demos.customer.rest.RecommendationService/mp-rest/url=http://recommendation:8080 \ No newline at end of file diff --git a/preference/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/NativePreferenceResourceIT.java b/preference/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/NativePreferenceResourceIT.java new file mode 100644 index 00000000..fc298d82 --- /dev/null +++ b/preference/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/NativePreferenceResourceIT.java @@ -0,0 +1,9 @@ +package com.redhat.developer.demos.customer.rest; + +import io.quarkus.test.junit.SubstrateTest; + +@SubstrateTest +public class NativePreferenceResourceIT extends PreferenceResourceTest { + + // Execute the same tests but in native mode. +} \ No newline at end of file diff --git a/preference/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/PreferenceResourceTest.java b/preference/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/PreferenceResourceTest.java new file mode 100644 index 00000000..2ff83cc9 --- /dev/null +++ b/preference/java/quarkus/src/test/java/com/redhat/developer/demos/customer/rest/PreferenceResourceTest.java @@ -0,0 +1,22 @@ +package com.redhat.developer.demos.customer.rest; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; + +@QuarkusTest +public class PreferenceResourceTest { + + @Test + public void testHelloEndpoint() { + given() + .when().get("/") + .then() + .statusCode(503) + .body(startsWith("preference =>")); + } + +} \ No newline at end of file diff --git a/recommendation/java/quarkus/.dockerignore b/recommendation/java/quarkus/.dockerignore new file mode 100644 index 00000000..b86c7ac3 --- /dev/null +++ b/recommendation/java/quarkus/.dockerignore @@ -0,0 +1,4 @@ +* +!target/*-runner +!target/*-runner.jar +!target/lib/* \ No newline at end of file diff --git a/recommendation/java/quarkus/Dockerfile b/recommendation/java/quarkus/Dockerfile new file mode 100644 index 00000000..ceb73e42 --- /dev/null +++ b/recommendation/java/quarkus/Dockerfile @@ -0,0 +1,6 @@ +FROM fabric8/java-jboss-openjdk8-jdk +ENV JAVA_OPTIONS=-Dquarkus.http.host=0.0.0.0 +EXPOSE 8080 8778 9779 +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/app.jar +ENTRYPOINT [ "/deployments/run-java.sh" ] \ No newline at end of file diff --git a/recommendation/java/quarkus/pom.xml b/recommendation/java/quarkus/pom.xml new file mode 100644 index 00000000..90a847e4 --- /dev/null +++ b/recommendation/java/quarkus/pom.xml @@ -0,0 +1,127 @@ + + + 4.0.0 + com.redhat.developers + recommendation + 1.0-SNAPSHOT + + 2.22.0 + 1.8 + 1.8 + 0.12.0 + UTF-8 + + + + + io.quarkus + quarkus-bom + ${quarkus.version} + pom + import + + + + + + io.quarkus + quarkus-resteasy + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-junit5 + + + io.rest-assured + rest-assured + test + + + io.quarkus + quarkus-smallrye-opentracing + + + io.quarkus + quarkus-smallrye-health + + + + + + io.quarkus + quarkus-maven-plugin + ${quarkus.version} + + + + build + + + + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + + + + + + + + native + + + native + + + + + + io.quarkus + quarkus-maven-plugin + ${quarkus.version} + + + + native-image + + + true + + + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + + ${project.build.directory}/${project.build.finalName}-runner + + + + + + + + + + + diff --git a/recommendation/java/quarkus/src/main/docker/Dockerfile.jvm b/recommendation/java/quarkus/src/main/docker/Dockerfile.jvm new file mode 100644 index 00000000..7171cb11 --- /dev/null +++ b/recommendation/java/quarkus/src/main/docker/Dockerfile.jvm @@ -0,0 +1,21 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the docker image run: +# +# mvn package +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/recommendation-jvm . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/recommendation-jvm +# +### +FROM fabric8/java-jboss-openjdk8-jdk +ENV JAVA_OPTIONS=-Dquarkus.http.host=0.0.0.0 +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/app.jar +ENTRYPOINT [ "/deployments/run-java.sh" ] \ No newline at end of file diff --git a/recommendation/java/quarkus/src/main/docker/Dockerfile.native b/recommendation/java/quarkus/src/main/docker/Dockerfile.native new file mode 100644 index 00000000..9cde3d38 --- /dev/null +++ b/recommendation/java/quarkus/src/main/docker/Dockerfile.native @@ -0,0 +1,22 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode +# +# Before building the docker image run: +# +# mvn package -Pnative -Dnative-image.docker-build=true +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native -t quarkus/recommendation . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/recommendation +# +### +FROM registry.fedoraproject.org/fedora-minimal +WORKDIR /work/ +COPY target/*-runner /work/application +RUN chmod 775 /work +EXPOSE 8080 +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] \ No newline at end of file diff --git a/recommendation/java/quarkus/src/main/java/com/redhat/developer/demos/recommendation/rest/RecommendationResource.java b/recommendation/java/quarkus/src/main/java/com/redhat/developer/demos/recommendation/rest/RecommendationResource.java new file mode 100644 index 00000000..8f87e01d --- /dev/null +++ b/recommendation/java/quarkus/src/main/java/com/redhat/developer/demos/recommendation/rest/RecommendationResource.java @@ -0,0 +1,78 @@ +package com.redhat.developer.demos.recommendation.rest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +@Path("/") +public class RecommendationResource { + + private static final String RESPONSE_STRING_FORMAT = "recommendation v1 from '%s': %d\n"; + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + /** + * Counter to help us see the lifecycle + */ + private int count = 0; + + /** + * Flag for throwing a 503 when enabled + */ + private boolean misbehave = false; + + private static final String HOSTNAME = parseContainerIdFromHostname( + System.getenv().getOrDefault("HOSTNAME", "unknown")); + + static String parseContainerIdFromHostname(String hostname) { + return hostname.replaceAll("recommendation-v\\d+-", ""); + } + + @GET + public Response recommendations() { + count++; + logger.debug(String.format("recommendation request from %s: %d", HOSTNAME, count)); + + // timeout(); + + logger.debug("recommendation service ready to return"); + if (misbehave) { + return doMisbehavior(); + } + return Response.ok(String.format(RESPONSE_STRING_FORMAT, HOSTNAME, count)).build(); + } + + private void timeout() { + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + logger.info("Thread interrupted"); + } + } + + private Response doMisbehavior() { + logger.debug(String.format("Misbehaving %d", count)); + return Response.status(Response.Status.SERVICE_UNAVAILABLE) + .entity(String.format("recommendation misbehavior from '%s'\n", HOSTNAME)).build(); + } + + @GET + @Path("/misbehave") + public Response flagMisbehave() { + this.misbehave = true; + logger.debug("'misbehave' has been set to 'true'"); + return Response.ok("Following requests to / will return a 503\n").build(); + } + + @GET + @Path("/behave") + public Response flagBehave() { + this.misbehave = false; + logger.debug("'misbehave' has been set to 'false'"); + return Response.ok("Following requests to / will return 200\n").build(); + } + +} \ No newline at end of file diff --git a/recommendation/java/quarkus/src/test/java/com/redhat/developer/demos/recommendation/rest/NativeRecommendationResourceIT.java b/recommendation/java/quarkus/src/test/java/com/redhat/developer/demos/recommendation/rest/NativeRecommendationResourceIT.java new file mode 100644 index 00000000..6f2bba31 --- /dev/null +++ b/recommendation/java/quarkus/src/test/java/com/redhat/developer/demos/recommendation/rest/NativeRecommendationResourceIT.java @@ -0,0 +1,9 @@ +package com.redhat.developer.demos.recommendation.rest; + +import io.quarkus.test.junit.SubstrateTest; + +@SubstrateTest +public class NativeRecommendationResourceIT extends RecommendationResourceTest { + + // Execute the same tests but in native mode. +} \ No newline at end of file diff --git a/recommendation/java/quarkus/src/test/java/com/redhat/developer/demos/recommendation/rest/RecommendationResourceTest.java b/recommendation/java/quarkus/src/test/java/com/redhat/developer/demos/recommendation/rest/RecommendationResourceTest.java new file mode 100644 index 00000000..7d8b46ac --- /dev/null +++ b/recommendation/java/quarkus/src/test/java/com/redhat/developer/demos/recommendation/rest/RecommendationResourceTest.java @@ -0,0 +1,21 @@ +package com.redhat.developer.demos.recommendation.rest; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.startsWith; + +@QuarkusTest +public class RecommendationResourceTest { + + @Test + public void testHelloEndpoint() { + given() + .when().get("/") + .then() + .statusCode(200) + .body(startsWith("recommendation")); + } + +} \ No newline at end of file diff --git a/recommendation/java/vertx/src/main/java/com/redhat/developer/demos/recommendation/RecommendationVerticle.java b/recommendation/java/vertx/src/main/java/com/redhat/developer/demos/recommendation/RecommendationVerticle.java index 0a956541..31bcbb01 100755 --- a/recommendation/java/vertx/src/main/java/com/redhat/developer/demos/recommendation/RecommendationVerticle.java +++ b/recommendation/java/vertx/src/main/java/com/redhat/developer/demos/recommendation/RecommendationVerticle.java @@ -16,6 +16,7 @@ public class RecommendationVerticle extends AbstractVerticle { private static final String RESPONSE_STRING_FORMAT = "recommendation v1 from '%s': %d\n"; + private static final String HTTP_NOW = "now.httpbin.org"; private static final String HOSTNAME = parseContainerIdFromHostname(