Bael 6917/api versioning micronaut (#14868)
* BAEL-6917: Micronaut API Versioning * Fix test names
This commit is contained in:
committed by
GitHub
parent
7e8f6ef9c4
commit
3ecaaaec28
+42
@@ -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);
|
||||
}
|
||||
+36
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -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);
|
||||
}
|
||||
+36
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -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);
|
||||
}
|
||||
+37
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -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);
|
||||
}
|
||||
+26
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+7
-8
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -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 {
|
||||
|
||||
+1
-1
@@ -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 {
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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 {
|
||||
|
||||
+4
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
-15
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package com.baeldung.micronaut.vs.springboot.service;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class ArithmeticService {
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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>
|
||||
|
||||
+42
@@ -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);
|
||||
}
|
||||
}
|
||||
+40
@@ -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);
|
||||
}
|
||||
}
|
||||
+38
@@ -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);
|
||||
}
|
||||
}
|
||||
+44
@@ -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);
|
||||
}
|
||||
}
|
||||
+10
-22
@@ -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");
|
||||
|
||||
+11
-20
@@ -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");
|
||||
|
||||
+12
-25
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user