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

Commit

Permalink
Merge pull request #7 from ralf-ueberfuhr-ars/feature/validation
Browse files Browse the repository at this point in the history
Validation - Return 400 if customer is not valid.
  • Loading branch information
ralf-ueberfuhr-ars authored Jun 18, 2024
2 parents 5be446a + c3907dd commit 96becaf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions customer-api-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.json.bind.annotation.JsonbProperty;
import jakarta.json.bind.annotation.JsonbTransient;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -19,9 +21,11 @@ public class Customer {
// readonly property
@Setter(onMethod_ = @JsonbTransient)
private UUID uuid;
@Size(min = 3, max = 100)
private String name;
@JsonbProperty("birth_date") // TODO -> use snake_case globally?
private LocalDate birthdate;
@Pattern(regexp = "active|locked|disabled")
private String state;

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

import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
Expand Down Expand Up @@ -33,7 +34,7 @@ public Collection<Customer> getCustomers() {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createCustomer(Customer customer) {
public Response createCustomer(@Valid Customer customer) {
customer.setUuid(UUID.randomUUID());
customers.put(customer.getUuid(), customer);
final var location = UriBuilder.fromResource(CustomersResource.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@ void shouldCreateCustomer() {
.body("birth_date", is(equalTo("2000-10-04")));
}

@Test
void shouldNotCreateCustomerWithInvalidName() {
given()
.when()
.contentType(ContentType.JSON)
.body("""
{
"name": "T",
"birth_date": "2000-10-04",
"state": "active"
}
""")
.accept(ContentType.JSON)
.post("/customers")
.then()
.statusCode(400);
}

@Test
void shouldNotCreateCustomerWithInvalidState() {
given()
.when()
.contentType(ContentType.JSON)
.body("""
{
"name": "Tom Mayer",
"birth_date": "2000-10-04",
"state": "gelbekatze"
}
""")
.accept(ContentType.JSON)
.post("/customers")
.then()
.statusCode(400);
}

}

@Nested
Expand Down

0 comments on commit 96becaf

Please sign in to comment.