|
16 | 16 | */ |
17 | 17 | package org.eclipse.jnosql.communication.couchdb.document; |
18 | 18 |
|
| 19 | +import jakarta.nosql.TypeReference; |
19 | 20 | import jakarta.nosql.document.Document; |
20 | 21 | import jakarta.nosql.document.DocumentDeleteQuery; |
21 | 22 | import jakarta.nosql.document.DocumentEntity; |
|
34 | 35 | import static jakarta.nosql.document.DocumentQuery.select; |
35 | 36 | import static java.util.Arrays.asList; |
36 | 37 | import static org.eclipse.jnosql.communication.couchdb.document.configuration.CouchDBDocumentTcConfiguration.INSTANCE; |
| 38 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | +import static org.hamcrest.Matchers.contains; |
| 40 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
37 | 41 | import static org.junit.jupiter.api.Assertions.assertEquals; |
38 | 42 | import static org.junit.jupiter.api.Assertions.assertFalse; |
39 | 43 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
@@ -194,6 +198,40 @@ public void shouldRetrieveListSubdocumentList() { |
194 | 198 | assertTrue(contacts.stream().allMatch(d -> d.size() == 3)); |
195 | 199 | } |
196 | 200 |
|
| 201 | + @Test |
| 202 | + public void shouldSaveSubDocument() { |
| 203 | + DocumentEntity entity = getEntity(); |
| 204 | + entity.add(Document.of("phones", Document.of("mobile", "1231231"))); |
| 205 | + DocumentEntity entitySaved = entityManager.insert(entity); |
| 206 | + Document id = entitySaved.find("_id").get(); |
| 207 | + DocumentQuery query = select().from(COLLECTION_NAME) |
| 208 | + .where("_id").eq(id.get()) |
| 209 | + .build(); |
| 210 | + |
| 211 | + DocumentEntity entityFound = entityManager.select(query).collect(Collectors.toList()).get(0); |
| 212 | + Document subDocument = entityFound.find("phones").get(); |
| 213 | + List<Document> documents = subDocument.get(new TypeReference<List<Document>>() { |
| 214 | + }); |
| 215 | + assertThat(documents, contains(Document.of("mobile", "1231231"))); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void shouldSaveSubDocument2() { |
| 220 | + DocumentEntity entity = getEntity(); |
| 221 | + entity.add(Document.of("phones", asList(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231")))); |
| 222 | + DocumentEntity entitySaved = entityManager.insert(entity); |
| 223 | + Document id = entitySaved.find("_id").get(); |
| 224 | + |
| 225 | + DocumentQuery query = select().from(COLLECTION_NAME) |
| 226 | + .where(id.getName()).eq(id.get()) |
| 227 | + .build(); |
| 228 | + DocumentEntity entityFound = entityManager.select(query).collect(Collectors.toList()).get(0); |
| 229 | + Document subDocument = entityFound.find("phones").get(); |
| 230 | + List<Document> documents = subDocument.get(new TypeReference<List<Document>>() { |
| 231 | + }); |
| 232 | + assertThat(documents, containsInAnyOrder(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231"))); |
| 233 | + } |
| 234 | + |
197 | 235 | private DocumentEntity createSubdocumentList() { |
198 | 236 | DocumentEntity entity = DocumentEntity.of("AppointmentBook"); |
199 | 237 | List<List<Document>> documents = new ArrayList<>(); |
|
0 commit comments