Merge remote-tracking branch 'upstream/master'

This commit is contained in:
mcasari 2023-10-03 23:32:01 +02:00
commit d0ca3f2c17
29 changed files with 683 additions and 176 deletions

View File

@ -7,6 +7,7 @@
<artifactId>micronaut</artifactId>
<version>0.1</version>
<name>micronaut</name>
<packaging>${packaging}</packaging>
<parent>
<groupId>com.baeldung</groupId>
@ -18,7 +19,7 @@
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>bom</artifactId>
<artifactId>micronaut-bom</artifactId>
<version>${micronaut.version}</version>
<type>pom</type>
<scope>import</scope>
@ -29,120 +30,136 @@
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>http-client</artifactId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>http-server-netty</artifactId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>inject</artifactId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>runtime</artifactId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${annotation.api.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>inject-java</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>${reactor.version}</version>
<groupId>io.micronaut.rxjava3</groupId>
<artifactId>micronaut-rxjava3</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.rxjava3</groupId>
<artifactId>micronaut-rxjava3-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.serde</groupId>
<artifactId>micronaut-serde-jackson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.self="append">
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${shade.plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${exec.mainClass}</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
<id>default-shade</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>${exec.mainClass}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.plugin.version}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>io.micronaut</groupId>
<artifactId>inject-java</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<!-- <exec.mainClass>com.baeldung.micronaut.helloworld.server.ServerApplication</exec.mainClass> -->
<exec.mainClass>com.baeldung.micronaut.vs.springboot.CompareApplication</exec.mainClass>
<micronaut.version>1.0.0.RC2</micronaut.version>
<micronaut.version>3.10.1</micronaut.version>
<jdk.version>17</jdk.version>
<annotation.api.version>1.3.2</annotation.api.version>
<reactor.version>3.1.6.RELEASE</reactor.version>
<release.version>17</release.version>
<packaging>jar</packaging>
<compiler.plugin.version>3.7.0</compiler.plugin.version>
<shade.plugin.version>3.1.0</shade.plugin.version>
<micronaut.runtime>netty</micronaut.runtime>
<shade.plugin.version>3.2.0</shade.plugin.version>
</properties>
</project>

View File

@ -0,0 +1,42 @@
package com.baeldung.micronaut.apiversioning.custom.client;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Flowable;
@Client("/")
@Header(name = HttpHeaders.ACCEPT, value = "application/json")
public interface BirdCountClient {
@Get(
uri = "/bird/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Header(name = "api-key", value = "11")
@SingleResult
Flowable<String> countV1(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/bird/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Header(name = "api-key", value = "10")
@SingleResult
Flowable<String> countV2(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/bird/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Header(name = "api-key", value = "")
@SingleResult
Flowable<String> countDefault(@QueryValue("max") @Nullable Integer max);
}

View File

@ -0,0 +1,36 @@
package com.baeldung.micronaut.apiversioning.custom.server;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller("/bird")
public class BirdCountController {
@Get(value = "/count", produces = {"application/json"})
@Version("1")
public Flowable<String> countV1(@QueryValue("max") @Nullable Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Bird " + index)
.limit(max == null ? 10 : max)
);
}
@Get(value = "/count", produces = {"application/json"})
@Version("2")
public Flowable<String> countV2(@QueryValue("max") @NonNull Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Bird " + index)
.limit(max)
);
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.micronaut.apiversioning.custom.server;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Value;
import io.micronaut.http.HttpRequest;
import io.micronaut.web.router.version.resolution.RequestVersionResolver;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.Optional;
@Singleton
@Requires(property = "my.router.versioning.enabled", value = "true")
public class CustomVersionResolver implements RequestVersionResolver {
@Inject
@Value("${micronaut.router.versioning.default-version}")
private String defaultVersion;
@Override
public Optional<String> resolve(HttpRequest<?> request) {
var apiKey = Optional.ofNullable(request.getHeaders().get("api-key"));
if (apiKey.isPresent() && !apiKey.get().isEmpty()) {
return Optional.of(Integer.parseInt(apiKey.get()) % 2 == 0 ? "2" : "1");
}
return Optional.of(defaultVersion);
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.micronaut.apiversioning.header.client;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Flowable;
@Client("/")
@Header(name = HttpHeaders.ACCEPT, value = "application/json")
public interface DogCountClient {
@Get(
uri = "/dog/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Version("1")
@SingleResult
Flowable<String> countV1(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/dog/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@Version("2")
@SingleResult
Flowable<String> countV2(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/dog/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> countDefault(@QueryValue("max") @Nullable Integer max);
}

View File

@ -0,0 +1,36 @@
package com.baeldung.micronaut.apiversioning.header.server;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller("/dog")
public class DogCountController {
@Get(value = "/count", produces = {"application/json"})
@Version("1")
public Flowable<String> countV1(@QueryValue("max") @Nullable Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Dog " + index)
.limit(max == null ? 10 : max)
);
}
@Get(value = "/count", produces = {"application/json"})
@Version("2")
public Flowable<String> countV2(@QueryValue("max") @NonNull Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Dog " + index)
.limit(max)
);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.micronaut.apiversioning.param.client;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Flowable;
@Client("/")
@Header(name = HttpHeaders.ACCEPT, value = "application/json")
public interface CatCountClient {
@Get(
uri = "/cat/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> count(@QueryValue("max") @Nullable Integer max, @QueryValue(value = "v", defaultValue = "1") @Nullable Integer version);
}

View File

@ -0,0 +1,37 @@
package com.baeldung.micronaut.apiversioning.param.server;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller("/cat")
public class CatCountController {
@Get(value = "/count", produces = {"application/json"})
@Version("1")
public Flowable<String> countV1(@QueryValue("max") @Nullable Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Cat " + index)
.limit(max == null ? 10 : max)
);
}
@Get(value = "/count", produces = {"application/json"})
@Version("2")
public Flowable<String> countV2(@QueryValue("max") @NonNull Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Cat " + index)
.limit(max)
);
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.micronaut.apiversioning.url.client;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Flowable;
@Client("/")
@Header(name = HttpHeaders.ACCEPT, value = "application/json")
public interface SheepCountClient {
@Get(
uri = "/v1/sheep/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> countV1(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/v2/sheep/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> countV2(@QueryValue("max") @Nullable Integer max);
@Get(
uri = "/sheep/count",
consumes = {"application/json"},
produces = {"application/json"}
)
@SingleResult
Flowable<String> countDefault(@QueryValue("max") @Nullable Integer max);
}

View File

@ -0,0 +1,26 @@
package com.baeldung.micronaut.apiversioning.url.server;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller("/v1/sheep/count")
public class SheepCountControllerV1 {
@Get(
uri = "{?max}",
consumes = {"application/json"},
produces = {"application/json"}
)
Flowable<String> countV1(@Nullable Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Sheep " + index)
.limit(max == null ? 10 : max)
);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.micronaut.apiversioning.url.server;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.reactivex.rxjava3.core.Flowable;
import java.util.stream.Stream;
@Controller
public class SheepCountControllerV2 {
@Get(
uris = {"/v2/sheep/count", "/sheep/count"},
consumes = {"application/json"},
produces = {"application/json"}
)
Flowable<String> countV2(@QueryValue("max") @NonNull Integer max) {
return Flowable.fromStream(
Stream.iterate(0, i -> i + 1)
.map(index -> "Sheep " + index)
.limit(max)
);
}
}

View File

@ -1,28 +1,27 @@
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.client.RxHttpClient;
import io.reactivex.Single;
import javax.inject.Singleton;
import io.reactivex.rxjava3.core.Single;
import jakarta.inject.Singleton;
@Singleton
public class ConcreteGreetingClient
{
private RxHttpClient httpClient;
private HttpClient httpClient;
public ConcreteGreetingClient(@Client("/") RxHttpClient httpClient) {
public ConcreteGreetingClient(@Client("/") HttpClient httpClient) {
this.httpClient = httpClient;
}
public String greet(String name) {
HttpRequest<String> req = HttpRequest.GET("/greet/" + name);
return httpClient.retrieve(req).blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req)).blockingGet();
}
public Single<String> greetAsync(String name) {
HttpRequest<String> req = HttpRequest.GET("/async/greet/" + name);
return httpClient.retrieve(req).first("An error as occurred");
return Single.fromPublisher(httpClient.retrieve(req));
}
}

View File

@ -3,9 +3,9 @@ package com.baeldung.micronaut.helloworld.server.controller;
import com.baeldung.micronaut.helloworld.server.service.GreetingService;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.Single;
import javax.inject.Inject;
import io.reactivex.rxjava3.core.Single;
import jakarta.inject.Inject;
@Controller("/async/greet")
public class AsyncGreetController {

View File

@ -7,7 +7,7 @@ import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import javax.inject.Inject;
import jakarta.inject.Inject;
@Controller("/greet")
public class GreetController {

View File

@ -2,7 +2,7 @@ package com.baeldung.micronaut.helloworld.server.service;
import io.micronaut.context.annotation.Primary;
import javax.inject.Singleton;
import jakarta.inject.Singleton;
@Primary
@Singleton

View File

@ -1,6 +1,6 @@
package com.baeldung.micronaut.helloworld.server.service;
import javax.inject.Singleton;
import jakarta.inject.Singleton;
@Singleton
public class SpanishGreetingService implements GreetingService {

View File

@ -1,9 +1,11 @@
package com.baeldung.micronaut.vs.springboot;
import io.micronaut.runtime.Micronaut;
public class CompareApplication {
public static void main(String[] args) {
Micronaut.run(CompareApplication.class);
Micronaut.run(CompareApplication.class, args);
}
}
}

View File

@ -1,46 +1,46 @@
package com.baeldung.micronaut.vs.springboot.client;
import javax.inject.Singleton;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.rxjava3.core.Single;
import jakarta.inject.Singleton;
@Singleton
public class ArithmeticClientImpl {
private RxHttpClient httpClient;
private HttpClient httpClient;
public ArithmeticClientImpl(@Client("/") RxHttpClient httpClient) {
public ArithmeticClientImpl(@Client("/") HttpClient httpClient) {
this.httpClient = httpClient;
}
public String sum(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/sum/" + number1 + "/" + number2);
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
public String subtract(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/subtract/" + number1 + "/" + number2);
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
public String multiply(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/multiply/" + number1 + "/" + number2);
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
public String divide(float number1, float number2) {
HttpRequest<String> req = HttpRequest.GET("/math/divide/" + number1 + "/" + number2);
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
public String memory() {
HttpRequest<String> req = HttpRequest.GET("/math/memory");
return httpClient.retrieve(req)
.blockingFirst();
return Single.fromPublisher(httpClient.retrieve(req))
.blockingGet();
}
}

View File

@ -3,7 +3,7 @@ package com.baeldung.micronaut.vs.springboot.controller;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import javax.inject.Inject;
import jakarta.inject.Inject;
import com.baeldung.micronaut.vs.springboot.service.ArithmeticService;

View File

@ -1,6 +1,6 @@
package com.baeldung.micronaut.vs.springboot.service;
import javax.inject.Singleton;
import jakarta.inject.Singleton;
@Singleton
public class ArithmeticService {

View File

@ -1,5 +1,19 @@
context-path: /
micronaut:
application:
name: hello-world-server
server:
port: ${random.port}
router:
versioning:
enabled: true
default-version: 2
parameter:
enabled: true
names: 'v,api-version'
header:
enabled: true
names:
- 'X-API-VERSION'
application:
name: hello-world-server
server:
port: ${random.port}

View File

@ -8,6 +8,12 @@
</encoder>
</appender>
<!-- Uncomment these lines to see the request logs -->
<!--
<logger name="io.micronaut.http.client" level="TRACE" />
<logger name="io.micronaut.http.server" level="DEBUG" />
-->
<root level="info">
<appender-ref ref="STDOUT" />
</root>

View File

@ -0,0 +1,42 @@
package com.baeldung.micronaut.apiversioning.custom.client;
import io.micronaut.context.annotation.Property;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@MicronautTest
@Property(name = "my.router.versioning.enabled", value = "true")
class BirdCountClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private BirdCountClient client;
@Test
void givenTheCountApi_whenUsingV1ViaCustomStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, client.countV1(null).blockingSingle().split(",").length);
Assertions.assertEquals(4, client.countV1(4).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingV2ViaCustomStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.countV2(null).count().blockingGet());
Assertions.assertEquals(6, client.countV2(6).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingDefaultVersionViaCustomStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.countDefault(null).count().blockingGet());
Assertions.assertEquals(6, client.countDefault(6).blockingSingle().split(",").length);
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.micronaut.apiversioning.header.client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@MicronautTest
class DogCountClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private DogCountClient dogCountClient;
@Test
void givenTheCountApi_whenUsingV1ViaHeaderStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, dogCountClient.countV1(null).blockingSingle().split(",").length);
Assertions.assertEquals(4, dogCountClient.countV1(4).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingV2ViaHeaderStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> dogCountClient.countV2(null).count().blockingGet());
Assertions.assertEquals(6, dogCountClient.countV2(6).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingDefaultVersionViaHeaderStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> dogCountClient.countDefault(null).count().blockingGet());
Assertions.assertEquals(6, dogCountClient.countDefault(6).blockingSingle().split(",").length);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.micronaut.apiversioning.param.client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@MicronautTest
class CatCountClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private CatCountClient client;
@Test
void givenTheCountApi_whenUsingV1ViaParameterStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, client.count(null, 1).blockingSingle().split(",").length);
Assertions.assertEquals(5, client.count(5, 1).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingV2ViaParameterStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.count(null, 2).count().blockingGet());
Assertions.assertEquals(6, client.count(6, 2).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingDefaultVersionViaParameterStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, client.count(null, null).blockingSingle().split(",").length);
Assertions.assertEquals(6, client.count(6, null).blockingSingle().split(",").length);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.micronaut.apiversioning.url.client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.stream.Collectors;
@MicronautTest
class SheepCountClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private SheepCountClient client;
@Test
void givenTheCountApi_whenUsingV1ViaUrlStrategy_shouldRouteToProperHandler() {
Assertions.assertEquals(10, client.countV1(null).blockingSingle().split(",").length);
Assertions.assertEquals(4, client.countV1(4).blockingSingle().split(",").length);
}
@Test
void givenTheCountApi_whenUsingV2ViaUrlStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.countV2(null).count().blockingGet());
final var actual = client.countV2(4).blockingSingle().split(",").length;
Assertions.assertEquals(4, actual);
}
@Test
void givenTheCountApi_whenUsingDefaultVersionViaUrlStrategy_shouldRouteToProperHandler() {
Assertions.assertThrows(HttpClientResponseException.class,
() -> client.countDefault(null).count().blockingGet());
final var actual = client.countDefault(4).blockingSingle().split(",").length;
Assertions.assertEquals(4, actual);
}
}

View File

@ -1,31 +1,19 @@
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import static junit.framework.TestCase.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConcreteGreetingClientUnitTest
{
private EmbeddedServer server;
@MicronautTest
public class ConcreteGreetingClientUnitTest {
@Inject
private EmbeddedApplication<?> application;
@Inject
private ConcreteGreetingClient client;
@Before
public void setup()
{
server = ApplicationContext.run(EmbeddedServer.class);
client = server.getApplicationContext().getBean(ConcreteGreetingClient.class);
}
@After
public void cleanup()
{
server.stop();
}
@Test
public void testGreeting() {
assertEquals(client.greet("Mike"), "Hello Mike");

View File

@ -1,30 +1,21 @@
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import static junit.framework.TestCase.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@MicronautTest
public class GreetingClientUnitTest {
private EmbeddedServer server;
@Inject
private EmbeddedApplication<?> application;
@Inject
private GreetingClient client;
@Before
public void setup()
{
server = ApplicationContext.run(EmbeddedServer.class);
client = server.getApplicationContext().getBean(GreetingClient.class);
}
@After
public void cleanup()
{
server.stop();
}
@Test
public void testGreeting() {
assertEquals(client.greet("Mike"), "Hello Mike");

View File

@ -1,34 +1,21 @@
package com.baeldung.micronaut.vs.springboot;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.micronaut.vs.springboot.client.ArithmeticClientImpl;
import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@MicronautTest
public class ArithmeticClientUnitTest {
private EmbeddedServer server;
@Inject
private EmbeddedApplication<?> server;
@Inject
private ArithmeticClientImpl client;
@Before
public void setup() {
server = ApplicationContext.run(EmbeddedServer.class);
client = server.getApplicationContext()
.getBean(ArithmeticClientImpl.class);
}
@After
public void cleanup() {
server.stop();
}
@Test
public void givenTwoNumbers_whenAdd_thenCorrectAnswerReturned() {
String expected = Float.valueOf(10 + 20).toString();
@ -56,6 +43,6 @@ public class ArithmeticClientUnitTest {
@Test
public void whenMemory_thenCorrectAnswerReturned() {
String expected = "Initial:";
assertThat(client.memory(), containsString(expected));
assertThat(client.memory()).contains(expected);
}
}