Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring boot to Micronaut Framework Configuration #1360

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package example.micronaut;

import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public class Droid {

private String id;
private String description;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package example.micronaut;

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

@Controller("/droid")
class DroidController {

private final Droid droid;

DroidController(Droid droid) {
this.droid = droid;
}

@Get
Droid getDroid() {
return droid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package example.micronaut;

import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.context.annotation.Factory;

@Factory
public class DroidFactory {
@ConfigurationProperties("droid")
Droid create() {
return new Droid();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package example.micronaut;

import io.micronaut.context.annotation.ConfigurationProperties;

@ConfigurationProperties("greeting")
public class Greeting {

private String name;
private String coffee;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCoffee() {
return coffee;
}

public void setCoffee(String coffee) {
this.coffee = coffee;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package example.micronaut;

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

@Controller("/greet")
class GreetingConfigurationPropertiesController {

private final Greeting greeting;

GreetingConfigurationPropertiesController(Greeting greeting) {
this.greeting = greeting;
}

@Produces(MediaType.TEXT_PLAIN)
@Get("/name")
String getGreeting() {
return greeting.getName();
}

@Produces(MediaType.TEXT_PLAIN)
@Get("/coffee")
String getCoffee() {
return greeting.getCoffee();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package example.micronaut;

import io.micronaut.context.annotation.Value;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;

@Controller("/greeting")
class GreetingController {

@Value("${greeting-name:Mirage}")
protected String name;

@Value("${greeting-coffee:${greeting-name} is drinking Café Ganador}")
protected String coffee;

@Produces(MediaType.TEXT_PLAIN)
@Get
String getGreeting() {
return name;
}

@Produces(MediaType.TEXT_PLAIN)
@Get("/coffee")
String getCoffee() {
return coffee;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package example.micronaut;

import io.micronaut.context.annotation.Property;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

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

@Property(name = "droid.id", value = "BB-8")
@Property(name = "droid.description", value = "Small, rolling android. Probably does not drink coffee")
@MicronautTest
class DroidControllerTest {

@Disabled
@Test
void droidViaFactory(@Client("/")HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
HttpRequest<?> request = HttpRequest.GET("/droid");
Droid droid = assertDoesNotThrow(() -> client.retrieve(request, Droid.class));
assertNotNull(droid);
assertEquals("BB-8", droid.getId());
assertEquals("Small, rolling android. Probably does not drink coffee", droid.getDescription());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package example.micronaut;

import io.micronaut.context.annotation.Property;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.uri.UriBuilder;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import java.net.URI;

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

@Property(name = "greeting.name", value = "Dakota")
@Property(name = "greeting.coffee", value = "Dakota is drinking Café Cereza")
@MicronautTest
class GreetingConfigurationPropertiesControllerTest {

@Test
void defaultValue(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
URI uri = UriBuilder.of("/greet").path("name").build();

String txt = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET(uri).accept(MediaType.TEXT_PLAIN)));
assertEquals("Dakota", txt);
}

@Test
void derivedProperties(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
URI uri = UriBuilder.of("/greet").path("coffee").build();
String txt = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET(uri).accept(MediaType.TEXT_PLAIN)));
assertEquals("Dakota is drinking Café Cereza", txt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package example.micronaut;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.uri.UriBuilder;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.net.URI;

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

@MicronautTest
class GreetingControllerDefaultValueTest {

@Test
void defaultValue(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
String txt = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET("/greeting").accept(MediaType.TEXT_PLAIN)));
assertEquals("Mirage", txt);
}

@Disabled
@Test
void derivedProperties(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
URI uri = UriBuilder.of("/greeting").path("coffee").build();
String txt = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET(uri).accept(MediaType.TEXT_PLAIN)));
assertEquals("Mirage is drinking Café Ganador", txt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package example.micronaut;

import io.micronaut.context.annotation.Property;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.uri.UriBuilder;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.net.URI;

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

@Property(name = "greeting-name", value = "Dakota")
@Property(name = "greeting-coffee", value = "Dakota is drinking Café Cereza")
@MicronautTest
class GreetingControllerTest {

@Test
void defaultValue(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
String txt = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET("/greeting").accept(MediaType.TEXT_PLAIN)));
assertEquals("Dakota", txt);
}

@Disabled
@Test
void derivedProperties(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
URI uri = UriBuilder.of("/greeting").path("coffee").build();
String txt = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET(uri).accept(MediaType.TEXT_PLAIN)));
assertEquals("Dakota is drinking Café Cereza", txt);
}
}
Loading