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 vs Micronaut Framework 4 - database access #1358

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,46 @@
package example.micronaut;

import io.micronaut.serde.annotation.Serdeable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import java.util.UUID;

@Serdeable
@Entity
public class Coffee {

@Id
private String id;

private String name;


public Coffee() {
}

public Coffee(String id, String name) {
this.id = id;
this.name = name;
}

public Coffee(String name) {
this(UUID.randomUUID().toString(), name);
}

public String getId() {
return id;
}

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

public String getName() {
return name;
}

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

import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface CoffeeRepository extends CrudRepository<Coffee, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package example.micronaut;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.*;
import jakarta.annotation.PostConstruct;

import java.util.List;
import java.util.Optional;

@Controller
class RestApiDemoController {

private final CoffeeRepository coffeeRepository;

@PostConstruct
void loadData() {
coffeeRepository.saveAll(List.of(
new Coffee("Café Cereza"),
new Coffee("Café Ganador"),
new Coffee("Café Lareño"),
new Coffee("Café Três Pontas")
));
}

RestApiDemoController(CoffeeRepository coffeeRepository) {
this.coffeeRepository = coffeeRepository;
}

@Get("/coffees")
Iterable<Coffee> getCoffees() {
return coffeeRepository.findAll();
}

@Get("/coffees/{id}")
Optional<Coffee> getCoffeById(@PathVariable String id) {
return coffeeRepository.findById(id);
}

@Post("/coffees")
Coffee postCoffee(@Body Coffee coffee) {
coffeeRepository.save(coffee);
return coffee;
}

@Put("/coffees/{id}")
HttpResponse<Coffee> putCoffee(@PathVariable String id, @Body Coffee coffee) {
return coffeeRepository.existsById(id) ?
HttpResponse.ok(coffeeRepository.update(coffee)) :
HttpResponse.created(coffeeRepository.save(coffee));
}

@Delete("/coffees/{id}")
@Status(HttpStatus.NO_CONTENT)
void deleteCoffee(@PathVariable String id) {
coffeeRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
micronaut.application.name=micronautguide
datasources.default.password=
datasources.default.dialect=H2
datasources.default.url=jdbc\:h2\:mem\:devDb;LOCK_TIMEOUT\=10000;DB_CLOSE_ON_EXIT\=FALSE
jpa.default.properties.hibernate.hbm2ddl.auto=update
datasources.default.username=sa
datasources.default.driver-class-name=org.h2.Driver
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package example.micronaut;

import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
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 java.util.List;
import java.util.UUID;

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

@MicronautTest
class RestApiDemoControllerTest {
@Test
void findAllTest(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
HttpRequest<?> request = HttpRequest.GET("/coffees");
List<Coffee> coffeeList = assertDoesNotThrow(() -> client.retrieve(request, Argument.listOf(Coffee.class)));
assertNotNull(coffeeList);
assertEquals(4, coffeeList.size());
}

@Test
void findByIdTest(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
HttpRequest<?> request = HttpRequest.GET(UriBuilder.of("/coffees").path("99").build());
HttpClientResponseException ex = assertThrows(HttpClientResponseException.class, () -> client.retrieve(request));
assertNotNull(ex);
assertEquals(HttpStatus.NOT_FOUND, ex.getStatus());


List<Coffee> coffeeList = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET("/coffees"), Argument.listOf(Coffee.class)));
assertNotNull(coffeeList);
assertEquals(4, coffeeList.size());

HttpRequest<?> findByIdRequest = HttpRequest.GET(UriBuilder.of("/coffees")
.path(coffeeList.get(0).getId())
.build());

HttpResponse<Coffee> coffee = assertDoesNotThrow(() -> client.exchange(findByIdRequest, Coffee.class));
assertNotNull(coffee);
assertTrue(coffee.getBody().isPresent());
assertEquals(coffeeList.get(0).getName(), coffee.getBody().get().getName());
}

@Test
void postTest(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
HttpRequest<?> request = HttpRequest.POST("/coffees", new Coffee("Café Olé"));
HttpResponse<Coffee> coffeeHttpResponse = assertDoesNotThrow(() -> client.exchange(request, Coffee.class));
assertNotNull(coffeeHttpResponse);
assertEquals(HttpStatus.OK, coffeeHttpResponse.getStatus());
assertTrue(coffeeHttpResponse.getBody().isPresent());
Coffee coffee = coffeeHttpResponse.getBody().get();
assertEquals("Café Olé", coffee.getName());

List<Coffee> coffeeList = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET("/coffees"), Argument.listOf(Coffee.class)));
assertNotNull(coffeeList);
assertEquals(5, coffeeList.size());

HttpRequest<?> deleteRequest = HttpRequest.DELETE(UriBuilder.of("/coffees")
.path(coffee.getId())
.build());

HttpResponse<?> deleteResponse = assertDoesNotThrow(() -> client.exchange(deleteRequest));
assertNotNull(deleteResponse);
assertEquals(HttpStatus.NO_CONTENT, deleteResponse.getStatus());

coffeeList = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET("/coffees"), Argument.listOf(Coffee.class)));
assertNotNull(coffeeList);
assertEquals(4, coffeeList.size());
}

@Test
void putTest(@Client("/") HttpClient httpClient) {
BlockingHttpClient client = httpClient.toBlocking();
String id = UUID.randomUUID().toString();
URI path = UriBuilder.of("/coffees").path(id).build();
HttpRequest<?> request = HttpRequest.PUT(path, new Coffee(id, "Café Astro"));
HttpResponse<Coffee> coffeeHttpResponse = assertDoesNotThrow(() -> client.exchange(request, Coffee.class));
assertNotNull(coffeeHttpResponse);
assertEquals(HttpStatus.CREATED, coffeeHttpResponse.getStatus());
assertTrue(coffeeHttpResponse.getBody().isPresent());
Coffee coffee = coffeeHttpResponse.getBody().get();
assertEquals("Café Astro", coffee.getName());

List<Coffee> coffeeList = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET("/coffees"), Argument.listOf(Coffee.class)));
assertNotNull(coffeeList);
assertEquals(5, coffeeList.size());

HttpRequest<?> updateRequest = HttpRequest.PUT(path, new Coffee(coffee.getId(), "Café Alien"));
coffeeHttpResponse = assertDoesNotThrow(() -> client.exchange(updateRequest, Coffee.class));
assertNotNull(coffeeHttpResponse);
assertEquals(HttpStatus.OK, coffeeHttpResponse.getStatus());
assertTrue(coffeeHttpResponse.getBody().isPresent());
coffee = coffeeHttpResponse.getBody().get();
assertEquals("Café Alien", coffee.getName());

coffeeList = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET("/coffees"), Argument.listOf(Coffee.class)));
assertNotNull(coffeeList);
assertEquals(5, coffeeList.size());

HttpRequest<?> deleteRequest = HttpRequest.DELETE(UriBuilder.of("/coffees")
.path(coffee.getId())
.build());

HttpResponse<?> deleteResponse = assertDoesNotThrow(() -> client.exchange(deleteRequest));
assertNotNull(deleteResponse);
assertEquals(HttpStatus.NO_CONTENT, deleteResponse.getStatus());

coffeeList = assertDoesNotThrow(() -> client.retrieve(HttpRequest.GET("/coffees"), Argument.listOf(Coffee.class)));
assertNotNull(coffeeList);
assertEquals(4, coffeeList.size());
}
}
Loading