diff --git a/src/integration/java/org/springframework/data/couchbase/IntegrationTestApplicationConfig.java b/src/integration/java/org/springframework/data/couchbase/IntegrationTestApplicationConfig.java index 2457c63d..59abf235 100644 --- a/src/integration/java/org/springframework/data/couchbase/IntegrationTestApplicationConfig.java +++ b/src/integration/java/org/springframework/data/couchbase/IntegrationTestApplicationConfig.java @@ -66,12 +66,6 @@ public class IntegrationTestApplicationConfig extends AbstractCouchbaseConfigura return template; } - //change the name of the field that will hold type information - @Override - public String typeKey() { - return "javaClass"; - } - @Override protected Consistency getDefaultConsistency() { return Consistency.READ_YOUR_OWN_WRITES; diff --git a/src/integration/java/org/springframework/data/couchbase/IntegrationTestCustomTypeKeyConfig.java b/src/integration/java/org/springframework/data/couchbase/IntegrationTestCustomTypeKeyConfig.java new file mode 100644 index 00000000..1ae3206e --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/IntegrationTestCustomTypeKeyConfig.java @@ -0,0 +1,13 @@ +package org.springframework.data.couchbase; + +import org.springframework.context.annotation.Configuration; + +@Configuration +public class IntegrationTestCustomTypeKeyConfig extends IntegrationTestApplicationConfig { + + //change the name of the field that will hold type information + @Override + public String typeKey() { + return "javaClass"; + } +} diff --git a/src/integration/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java b/src/integration/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java index 50dc1dee..81c5829c 100644 --- a/src/integration/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java +++ b/src/integration/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java @@ -98,9 +98,9 @@ public class CouchbaseTemplateTests { assertNotNull(result); Map resultConv = MAPPER.readValue(result, new TypeReference>() {}); - //DATACOUCH-134: the class holding field can be changed - assertNull(resultConv.get(MappingCouchbaseConverter.TYPEKEY_DEFAULT)); - assertEquals("org.springframework.data.couchbase.core.Beer", resultConv.get("javaClass")); + assertNotNull(resultConv.get(MappingCouchbaseConverter.TYPEKEY_DEFAULT)); + assertNull(resultConv.get("javaClass")); + assertEquals("org.springframework.data.couchbase.core.Beer", resultConv.get(MappingCouchbaseConverter.TYPEKEY_DEFAULT)); assertEquals(false, resultConv.get("is_active")); assertEquals("The Awesome Stout", resultConv.get("name")); } diff --git a/src/integration/java/org/springframework/data/couchbase/core/TypeKeyTests.java b/src/integration/java/org/springframework/data/couchbase/core/TypeKeyTests.java new file mode 100644 index 00000000..d6ffdcda --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/core/TypeKeyTests.java @@ -0,0 +1,77 @@ +/* + * Copyright 2013 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; + +import static org.junit.Assert.*; + +import java.util.Map; + +import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.document.RawJsonDocument; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.couchbase.IntegrationTestCustomTypeKeyConfig; +import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Tests the Java Config template around type key modification (DATACOUCH-134) + * + * @author Simon Baslé + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = IntegrationTestCustomTypeKeyConfig.class) +public class TypeKeyTests { + + @Autowired + private Bucket client; + + @Autowired + private CouchbaseTemplate template; + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + /** + * see DATACOUCH-134 + */ + @Test + public void saveSimpleEntityCorrectlyWithDifferentTypeKey() throws Exception { + String id = "beers:awesome-stout"; + String name = "The Awesome Stout"; + boolean active = false; + Beer beer = new Beer(id).setName(name).setActive(active); + + template.save(beer); + RawJsonDocument resultDoc = client.get(id, RawJsonDocument.class); + assertNotNull(resultDoc); + String result = resultDoc.content(); + assertNotNull(result); + Map resultConv = MAPPER.readValue(result, new TypeReference>() {}); + + assertNull(resultConv.get(MappingCouchbaseConverter.TYPEKEY_DEFAULT)); + assertNotNull(resultConv.get("javaClass")); + assertEquals("org.springframework.data.couchbase.core.Beer", resultConv.get("javaClass")); + assertEquals(false, resultConv.get("is_active")); + assertEquals("The Awesome Stout", resultConv.get("name")); + } +}