Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quickstart cleanup, using @HelidonTest in MP. (#4011) #4014

Merged
merged 3 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package {{package}};

import java.util.Collections;

import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
Expand Down Expand Up @@ -39,7 +38,6 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
* The message is returned as a JSON object.
*/
@Path("/greet")
@RequestScoped
public class GreetResource {

private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Use a profile (without a name) to use a random port in tests
tomas-langer marked this conversation as resolved.
Show resolved Hide resolved
# If a profile would be used for production sources, this should be
# a named profile (such as config-profile-dev.yaml) and you would need
# to switch profile for tests using system property config.profile
sources:
- type: "inlined"
properties:
server.port: 0
- type: "classpath"
properties:
resource: "application.yaml"
7 changes: 6 additions & 1 deletion examples/quickstarts/helidon-quickstart-mp/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2018, 2021 Oracle and/or its affiliates.
Copyright (c) 2018, 2022 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,6 +47,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
* Copyright (c) 2018, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@

import java.util.Collections;

import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
Expand Down Expand Up @@ -53,7 +52,6 @@
* The message is returned as a JSON object.
*/
@Path("/greet")
@RequestScoped
public class GreetResource {

private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
* Copyright (c) 2018, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,85 +16,63 @@

package io.helidon.examples.quickstart.mp;

import io.helidon.microprofile.server.Server;
import io.helidon.microprofile.tests.junit5.HelidonTest;

import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.inject.Inject;
import jakarta.json.JsonObject;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@HelidonTest
class MainTest {
private static Server server;
private final WebTarget target;

@BeforeAll
public static void startTheServer() throws Exception {
server = Server.create().start();
@Inject
MainTest(WebTarget target) {
this.target = target;
}

@Test
void testHelloWorld() {

Client client = ClientBuilder.newClient();

JsonObject jsonObject = client
.target(getConnectionString("/greet"))
JsonObject jsonObject = target.path("/greet")
.request()
.get(JsonObject.class);
Assertions.assertEquals("Hello World!", jsonObject.getString("message"),
"default message");

jsonObject = client
.target(getConnectionString("/greet/Joe"))
jsonObject = target.path("/greet/Joe")
.request()
.get(JsonObject.class);
Assertions.assertEquals("Hello Joe!", jsonObject.getString("message"),
"hello Joe message");

try (Response r = client
.target(getConnectionString("/greet/greeting"))
try (Response r = target.path("/greet/greeting")
.request()
.put(Entity.entity("{\"greeting\" : \"Hola\"}", MediaType.APPLICATION_JSON))) {
Assertions.assertEquals(204, r.getStatus(), "PUT status code");
}

jsonObject = client
.target(getConnectionString("/greet/Jose"))
jsonObject = target.path("/greet/Jose")
.request()
.get(JsonObject.class);
Assertions.assertEquals("Hola Jose!", jsonObject.getString("message"),
"hola Jose message");

try (Response r = client
.target(getConnectionString("/metrics"))
try (Response r = target.path("/metrics")
.request()
.get()) {
Assertions.assertEquals(200, r.getStatus(), "GET metrics status code");
}

try (Response r = client
.target(getConnectionString("/health"))
try (Response r = target.path("/health")
.request()
.get()) {
Assertions.assertEquals(200, r.getStatus(), "GET health status code");
}
}

@AfterAll
static void destroyClass() {
CDI<Object> current = CDI.current();
((SeContainer) current).close();
}

private String getConnectionString(String path) {
return "http://localhost:" + server.port() + path;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
* Copyright (c) 2018, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MainTest {
class MainTest {

private static WebServer webServer;
private static WebClient webClient;
Expand All @@ -47,7 +47,7 @@ public class MainTest {
}

@BeforeAll
public static void startTheServer() {
static void startTheServer() {
webServer = Main.startServer().await();

webClient = WebClient.builder()
Expand All @@ -57,15 +57,15 @@ public static void startTheServer() {
}

@AfterAll
public static void stopServer() {
static void stopServer() {
if (webServer != null) {
webServer.shutdown()
.await(10, TimeUnit.SECONDS);
}
}

@Test
public void testHelloWorld() {
void testHelloWorld() {
JsonObject jsonObject;
WebClientResponse response;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2018, 2021 Oracle and/or its affiliates.
# Copyright (c) 2022 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,9 +14,10 @@
# limitations under the License.
#


# Override configuration to use a random port for the unit tests
config_ordinal=1000
# Microprofile server properties
server.port=-1
server.host=0.0.0.0
sources:
- type: "inlined"
properties:
server.port: 0
- type: "classpath"
properties:
resource: "application.yaml"
5 changes: 5 additions & 0 deletions examples/quickstarts/helidon-standalone-quickstart-mp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading