Skip to content

Commit 457fba9

Browse files
authored
Add fixtureMonkey in guide project (#186)
* Add fixtureMonkey in guide project * Refactor build.gradle
1 parent 74a6730 commit 457fba9

File tree

18 files changed

+639
-835
lines changed

18 files changed

+639
-835
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
1+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2+
13
plugins {
24
id "spring.jdbc.plus.java-conventions"
35
id "spring.jdbc.plus.spring-bom-conventions"
46
id "org.jetbrains.kotlin.jvm"
57
}
68

7-
compileKotlin {
8-
kotlinOptions {
9-
javaParameters = true
10-
freeCompilerArgs = ["-Xjsr305=strict"]
11-
jvmTarget = "17"
12-
}
13-
}
14-
15-
compileTestKotlin {
16-
kotlinOptions {
9+
kotlin {
10+
compilerOptions {
1711
javaParameters = true
18-
freeCompilerArgs = ["-Xjsr305=strict"]
19-
jvmTarget = "17"
12+
freeCompilerArgs.addAll("-Xjsr305=strict")
13+
jvmTarget = JvmTarget.JVM_17
2014
}
2115
}
2216

2317
dependencies {
2418
implementation("org.springframework.boot:spring-boot-starter-web")
2519
implementation project(":spring-boot-starter-data-jdbc-plus-sql")
2620
implementation("org.springframework.boot:spring-boot-starter-actuator")
27-
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
2821
implementation("org.jetbrains.kotlin:kotlin-reflect")
2922
implementation("com.h2database:h2")
3023
implementation("org.projectlombok:lombok")
@@ -37,6 +30,13 @@ dependencies {
3730

3831
testImplementation("org.springframework.boot:spring-boot-starter-test")
3932
testImplementation("org.projectlombok:lombok")
33+
34+
// FixtureMonkey
35+
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter:${fixtureMonkeyVersion}")
36+
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-jackson:${fixtureMonkeyVersion}")
37+
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-jakarta-validation:${fixtureMonkeyVersion}")
38+
4039
testAnnotationProcessor("org.projectlombok:lombok")
40+
4141
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
4242
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fixtureMonkeyVersion=1.1.6

guide-projects/plus-sql-java-kotlin-guide/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/guide/board/Board.java

Lines changed: 117 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
package com.navercorp.spring.data.jdbc.plus.sql.guide.board;
2020

21-
import java.util.ArrayList;
22-
import java.util.HashMap;
23-
import java.util.HashSet;
21+
import static java.util.Comparator.comparing;
22+
import static java.util.stream.Collectors.toCollection;
23+
24+
import java.util.LinkedHashSet;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Set;
@@ -34,169 +35,205 @@
3435
import org.springframework.data.relational.core.mapping.Table;
3536

3637
import lombok.Builder;
37-
import lombok.Getter;
38-
import lombok.Value;
39-
import lombok.With;
4038

4139
import com.navercorp.spring.jdbc.plus.commons.annotations.SqlTableAlias;
4240

4341
/**
4442
* @author Myeonghyeon Lee
4543
*/
44+
@Builder(toBuilder = true)
4645
@Table("n_board")
47-
@Getter
48-
@Builder
49-
public class Board {
46+
public record Board(
5047
@Id
51-
private Long id;
48+
Long id,
5249

53-
private String name;
50+
String name,
5451

5552
@MappedCollection(idColumn = "board_id")
56-
@Builder.Default
57-
private Set<Label> labels = new HashSet<>();
53+
@Nullable Set<Label> labels,
5854

5955
@MappedCollection(idColumn = "board_id", keyColumn = "board_index")
60-
@Builder.Default
61-
private List<Post> posts = new ArrayList<>();
56+
@Nullable List<Post> posts,
6257

6358
@SqlTableAlias("b_audit")
6459
@Column("board_id")
65-
private @Nullable Audit audit;
60+
@Nullable Audit audit,
6661

6762
@Embedded.Nullable(prefix = "board_")
68-
private @Nullable Memo memo;
63+
@Nullable Memo memo,
6964

7065
@MappedCollection(idColumn = "board_id", keyColumn = "config_key")
71-
@Builder.Default
72-
private @Nullable Map<String, Config> configMap = new HashMap<>();
66+
@Nullable Map<String, Config> configMap
67+
) {
68+
public Board {
69+
labels = labels == null ? Set.of() : labels;
70+
posts = posts == null ? List.of() : posts;
71+
configMap = configMap == null ? Map.of() : configMap;
72+
}
73+
74+
public Board sort() {
75+
return toBuilder()
76+
.labels(labels.stream()
77+
.sorted(comparing(label -> label.id().title()))
78+
.collect(toCollection(LinkedHashSet::new)))
79+
.posts(posts.stream()
80+
.map(Post::sort)
81+
.sorted(comparing(Post::id))
82+
.toList())
83+
.audit(audit != null ? audit.sort() : null)
84+
.build();
85+
}
7386

87+
@Builder(toBuilder = true)
7488
@Table("n_label")
75-
@Getter
76-
@Builder
77-
public static class Label implements Persistable<LabelId> {
89+
public record Label(
7890
@Id
7991
@Nullable
8092
@Embedded.Empty
81-
private LabelId id;
93+
LabelId id,
8294

83-
private String name;
95+
String name
96+
) implements Persistable<LabelId> {
97+
@Override
98+
public @Nullable LabelId getId() {
99+
return id;
100+
}
84101

85102
@Override
86103
public boolean isNew() {
87104
return false;
88105
}
89106
}
90107

91-
@Value
92-
@Builder
93-
public static class LabelId {
108+
@Builder(toBuilder = true)
109+
public record LabelId(
94110
@Column("title")
95-
String title;
111+
String title,
96112

97113
@Column("project_name")
98-
String projectName;
114+
String projectName
115+
) {
99116
}
100117

118+
@Builder(toBuilder = true)
101119
@Table("n_post")
102-
@Getter
103-
@Builder
104-
public static class Post {
120+
public record Post(
105121
@Id
106-
private Long id;
122+
Long id,
107123

108-
private Long postNo;
124+
Long postNo,
109125

110-
private String title;
126+
String title,
111127

112-
private String content;
128+
String content,
113129

114130
@MappedCollection(idColumn = "post_id")
115-
@Builder.Default
116-
private Set<Tag> tags = new HashSet<>();
131+
@Nullable Set<Tag> tags,
117132

118133
@MappedCollection(idColumn = "post_id", keyColumn = "post_index")
119-
@Builder.Default
120-
private List<Comment> comments = new ArrayList<>();
134+
@Nullable List<Comment> comments,
121135

122136
@Column("post_id")
123-
private @Nullable Audit audit;
137+
@Nullable Audit audit,
124138

125139
@Embedded.Nullable
126-
private @Nullable Memo memo;
140+
@Nullable Memo memo,
127141

128142
@MappedCollection(idColumn = "post_id", keyColumn = "config_key")
129-
@Builder.Default
130-
private @Nullable Map<String, Config> configMap = new HashMap<>();
143+
@Nullable Map<String, Config> configMap
144+
) {
145+
public Post {
146+
tags = tags == null ? Set.of() : tags;
147+
comments = comments == null ? List.of() : comments;
148+
configMap = configMap == null ? Map.of() : configMap;
149+
}
150+
151+
public Post sort() {
152+
return toBuilder()
153+
.tags(tags.stream()
154+
.sorted(comparing(Tag::id))
155+
.collect(toCollection(LinkedHashSet::new)))
156+
.comments(comments.stream()
157+
.map(Comment::sort)
158+
.sorted(comparing(Comment::id))
159+
.toList())
160+
.audit(audit != null ? audit.sort() : null)
161+
.build();
162+
}
131163
}
132164

165+
@Builder(toBuilder = true)
133166
@Table("n_tag")
134-
@Value
135-
@Builder
136-
public static class Tag {
167+
public record Tag(
137168
@Id
138-
@With
139-
Long id;
169+
Long id,
140170

141-
String content;
171+
String content
172+
) {
142173
}
143174

175+
@Builder(toBuilder = true)
144176
@Table("n_comment")
145-
@Getter
146-
@Builder
147-
public static class Comment {
177+
public record Comment(
148178
@Id
149-
private Long id;
179+
Long id,
150180

151-
private String content;
181+
String content,
152182

153183
@Column("comment_id")
154-
private @Nullable Audit audit;
184+
@Nullable Audit audit
185+
) {
186+
public Comment sort() {
187+
return toBuilder()
188+
.audit(audit != null ? audit.sort() : null)
189+
.build();
190+
}
155191
}
156192

193+
@Builder(toBuilder = true)
157194
@Table("n_audit")
158-
@Getter
159-
@Builder
160-
public static class Audit {
195+
public record Audit(
161196
@Id
162-
private Long id;
197+
Long id,
163198

164-
private String name;
199+
String name,
165200

166201
@Embedded.Nullable
167-
private @Nullable Memo memo;
202+
@Nullable Memo memo,
168203

169204
@Column("audit_id")
170-
private @Nullable AuditSecret secret;
205+
@Nullable AuditSecret secret
206+
) {
207+
public Audit sort() {
208+
return this;
209+
}
171210
}
172211

173-
@Value
174-
@Builder
175-
public static class Memo {
176-
String memo;
212+
@Builder(toBuilder = true)
213+
public record Memo(
214+
String memo
215+
) {
177216
}
178217

218+
@Builder(toBuilder = true)
179219
@Table("n_audit_secret")
180-
@Value
181-
@Builder
182-
public static class AuditSecret {
220+
public record AuditSecret(
183221
@Id
184-
@With
185-
Long id;
222+
Long id,
186223

187-
String secret;
224+
String secret
225+
) {
188226
}
189227

228+
@Builder(toBuilder = true)
190229
@Table("n_config")
191-
@Value
192-
@Builder
193-
public static class Config {
230+
public record Config(
194231
@Id
195-
@With
196-
Long id;
232+
Long id,
197233

198-
String configKey;
234+
String configKey,
199235

200-
String configValue;
236+
String configValue
237+
) {
201238
}
202239
}

guide-projects/plus-sql-java-kotlin-guide/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/guide/board/BoardRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
package com.navercorp.spring.data.jdbc.plus.sql.guide.board;
2020

2121
import org.jspecify.annotations.NullMarked;
22-
import org.springframework.data.repository.CrudRepository;
22+
import org.springframework.data.repository.ListCrudRepository;
2323

2424
/**
2525
* @author Myeonghyeon Lee
2626
*/
2727
@NullMarked
28-
public interface BoardRepository extends CrudRepository<Board, Long>, BoardRepositoryCustom {
28+
public interface BoardRepository extends ListCrudRepository<Board, Long>, BoardRepositoryCustom {
2929
}

guide-projects/plus-sql-java-kotlin-guide/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/guide/board/PostDto.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,23 @@
2626
import org.springframework.data.relational.core.mapping.Table;
2727

2828
import lombok.Builder;
29-
import lombok.Value;
3029

3130
import com.navercorp.spring.jdbc.plus.commons.annotations.SqlTableAlias;
3231

3332
/**
3433
* @author Myeonghyeon Lee
3534
*/
36-
@Value
3735
@Builder
3836
@Table("post")
39-
public class PostDto {
37+
public record PostDto(
4038
@Id
41-
Long id;
39+
Long id,
4240

4341
@Column
44-
Board.Post post;
42+
Board.Post post,
4543

4644
@SqlTableAlias("p_labels")
4745
@MappedCollection(idColumn = "board_id")
48-
Set<Board.Label> labels;
46+
Set<Board.Label> labels
47+
) {
4948
}

0 commit comments

Comments
 (0)