Skip to content

Commit 88dacde

Browse files
authored
Merge pull request #149 from eclipse/test_custom_type
Create test to custom Reader and Writer
2 parents 3289d9e + 2c7f676 commit 88dacde

File tree

6 files changed

+155
-9
lines changed

6 files changed

+155
-9
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()));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2019 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
16+
package org.eclipse.jnosql.diana.mongodb.document.type;
17+
18+
import java.math.BigDecimal;
19+
import java.util.Currency;
20+
import java.util.Objects;
21+
22+
public class Money {
23+
24+
private final Currency currency;
25+
26+
private final BigDecimal value;
27+
28+
private Money(Currency currency, BigDecimal value) {
29+
this.currency = currency;
30+
this.value = value;
31+
}
32+
33+
public Currency getCurrency() {
34+
return currency;
35+
}
36+
37+
public BigDecimal getValue() {
38+
return value;
39+
}
40+
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+
59+
@Override
60+
public String toString() {
61+
return currency.getCurrencyCode() + ' ' + value;
62+
}
63+
64+
public static Money of(Currency currency, BigDecimal value) {
65+
return new Money(currency, value);
66+
}
67+
68+
public static Money parse(String text) {
69+
String[] texts = text.split(" ");
70+
return new Money(Currency.getInstance(texts[0]),
71+
BigDecimal.valueOf(Double.valueOf(texts[1])));
72+
}
73+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2019 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.diana.mongodb.document.type;
16+
17+
import jakarta.nosql.ValueReader;
18+
19+
public class MoneyValueReader implements ValueReader {
20+
21+
@Override
22+
public boolean isCompatible(Class clazz) {
23+
return Money.class.equals(clazz);
24+
}
25+
26+
@Override
27+
public <T> T read(Class<T> clazz, Object value) {
28+
return (T) Money.parse(value.toString());
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2019 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.diana.mongodb.document.type;
16+
17+
import jakarta.nosql.ValueWriter;
18+
19+
public class MoneyValueWriter implements ValueWriter<Money, String> {
20+
21+
@Override
22+
public boolean isCompatible(Class clazz) {
23+
return Money.class.equals(clazz);
24+
}
25+
26+
@Override
27+
public String write(Money money) {
28+
return money.toString();
29+
}
30+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.jnosql.diana.mongodb.document.type.MoneyValueReader
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.jnosql.diana.mongodb.document.type.MoneyValueWriter

0 commit comments

Comments
 (0)