DATACOUCH-309 Convert IDs as well if custom conversions are available in Simple Repository
Original pull request: #146.
This commit is contained in:
@@ -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.CustomConversions;
|
||||
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<UUID, String> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public String convert(UUID source) {
|
||||
return source.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public enum StringToUUIDConverter implements Converter<String, UUID> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public UUID convert(String source) {
|
||||
return UUID.fromString(source);
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
converter.setCustomConversions(new CustomConversions(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.findOne(doc.id);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<TestUUID, UUID> {
|
||||
}
|
||||
@@ -19,6 +19,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;
|
||||
@@ -51,15 +53,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));
|
||||
}
|
||||
|
||||
|
||||
@@ -102,19 +102,19 @@ public class SimpleCouchbaseRepository<T, ID extends Serializable> implements Co
|
||||
@Override
|
||||
public T findOne(ID id) {
|
||||
Assert.notNull(id, "The given id must not be null!");
|
||||
return couchbaseOperations.findById(id.toString(), entityInformation.getJavaType());
|
||||
return couchbaseOperations.findById(couchbaseOperations.getConverter().convertForWriteIfNeeded(id).toString(), entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(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 delete(ID id) {
|
||||
Assert.notNull(id, "The given id must not be null!");
|
||||
couchbaseOperations.remove(id.toString());
|
||||
couchbaseOperations.remove(couchbaseOperations.getConverter().convertForWriteIfNeeded(id).toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -148,7 +148,7 @@ public class SimpleCouchbaseRepository<T, ID extends Serializable> 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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user