DATACOUCH-560 - Serialization of CouchbaseDocument and CouchbaseList.

Add support for saving document with lists and composed objects.
Convert CouchbaseDocument/CouchbaseList to Map/List before saving.
This commit is contained in:
Andrea Torlaschi
2020-06-09 01:19:04 +02:00
committed by mikereiche
parent dddee9fb82
commit c7f12a2237
6 changed files with 123 additions and 3 deletions

View File

@@ -68,7 +68,7 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
return Mono.just(object).flatMap(o -> {
CouchbaseDocument converted = template.support().encodeEntity(o);
return template.getCollection(collection).reactive()
.insert(converted.getId(), converted.getContent(), buildInsertOptions()).map(result -> {
.insert(converted.getId(), converted.export(), buildInsertOptions()).map(result -> {
template.support().applyUpdatedCas(object, result.cas());
return o;
});

View File

@@ -68,7 +68,7 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
return Mono.just(object).flatMap(o -> {
CouchbaseDocument converted = template.support().encodeEntity(o);
return template.getCollection(collection).reactive()
.replace(converted.getId(), converted.getContent(), buildReplaceOptions()).map(result -> {
.replace(converted.getId(), converted.export(), buildReplaceOptions()).map(result -> {
template.support().applyUpdatedCas(object, result.cas());
return o;
});

View File

@@ -68,7 +68,7 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
return Mono.just(object).flatMap(o -> {
CouchbaseDocument converted = template.support().encodeEntity(o);
return template.getCollection(collection).reactive()
.upsert(converted.getId(), converted.getContent(), buildUpsertOptions()).map(result -> {
.upsert(converted.getId(), converted.export(), buildUpsertOptions()).map(result -> {
template.support().applyUpdatedCas(object, result.cas());
return o;
});

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2012-2020 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.domain;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Document;
@Document
public class Library {
@Id String id;
List<String> books;
public Library(String id, List<String> books) {
this.id = id;
this.books = books;
}
public String getId() {
return id;
}
public List<String> getBooks() {
return books;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Library library = (Library) o;
if (id != null ? !id.equals(library.id) : library.id != null)
return false;
return books != null ? books.equals(library.books) : library.books == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (books != null ? books.hashCode() : 0);
return result;
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2012-2020 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.domain;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
/**
* Library Repository for tests
*
* @author Andrea Torlaschi
*/
@Repository
public interface LibraryRepository extends PagingAndSortingRepository<Library, String> {
}

View File

@@ -18,6 +18,8 @@ package org.springframework.data.couchbase.repository;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@@ -25,6 +27,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.domain.Library;
import org.springframework.data.couchbase.domain.LibraryRepository;
import org.springframework.data.couchbase.domain.User;
import org.springframework.data.couchbase.domain.UserRepository;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
@@ -44,6 +48,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
public class CouchbaseRepositoryKeyValueIntegrationTests extends ClusterAwareIntegrationTests {
@Autowired UserRepository userRepository;
@Autowired LibraryRepository libraryRepository;
@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
@@ -62,6 +67,28 @@ public class CouchbaseRepositoryKeyValueIntegrationTests extends ClusterAwareInt
userRepository.delete(user);
}
@Test // DATACOUCH-564
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void saveAndFindByIdWithList() {
List<String> books = new ArrayList<>();
books.add("book1");
books.add("book2");
Library library = new Library(UUID.randomUUID().toString(), books);
// this currently fails when using mocked in integration.properties with status "UNKNOWN"
assertFalse(libraryRepository.existsById(library.getId()));
libraryRepository.save(library);
Optional<Library> found = libraryRepository.findById(library.getId());
assertTrue(found.isPresent());
found.ifPresent(l -> assertEquals(library, l));
assertTrue(userRepository.existsById(library.getId()));
libraryRepository.delete(library);
assertFalse(userRepository.existsById(library.getId()));
}
@Configuration
@EnableCouchbaseRepositories("org.springframework.data.couchbase")
static class Config extends AbstractCouchbaseConfiguration {