Skip to content

Commit 29f8fd4

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

File tree

4 files changed

+332
-240
lines changed

4 files changed

+332
-240
lines changed

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

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
5+
import com.vladmihalcea.hibernate.type.json.JsonType;
56
import com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper;
67
import com.vladmihalcea.hibernate.util.AbstractPostgreSQLIntegrationTest;
78
import org.hibernate.Session;
@@ -12,9 +13,10 @@
1213

1314
import javax.persistence.*;
1415
import java.io.Serializable;
15-
import java.sql.Timestamp;
16-
import java.time.LocalDateTime;
16+
import java.math.BigDecimal;
1717
import java.util.*;
18+
import java.util.function.Function;
19+
import java.util.stream.Collectors;
1820

1921
import static org.junit.Assert.assertEquals;
2022

@@ -37,15 +39,19 @@ protected void additionalProperties(Properties properties) {
3739
(TypeContributorList) () -> Collections.singletonList(
3840
(typeContributions, serviceRegistry) ->
3941
typeContributions.contributeType(
40-
new JsonBinaryType(
42+
new JsonType(
4143
objectMapper.activateDefaultTypingAsProperty(
4244
objectMapper.getPolymorphicTypeValidator(),
4345
ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE,
44-
"className"
46+
"type"
4547
),
4648
ArrayList.class
47-
),
48-
"json-polymorphic-list"
49+
) {
50+
@Override
51+
public String getName() {
52+
return "json-polymorphic-list";
53+
}
54+
}
4955
)
5056
)
5157
);
@@ -54,17 +60,15 @@ protected void additionalProperties(Properties properties) {
5460
@Test
5561
public void test() {
5662

57-
Timestamp validUntil = Timestamp.valueOf(LocalDateTime.now().plusDays(10));
58-
5963
doInJPA(entityManager -> {
6064
entityManager.persist(
6165
new Book()
6266
.setIsbn("978-9730228236")
63-
.addTopic(new Post("High-Performance Java Persistence")
64-
.setContent("It rocks!")
67+
.addCoupon(new AmountDiscountCoupon("PPP")
68+
.setAmount(new BigDecimal("4.99"))
6569
)
66-
.addTopic(new Announcement("Black Friday - 50% discount")
67-
.setValidUntil(validUntil)
70+
.addCoupon(new PercentageDiscountCoupon("Black Friday")
71+
.setPercentage(BigDecimal.valueOf(0.02))
6872
)
6973
);
7074
});
@@ -74,14 +78,28 @@ public void test() {
7478
.bySimpleNaturalId(Book.class)
7579
.load("978-9730228236");
7680

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

101119
@Type(type = "json-polymorphic-list")
102120
@Column(columnDefinition = "jsonb")
103-
private List<Topic> topics = new ArrayList<>();
121+
private List<DiscountCoupon> coupons = new ArrayList<>();
104122

105123
public String getIsbn() {
106124
return isbn;
@@ -111,83 +129,92 @@ public Book setIsbn(String isbn) {
111129
return this;
112130
}
113131

114-
public List<Topic> getTopics() {
115-
return topics;
132+
public List<DiscountCoupon> getCoupons() {
133+
return coupons;
116134
}
117135

118-
public Book setTopics(List<Topic> topics) {
119-
this.topics = topics;
136+
public Book setCoupons(List<DiscountCoupon> coupons) {
137+
this.coupons = coupons;
120138
return this;
121139
}
122140

123-
public Book addTopic(Topic topic) {
124-
topics.add(topic);
141+
public Book addCoupon(DiscountCoupon topic) {
142+
coupons.add(topic);
125143
return this;
126144
}
127145
}
128146

129-
public static abstract class Topic implements Serializable {
147+
public abstract static class DiscountCoupon implements Serializable {
130148

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

133-
public Topic() {
154+
public DiscountCoupon(String name) {
155+
this.name = name;
134156
}
135157

136-
public Topic(String title) {
137-
this.title = title;
158+
public String getName() {
159+
return name;
138160
}
139161

140-
public String getTitle() {
141-
return title;
162+
public void setName(String name) {
163+
this.name = name;
142164
}
143165

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

148-
String getType() {
149-
return getClass().getSimpleName();
174+
@Override
175+
public int hashCode() {
176+
return Objects.hash(getName());
150177
}
151178
}
152179

153-
public static class Post extends Topic {
180+
public static class AmountDiscountCoupon extends DiscountCoupon {
154181

155-
private String content;
182+
private BigDecimal amount;
156183

157-
public Post() {
184+
public AmountDiscountCoupon() {
158185
}
159186

160-
public Post(String title) {
161-
super(title);
187+
public AmountDiscountCoupon(String name) {
188+
super(name);
162189
}
163190

164-
public String getContent() {
165-
return content;
191+
public BigDecimal getAmount() {
192+
return amount;
166193
}
167194

168-
public Post setContent(String content) {
169-
this.content = content;
195+
public AmountDiscountCoupon setAmount(BigDecimal amount) {
196+
this.amount = amount;
170197
return this;
171198
}
172199
}
173200

174-
public static class Announcement extends Topic {
201+
public static class PercentageDiscountCoupon extends DiscountCoupon {
175202

176-
private Date validUntil;
203+
private BigDecimal percentage;
177204

178-
public Announcement() {
205+
public PercentageDiscountCoupon() {
179206
}
180207

181-
public Announcement(String title) {
182-
super(title);
208+
public PercentageDiscountCoupon(String name) {
209+
super(name);
183210
}
184211

185-
public Date getValidUntil() {
186-
return validUntil;
212+
public BigDecimal getPercentage() {
213+
return percentage;
187214
}
188215

189-
public Announcement setValidUntil(Date validUntil) {
190-
this.validUntil = validUntil;
216+
public PercentageDiscountCoupon setPercentage(BigDecimal amount) {
217+
this.percentage = amount;
191218
return this;
192219
}
193220
}

0 commit comments

Comments
 (0)