This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ralf-ueberfuhr-ars/feature/service
Split JAX-RS resource and service (separation of concerns)
- Loading branch information
Showing
7 changed files
with
233 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
customer-api-provider/src/main/java/de/schulung/sample/quarkus/CustomersService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package de.schulung.sample.quarkus; | ||
|
||
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; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
@ApplicationScoped | ||
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" | ||
); | ||
customers.put(customer.getUuid(), customer); | ||
} | ||
|
||
public Stream<Customer> getAll() { | ||
return this.customers | ||
.values() | ||
.stream(); | ||
} | ||
|
||
public Stream<Customer> getByState( | ||
@NotNull | ||
@Pattern(regexp = "active|locked|disabled") | ||
String state | ||
) { | ||
return this.getAll() | ||
.filter(c -> c.getState().equals(state)); | ||
} | ||
|
||
public void createCustomer(@Valid Customer customer) { | ||
customer.setUuid(UUID.randomUUID()); | ||
customers.put(customer.getUuid(), customer); | ||
} | ||
|
||
public Optional<Customer> getByUuid(@NotNull UUID uuid) { | ||
return Optional.ofNullable(customers.get(uuid)); | ||
} | ||
|
||
public boolean exists(@NotNull UUID uuid) { | ||
return customers.containsKey(uuid); | ||
} | ||
|
||
public boolean delete(@NotNull UUID uuid) { | ||
return customers.remove(uuid) != null; | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...-api-provider/src/test/java/de/schulung/sample/quarkus/CustomerApiMockedServiceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package de.schulung.sample.quarkus; | ||
|
||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.http.ContentType; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.mockito.Mockito.*; | ||
|
||
@QuarkusTest | ||
public class CustomerApiMockedServiceTests { | ||
|
||
@InjectMock | ||
CustomersService service; | ||
|
||
@Test | ||
void shouldReturn404WhenCustomerNotExists() { | ||
|
||
var uuid = UUID.randomUUID(); | ||
when(service.getByUuid(uuid)) | ||
.thenReturn(Optional.empty()); | ||
|
||
given() | ||
.when() | ||
.accept(ContentType.JSON) | ||
.get("/customers/{uuid}", uuid) | ||
.then() | ||
.statusCode(404); | ||
|
||
verify(service).getByUuid(uuid); | ||
|
||
} | ||
|
||
// TODO: wenn invalider Kunde angelegt werden soll, dann 400 + kein Service-Aufruf | ||
|
||
@Test | ||
void shouldNotInvokeServiceWhenInvalidCustomerIsCreated() { | ||
given() | ||
.when() | ||
.contentType(ContentType.JSON) | ||
.body(""" | ||
{ | ||
"name": "T", | ||
"birth_date": "2000-10-04", | ||
"state": "active" | ||
} | ||
""") | ||
.accept(ContentType.JSON) | ||
.post("/customers") | ||
.then() | ||
.statusCode(400); | ||
|
||
verifyNoInteractions(service); | ||
|
||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...i-provider/src/test/java/de/schulung/sample/quarkus/CustomersServiceIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package de.schulung.sample.quarkus; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
@QuarkusTest | ||
public class CustomersServiceIntegrationTests { | ||
|
||
@Inject | ||
CustomersService service; | ||
|
||
// TODO Testfall: Customer anlegen mit zu kurzem Namen -> Validierungsfehler | ||
|
||
@Test | ||
void shouldNotCreateInvalidCustomer() { | ||
var customer = new Customer(); | ||
assertThatThrownBy(() -> service.createCustomer(customer)) | ||
.isNotNull(); | ||
} | ||
|
||
@Test | ||
void shouldNotCreateNullCustomer() { | ||
assertThatThrownBy(() -> service.createCustomer(null)) | ||
.isNotNull(); | ||
} | ||
|
||
@Test | ||
void shouldNotInvokeSearchWithNullUuid() { | ||
assertThatThrownBy(() -> service.getByUuid(null)) | ||
.isNotNull(); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
customer-api-provider/src/test/java/de/schulung/sample/quarkus/CustomersServiceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.schulung.sample.quarkus; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Month; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class CustomersServiceTests { | ||
|
||
private final CustomersService service = new CustomersService(); | ||
|
||
@Test | ||
void shouldAddUuidToNewCustomer() { | ||
var customer = new Customer(); | ||
customer.setName("Tom"); | ||
customer.setBirthdate(LocalDate.of(2000, Month.FEBRUARY, 2)); | ||
customer.setState("active"); | ||
|
||
service.createCustomer(customer); | ||
|
||
assertThat(customer.getUuid()) | ||
.isNotNull(); | ||
} | ||
|
||
@Test | ||
void shouldFindNewCustomer() { | ||
var customer = new Customer(); | ||
customer.setName("Tom"); | ||
customer.setBirthdate(LocalDate.of(2000, Month.FEBRUARY, 2)); | ||
customer.setState("active"); | ||
service.createCustomer(customer); | ||
var uuid = customer.getUuid(); | ||
|
||
var result = service.getByUuid(uuid); | ||
|
||
assertThat(result).isNotEmpty(); | ||
} | ||
|
||
} |