Skip to content

Commit 1b506d4

Browse files
author
Saborowski, Kai
committed
Refactored to use Java Records instead of Lombok Data classes.
1 parent 5183dbf commit 1b506d4

File tree

7 files changed

+43
-59
lines changed

7 files changed

+43
-59
lines changed

src/main/java/de/ksbrwsk/people/Person.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,27 @@
33
import io.swagger.v3.oas.annotations.media.Schema;
44
import jakarta.validation.constraints.NotBlank;
55
import jakarta.validation.constraints.Size;
6-
import lombok.AllArgsConstructor;
7-
import lombok.Data;
8-
import lombok.NoArgsConstructor;
96
import org.springframework.data.annotation.Id;
107

118
/**
129
* This class represents a Person entity.
1310
* It includes validation and API documentation annotations.
1411
*/
15-
@Data
16-
@NoArgsConstructor
17-
@AllArgsConstructor
18-
public class Person {
19-
/**
20-
* The unique identifier of the person.
21-
* It is automatically generated.
22-
*/
23-
@Id
24-
@Schema(name = "id", description = "The person's id")
25-
private Long id;
26-
27-
/**
28-
* The name of the person.
29-
* It cannot be blank and its size must be between 1 and 10 characters.
30-
*/
31-
@NotBlank
32-
@Size(min = 1, max = 10)
33-
@Schema(minLength = 1, maxLength = 10, nullable = false, name = "name", description = "The person's name")
34-
private String name;
35-
12+
public record Person(
13+
@Id
14+
@Schema(name = "id", description = "The person's id")
15+
Long id,
16+
@NotBlank
17+
@Size(min = 1, max = 10)
18+
@Schema(minLength = 1, maxLength = 10, nullable = false, name = "name", description = "The person's name")
19+
String name
20+
) {
3621
/**
3722
* Constructor that sets the name of the person.
23+
*
3824
* @param name The name of the person.
3925
*/
4026
public Person(String name) {
41-
this.name = name;
27+
this(null, name);
4228
}
4329
}

src/main/java/de/ksbrwsk/people/PersonHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public Mono<ServerResponse> handleCreate(ServerRequest serverRequest) {
104104
.doOnNext(this::validate)
105105
.flatMap(this.personRepository::save)
106106
.flatMap(person ->
107-
created(URI.create(API + "/" + person.getId()))
107+
created(URI.create(API + "/" + person.id()))
108108
.bodyValue(person));
109109
}
110110

@@ -120,7 +120,7 @@ public Mono<ServerResponse> handleUpdate(ServerRequest serverRequest) {
120120
.doOnNext(this::validate)
121121
.switchIfEmpty(Mono.error(new ResponseStatusException(BAD_REQUEST, "Body is required")));
122122
return this.personRepository.findById(id)
123-
.zipWith(update, (person, personUpdate) -> new Person(person.getId(), personUpdate.getName()))
123+
.zipWith(update, (person, personUpdate) -> new Person(person.id(), personUpdate.name()))
124124
.flatMap(this.personRepository::save)
125125
.flatMap(person -> ServerResponse.ok().bodyValue(person))
126126
.switchIfEmpty(ServerResponse.notFound().build());

src/test/java/de/ksbrwsk/people/PersonHandlerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ void should_handle_find_by_id() {
6767
.expectBody(Person.class)
6868
.consumeWith(response -> {
6969
assertThat(response.getResponseBody()).isNotNull();
70-
assertThat(response.getResponseBody().getName()).isEqualTo("Name");
71-
assertThat(response.getResponseBody().getId()).isEqualTo(1L);
70+
assertThat(response.getResponseBody().name()).isEqualTo("Name");
71+
assertThat(response.getResponseBody().id()).isEqualTo(1L);
7272
});
7373
}
7474

src/test/java/de/ksbrwsk/people/PersonRepositoryTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ void should_persist_people() {
3131
.thenMany(this.personRepository.findAll());
3232
StepVerifier
3333
.create(personFlux)
34-
.expectNextMatches(person -> person.getId() != null &&
35-
person.getName().equalsIgnoreCase("name"))
36-
.expectNextMatches(person -> person.getId() != null &&
37-
person.getName().equalsIgnoreCase("sabo"))
34+
.expectNextMatches(person -> person.id() != null &&
35+
person.name().equalsIgnoreCase("name"))
36+
.expectNextMatches(person -> person.id() != null &&
37+
person.name().equalsIgnoreCase("sabo"))
3838
.verifyComplete();
3939
}
4040

@@ -46,10 +46,10 @@ void should_find_person_by_id() {
4646
.then(this.personRepository.save(new Person(null, "Name")))
4747
.then(this.personRepository.save(new Person(null, "Sabo")))
4848
.then(this.personRepository.findFirstByName("Sabo"))
49-
.flatMap(person -> this.personRepository.findById(person.getId()));
49+
.flatMap(person -> this.personRepository.findById(person.id()));
5050
StepVerifier
5151
.create(personFlux)
52-
.expectNextMatches(person -> person.getName().equalsIgnoreCase("sabo"))
52+
.expectNextMatches(person -> person.name().equalsIgnoreCase("sabo"))
5353
.verifyComplete();
5454
}
5555

@@ -63,8 +63,8 @@ void should_find_first_person_by_name() {
6363
.then(this.personRepository.findFirstByName("Sabo"));
6464
StepVerifier
6565
.create(personFlux)
66-
.expectNextMatches(person -> person.getId() != null &&
67-
person.getName().equalsIgnoreCase("sabo"))
66+
.expectNextMatches(person -> person.id() != null &&
67+
person.name().equalsIgnoreCase("sabo"))
6868
.verifyComplete();
6969
}
7070

@@ -77,7 +77,7 @@ void should_delete_person_by_id() {
7777
.block();
7878
Person first = this.findFirst();
7979
Mono<Long> longMono = this.personRepository
80-
.findById(first.getId())
80+
.findById(first.id())
8181
.flatMap(this.personRepository::delete)
8282
.then(this.personRepository.count());
8383
StepVerifier

src/test/java/de/ksbrwsk/people/PersonTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class PersonTest {
1818
@Test
1919
void should_create_person() {
2020
Person person = new Person(1L, "Name");
21-
assertThat(person.getId()).isEqualTo(1L);
22-
assertThat(person.getName()).isEqualTo("Name");
21+
assertThat(person.id()).isEqualTo(1L);
22+
assertThat(person.name()).isEqualTo("Name");
2323
}
2424

2525
@ParameterizedTest

src/test/java/de/ksbrwsk/people/RestDocsTest.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public void setUp(ApplicationContext applicationContext, RestDocumentationContex
5050
this.webTestClient = WebTestClient.bindToApplicationContext(applicationContext).configureClient()
5151
.filter(documentationConfiguration(restDocumentation))
5252
.build();
53-
5453
List<Person> people = new ArrayList<>();
5554
for (int i = 1; i <= 100; i++) {
5655
people.add(new Person("Person@" + i));
@@ -112,13 +111,13 @@ void handleFindById() {
112111
Person first = this.fetchFirst();
113112
this.webTestClient
114113
.get()
115-
.uri(API + "/" + first.getId())
114+
.uri(API + "/" + first.id())
116115
.accept(MediaType.APPLICATION_JSON)
117116
.exchange()
118117
.expectStatus().isOk()
119118
.expectBody()
120-
.jsonPath("$.id").isEqualTo(first.getId())
121-
.jsonPath("$.name").isEqualTo(first.getName())
119+
.jsonPath("$.id").isEqualTo(first.id())
120+
.jsonPath("$.name").isEqualTo(first.name())
122121
.consumeWith(document("handle-find-by-id",
123122
responseFields(
124123
fieldWithPath("id")
@@ -136,7 +135,7 @@ void handleDeleteById() {
136135
Person first = this.fetchFirst();
137136
this.webTestClient
138137
.delete()
139-
.uri(API + "/" + first.getId())
138+
.uri(API + "/" + first.id())
140139
.accept(MediaType.APPLICATION_JSON)
141140
.exchange()
142141
.expectStatus().isOk()
@@ -176,13 +175,13 @@ void handleUpdate() {
176175
Person first = this.fetchFirst();
177176
this.webTestClient
178177
.put()
179-
.uri(API + "/" + first.getId())
180-
.bodyValue(new Person(first.getId(), "Update"))
178+
.uri(API + "/" + first.id())
179+
.bodyValue(new Person(first.id(), "Update"))
181180
.accept(MediaType.APPLICATION_JSON)
182181
.exchange()
183182
.expectStatus().isOk()
184183
.expectBody()
185-
.jsonPath("$.id").isEqualTo(first.getId())
184+
.jsonPath("$.id").isEqualTo(first.id())
186185
.jsonPath("$.name").isEqualTo("Update")
187186
.consumeWith(document("handle-update",
188187
responseFields(
@@ -201,8 +200,8 @@ void handleUpdateInvalid() {
201200
Person first = this.fetchFirst();
202201
this.webTestClient
203202
.put()
204-
.uri(API + "/" + first.getId())
205-
.bodyValue(new Person(first.getId(), ""))
203+
.uri(API + "/" + first.id())
204+
.bodyValue(new Person(first.id(), ""))
206205
.accept(MediaType.APPLICATION_JSON)
207206
.exchange()
208207
.expectStatus().isBadRequest()
@@ -229,7 +228,7 @@ void handleUpdateBadRequest() {
229228
Person first = this.fetchFirst();
230229
this.webTestClient
231230
.put()
232-
.uri(API + "/" + first.getId())
231+
.uri(API + "/" + first.id())
233232
.bodyValue(Optional.empty())
234233
.accept(MediaType.APPLICATION_JSON)
235234
.exchange()
@@ -286,5 +285,4 @@ void handleCreateInvalid() {
286285
.expectBody()
287286
.consumeWith(document("handle-create-invalid"));
288287
}
289-
290288
}

src/test/java/de/ksbrwsk/people/WebIntegrationTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class WebIntegrationTest extends PostgreSqlContainer {
3131
@BeforeEach
3232
public void setUp() {
3333
log.info("Running setUp -> creatíng 100 people");
34-
ArrayList<Person> people = new ArrayList<>();
34+
var people = new ArrayList<Person>();
3535
for (int i = 1; i <= 100; i++) {
3636
people.add(new Person("Person@" + i));
3737
}
@@ -56,7 +56,7 @@ Person fetchFirstPerson() {
5656
@ValueSource(strings = {"N", "Name", "0123456789"})
5757
void handleUpdateValid(String name) {
5858
Person first = this.fetchFirstPerson();
59-
var id = first.getId();
59+
var id = first.id();
6060
this.webTestClient
6161
.put()
6262
.uri(API + "/" + id)
@@ -73,7 +73,7 @@ void handleUpdateValid(String name) {
7373
@NullAndEmptySource
7474
void handleUpdateInvalid(String name) {
7575
Person first = this.fetchFirstPerson();
76-
var id = first.getId();
76+
var id = first.id();
7777
this.webTestClient
7878
.put()
7979
.uri(API + "/" + id)
@@ -97,7 +97,7 @@ void handleUpdateNotFound() {
9797
@Test
9898
void handleUpdateBadRequest() {
9999
Person person = fetchFirstPerson();
100-
var id = person.getId();
100+
var id = person.id();
101101
this.webTestClient
102102
.put()
103103
.uri(API + "/" + id)
@@ -154,7 +154,7 @@ void handleCreateInvalid(String name) {
154154
@Test
155155
void handleDeleteById() {
156156
Person first = this.fetchFirstPerson();
157-
var id = first.getId();
157+
var id = first.id();
158158
this.webTestClient
159159
.delete()
160160
.uri(API + "/" + id)
@@ -196,7 +196,7 @@ void handleNotFound() {
196196
@Test
197197
void handleFindById() {
198198
Person first = this.fetchFirstPerson();
199-
var id = first.getId();
199+
var id = first.id();
200200
this.webTestClient
201201
.get()
202202
.uri(API + "/" + id)
@@ -205,7 +205,7 @@ void handleFindById() {
205205
.isOk()
206206
.expectBody()
207207
.jsonPath("$.id").isEqualTo(id.toString())
208-
.jsonPath("$.name").isEqualTo(first.getName());
208+
.jsonPath("$.name").isEqualTo(first.name());
209209
}
210210

211211
@Test

0 commit comments

Comments
 (0)