diff --git a/src/integration/java/org/springframework/data/couchbase/core/mapping/CustomConverterTests.java b/src/integration/java/org/springframework/data/couchbase/core/mapping/CustomConverterTests.java new file mode 100644 index 00000000..8bf766e0 --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/core/mapping/CustomConverterTests.java @@ -0,0 +1,92 @@ +/* + * Copyright 2017 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 + * + * http://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.core.mapping; + +import java.util.Arrays; +import java.util.UUID; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.convert.converter.Converter; +import org.springframework.data.convert.ReadingConverter; +import org.springframework.data.convert.WritingConverter; +import org.springframework.data.couchbase.IntegrationTestApplicationConfig; +import org.springframework.data.couchbase.core.convert.CouchbaseCustomConversions; +import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter; +import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping; +import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory; +import org.springframework.data.couchbase.repository.support.IndexManager; +import org.springframework.data.repository.core.support.RepositoryFactorySupport; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Subhashni Balakrishnan + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = IntegrationTestApplicationConfig.class) +public class CustomConverterTests { + + @Autowired + private MappingCouchbaseConverter converter; + + @Autowired + private RepositoryOperationsMapping operationsMapping; + + @Autowired + private IndexManager indexManager; + + + private TestUUIDRepository repository; + + @WritingConverter + public enum UUIDToStringConverter implements Converter { + INSTANCE; + + @Override + public String convert(UUID source) { + return source.toString(); + } + } + + @ReadingConverter + public enum StringToUUIDConverter implements Converter { + INSTANCE; + + @Override + public UUID convert(String source) { + return UUID.fromString(source); + } + } + + @Before + public void setup() { + converter.setCustomConversions(new CouchbaseCustomConversions(Arrays.asList(UUIDToStringConverter.INSTANCE, StringToUUIDConverter.INSTANCE))); + converter.afterPropertiesSet(); + RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(operationsMapping, indexManager); + repository = factory.getRepository(TestUUIDRepository.class); + } + + @Test + public void testIdConversion() { + TestUUID doc = new TestUUID(); + doc.id = UUID.randomUUID(); + repository.save(doc); + repository.findById(doc.id); + } +} \ No newline at end of file diff --git a/src/integration/java/org/springframework/data/couchbase/core/mapping/TestUUID.java b/src/integration/java/org/springframework/data/couchbase/core/mapping/TestUUID.java new file mode 100644 index 00000000..319fa4cc --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/core/mapping/TestUUID.java @@ -0,0 +1,30 @@ +/* + * Copyright 2017 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 + * + * http://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.core.mapping; + +import java.util.UUID; +import com.couchbase.client.java.repository.annotation.Id; +import org.springframework.data.couchbase.core.query.ViewIndexed; + +/** + * @author Subhashni Balakrishnan + */ +@ViewIndexed(designDoc = "testUUID", viewName = "all") +public class TestUUID { + @Id + public UUID id; +} diff --git a/src/integration/java/org/springframework/data/couchbase/core/mapping/TestUUIDRepository.java b/src/integration/java/org/springframework/data/couchbase/core/mapping/TestUUIDRepository.java new file mode 100644 index 00000000..6eeb0cc9 --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/core/mapping/TestUUIDRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 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 + * + * http://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.core.mapping; + +import java.util.UUID; + +import org.springframework.data.couchbase.repository.CouchbaseRepository; + +/** + * @author Subhashni Balakrishnan + */ +public interface TestUUIDRepository extends CouchbaseRepository { +} diff --git a/src/integration/java/org/springframework/data/couchbase/repository/wiring/RepositoryTemplateWiringTests.java b/src/integration/java/org/springframework/data/couchbase/repository/wiring/RepositoryTemplateWiringTests.java index 6d2ba252..9a8038bc 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/wiring/RepositoryTemplateWiringTests.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/wiring/RepositoryTemplateWiringTests.java @@ -20,6 +20,8 @@ import org.springframework.data.annotation.Id; import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; import org.springframework.data.couchbase.core.CouchbaseOperations; import org.springframework.data.couchbase.core.CouchbaseTemplate; +import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter; +import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext; import org.springframework.data.couchbase.repository.CouchbaseRepository; import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping; @@ -53,15 +55,20 @@ public class RepositoryTemplateWiringTests { mockOpsA = mock(CouchbaseOperations.class); when(mockOpsA.getCouchbaseClusterInfo()).thenReturn(info); when(mockOpsA.exists(any(String.class))).thenReturn(true); + when(mockOpsA.getConverter()).thenReturn(new MappingCouchbaseConverter(new CouchbaseMappingContext())); + mockOpsB = mock(CouchbaseOperations.class); when(mockOpsB.getCouchbaseClusterInfo()).thenReturn(info); when(mockOpsB.exists(any(String.class))).thenReturn(false); + when(mockOpsB.getConverter()).thenReturn(new MappingCouchbaseConverter(new CouchbaseMappingContext())); + mockOpsC = spy(new CouchbaseTemplate(info, null)); Misc cValue = new Misc(); cValue.id = "mock"; cValue.random = true; + when(mockOpsC.getConverter()).thenReturn(new MappingCouchbaseConverter(new CouchbaseMappingContext())); doReturn(cValue).when(mockOpsC).findById(any(String.class), any(Class.class)); } diff --git a/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java b/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java index 8cc5a91a..60109da6 100644 --- a/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java +++ b/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java @@ -103,19 +103,19 @@ public class SimpleCouchbaseRepository implements Co @Override public Optional findById(ID id) { Assert.notNull(id, "The given id must not be null!"); - return Optional.ofNullable(couchbaseOperations.findById(id.toString(), entityInformation.getJavaType())); + return Optional.ofNullable(couchbaseOperations.findById(couchbaseOperations.getConverter().convertForWriteIfNeeded(id).toString(), entityInformation.getJavaType())); } @Override public boolean existsById(ID id) { Assert.notNull(id, "The given id must not be null!"); - return couchbaseOperations.exists(id.toString()); + return couchbaseOperations.exists(couchbaseOperations.getConverter().convertForWriteIfNeeded(id).toString()); } @Override public void deleteById(ID id) { Assert.notNull(id, "The given id must not be null!"); - couchbaseOperations.remove(id.toString()); + couchbaseOperations.remove(couchbaseOperations.getConverter().convertForWriteIfNeeded(id).toString()); } @Override @@ -149,7 +149,7 @@ public class SimpleCouchbaseRepository implements Co query.stale(getCouchbaseOperations().getDefaultConsistency().viewConsistency()); JsonArray keys = JsonArray.create(); for (ID id : ids) { - keys.add(id); + keys.add(couchbaseOperations.getConverter().convertForWriteIfNeeded(id)); } query.keys(keys);