Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Introduce layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Ueberfuhr committed Jun 19, 2024
1 parent d4aec2e commit 9b42f89
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 37 deletions.
17 changes: 17 additions & 0 deletions customer-api-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<skipITs>true</skipITs>
<surefire-plugin.version>3.2.5</surefire-plugin.version>
<lombok.version>1.18.32</lombok.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -45,6 +47,11 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
Expand Down Expand Up @@ -92,6 +99,16 @@
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.schulung.sample.quarkus;
package de.schulung.sample.quarkus.boundary;

import jakarta.json.bind.annotation.JsonbProperty;
import jakarta.json.bind.annotation.JsonbTransient;
Expand All @@ -17,7 +17,7 @@
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
public class CustomerDto {

// readonly property
@Setter(onMethod_ = @JsonbTransient)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.schulung.sample.quarkus.boundary;

import de.schulung.sample.quarkus.domain.Customer;
import org.mapstruct.Mapper;

@Mapper(componentModel = "cdi")
public interface CustomerDtoMapper {

Customer map(CustomerDto source);

CustomerDto map(Customer source);

default String mapState(Customer.CustomerState source) {
return null == source ? null : switch (source) {
case ACTIVE -> "active";
case LOCKED -> "locked";
case DISABLED -> "disabled";
};
}

default Customer.CustomerState mapState(String source) {
return null == source ? null : switch (source) {
case "active" -> Customer.CustomerState.ACTIVE;
case "locked" -> Customer.CustomerState.LOCKED;
case "disabled" -> Customer.CustomerState.DISABLED;
default -> throw new IllegalArgumentException(source + " is not allowed here.");
};
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.schulung.sample.quarkus;
package de.schulung.sample.quarkus.boundary;

import de.schulung.sample.quarkus.domain.CustomersService;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
Expand All @@ -15,19 +16,22 @@
public class CustomersResource {

private final CustomersService service;
private final CustomerDtoMapper mapper;

@GET
@Produces(MediaType.APPLICATION_JSON)
public Collection<Customer> getCustomers() {
public Collection<CustomerDto> getCustomers() {
return service
.getAll()
.map(mapper::map)
.toList();
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createCustomer(@Valid Customer customer) {
public Response createCustomer(@Valid CustomerDto dto) {
var customer = mapper.map(dto);
service.createCustomer(customer);
final var location = UriBuilder.fromResource(CustomersResource.class)
.path(CustomersResource.class, "findCustomerById")
Expand All @@ -41,17 +45,18 @@ public Response createCustomer(@Valid Customer customer) {
*/
return Response
.created(location)
.entity(customer)
.entity(mapper.map(customer))
.build();
}

// Exceptions: https://cristian.sulea.net/blog/rest-java-jax-rs-exception-handling/

@GET
@Path("/{uuid}")
public Customer findCustomerById(@PathParam("uuid") UUID uuid) {
public CustomerDto findCustomerById(@PathParam("uuid") UUID uuid) {
return service.getByUuid(uuid)
.orElseThrow(NotFoundException::new);
.map(mapper::map)
.orElseThrow(NotFoundException::new);
}

@DELETE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.schulung.sample.quarkus.domain;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;
import java.util.UUID;

@Getter
@Setter
@Builder
public class Customer {

private UUID uuid;
@Size(min = 3, max = 100)
@NotNull
private String name;
private LocalDate birthdate;
@NotNull
private CustomerState state = CustomerState.ACTIVE;

public enum CustomerState {
ACTIVE, LOCKED, DISABLED
}


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package de.schulung.sample.quarkus;
package de.schulung.sample.quarkus.domain;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;

import java.time.LocalDate;
import java.time.Month;
Expand All @@ -19,12 +18,11 @@ public class CustomersService {
private final Map<UUID, Customer> customers = new HashMap<>();

{ // TODO replace this?
var customer = new Customer(
UUID.randomUUID(),
"Tom",
LocalDate.of(2000, Month.DECEMBER, 6),
"active"
);
var customer = Customer.builder()
.uuid(UUID.randomUUID())
.name("Tom")
.birthdate(LocalDate.of(2000, Month.DECEMBER, 6))
.build();
customers.put(customer.getUuid(), customer);
}

Expand All @@ -34,13 +32,9 @@ public Stream<Customer> getAll() {
.stream();
}

public Stream<Customer> getByState(
@NotNull
@Pattern(regexp = "active|locked|disabled")
String state
) {
public Stream<Customer> getByState(@NotNull Customer.CustomerState state) {
return this.getAll()
.filter(c -> c.getState().equals(state));
.filter(c -> c.getState() == state);
}

public void createCustomer(@Valid Customer customer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.schulung.sample.quarkus;
package de.schulung.sample.quarkus.boundary;

import de.schulung.sample.quarkus.domain.CustomersService;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.schulung.sample.quarkus;
package de.schulung.sample.quarkus.boundary;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.schulung.sample.quarkus;
package de.schulung.sample.quarkus.domain;

import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
Expand All @@ -12,11 +12,9 @@ public class CustomersServiceIntegrationTests {
@Inject
CustomersService service;

// TODO Testfall: Customer anlegen mit zu kurzem Namen -> Validierungsfehler

@Test
void shouldNotCreateInvalidCustomer() {
var customer = new Customer();
var customer = Customer.builder().build();
assertThatThrownBy(() -> service.createCustomer(customer))
.isNotNull();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.schulung.sample.quarkus;
package de.schulung.sample.quarkus.domain;

import org.junit.jupiter.api.Test;

Expand All @@ -13,10 +13,10 @@ class CustomersServiceTests {

@Test
void shouldAddUuidToNewCustomer() {
var customer = new Customer();
customer.setName("Tom");
customer.setBirthdate(LocalDate.of(2000, Month.FEBRUARY, 2));
customer.setState("active");
var customer = Customer.builder()
.name("Tom")
.birthdate(LocalDate.of(2000, Month.FEBRUARY, 2))
.build();

service.createCustomer(customer);

Expand All @@ -26,10 +26,10 @@ void shouldAddUuidToNewCustomer() {

@Test
void shouldFindNewCustomer() {
var customer = new Customer();
customer.setName("Tom");
customer.setBirthdate(LocalDate.of(2000, Month.FEBRUARY, 2));
customer.setState("active");
var customer = Customer.builder()
.name("Tom")
.birthdate(LocalDate.of(2000, Month.FEBRUARY, 2))
.build();
service.createCustomer(customer);
var uuid = customer.getUuid();

Expand Down

0 comments on commit 9b42f89

Please sign in to comment.