Skip to content

Commit 2c7f676

Browse files
committed
creates custom type
1 parent d0a2069 commit 2c7f676

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

mongodb-driver/src/test/java/org/eclipse/jnosql/diana/mongodb/document/MongoDBDocumentCollectionManagerTest.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,15 @@
2222
import jakarta.nosql.document.DocumentEntity;
2323
import jakarta.nosql.document.DocumentQuery;
2424
import org.eclipse.jnosql.diana.document.Documents;
25+
import org.eclipse.jnosql.diana.mongodb.document.type.Money;
2526
import org.junit.jupiter.api.BeforeAll;
2627
import org.junit.jupiter.api.Test;
2728

2829
import java.io.IOException;
30+
import java.math.BigDecimal;
2931
import java.time.Duration;
3032
import java.time.LocalDate;
31-
import java.util.ArrayList;
32-
import java.util.Arrays;
33-
import java.util.Date;
34-
import java.util.HashMap;
35-
import java.util.List;
36-
import java.util.Map;
37-
import java.util.Optional;
38-
import java.util.Random;
33+
import java.util.*;
3934
import java.util.concurrent.ThreadLocalRandom;
4035
import java.util.stream.Collectors;
4136
import java.util.stream.StreamSupport;
@@ -76,7 +71,7 @@ public void shouldThrowExceptionWhenInsertWithTTL() {
7671
}
7772

7873
@Test
79-
public void shouldUpdateSave() {
74+
public void shouldUpdate() {
8075
DocumentEntity entity = getEntity();
8176
DocumentEntity documentEntity = entityManager.insert(entity);
8277
Document newField = Documents.of("newField", "10");
@@ -482,6 +477,22 @@ public void shouldCount() {
482477
assertTrue(entityManager.count(COLLECTION_NAME) > 0);
483478
}
484479

480+
@Test
481+
public void shouldCustomTypeWork() {
482+
DocumentEntity entity = getEntity();
483+
Currency currency = Currency.getInstance("USD");
484+
Money money = Money.of(currency, BigDecimal.valueOf(10D));
485+
entity.add("money", money);
486+
DocumentEntity documentEntity = entityManager.insert(entity);
487+
Document id = documentEntity.find("_id").get();
488+
DocumentQuery query = DocumentQuery.select().from(documentEntity.getName())
489+
.where(id.getName()).eq(id.get()).build();
490+
491+
DocumentEntity result = entityManager.singleResult(query).get();
492+
assertEquals(money, result.find("money").get().get(Money.class));
493+
494+
}
495+
485496
private DocumentEntity createSubdocumentList() {
486497
DocumentEntity entity = DocumentEntity.of("AppointmentBook");
487498
entity.add(Document.of("_id", new Random().nextInt()));

mongodb-driver/src/test/java/org/eclipse/jnosql/diana/mongodb/document/type/Money.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,58 @@
1616
package org.eclipse.jnosql.diana.mongodb.document.type;
1717

1818
import java.math.BigDecimal;
19+
import java.util.Currency;
20+
import java.util.Objects;
1921

2022
public class Money {
2123

22-
private final String currency;
24+
private final Currency currency;
2325

2426
private final BigDecimal value;
2527

26-
Money(String currency, BigDecimal value) {
28+
private Money(Currency currency, BigDecimal value) {
2729
this.currency = currency;
2830
this.value = value;
2931
}
3032

31-
public String getCurrency() {
33+
public Currency getCurrency() {
3234
return currency;
3335
}
3436

3537
public BigDecimal getValue() {
3638
return value;
3739
}
3840

41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) {
44+
return true;
45+
}
46+
if (o == null || getClass() != o.getClass()) {
47+
return false;
48+
}
49+
Money money = (Money) o;
50+
return Objects.equals(currency, money.currency) &&
51+
Objects.equals(value, money.value);
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(currency, value);
57+
}
58+
3959
@Override
4060
public String toString() {
41-
return currency + ' ' + value;
61+
return currency.getCurrencyCode() + ' ' + value;
62+
}
63+
64+
public static Money of(Currency currency, BigDecimal value) {
65+
return new Money(currency, value);
4266
}
4367

4468
public static Money parse(String text) {
4569
String[] texts = text.split(" ");
46-
return new Money(texts[0], BigDecimal.valueOf(Double.valueOf(texts[1])));
70+
return new Money(Currency.getInstance(texts[0]),
71+
BigDecimal.valueOf(Double.valueOf(texts[1])));
4772
}
4873
}

0 commit comments

Comments
 (0)