Tuesday, 3 January 2023

Test Micronaut app with Micronaut client

This is continuation to my previous post. In this post, I am going to test the Hello world app that we developed in previous post.

 

Micronaut HTTP Client

Micronaut provides a Http client, that helps in executing HTTP requests and receiving HTTP responses.

 

How to obtain a HTTP Client?

Using @Client annotation, we can obtain an instance of HTTP Client.

@Client("/")
HttpClient client;

 

Test class definition

@MicronautTest
public class HelloWorldControllerTest {

	@Inject
	EmbeddedServer server;

	@Inject
	@Client("/")
	HttpClient client;

	@Test
	void testHelloWorldResponse() {
		String response = client.toBlocking().retrieve(HttpRequest.GET("/hello"));
		assertEquals("Hello World", response);
	}
}

 

a.   EmbeddedServer is a general abstraction to manage the lifecycle of any server implementation within a running Micronaut application.

b.   Since /hello is a blocking API, The test uses the toBlocking() method to make a blocking call.

c.    The retrieve method returns the controller response as a String

 

Write the test in more declarative way

Define an interface annotated with @Client.

 

for example:

 

@Client("/hello")
public interface HelloWorldClient {

	@Get(consumes = MediaType.TEXT_PLAIN)
	@SingleResult
	Publisher<String> hello();
}

 

a.   @Client annotation is used with a value that is a relative path to the current server

b.   @Get annotation used on the server is used to define the client mapping

c.    Publisher annotated with SingleResult is returned with the value read from the server

 

Now, we can test the /hello api using HelloWorldClient instance.

@MicronautTest 
public class HelloWorldControllerTestDeclarative {
	@Inject
    HelloWorldClient client; 

    @Test
    public void testHelloWorldResponse(){
        assertEquals("Hello World", Mono.from(client.hello()).block());
    }
}

Follow below step-by-step procedure to build complete working application.

 

Step 1: Create new maven project ‘hello-world-micronaut-client’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>hello-world-micronaut-client</artifactId>
	<version>1</version>

	<parent>
		<groupId>io.micronaut</groupId>
		<artifactId>micronaut-parent</artifactId>
		<version>3.7.0</version>
	</parent>

	<properties>
		<packaging>jar</packaging>
		<jdk.version>11</jdk.version>
		<release.version>11</release.version>
		<micronaut.version>3.7.0</micronaut.version>
		<micronaut.runtime>netty</micronaut.runtime>
		<exec.mainClass>com.sample.app.App</exec.mainClass>
	</properties>

	<repositories>
		<repository>
			<id>central</id>
			<url>https://repo.maven.apache.org/maven2</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-validation</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-http-client</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-http-server-netty</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-jackson-databind</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>jakarta.annotation</groupId>
			<artifactId>jakarta.annotation-api</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut.test</groupId>
			<artifactId>micronaut-test-junit5</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>io.micronaut.build</groupId>
				<artifactId>micronaut-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Step 3: Define HelloWorldController class.

 

HelloWorldController.java

package com.sample.app.controller;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/hello")
public class HelloWorldController {

	@Get(produces = MediaType.TEXT_PLAIN)
	public String index() {
		return "Hello World";
	}
}

Step 4: Define main application class.

 

App.java

package com.sample.app;

import io.micronaut.runtime.Micronaut;

public class App {
	public static void main(String[] args) {
		Micronaut.run(App.class, args);
	}
}

Step 5: Define HelloWorldClient in src/test/java.

 

HelloWorldClient.java

package com.sample.app.client.interfaces;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.client.annotation.Client;
import org.reactivestreams.Publisher;
import io.micronaut.core.async.annotation.SingleResult;

@Client("/hello")
public interface HelloWorldClient {

	@Get(consumes = MediaType.TEXT_PLAIN)
	@SingleResult
	Publisher<String> hello();
}

Step 6: Define test classes.

 

HelloWorldControllerTest.java

package com.sample.app.controller;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import jakarta.inject.Inject;

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

@MicronautTest
public class HelloWorldControllerTest {

	@Inject
	EmbeddedServer server;

	@Inject
	@Client("/")
	HttpClient client;

	@Test
	void testHelloWorldResponse() {
		String response = client.toBlocking().retrieve(HttpRequest.GET("/hello"));
		assertEquals("Hello World", response);
	}
}

HelloWorldControllerTestDeclarative.java

package com.sample.app.controller;

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

import org.junit.jupiter.api.Test;

import com.sample.app.client.interfaces.HelloWorldClient;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import reactor.core.publisher.Mono;

@MicronautTest 
public class HelloWorldControllerTestDeclarative {
	@Inject
    HelloWorldClient client; 

    @Test
    public void testHelloWorldResponse(){
        assertEquals("Hello World", Mono.from(client.hello()).block());
    }
}

Total project structure looks like below.



How to run the test cases?

Navigate to the folder where pom.xml is located.

 

Execute below command to run the test class HelloWorldControllerTest

mvn -DTest=HelloWorldControllerTest test

 

Execute below command to run the test class HelloWorldControllerTestDeclarative

mvn -DTest=HelloWorldControllerTestDeclarative test

 

You can download the application from this link.


 

Previous                                                    Next                                                    Home

Monday, 2 January 2023

Micronaut hello world application in Eclipse

In this post, I am going to explain a hello world project in Eclipse.

 

