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

Use Enum for CustomerState #4

Draft
wants to merge 2 commits into
base: main
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
@@ -1,6 +1,7 @@
package de.schulung.sample.quarkus;

import jakarta.json.bind.annotation.JsonbTransient;
import jakarta.json.bind.annotation.JsonbTypeAdapter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -20,6 +21,16 @@ public class Customer {
private UUID uuid;
private String name;
private LocalDate birthdate; // TODO birth_date?
private String state;
@JsonbTypeAdapter(CustomerStateAdapter.class) // TODO register globally?
private CustomerState state = CustomerState.ACTIVE;

// Would work without any adapter, if the name() matches the JSON value.
// Here: "active", "locked" and "disabled".
// - DON'T: This would break with Java naming conventions.
public enum CustomerState {

ACTIVE, LOCKED, DISABLED

}

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

import jakarta.json.bind.adapter.JsonbAdapter;

/*
* A more generic mapper is not possible easily, because
* - the adapter doesn't have any information about the mapped field (target type)
* - we need to be careful with reflection (GraalVM!)
*/
public class CustomerStateAdapter implements JsonbAdapter<Customer.CustomerState, String> {

@Override
public String adaptToJson(Customer.CustomerState source) throws Exception {
return null == source ? null : switch (source) {
case ACTIVE -> "active";
case LOCKED -> "locked";
case DISABLED -> "disabled";
};
}

@Override
public Customer.CustomerState adaptFromJson(String source) throws Exception {
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 supported.");
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CustomersResource {
UUID.randomUUID(),
"Tom",
LocalDate.of(2000, Month.DECEMBER, 6),
"active"
Customer.CustomerState.ACTIVE
);
customers.put(customer.getUuid(), customer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void shouldCreateCustomer() {
.contentType(ContentType.JSON)
.body("name", is(equalTo("Tom")))
.body("birthdate", is(equalTo("2000-10-04")))
.body("state", is(equalTo("active")))
.body("uuid", is(notNullValue()))
.extract()
.header("Location");
Expand All @@ -91,5 +92,24 @@ void shouldCreateCustomer() {
.body("birthdate", is(equalTo("2000-10-04")));
}

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

}

}
}
Loading