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

build: Micronaut Framework to 3.8.3 #1231

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.client.StreamingHttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import reactor.core.publisher.Flux
import spock.lang.Shared
import spock.lang.Specification

import java.util.regex.Pattern
import java.util.stream.Stream

@MicronautTest // <1>
class GithubDeclarativeControllerSpec extends Specification {

@Inject
@Client("/")
StreamingHttpClient streamingClient // <2>

@Shared
Pattern MICRONAUT_RELEASE = Pattern.compile("[Micronaut|Micronaut Framework] [0-9].[0-9].[0-9]([0-9])?( (RC|M)[0-9])?");

void 'verify github releases can be fetched with compile-time autogenerated @Client'() {
when:
HttpRequest request = HttpRequest.GET('/github/releases')

Stream<GithubRelease> githubReleases = Flux.from(streamingClient.jsonStream(request, GithubRelease)).toStream() // <3>

then:
githubReleases
.map(GithubRelease::getName)
.allMatch(name -> MICRONAUT_RELEASE.matcher(name).find())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Shared
import spock.lang.Specification

import java.util.regex.Pattern

@MicronautTest // <1>
class GithubControllerSpec extends Specification {
class GithubLowLevelControllerSpec extends Specification {

@Inject
@Client("/")
Expand All @@ -22,7 +23,6 @@ class GithubControllerSpec extends Specification {
@Shared
Pattern MICRONAUT_RELEASE = Pattern.compile("[Micronaut|Micronaut Framework] [0-9].[0-9].[0-9]([0-9])?( (RC|M)[0-9])?");


void 'verify github releases can be fetched with low level HttpClient'() {
when:
HttpRequest request = HttpRequest.GET('/github/releases-lowlevel')
Expand All @@ -38,23 +38,8 @@ class GithubControllerSpec extends Specification {
List<GithubRelease> releases = rsp.body()

then:
releases
releases.stream()
.map(GithubRelease::getName)
.allMatch(name -> MICRONAUT_RELEASE.matcher(name)
.find())
}

void 'verify github releases can be fetched with compile-time autogenerated @Client'() {
when:
HttpRequest request = HttpRequest.GET('/github/releases-lowlevel')

List<GithubRelease> githubReleases = client.toBlocking().retrieve(request, Argument.listOf(GithubRelease)) // <7>

then:
githubReleases.stream()
.map(GithubRelease::getName)
.allMatch(name -> MICRONAUT_RELEASE.matcher(name)
.find())
releases.collect { it.name }.every { name ->
MICRONAUT_RELEASE.matcher(name).find()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package example.micronaut;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.StreamingHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;

import java.util.regex.Pattern;
import java.util.stream.Stream;

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

@MicronautTest // <1>
class GithubDeclarativeControllerTest {

@Inject
@Client("/")
StreamingHttpClient streamingClient; // <2>

private static Pattern MICRONAUT_RELEASE = Pattern.compile("[Micronaut|Micronaut Framework] [0-9].[0-9].[0-9]([0-9])?( (RC|M)[0-9])?");

@Test
void verifyGithubReleasesCanBeFetchedWithCompileTimeAutoGeneratedAtClient() {
//when:
HttpRequest<Object> request = HttpRequest.GET("/github/releases");

Stream<GithubRelease> githubReleases = Flux.from(streamingClient.jsonStream(request, GithubRelease.class)).toStream(); // <3>

//then:
assertTrue(githubReleases
.map(GithubRelease::getName)
.allMatch(name -> MICRONAUT_RELEASE.matcher(name)
.find()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;

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

@MicronautTest // <1>
class GithubControllerTest {
class GithubLowLevelControllerTest {

@Inject
@Client("/")
Expand All @@ -25,7 +27,7 @@ class GithubControllerTest {
private static Pattern MICRONAUT_RELEASE = Pattern.compile("[Micronaut|Micronaut Framework] [0-9].[0-9].[0-9]([0-9])?( (RC|M)[0-9])?");

@Test
public void verifyGithubReleasesCanBeFetchedWithLowLevelHttpClient() {
void verifyGithubReleasesCanBeFetchedWithLowLevelHttpClient() {
//when:
HttpRequest<Object> request = HttpRequest.GET("/github/releases-lowlevel");

Expand All @@ -46,18 +48,4 @@ public void verifyGithubReleasesCanBeFetchedWithLowLevelHttpClient() {
.allMatch(name -> MICRONAUT_RELEASE.matcher(name)
.find()));
}

@Test
public void verifyGithubReleasesCanBeFetchedWithCompileTimeAutoGeneratedAtClient() {
//when:
HttpRequest<Object> request = HttpRequest.GET("/github/releases-lowlevel");

List<GithubRelease> githubReleases = client.toBlocking().retrieve(request, Argument.listOf(GithubRelease.class)); // <7>

//then:
assertTrue(githubReleases.stream()
.map(GithubRelease::getName)
.allMatch(name -> MICRONAUT_RELEASE.matcher(name)
.find()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.client.StreamingHttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import reactor.core.publisher.Flux

@MicronautTest // <1>
class GithubDeclarativeControllerTest {

@Inject
@field:Client("/")
lateinit var streamingClient: StreamingHttpClient

@Test
fun verifyGithubReleasesCanBeFetchedWithCompileTimeAutoGeneratedAtClient() {
//when:
val request: HttpRequest<Any> = HttpRequest.GET("/github/releases")

val githubReleases = Flux.from(streamingClient.jsonStream(request, GithubRelease::class.java)).toStream() // <7>

//then:
val regex = Regex("Micronaut( Framework)? [0-9].[0-9].[0-9]([0-9])?( (RC|M)[0-9])?")
//then:

//then:
assertTrue(
githubReleases
.map(GithubRelease::name)
.allMatch { name: String -> regex.matches(name) }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

@MicronautTest // <1>
class GithubControllerTest {
class GithubLowLevelControllerTest {

@Inject
@field:Client("/")
Expand All @@ -34,23 +34,9 @@ class GithubControllerTest {
val releases = rsp.body()

//then:
assertNotNull(releases)
val regex = Regex("Micronaut( Framework)? [0-9].[0-9].[0-9]([0-9])?( (RC|M)[0-9])?")
for (release in releases) {
assertTrue(regex.matches(release.name))
}
}

@Test
fun verifyGithubReleasesCanBeFetchedWithCompileTimeAutoGeneratedAtClient() {
//when:
val request: HttpRequest<Any> = HttpRequest.GET("/github/releases-lowlevel")
val githubReleases = client.toBlocking().retrieve(request, Argument.listOf(GithubRelease::class.java)) // <7>

//then:
val regex = Regex("Micronaut( Framework)? [0-9].[0-9].[0-9]([0-9])?( (RC|M)[0-9])?")
for (release in githubReleases) {
assertTrue(regex.matches(release.name))
}
assertTrue(
releases!!.map { it.name }.all { regex.matches(it) }
)
}
}
19 changes: 10 additions & 9 deletions guides/micronaut-http-client/micronaut-http-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ callout:http-request[4]
<6> GitHub encourages to explicitly request the version 3 via the `Accept` header. With `@Header`, you add the `Accept: application/vnd.github.v3+json` HTTP header to every request.
<7> Use `retrieve` to perform an HTTP request for the given request object and convert the full HTTP response's body into the specified type. e.g. `List<GithubRelease>`.

NOTE: Instead of `retrieve` we could have used `jsonStream`. You can use `jsonStream()` to stream arrays of type `application/json` or
JSON streams of type `application/x-json-stream`. If we use `retrieve`, such as in the previous code listing, the operation will not block.
However, it will not return until all the data has been received from the server. In the case of a JSON array that would be the whole array.
However, if you are interested in just the first element of the array, `jsonStream` provides a better alternative since it starts streaming data from the server without needing the whole response.
For example, `jsonStream().firstElement()` will only parse the first item in a JSON array. Hence it is more efficient.

Comment on lines -59 to -64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this, as jsonStream is not a method of HttpClient and I considered it too complex for this guide to start talking about the StreamingHttpClient at this point in the docs

=== Declarative Client

It is time to take a look at support for declarative clients via the Client annotation.
Expand Down Expand Up @@ -94,17 +88,24 @@ callout:controller[number=1,arg0=/github]

=== Tests

Create a test to verify that both clients work as expected, and the controller echoes the output of the GitHub API in a Reactive way.
Create a test to verify that the low-level client works as expected, and the controller echoes the output of the GitHub API in a Reactive way.

test:GithubControllerTest[]
test:GithubLowLevelControllerTest[]

callout:micronaut-test[1]
<2> Inject the `HttpClient` bean in the application context.
<3> Sometimes, receiving just the object is not enough, and you need information about the response. In this case, instead of `retrieve` you should use the `exchange` method.
<4> The Micronaut framework makes it easy to parse JSON into Java objects.
<5> Use `status` to check the HTTP status code.
callout:body-method[6]
callout:binding-json-array[7]

Then create a test that verifies the response from the `/github/releases` endpoint which uses the Declarative Client to generate a JSON stream response.

test:GithubDeclarativeControllerTest[]

callout:micronaut-test[1]
<2> For consuming a JSON stream, we inject a `StreamingHttpClient` bean.
<3> Read the JSON stream, and convert it to a Java Stream of GithubRelease objects.

common:testApp.adoc[]

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.8.2
3.8.3