Skip to content

Commit

Permalink
test: JaxRS Test (#1446)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo authored Feb 21, 2024
1 parent dee637f commit 8764e96
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id("io.micronaut.build.internal.openapi-base")
id("java-library")
}

dependencies {
testAnnotationProcessor(mn.micronaut.inject.java)
testImplementation(mnTest.micronaut.test.junit5)
testRuntimeOnly(libs.junit.jupiter.engine)
testRuntimeOnly(mnLogging.logback.classic)
}
tasks.test {
useJUnitPlatform()
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ commons-codec = "1.16.1"

micronaut = "4.3.7"
micronaut-platform = "4.3.2"
micronaut-jaxrs = "4.3.0"
micronaut-security = "4.6.4"
micronaut-serde = "2.8.1"
micronaut-rxjava2 = "2.2.1"
Expand All @@ -41,6 +42,7 @@ micronaut-docs = "2.0.0"
[libraries]
# Core
micronaut-core = { module = 'io.micronaut:micronaut-core-bom', version.ref = 'micronaut' }
micronaut-jaxrs = { module = "io.micronaut.jaxrs:micronaut-jaxrs-bom", version.ref = "micronaut-jaxrs" }
micronaut-logging = { module = "io.micronaut.logging:micronaut-logging-bom", version.ref = "micronaut-logging" }
micronaut-security = { module = "io.micronaut.security:micronaut-security-bom", version.ref = "micronaut-security" }
micronaut-serde = { module = "io.micronaut.serde:micronaut-serde-bom", version.ref = "micronaut-serde" }
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ include 'docs-examples:example-groovy'
include 'docs-examples:example-java'
include 'docs-examples:example-kotlin'
include 'test-suite-java-client-generator'
include 'test-suite-java-jaxrs'
include 'test-suite-java-server-generator'
include 'test-suite-kotlin-kapt-client-generator'
include 'test-suite-kotlin-kapt-server-generator'
Expand All @@ -49,4 +50,5 @@ micronautBuild {
importMicronautCatalog("micronaut-data")
importMicronautCatalog("micronaut-kotlin")
importMicronautCatalog("micronaut-session")
importMicronautCatalog("micronaut-jaxrs")
}
19 changes: 19 additions & 0 deletions test-suite-java-jaxrs/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id("io.micronaut.build.internal.openapi-test-java")
}

dependencies {
testAnnotationProcessor(mnJaxrs.micronaut.jaxrs.processor)
testImplementation(mnJaxrs.micronaut.jaxrs.server)
testImplementation(mn.micronaut.http.server.netty)

testImplementation(mn.snakeyaml)

testImplementation(mn.micronaut.http.client)

testAnnotationProcessor(mnSerde.micronaut.serde.processor)
testImplementation(mnSerde.micronaut.serde.jackson)

testAnnotationProcessor(projects.micronautOpenapi)
testCompileOnly(projects.micronautOpenapiAnnotations)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.micronaut.open.jaxrs;

import io.micronaut.runtime.Micronaut;
import io.swagger.v3.oas.annotations.*;
import io.swagger.v3.oas.annotations.info.*;

@OpenAPIDefinition(
info = @Info(
title = "demo",
version = "0.0"
)
)
public class Application {

public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.micronaut.open.jaxrs;

import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

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

@MicronautTest
class OpenApiExposedTest {

@Test
void openApi(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
assertDoesNotThrow(() -> client.exchange("/swagger/demo-0.0.yml"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.micronaut.open.jaxrs;

import io.micronaut.core.io.ResourceLoader;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

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

@MicronautTest(startApplication = false)
class OpenApiGeneratedTest {

@Test
void buildGeneratesOpenApi(ResourceLoader resourceLoader) {
assertTrue(resourceLoader.getResource("META-INF/swagger/demo-0.0.yml").isPresent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.micronaut.open.jaxrs;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;

@Path("/user")
class TestController {

@GET
@Path("/{userId}")
@Produces({ "application/json" })
String getUser(@PathParam("userId") String userId){
return "Pong version " + userId;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.micronaut.open.jaxrs;

import io.micronaut.context.BeanContext;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;

import java.util.Map;

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

@MicronautTest
class TestControllerTest {

@Test
void jaxrsOpenApiPathTest(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
String responseYaml = assertDoesNotThrow(() -> client.retrieve("/swagger/demo-0.0.yml", String.class));
assertNotNull(responseYaml);
Yaml yaml = new Yaml();
Map<String, Object> obj = yaml.load(responseYaml);
assertNotNull(obj);
assertInstanceOf(Map.class, obj.get("paths"));
Map<String, Object> paths = (Map<String, Object>) obj.get("paths");
assertTrue(paths.containsKey("/user/{userId}"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
micronaut.router.static-resources.swagger.paths=classpath:META-INF/swagger
micronaut.router.static-resources.swagger.mapping=/swagger/**
13 changes: 13 additions & 0 deletions test-suite-java-jaxrs/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit 8764e96

Please sign in to comment.