Step 1: Create new maven project ‘hello-world-eclipse’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>hello-world-eclipse</artifactId>
	<version>1</version>

	<parent>
		<groupId>io.micronaut</groupId>
		<artifactId>micronaut-parent</artifactId>
		<version>3.7.0</version>
	</parent>

	<properties>
		<packaging>jar</packaging>
		<jdk.version>11</jdk.version>
		<release.version>11</release.version>
		<micronaut.version>3.7.0</micronaut.version>
		<micronaut.runtime>netty</micronaut.runtime>
		<exec.mainClass>com.sample.app.App</exec.mainClass>
	</properties>

	<repositories>
		<repository>
			<id>central</id>
			<url>https://repo.maven.apache.org/maven2</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-validation</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-http-client</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-http-server-netty</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-jackson-databind</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>jakarta.annotation</groupId>
			<artifactId>jakarta.annotation-api</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut.test</groupId>
			<artifactId>micronaut-test-junit5</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>io.micronaut.build</groupId>
				<artifactId>micronaut-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

<exec.mainClass> specifies main class to run the application.

 

Step 3: Define HelloWorldController class.

 

HelloWorldController.java

package com.sample.app.controller;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/hello")
public class HelloWorldController {

	@Get(produces = MediaType.TEXT_PLAIN)
	public String index() {
		return "Hello World";
	}
}

 

a.   The @Controller annotation defines the class as a controller mapped to the path /hello

b.   The @Get annotation maps the index method to all requests that use an HTTP GET

c.    A String "Hello World" is returned as the response

 

 

Step 4: Define main application class.

 

App.java

package com.sample.app;

import io.micronaut.runtime.Micronaut;

public class App {
	public static void main(String[] args) {
		Micronaut.run(App.class, args);
	}
}

 

Total project structure looks like below.

 


How to get the jar file?

Navigate to the folder where pom.xml is located. Execute the command ‘mvn clean install’.

 

Upon command execution, you can see a jar file ‘hello-world-eclipse-1.jar’ is created in target folder.

 

How to run the jar file?

Run the jar file by executing below command.

java -jar ./target/hello-world-eclipse-1.jar

 

Once the application started successfully, open the url ‘http://localhost:8080/hello’ in browser, you will see following kind of screen.

 


 

You can download this application from this link.

 

In my next post, I am going to explain how to test hello world app using Micronaut Http Client.


 

Previous                                                    Next                                                    Home

PlantUML : Create a sudoku

‘sudoku’ command draws a sudoku.

 

sudoku.txt

@startuml
sudoku
@enduml

 

Above snippet generate below diagram.

 


We can pass custom seed to the sudoku command.

 

sudokuCustomSeed.txt

@startuml
sudoku abcd12789eo0
@enduml

 

Above snippet generate below diagram.


 

 

Previous                                                    Next                                                    Home

Micronaut: Hello world application using Micronaut CLI

Prerequiste

Setup Micronaut CLI

 

Step 1: Open terminal and execute below command to create a Maven-based project.

mn create-app hello-world --build maven

 

Generated project structure looks like below.

bash-3.2$ tree
.
|____hello-world
| |____pom.xml
| |____README.md
| |____.gitignore
| |____.mvn
| | |____wrapper
| | | |____MavenWrapperDownloader.java
| | | |____maven-wrapper.properties
| | | |____maven-wrapper.jar
| |____mvnw
| |____micronaut-cli.yml
| |____mvnw.bat
| |____src
| | |____test
| | | |____java
| | | | |____hello
| | | | | |____world
| | | | | | |____HelloWorldTest.java
| | |____main
| | | |____resources
| | | | |____logback.xml
| | | | |____application.yml
| | | |____java
| | | | |____hello
| | | | | |____world
| | | | | | |____Application.java

 

Step 2: Navigate to hello-world folder (where pom.xml is located) and execute below command to run the application.

./mvnw mn:run

$ ./mvnw mn:run
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< hello.world:hello-world >-----------------------
[INFO] Building hello-world 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> micronaut-maven-plugin:3.4.0:run (default-cli) > process-classes @ hello-world >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ hello-world ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/krishna/Documents/technical-documents/micronaut/examples/hello-world/target/classes
[INFO] 
[INFO] <<< micronaut-maven-plugin:3.4.0:run (default-cli) < process-classes @ hello-world <<<
[INFO] 
[INFO] 
[INFO] --- micronaut-maven-plugin:3.4.0:run (default-cli) @ hello-world ---
 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.7.0)

16:28:50.547 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 633ms. Server Running: http://localhost:8080

 

Upon successful command execution, open the url in ‘http://localhost:8080’ in browser.

 



You can download this application from this link. In my next post, I will explain how to create a Rest API.

 

Previous                                                    Next                                                    Home

PlantUML: Hello world application

loginSuccessFlow.txt

@startuml
Client -> LoginServer: Enter user name
Client -> LoginServer: Enter password
Client -> LoginServer: Submit the form 
LoginServer -> ADServer: Check the credentials
ADServer -> LoginServer: Login Successful
LoginServer -> Client: Login Successful
@enduml

 

Execute below command to generate the sequence diagram.

java -jar plantuml.jar loginSuccessFlow.txt

 You can download plantuml.jar file from this location.

Upon successful command execution, you can see 'loginSuccessFlow.png' file.

 

loginSuccessFlow.png


 

 

Previous                                                    Next                                                    Home

PlantUML: setup the environment locally

Install graphviz

 

On Mac

brew install graphviz (or)

sudo port install graphviz

 

Ubuntu

sudo apt update

sudo apt install graphviz

Previous                                                    Next                                                    Home