Skip to content

Commit 95ff7d1

Browse files
committed
Change example to use Coupon instead of Topic
1 parent 07b20c8 commit 95ff7d1

File tree

4 files changed

+319
-231
lines changed

4 files changed

+319
-231
lines changed

hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/json/polymorphic/PostgreSQLJsonPolymorphicListCustomTypeTest.java

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
import javax.persistence.*;
1414
import java.io.Serializable;
15-
import java.sql.Timestamp;
16-
import java.time.LocalDateTime;
15+
import java.math.BigDecimal;
1716
import java.util.*;
17+
import java.util.function.Function;
18+
import java.util.stream.Collectors;
1819

1920
import static org.junit.Assert.assertEquals;
2021

@@ -41,11 +42,15 @@ protected void additionalProperties(Properties properties) {
4142
objectMapper.activateDefaultTypingAsProperty(
4243
objectMapper.getPolymorphicTypeValidator(),
4344
ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE,
44-
"className"
45+
"type"
4546
),
4647
ArrayList.class
47-
),
48-
"json-polymorphic-list"
48+
) {
49+
@Override
50+
public String getName() {
51+
return "json-polymorphic-list";
52+
}
53+
}
4954
)
5055
)
5156
);
@@ -54,17 +59,15 @@ protected void additionalProperties(Properties properties) {
5459
@Test
5560
public void test() {
5661

57-
Timestamp validUntil = Timestamp.valueOf(LocalDateTime.now().plusDays(10));
58-
5962
doInJPA(entityManager -> {
6063
entityManager.persist(
6164
new Book()
6265
.setIsbn("978-9730228236")
63-
.addTopic(new Post("High-Performance Java Persistence")
64-
.setContent("It rocks!")
66+
.addCoupon(new AmountDiscountCoupon("PPP")
67+
.setAmount(new BigDecimal("4.99"))
6568
)
66-
.addTopic(new Announcement("Black Friday - 50% discount")
67-
.setValidUntil(validUntil)
69+
.addCoupon(new PercentageDiscountCoupon("Black Friday")
70+
.setPercentage(BigDecimal.valueOf(0.02))
6871
)
6972
);
7073
});
@@ -74,14 +77,28 @@ public void test() {
7477
.bySimpleNaturalId(Book.class)
7578
.load("978-9730228236");
7679

77-
List<Topic> topics = book.getTopics();
80+
Map<String, DiscountCoupon> topics = book.getCoupons()
81+
.stream()
82+
.collect(
83+
Collectors.toMap(
84+
DiscountCoupon::getName,
85+
Function.identity()
86+
)
87+
);
88+
7889
assertEquals(2, topics.size());
79-
Post post = (Post) topics.get(0);
80-
assertEquals("It rocks!", post.getContent());
81-
Announcement announcement = (Announcement) topics.get(1);
90+
AmountDiscountCoupon amountDiscountCoupon = (AmountDiscountCoupon)
91+
topics.get("PPP");
92+
assertEquals(
93+
new BigDecimal("4.99"),
94+
amountDiscountCoupon.getAmount()
95+
);
96+
97+
PercentageDiscountCoupon percentageDiscountCoupon = (PercentageDiscountCoupon)
98+
topics.get("Black Friday");
8299
assertEquals(
83-
validUntil.getTime(),
84-
announcement.getValidUntil().getTime()
100+
BigDecimal.valueOf(0.02),
101+
percentageDiscountCoupon.getPercentage()
85102
);
86103
});
87104
}
@@ -100,7 +117,7 @@ public static class Book {
100117

101118
@Type(type = "json-polymorphic-list")
102119
@Column(columnDefinition = "jsonb")
103-
private List<Topic> topics = new ArrayList<>();
120+
private List<DiscountCoupon> coupons = new ArrayList<>();
104121

105122
public String getIsbn() {
106123
return isbn;
@@ -111,83 +128,92 @@ public Book setIsbn(String isbn) {
111128
return this;
112129
}
113130

114-
public List<Topic> getTopics() {
115-
return topics;
131+
public List<DiscountCoupon> getCoupons() {
132+
return coupons;
116133
}
117134

118-
public Book setTopics(List<Topic> topics) {
119-
this.topics = topics;
135+
public Book setCoupons(List<DiscountCoupon> coupons) {
136+
this.coupons = coupons;
120137
return this;
121138
}
122139

123-
public Book addTopic(Topic topic) {
124-
topics.add(topic);
140+
public Book addCoupon(DiscountCoupon topic) {
141+
coupons.add(topic);
125142
return this;
126143
}
127144
}
128145

129-
public static abstract class Topic implements Serializable {
146+
public abstract static class DiscountCoupon implements Serializable {
130147

131-
private String title;
148+
private String name;
149+
150+
public DiscountCoupon() {
151+
}
132152

133-
public Topic() {
153+
public DiscountCoupon(String name) {
154+
this.name = name;
134155
}
135156

136-
public Topic(String title) {
137-
this.title = title;
157+
public String getName() {
158+
return name;
138159
}
139160

140-
public String getTitle() {
141-
return title;
161+
public void setName(String name) {
162+
this.name = name;
142163
}
143164

144-
public void setTitle(String title) {
145-
this.title = title;
165+
@Override
166+
public boolean equals(Object o) {
167+
if (this == o) return true;
168+
if (!(o instanceof DiscountCoupon)) return false;
169+
DiscountCoupon that = (DiscountCoupon) o;
170+
return Objects.equals(getName(), that.getName());
146171
}
147172

148-
String getType() {
149-
return getClass().getSimpleName();
173+
@Override
174+
public int hashCode() {
175+
return Objects.hash(getName());
150176
}
151177
}
152178

153-
public static class Post extends Topic {
179+
public static class AmountDiscountCoupon extends DiscountCoupon {
154180

155-
private String content;
181+
private BigDecimal amount;
156182

157-
public Post() {
183+
public AmountDiscountCoupon() {
158184
}
159185

160-
public Post(String title) {
161-
super(title);
186+
public AmountDiscountCoupon(String name) {
187+
super(name);
162188
}
163189

164-
public String getContent() {
165-
return content;
190+
public BigDecimal getAmount() {
191+
return amount;
166192
}
167193

168-
public Post setContent(String content) {
169-
this.content = content;
194+
public AmountDiscountCoupon setAmount(BigDecimal amount) {
195+
this.amount = amount;
170196
return this;
171197
}
172198
}
173199

174-
public static class Announcement extends Topic {
200+
public static class PercentageDiscountCoupon extends DiscountCoupon {
175201

176-
private Date validUntil;
202+
private BigDecimal percentage;
177203

178-
public Announcement() {
204+
public PercentageDiscountCoupon() {
179205
}
180206

181-
public Announcement(String title) {
182-
super(title);
207+
public PercentageDiscountCoupon(String name) {
208+
super(name);
183209
}
184210

185-
public Date getValidUntil() {
186-
return validUntil;
211+
public BigDecimal getPercentage() {
212+
return percentage;
187213
}
188214

189-
public Announcement setValidUntil(Date validUntil) {
190-
this.validUntil = validUntil;
215+
public PercentageDiscountCoupon setPercentage(BigDecimal amount) {
216+
this.percentage = amount;
191217
return this;
192218
}
193219
}

0 commit comments

Comments
 (0)