From b4969dd2b8b635d42d1080154bcbcc9f1b2c70ee Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Fri, 6 Jul 2018 13:49:23 +0000 Subject: [PATCH] Rename directory, new concerete client example --- .../controller/AsyncGreetController.java | 20 --------- .../.mvn/jvm.config | 0 .../.mvn/wrapper/MavenWrapperDownloader.java | 0 .../.mvn/wrapper/maven-wrapper.jar | Bin .../.mvn/wrapper/maven-wrapper.properties | 0 .../Dockerfile | 0 .../micronaut-cli.yml | 0 .../{hello-world-server => hello-world}/mvnw | 0 .../mvnw.cmd | 0 .../pom.xml | 8 ++-- .../client/ConcreteGreetingClient.java | 28 +++++++++++++ .../helloworld}/client/GreetingClient.java | 6 ++- .../helloworld}/server/ServerApplication.java | 2 +- .../controller/AsyncGreetController.java | 20 +++++++++ .../server/controller/GreetController.java | 4 +- .../service/EnglishGreetingService.java | 2 +- .../server/service/GreetingService.java | 2 +- .../service/SpanishGreetingService.java | 2 +- .../src/main/resources/application.yml | 2 +- .../src/main/resources/logback.xml | 0 .../client/ConcreteGreetingClientTest.java | 38 ++++++++++++++++++ .../client/GreetingClientTest.java | 19 +++++---- 22 files changed, 111 insertions(+), 42 deletions(-) delete mode 100644 micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/controller/AsyncGreetController.java rename micronaut/{hello-world-server => hello-world}/.mvn/jvm.config (100%) rename micronaut/{hello-world-server => hello-world}/.mvn/wrapper/MavenWrapperDownloader.java (100%) rename micronaut/{hello-world-server => hello-world}/.mvn/wrapper/maven-wrapper.jar (100%) rename micronaut/{hello-world-server => hello-world}/.mvn/wrapper/maven-wrapper.properties (100%) rename micronaut/{hello-world-server => hello-world}/Dockerfile (100%) rename micronaut/{hello-world-server => hello-world}/micronaut-cli.yml (100%) rename micronaut/{hello-world-server => hello-world}/mvnw (100%) rename micronaut/{hello-world-server => hello-world}/mvnw.cmd (100%) rename micronaut/{hello-world-server => hello-world}/pom.xml (95%) create mode 100644 micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/client/ConcreteGreetingClient.java rename micronaut/{hello-world-server/src/main/java/com/baeldung/micronaut => hello-world/src/main/java/com/baeldung/micronaut/helloworld}/client/GreetingClient.java (59%) rename micronaut/{hello-world-server/src/main/java/com/baeldung/micronaut => hello-world/src/main/java/com/baeldung/micronaut/helloworld}/server/ServerApplication.java (77%) create mode 100644 micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/controller/AsyncGreetController.java rename micronaut/{hello-world-server/src/main/java/com/baeldung/micronaut => hello-world/src/main/java/com/baeldung/micronaut/helloworld}/server/controller/GreetController.java (82%) rename micronaut/{hello-world-server/src/main/java/com/baeldung/micronaut => hello-world/src/main/java/com/baeldung/micronaut/helloworld}/server/service/EnglishGreetingService.java (81%) rename micronaut/{hello-world-server/src/main/java/com/baeldung/micronaut => hello-world/src/main/java/com/baeldung/micronaut/helloworld}/server/service/GreetingService.java (52%) rename micronaut/{hello-world-server/src/main/java/com/baeldung/micronaut => hello-world/src/main/java/com/baeldung/micronaut/helloworld}/server/service/SpanishGreetingService.java (76%) rename micronaut/{hello-world-server => hello-world}/src/main/resources/application.yml (80%) rename micronaut/{hello-world-server => hello-world}/src/main/resources/logback.xml (100%) create mode 100644 micronaut/hello-world/src/test/java/com/baeldung/micronaut/helloworld/client/ConcreteGreetingClientTest.java rename micronaut/{hello-world-server/src/test/java/com/baeldung/micronaut => hello-world/src/test/java/com/baeldung/micronaut/helloworld}/client/GreetingClientTest.java (56%) diff --git a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/controller/AsyncGreetController.java b/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/controller/AsyncGreetController.java deleted file mode 100644 index dcc64eb26e..0000000000 --- a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/controller/AsyncGreetController.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.micronaut.server.controller; - -import com.baeldung.micronaut.server.service.GreetingService; -import io.micronaut.http.annotation.Controller; -import io.micronaut.http.annotation.Get; -import reactor.core.publisher.Mono; - -import javax.inject.Inject; - -@Controller("/async/greet") -public class AsyncGreetController { - - @Inject - private GreetingService greetingService; - - @Get("/{name}") - public Mono greet(String name) { - return Mono.just(greetingService.getGreeting() + name); - } -} diff --git a/micronaut/hello-world-server/.mvn/jvm.config b/micronaut/hello-world/.mvn/jvm.config similarity index 100% rename from micronaut/hello-world-server/.mvn/jvm.config rename to micronaut/hello-world/.mvn/jvm.config diff --git a/micronaut/hello-world-server/.mvn/wrapper/MavenWrapperDownloader.java b/micronaut/hello-world/.mvn/wrapper/MavenWrapperDownloader.java similarity index 100% rename from micronaut/hello-world-server/.mvn/wrapper/MavenWrapperDownloader.java rename to micronaut/hello-world/.mvn/wrapper/MavenWrapperDownloader.java diff --git a/micronaut/hello-world-server/.mvn/wrapper/maven-wrapper.jar b/micronaut/hello-world/.mvn/wrapper/maven-wrapper.jar similarity index 100% rename from micronaut/hello-world-server/.mvn/wrapper/maven-wrapper.jar rename to micronaut/hello-world/.mvn/wrapper/maven-wrapper.jar diff --git a/micronaut/hello-world-server/.mvn/wrapper/maven-wrapper.properties b/micronaut/hello-world/.mvn/wrapper/maven-wrapper.properties similarity index 100% rename from micronaut/hello-world-server/.mvn/wrapper/maven-wrapper.properties rename to micronaut/hello-world/.mvn/wrapper/maven-wrapper.properties diff --git a/micronaut/hello-world-server/Dockerfile b/micronaut/hello-world/Dockerfile similarity index 100% rename from micronaut/hello-world-server/Dockerfile rename to micronaut/hello-world/Dockerfile diff --git a/micronaut/hello-world-server/micronaut-cli.yml b/micronaut/hello-world/micronaut-cli.yml similarity index 100% rename from micronaut/hello-world-server/micronaut-cli.yml rename to micronaut/hello-world/micronaut-cli.yml diff --git a/micronaut/hello-world-server/mvnw b/micronaut/hello-world/mvnw similarity index 100% rename from micronaut/hello-world-server/mvnw rename to micronaut/hello-world/mvnw diff --git a/micronaut/hello-world-server/mvnw.cmd b/micronaut/hello-world/mvnw.cmd similarity index 100% rename from micronaut/hello-world-server/mvnw.cmd rename to micronaut/hello-world/mvnw.cmd diff --git a/micronaut/hello-world-server/pom.xml b/micronaut/hello-world/pom.xml similarity index 95% rename from micronaut/hello-world-server/pom.xml rename to micronaut/hello-world/pom.xml index 8d42259776..56d051e506 100644 --- a/micronaut/hello-world-server/pom.xml +++ b/micronaut/hello-world/pom.xml @@ -1,17 +1,17 @@ 4.0.0 - hello.world.server - hello-world-server + com.baeldung.micronaut + hello-world 0.1 - com.baeldung.micronaut.server.ServerApplication + com.baeldung.micronaut.helloworld.server.ServerApplication 1.0.0.M2 9 jcenter.bintray.com - https://jcenter.bintray.com + http://jcenter.bintray.com diff --git a/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/client/ConcreteGreetingClient.java b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/client/ConcreteGreetingClient.java new file mode 100644 index 0000000000..d4051cef52 --- /dev/null +++ b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/client/ConcreteGreetingClient.java @@ -0,0 +1,28 @@ +package com.baeldung.micronaut.helloworld.client; + +import io.micronaut.http.HttpRequest; +import io.micronaut.http.client.Client; +import io.micronaut.http.client.RxHttpClient; +import io.reactivex.Single; + +import javax.inject.Singleton; + +@Singleton +public class ConcreteGreetingClient +{ + private RxHttpClient httpClient; + + public ConcreteGreetingClient(@Client("/") RxHttpClient httpClient) { + this.httpClient = httpClient; + } + + public String greet(String name) { + HttpRequest req = HttpRequest.GET("/greet/" + name); + return httpClient.retrieve(req).blockingFirst(); + } + + public Single greetAsync(String name) { + HttpRequest req = HttpRequest.GET("/async/greet/" + name); + return httpClient.retrieve(req).first("An error as occurred"); + } +} diff --git a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/client/GreetingClient.java b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/client/GreetingClient.java similarity index 59% rename from micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/client/GreetingClient.java rename to micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/client/GreetingClient.java index de61ecd5f3..8a691d5b06 100644 --- a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/client/GreetingClient.java +++ b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/client/GreetingClient.java @@ -1,10 +1,14 @@ -package com.baeldung.micronaut.client; +package com.baeldung.micronaut.helloworld.client; import io.micronaut.http.annotation.Get; import io.micronaut.http.client.Client; +import io.micronaut.http.client.RxHttpClient; + +import javax.inject.Inject; @Client("/greet") public interface GreetingClient { + @Get("/{name}") String greet(String name); } diff --git a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/ServerApplication.java b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/ServerApplication.java similarity index 77% rename from micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/ServerApplication.java rename to micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/ServerApplication.java index 905acc8433..c5936051dd 100644 --- a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/ServerApplication.java +++ b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/ServerApplication.java @@ -1,4 +1,4 @@ -package com.baeldung.micronaut.server; +package com.baeldung.micronaut.helloworld.server; import io.micronaut.runtime.Micronaut; diff --git a/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/controller/AsyncGreetController.java b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/controller/AsyncGreetController.java new file mode 100644 index 0000000000..4d86b9dfed --- /dev/null +++ b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/controller/AsyncGreetController.java @@ -0,0 +1,20 @@ +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; + +@Controller("/async/greet") +public class AsyncGreetController { + + @Inject + private GreetingService greetingService; + + @Get("/{name}") + public Single greet(String name) { + return Single.just(greetingService.getGreeting() + name); + } +} diff --git a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/controller/GreetController.java b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/controller/GreetController.java similarity index 82% rename from micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/controller/GreetController.java rename to micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/controller/GreetController.java index 64a499ce52..c890c037e4 100644 --- a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/controller/GreetController.java +++ b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/controller/GreetController.java @@ -1,6 +1,6 @@ -package com.baeldung.micronaut.server.controller; +package com.baeldung.micronaut.helloworld.server.controller; -import com.baeldung.micronaut.server.service.GreetingService; +import com.baeldung.micronaut.helloworld.server.service.GreetingService; import io.micronaut.http.MediaType; import io.micronaut.http.annotation.Body; import io.micronaut.http.annotation.Controller; diff --git a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/EnglishGreetingService.java b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/EnglishGreetingService.java similarity index 81% rename from micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/EnglishGreetingService.java rename to micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/EnglishGreetingService.java index 81cc18c0ff..8ea5172cf6 100644 --- a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/EnglishGreetingService.java +++ b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/EnglishGreetingService.java @@ -1,4 +1,4 @@ -package com.baeldung.micronaut.server.service; +package com.baeldung.micronaut.helloworld.server.service; import io.micronaut.context.annotation.Primary; diff --git a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/GreetingService.java b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/GreetingService.java similarity index 52% rename from micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/GreetingService.java rename to micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/GreetingService.java index a27957566a..6e28baaebe 100644 --- a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/GreetingService.java +++ b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/GreetingService.java @@ -1,4 +1,4 @@ -package com.baeldung.micronaut.server.service; +package com.baeldung.micronaut.helloworld.server.service; public interface GreetingService { diff --git a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/SpanishGreetingService.java b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/SpanishGreetingService.java similarity index 76% rename from micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/SpanishGreetingService.java rename to micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/SpanishGreetingService.java index 6ce6bfe8df..1ec53d8b2d 100644 --- a/micronaut/hello-world-server/src/main/java/com/baeldung/micronaut/server/service/SpanishGreetingService.java +++ b/micronaut/hello-world/src/main/java/com/baeldung/micronaut/helloworld/server/service/SpanishGreetingService.java @@ -1,4 +1,4 @@ -package com.baeldung.micronaut.server.service; +package com.baeldung.micronaut.helloworld.server.service; import javax.inject.Singleton; diff --git a/micronaut/hello-world-server/src/main/resources/application.yml b/micronaut/hello-world/src/main/resources/application.yml similarity index 80% rename from micronaut/hello-world-server/src/main/resources/application.yml rename to micronaut/hello-world/src/main/resources/application.yml index 03d8f4fa1a..4119026dd6 100644 --- a/micronaut/hello-world-server/src/main/resources/application.yml +++ b/micronaut/hello-world/src/main/resources/application.yml @@ -2,4 +2,4 @@ micronaut: application: name: hello-world-server server: - port: 8080 \ No newline at end of file + port: 9080 \ No newline at end of file diff --git a/micronaut/hello-world-server/src/main/resources/logback.xml b/micronaut/hello-world/src/main/resources/logback.xml similarity index 100% rename from micronaut/hello-world-server/src/main/resources/logback.xml rename to micronaut/hello-world/src/main/resources/logback.xml diff --git a/micronaut/hello-world/src/test/java/com/baeldung/micronaut/helloworld/client/ConcreteGreetingClientTest.java b/micronaut/hello-world/src/test/java/com/baeldung/micronaut/helloworld/client/ConcreteGreetingClientTest.java new file mode 100644 index 0000000000..c8c1d6b12a --- /dev/null +++ b/micronaut/hello-world/src/test/java/com/baeldung/micronaut/helloworld/client/ConcreteGreetingClientTest.java @@ -0,0 +1,38 @@ +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 static junit.framework.TestCase.assertEquals; + +public class ConcreteGreetingClientTest +{ + private EmbeddedServer server; + 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"); + } + + @Test + public void testGreetingAsync() { + assertEquals(client.greetAsync("Mike").blockingGet(), "Hello Mike"); + } +} diff --git a/micronaut/hello-world-server/src/test/java/com/baeldung/micronaut/client/GreetingClientTest.java b/micronaut/hello-world/src/test/java/com/baeldung/micronaut/helloworld/client/GreetingClientTest.java similarity index 56% rename from micronaut/hello-world-server/src/test/java/com/baeldung/micronaut/client/GreetingClientTest.java rename to micronaut/hello-world/src/test/java/com/baeldung/micronaut/helloworld/client/GreetingClientTest.java index 0c1634d01d..0f2ca460ac 100644 --- a/micronaut/hello-world-server/src/test/java/com/baeldung/micronaut/client/GreetingClientTest.java +++ b/micronaut/hello-world/src/test/java/com/baeldung/micronaut/helloworld/client/GreetingClientTest.java @@ -1,4 +1,4 @@ -package com.baeldung.micronaut.client; +package com.baeldung.micronaut.helloworld.client; import io.micronaut.context.ApplicationContext; import io.micronaut.runtime.server.EmbeddedServer; @@ -15,19 +15,18 @@ public class GreetingClientTest { @Before public void setup() { - this.server = ApplicationContext.run(EmbeddedServer.class); - this.client = server.getApplicationContext().getBean(GreetingClient.class); - } - - @Test - public void shouldReturnName() { - String response = client.greet("Mike"); - assertEquals(response, "Hello Mike"); + server = ApplicationContext.run(EmbeddedServer.class); + client = server.getApplicationContext().getBean(GreetingClient.class); } @After public void cleanup() { - this.server.stop(); + server.stop(); + } + + @Test + public void testGreeting() { + assertEquals(client.greet("Mike"), "Hello Mike"); } }