From 54c7ab6928b79f4ec7b7b169062bb071ce50767e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 7 Oct 2016 19:53:47 +0200 Subject: [PATCH] DATACASS-335 - Reuse Cluster connection resources during tests where possible. High client churn seems to affect Cassandra in a negative way (connection timeouts, driver considers hosts as down). Almost all integration tests bootstrap their own Cluster instance and dispose it once the tests has finished. Identify tests where reuse of a global Cluster instance provided by CassandraRule is possible and switch from context bootstrapping to reuse. This helps to prevent integration test failures. --- .../test/integration/CassandraRule.java | 74 ++++-- .../test/integration/KeyspaceRule.java | 47 ++-- .../config/cassandra-connection.properties | 1 + ...assandraBatchTemplateIntegrationTests.java | 89 ++++---- ...tionsRowValueProviderIntegrationTests.java | 80 +++---- ...nousCassandraTemplateIntegrationTests.java | 55 +++-- ...assandraAdminTemplateIntegrationTests.java | 32 +-- .../CassandraOperationsIntegrationTests.java | 45 ++-- .../CompositeKeyCrudIntegrationTests.java | 37 ++- .../CustomConversionTests.java | 65 +++--- ...ateMapIdProxyDelegateIntegrationTests.java | 60 +++-- ...CassandraTemplateMapIdIntegrationTest.java | 58 +++-- .../CassandraTypeMappingIntegrationTest.java | 211 +++++++++--------- .../integration/support/SchemaTestUtils.java | 68 ++++++ .../config/cassandra-connection.properties | 1 + 15 files changed, 455 insertions(+), 468 deletions(-) create mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/SchemaTestUtils.java diff --git a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/CassandraRule.java b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/CassandraRule.java index 7182dfe1d..79b57d804 100644 --- a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/CassandraRule.java +++ b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/CassandraRule.java @@ -16,13 +16,13 @@ package org.springframework.cassandra.test.integration; -import static org.apache.cassandra.db.marshal.CompositeType.build; import static org.springframework.cassandra.test.integration.CassandraRule.InvocationMode.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import org.junit.rules.ExternalResource; import org.springframework.cassandra.core.SessionCallback; @@ -36,6 +36,7 @@ import org.springframework.util.SocketUtils; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.QueryOptions; import com.datastax.driver.core.Session; +import com.datastax.driver.core.SocketOptions; /** * Rule to provide a Cassandra context for integration tests. This rule can use/spin up either an embedded Cassandra @@ -55,6 +56,8 @@ import com.datastax.driver.core.Session; */ public class CassandraRule extends ExternalResource { + private static ResourceHolder resourceHolder; + private final CassandraConnectionProperties properties = new CassandraConnectionProperties(); private final String configurationFileName; private final long startUpTimeout; @@ -318,28 +321,51 @@ public class CassandraRule extends ExternalResource { QueryOptions queryOptions = new QueryOptions(); queryOptions.setRefreshSchemaIntervalMillis(0); - cluster = new Cluster.Builder().addContactPoints(hostIp) // - .withPort(port) // - .withMaxSchemaAgreementWaitSeconds(3) // - .withQueryOptions(queryOptions) // - .withNettyOptions(FastShutdownNettyOptions.INSTANCE) // - .build(); + SocketOptions socketOptions = new SocketOptions(); + socketOptions.setConnectTimeoutMillis((int) TimeUnit.SECONDS.toMillis(15)); + socketOptions.setReadTimeoutMillis((int) TimeUnit.SECONDS.toMillis(15)); + + if (resourceHolder == null) { + + cluster = new Cluster.Builder().addContactPoints(hostIp) // + .withPort(port) // + .withQueryOptions(queryOptions) // + .withMaxSchemaAgreementWaitSeconds(3) // + .withSocketOptions(socketOptions) // + .withNettyOptions(FastShutdownNettyOptions.INSTANCE) // + .build(); + + if (properties.getBoolean("build.cassandra.reuse-cluster")) { + resourceHolder = new ResourceHolder(cluster, cluster.connect()); + } + } else { + cluster = resourceHolder.cluster; + } + } else { cluster = parent.cluster; cassandraPort = parent.cassandraPort; } - session = cluster.connect(); + if (parent != null) { + session = parent.getSession(); + } else if (resourceHolder == null) { + session = cluster.connect(); + } else { + session = resourceHolder.session; + } } private void cleanupConnection() { - if (parent == null) { - session.close(); - cluster.closeAsync(); - cluster = null; - } else { - session.closeAsync(); + if (resourceHolder == null) { + if (parent == null) { + session.close(); + cluster.closeAsync(); + cluster = null; + } else { + session.closeAsync(); + } } session = null; @@ -396,4 +422,24 @@ public class CassandraRule extends ExternalResource { } } + + private static class ResourceHolder { + + private Cluster cluster; + private Session session; + + public ResourceHolder(final Cluster cluster, final Session session) { + this.cluster = cluster; + this.session = session; + + Runtime.getRuntime().addShutdownHook(new Thread() { + + @Override + public void run() { + session.close(); + cluster.close(); + } + }); + } + } } diff --git a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/KeyspaceRule.java b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/KeyspaceRule.java index 5cc80a2f6..7c7984950 100644 --- a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/KeyspaceRule.java +++ b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/KeyspaceRule.java @@ -40,7 +40,7 @@ import com.datastax.driver.core.Session; */ public class KeyspaceRule extends ExternalResource { - private Cluster cluster; + private final CassandraRule cassandraRule; private Session session; private final String keyspaceName; @@ -65,43 +65,26 @@ public class KeyspaceRule extends ExternalResource { Assert.notNull(cassandraRule, "CassandraRule must not be null!"); Assert.hasText(keyspaceName, "KeyspaceName must not be empty!"); - // Support initialized and initializing CassandraRule. - if (cassandraRule.getCluster() != null) { - this.cluster = cassandraRule.getCluster(); - this.session = cluster.connect(); - } else { - cassandraRule.before(new SessionCallback() { - @Override - public Object doInSession(Session s) throws DataAccessException { - KeyspaceRule.this.cluster = s.getCluster(); - KeyspaceRule.this.session = cluster.connect(); - return null; - } - }); - } - this.keyspaceName = keyspaceName; - } - - /** - * Create a {@link KeyspaceRule} initialized with a {@link Cluster} for creating a keyspace using the given - * {@code keyspaceName}. - * - * @param cluster - * @param keyspaceName - */ - public KeyspaceRule(Cluster cluster, String keyspaceName) { - - Assert.notNull(cluster, "Cluster must not be null!"); - Assert.hasText(keyspaceName, "KeyspaceName must not be empty!"); - - this.cluster = cluster; - this.session = cluster.connect(); this.keyspaceName = keyspaceName; + this.cassandraRule = cassandraRule; } @Override protected void before() throws Throwable { + // Support initialized and initializing CassandraRule. + if (cassandraRule.getCluster() != null) { + this.session = cassandraRule.getSession(); + } else { + cassandraRule.before(new SessionCallback() { + @Override + public Object doInSession(Session s) throws DataAccessException { + KeyspaceRule.this.session = cassandraRule.getSession(); + return null; + } + }); + } + Assert.state(session != null, "Session was not initialized"); session.execute(String.format("CREATE KEYSPACE %s WITH durable_writes = false AND " diff --git a/spring-cql/src/test/resources/config/cassandra-connection.properties b/spring-cql/src/test/resources/config/cassandra-connection.properties index be62cb0ef..c28cb2327 100644 --- a/spring-cql/src/test/resources/config/cassandra-connection.properties +++ b/spring-cql/src/test/resources/config/cassandra-connection.properties @@ -6,3 +6,4 @@ build.cassandra.storage_port=@build.cassandra.storage_port@ build.cassandra.ssl_storage_port=@build.cassandra.ssl_storage_port@ build.cassandra.mode=@build.cassandra.mode@ build.cassandra.host=@build.cassandra.host@ +build.cassandra.reuse-cluster=true diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraBatchTemplateIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraBatchTemplateIntegrationTests.java index 577d152eb..e3d4dbc57 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraBatchTemplateIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraBatchTemplateIntegrationTests.java @@ -23,16 +23,11 @@ import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; import org.springframework.data.cassandra.domain.FlatGroup; import org.springframework.data.cassandra.domain.Group; import org.springframework.data.cassandra.domain.GroupKey; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -42,24 +37,20 @@ import com.datastax.driver.core.Row; * * @author Mark Paluch */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - public static class Config extends IntegrationTestConfig { - - @Override - public String[] getEntityBasePackages() { - return new String[] { Group.class.getPackage().getName() }; - } - } - - @Autowired CassandraTemplate cassandraTemplate; + CassandraTemplate template; @Before public void setUp() throws Exception { - cassandraTemplate.deleteAll(Group.class); + + template = new CassandraTemplate(session); + + SchemaTestUtils.potentiallyCreateTableFor(Group.class, template); + SchemaTestUtils.potentiallyCreateTableFor(FlatGroup.class, template); + + SchemaTestUtils.truncate(Group.class, template); + SchemaTestUtils.truncate(FlatGroup.class, template); } /** @@ -71,10 +62,10 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm Group walter = new Group(new GroupKey("users", "0x1", "walter")); Group mike = new Group(new GroupKey("users", "0x1", "mike")); - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.insert(walter).insert(mike).execute(); - Group loaded = cassandraTemplate.selectOneById(Group.class, walter.getId()); + Group loaded = template.selectOneById(Group.class, walter.getId()); assertThat(loaded.getId().getUsername(), is(equalTo(walter.getId().getUsername()))); } @@ -88,10 +79,10 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm Group walter = new Group(new GroupKey("users", "0x1", "walter")); Group mike = new Group(new GroupKey("users", "0x1", "mike")); - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.insert(Arrays.asList(walter, mike)).execute(); - Group loaded = cassandraTemplate.selectOneById(Group.class, walter.getId()); + Group loaded = template.selectOneById(Group.class, walter.getId()); assertThat(loaded.getId().getUsername(), is(equalTo(walter.getId().getUsername()))); } @@ -102,16 +93,16 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm @Test public void shouldUpdateEntities() { - Group walter = cassandraTemplate.insert(new Group(new GroupKey("users", "0x1", "walter"))); - Group mike = cassandraTemplate.insert(new Group(new GroupKey("users", "0x1", "mike"))); + Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter"))); + Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike"))); walter.setEmail("walter@white.com"); mike.setEmail("mike@sauls.com"); - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.update(walter).update(mike).execute(); - Group loaded = cassandraTemplate.selectOneById(Group.class, walter.getId()); + Group loaded = template.selectOneById(Group.class, walter.getId()); assertThat(loaded.getEmail(), is(equalTo(walter.getEmail()))); } @@ -122,16 +113,16 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm @Test public void shouldUpdateCollectionOfEntities() { - Group walter = cassandraTemplate.insert(new Group(new GroupKey("users", "0x1", "walter"))); - Group mike = cassandraTemplate.insert(new Group(new GroupKey("users", "0x1", "mike"))); + Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter"))); + Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike"))); walter.setEmail("walter@white.com"); mike.setEmail("mike@sauls.com"); - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.update(Arrays.asList(walter, mike)).execute(); - Group loaded = cassandraTemplate.selectOneById(Group.class, walter.getId()); + Group loaded = template.selectOneById(Group.class, walter.getId()); assertThat(loaded.getEmail(), is(equalTo(walter.getEmail()))); } @@ -142,16 +133,16 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm @Test public void shouldUpdatesCollectionOfEntities() { - FlatGroup walter = cassandraTemplate.insert(new FlatGroup("users", "0x1", "walter")); - FlatGroup mike = cassandraTemplate.insert(new FlatGroup("users", "0x1", "mike")); + FlatGroup walter = template.insert(new FlatGroup("users", "0x1", "walter")); + FlatGroup mike = template.insert(new FlatGroup("users", "0x1", "mike")); walter.setEmail("walter@white.com"); mike.setEmail("mike@sauls.com"); - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.update(Arrays.asList(walter, mike)).execute(); - FlatGroup loaded = cassandraTemplate.selectOneById(FlatGroup.class, walter); + FlatGroup loaded = template.selectOneById(FlatGroup.class, walter); assertThat(loaded.getEmail(), is(equalTo(walter.getEmail()))); } @@ -162,14 +153,14 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm @Test public void shouldDeleteEntities() { - Group walter = cassandraTemplate.insert(new Group(new GroupKey("users", "0x1", "walter"))); - Group mike = cassandraTemplate.insert(new Group(new GroupKey("users", "0x1", "mike"))); + Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter"))); + Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike"))); - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.delete(walter).delete(mike).execute(); - Group loaded = cassandraTemplate.selectOneById(Group.class, walter.getId()); + Group loaded = template.selectOneById(Group.class, walter.getId()); assertThat(loaded, is(nullValue())); } @@ -180,14 +171,14 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm @Test public void shouldDeleteCollectionOfEntities() { - Group walter = cassandraTemplate.insert(new Group(new GroupKey("users", "0x1", "walter"))); - Group mike = cassandraTemplate.insert(new Group(new GroupKey("users", "0x1", "mike"))); + Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter"))); + Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike"))); - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.delete(Arrays.asList(walter, mike)).execute(); - Group loaded = cassandraTemplate.selectOneById(Group.class, walter.getId()); + Group loaded = template.selectOneById(Group.class, walter.getId()); assertThat(loaded, is(nullValue())); } @@ -206,10 +197,10 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm long timestamp = (System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1)) * 1000; - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.insert(walter).insert(mike).withTimestamp(timestamp).execute(); - ResultSet resultSet = cassandraTemplate.query("SELECT writetime(email) FROM group;"); + ResultSet resultSet = template.query("SELECT writetime(email) FROM group;"); assertThat(resultSet.getAvailableWithoutFetching(), is(2)); @@ -224,7 +215,7 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm @Test(expected = IllegalStateException.class) public void shouldNotExecuteTwice() { - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.insert(new Group(new GroupKey("users", "0x1", "walter"))).execute(); batchOperations.execute(); @@ -238,7 +229,7 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractSpringDataEm @Test(expected = IllegalStateException.class) public void shouldNotAllowModificationAfterExecution() { - CassandraBatchOperations batchOperations = new CassandraBatchTemplate(cassandraTemplate); + CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template); batchOperations.insert(new Group(new GroupKey("users", "0x1", "walter"))).execute(); batchOperations.update(new Group()); diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/collections/CollectionsRowValueProviderIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/collections/CollectionsRowValueProviderIntegrationTests.java index 4e75ef619..d02eb2a37 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/collections/CollectionsRowValueProviderIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/collections/CollectionsRowValueProviderIntegrationTests.java @@ -15,6 +15,8 @@ */ package org.springframework.data.cassandra.test.integration.collections; +import static org.junit.Assert.*; + import java.io.IOException; import java.util.Date; import java.util.HashMap; @@ -24,45 +26,39 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.test.integration.simpletons.Book; import org.springframework.data.cassandra.test.integration.simpletons.BookHistory; import org.springframework.data.cassandra.test.integration.simpletons.BookReference; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Select; /** * @author dwebb + * @author Mark Paluch */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CollectionsRowValueProviderIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class CollectionsRowValueProviderIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - public static class Config extends IntegrationTestConfig { - - @Override - public String[] getEntityBasePackages() { - return new String[] { Book.class.getPackage().getName() }; - } - } - - @Autowired CassandraOperations template; + CassandraOperations operations; @Before public void before() throws IOException { - deleteAllEntities(); + + operations = new CassandraTemplate(session); + + SchemaTestUtils.potentiallyCreateTableFor(Book.class, operations); + SchemaTestUtils.potentiallyCreateTableFor(BookHistory.class, operations); + SchemaTestUtils.potentiallyCreateTableFor(BookReference.class, operations); + + SchemaTestUtils.truncate(Book.class, operations); + SchemaTestUtils.truncate(BookHistory.class, operations); + SchemaTestUtils.truncate(BookReference.class, operations); } @Test @@ -83,22 +79,16 @@ public class CollectionsRowValueProviderIntegrationTests extends AbstractSpringD b1.setCheckOuts(checkOutMap); - template.insert(b1); + operations.insert(b1); Select select = QueryBuilder.select().all().from("bookHistory"); select.where(QueryBuilder.eq("isbn", "123456-1")); - BookHistory b = template.selectOne(select, BookHistory.class); + BookHistory b = operations.selectOne(select, BookHistory.class); - Assert.assertNotNull(b.getCheckOuts()); - - log.debug("Checkouts map data"); - for (String username : b.getCheckOuts().keySet()) { - log.debug(username + " has " + b.getCheckOuts().get(username) + " checkouts of this book."); - } - - Assert.assertEquals(b.getTitle(), "Spring Data Cassandra Guide"); - Assert.assertEquals(b.getAuthor(), "Cassandra Guru"); + assertNotNull(b.getCheckOuts()); + assertEquals(b.getTitle(), "Spring Data Cassandra Guide"); + assertEquals(b.getAuthor(), "Cassandra Guru"); } @@ -125,28 +115,16 @@ public class CollectionsRowValueProviderIntegrationTests extends AbstractSpringD marks.add(144); b1.setBookmarks(marks); - template.insert(b1); + operations.insert(b1); Select select = QueryBuilder.select().all().from("bookReference"); select.where(QueryBuilder.eq("isbn", "123456-1")); - BookReference b = template.selectOne(select, BookReference.class); - - Assert.assertNotNull(b.getReferences()); - Assert.assertNotNull(b.getBookmarks()); - - log.debug("Bookmark List Data"); - for (Integer mark : b.getBookmarks()) { - log.debug("Bookmark set on page " + mark); - } - - log.debug("Reference Set Data"); - for (String ref : b.getReferences()) { - log.debug("Reference -> " + ref); - } - - Assert.assertEquals(b.getTitle(), "Spring Data Cassandra Guide"); - Assert.assertEquals(b.getAuthor(), "Cassandra Guru"); + BookReference b = operations.selectOne(select, BookReference.class); + assertNotNull(b.getReferences()); + assertNotNull(b.getBookmarks()); + assertEquals(b.getTitle(), "Spring Data Cassandra Guide"); + assertEquals(b.getAuthor(), "Cassandra Guru"); } } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/AsynchronousCassandraTemplateIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/AsynchronousCassandraTemplateIntegrationTests.java index 5dda280cc..857ac50c9 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/AsynchronousCassandraTemplateIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/AsynchronousCassandraTemplateIntegrationTests.java @@ -24,21 +24,16 @@ import java.util.Collection; import java.util.UUID; import java.util.concurrent.CancellationException; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cassandra.core.Cancellable; import org.springframework.cassandra.core.ConsistencyLevel; import org.springframework.cassandra.core.PrimaryKeyType; import org.springframework.cassandra.core.RetryPolicy; import org.springframework.cassandra.core.WriteOptions; import org.springframework.cassandra.support.exception.CassandraConnectionFailureException; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; import org.springframework.cassandra.test.integration.support.ObjectListener; -import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.core.DeletionListener; @@ -46,11 +41,12 @@ import org.springframework.data.cassandra.core.WriteListener; import org.springframework.data.cassandra.mapping.Column; import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; import org.springframework.data.cassandra.mapping.Table; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; import org.springframework.data.cassandra.test.integration.support.TestListener; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; /** * Integration tests for asynchronous {@link CassandraTemplate} operations. @@ -58,15 +54,17 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * @author Matthew T. Adams * @author Mark Paluch */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class AsynchronousCassandraTemplateIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class AsynchronousCassandraTemplateIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { - @Autowired CassandraOperations cassandraOperations; + CassandraOperations operations; @Before public void before() { - deleteAllEntities(); + + operations = new CassandraTemplate(session); + + SchemaTestUtils.potentiallyCreateTableFor(Person.class, operations); + SchemaTestUtils.truncate(Person.class, operations); } @Test @@ -84,7 +82,7 @@ public class AsynchronousCassandraTemplateIntegrationTests extends AbstractSprin Person person = Person.random(); PersonListener listener = new PersonListener(); - cassandraOperations.insertAsynchronously(person, listener, new WriteOptions(cl, RetryPolicy.LOGGING)); + operations.insertAsynchronously(person, listener, new WriteOptions(cl, RetryPolicy.LOGGING)); listener.await(); if (listener.exception != null) { @@ -112,9 +110,9 @@ public class AsynchronousCassandraTemplateIntegrationTests extends AbstractSprin Cancellable cancellable; if (insert) { - cancellable = cassandraOperations.insertAsynchronously(person, listener, null); + cancellable = operations.insertAsynchronously(person, listener, null); } else { - cancellable = cassandraOperations.updateAsynchronously(person, listener, null); + cancellable = operations.updateAsynchronously(person, listener, null); } cancellable.cancel(); listener.await(); @@ -145,10 +143,10 @@ public class AsynchronousCassandraTemplateIntegrationTests extends AbstractSprin Person person = Person.random(); person.setFirstname("Homer"); - cassandraOperations.insert(person); + operations.insert(person); PersonListener listener = new PersonListener(); - cassandraOperations.updateAsynchronously(person, listener, new WriteOptions(cl, RetryPolicy.LOGGING)); + operations.updateAsynchronously(person, listener, new WriteOptions(cl, RetryPolicy.LOGGING)); listener.await(); if (listener.exception != null) { @@ -172,16 +170,16 @@ public class AsynchronousCassandraTemplateIntegrationTests extends AbstractSprin Person person = Person.random(); - cassandraOperations.insert(person); + operations.insert(person); PersonListener listener = new PersonListener(); - cassandraOperations.deleteAsynchronously(person, listener, new WriteOptions(cl, RetryPolicy.LOGGING)); + operations.deleteAsynchronously(person, listener, new WriteOptions(cl, RetryPolicy.LOGGING)); listener.await(); if (listener.exception != null) { throw listener.exception; } - assertFalse(cassandraOperations.exists(Person.class, id("id", person.id))); + assertFalse(operations.exists(Person.class, id("id", person.id))); } @Test(expected = CancellationException.class) @@ -189,7 +187,7 @@ public class AsynchronousCassandraTemplateIntegrationTests extends AbstractSprin Person person = Person.random(); PersonListener listener = new PersonListener(); - cassandraOperations.deleteAsynchronously(person, listener, null).cancel(); + operations.deleteAsynchronously(person, listener, null).cancel(); listener.await(); // if listener.success is true then the @@ -211,12 +209,12 @@ public class AsynchronousCassandraTemplateIntegrationTests extends AbstractSprin public void shouldSelectOneAsynchronously() throws Exception { Person person = Person.random(); - cassandraOperations.insert(person); + operations.insert(person); ObjectListener objectListener = ObjectListener.create(); String cql = String.format("SELECT * from person where id = '%s'", person.id); - cassandraOperations.selectOneAsynchronously(cql, Person.class, objectListener); + operations.selectOneAsynchronously(cql, Person.class, objectListener); objectListener.await(); assertThat(objectListener.getResult(), is(notNullValue())); @@ -232,15 +230,12 @@ public class AsynchronousCassandraTemplateIntegrationTests extends AbstractSprin ObjectListener objectListener = ObjectListener.create(); String cql = String.format("SELECT * from person where id = '%s'", "unknown"); - cassandraOperations.selectOneAsynchronously(cql, Person.class, objectListener); + operations.selectOneAsynchronously(cql, Person.class, objectListener); objectListener.await(); assertThat(objectListener.getResult(), is(nullValue())); } - @Configuration - public static class Config extends IntegrationTestConfig {} - @Table @Data @AllArgsConstructor diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/CassandraAdminTemplateIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/CassandraAdminTemplateIntegrationTests.java index 6e9564b5a..02b68ca55 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/CassandraAdminTemplateIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/CassandraAdminTemplateIntegrationTests.java @@ -22,21 +22,15 @@ import java.util.Collection; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cassandra.core.cql.CqlIdentifier; import org.springframework.cassandra.core.keyspace.DropTableSpecification; -import org.springframework.context.annotation.Configuration; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; +import org.springframework.data.cassandra.convert.MappingCassandraConverter; import org.springframework.data.cassandra.core.CassandraAdminTemplate; import org.springframework.data.cassandra.test.integration.simpletons.Book; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.datastax.driver.core.KeyspaceMetadata; import com.datastax.driver.core.Metadata; -import com.datastax.driver.core.Session; import com.datastax.driver.core.TableMetadata; /** @@ -44,23 +38,15 @@ import com.datastax.driver.core.TableMetadata; * * @author Mark Paluch */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CassandraAdminTemplateIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class CassandraAdminTemplateIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - public static class Config extends IntegrationTestConfig { - - @Override - public String[] getEntityBasePackages() { - return new String[] { Book.class.getPackage().getName() }; - } - } - - @Autowired private CassandraAdminTemplate cassandraAdminTemplate; + private CassandraAdminTemplate cassandraAdminTemplate; @Before public void before() { + + cassandraAdminTemplate = new CassandraAdminTemplate(session, new MappingCassandraConverter()); + KeyspaceMetadata keyspace = getKeyspaceMetadata(); Collection tables = keyspace.getTables(); for (TableMetadata table : tables) { @@ -73,10 +59,6 @@ public class CassandraAdminTemplateIntegrationTests extends AbstractSpringDataEm return metadata.getKeyspace(getSession().getLoggedKeyspace()); } - private Session getSession() { - return cassandraAdminTemplate.getSession(); - } - /** * @see DATACASS-173 */ diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/CassandraOperationsIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/CassandraOperationsIntegrationTests.java index aba473d43..963691c27 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/CassandraOperationsIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/core/CassandraOperationsIntegrationTests.java @@ -28,54 +28,46 @@ 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.cassandra.core.ConsistencyLevel; import org.springframework.cassandra.core.QueryOptions; import org.springframework.cassandra.core.RetryPolicy; import org.springframework.cassandra.core.WriteOptions; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; +import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.domain.UserToken; import org.springframework.data.cassandra.repository.support.BasicMapId; import org.springframework.data.cassandra.test.integration.simpletons.Book; import org.springframework.data.cassandra.test.integration.simpletons.BookCondition; import org.springframework.data.cassandra.test.integration.simpletons.BookReference; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Select; import com.datastax.driver.core.utils.UUIDs; /** - * Integration tests for {@link CassandraOperations}. + * Integration tests for {@link CassandraTemplate}. * * @author David Webb * @author Mark Paluch * @author John Blum */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CassandraOperationsIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class CassandraOperationsIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - public static class Config extends IntegrationTestConfig { - - @Override - public String[] getEntityBasePackages() { - return new String[] { Book.class.getPackage().getName(), UserToken.class.getPackage().getName() }; - } - } - - @Autowired - CassandraOperations template; + CassandraTemplate template; @Before public void before() { - deleteAllEntities(); + + template = new CassandraTemplate(session); + + SchemaTestUtils.potentiallyCreateTableFor(Book.class, template); + SchemaTestUtils.potentiallyCreateTableFor(BookReference.class, template); + SchemaTestUtils.potentiallyCreateTableFor(UserToken.class, template); + + SchemaTestUtils.truncate(Book.class, template); + SchemaTestUtils.truncate(BookReference.class, template); + SchemaTestUtils.truncate(UserToken.class, template); } @Test @@ -591,9 +583,6 @@ public class CassandraOperationsIntegrationTests extends AbstractSpringDataEmbed Book book = template.selectOne(select, Book.class); - log.debug("SingleSelect Book Title -> " + book.getTitle()); - log.debug("SingleSelect Book Author -> " + book.getAuthor()); - assertThat(book.getTitle(), is(equalTo("Spring Data Cassandra Guide"))); assertThat(book.getAuthor(), is(equalTo("Cassandra Guru"))); @@ -610,8 +599,6 @@ public class CassandraOperationsIntegrationTests extends AbstractSpringDataEmbed List selectedBooks = template.select(select, Book.class); - log.debug("Book Count -> " + selectedBooks.size()); - assertThat(selectedBooks.size(), is(equalTo(20))); for (Book book : selectedBooks) { diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/forcequote/compositeprimarykey/CompositeKeyCrudIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/forcequote/compositeprimarykey/CompositeKeyCrudIntegrationTests.java index 74d1f42e4..479ed8752 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/forcequote/compositeprimarykey/CompositeKeyCrudIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/forcequote/compositeprimarykey/CompositeKeyCrudIntegrationTests.java @@ -25,17 +25,12 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cassandra.core.QueryOptions; -import org.springframework.context.annotation.Configuration; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; +import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.data.cassandra.core.CassandraTemplate; -import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; import org.springframework.data.cassandra.test.integration.forcequote.compositeprimarykey.entity.CorrelationEntity; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; import com.datastax.driver.core.ConsistencyLevel; import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy; @@ -45,21 +40,20 @@ import com.datastax.driver.core.querybuilder.Select; /** * @author Mark Paluch */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CompositeKeyCrudIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class CompositeKeyCrudIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - @EnableCassandraRepositories(basePackageClasses = ImplicitRepository.class) - public static class Config extends IntegrationTestConfig {} - - @Autowired private CassandraTemplate template1; + CassandraOperations operations; private CorrelationEntity correlationEntity1, correlationEntity2; @Before public void setUp() throws Throwable { + operations = new CassandraTemplate(session); + + SchemaTestUtils.potentiallyCreateTableFor(CorrelationEntity.class, operations); + SchemaTestUtils.truncate(CorrelationEntity.class, operations); + Map map1 = new HashMap(2); map1.put("v", "1"); map1.put("labels", "1,2,3"); @@ -73,14 +67,15 @@ public class CompositeKeyCrudIntegrationTests extends AbstractSpringDataEmbedded @Test public void test() { - template1.insert(correlationEntity1); - template1.insert(correlationEntity2); + + operations.insert(correlationEntity1); + operations.insert(correlationEntity2); Select select = QueryBuilder.select().from("identity_correlations"); select.where(QueryBuilder.eq("type", "a")).and(QueryBuilder.eq("value", "b")); select.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE); select.setConsistencyLevel(ConsistencyLevel.ONE); - List correlationEntities = template1.select(select, CorrelationEntity.class); + List correlationEntities = operations.select(select, CorrelationEntity.class); assertEquals(2, correlationEntities.size()); @@ -89,9 +84,9 @@ public class CompositeKeyCrudIntegrationTests extends AbstractSpringDataEmbedded ArrayList entities = new ArrayList(); entities.add(correlationEntity1); entities.add(correlationEntity2); - template1.delete(entities, qo); + operations.delete(entities, qo); - correlationEntities = template1.select(select, CorrelationEntity.class); + correlationEntities = operations.select(select, CorrelationEntity.class); assertEquals(0, correlationEntities.size()); } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/customconversion/CustomConversionTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/customconversion/CustomConversionTests.java index 27ea39eee..e865c9983 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/customconversion/CustomConversionTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/customconversion/CustomConversionTests.java @@ -28,20 +28,15 @@ import java.util.Set; import org.codehaus.jackson.map.ObjectMapper; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; import org.springframework.core.convert.converter.Converter; import org.springframework.data.annotation.Id; -import org.springframework.data.cassandra.config.SchemaAction; import org.springframework.data.cassandra.convert.CustomConversions; -import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.convert.MappingCassandraConverter; +import org.springframework.data.cassandra.core.CassandraTemplate; +import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext; import org.springframework.data.cassandra.mapping.Table; -import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; -import org.springframework.data.cassandra.test.integration.repository.querymethods.datekey.DateThingRepo; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; import org.springframework.util.StringUtils; import com.datastax.driver.core.Row; @@ -57,40 +52,30 @@ import lombok.NoArgsConstructor; * * @author Mark Paluch */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CustomConversionTests { +public class CustomConversionTests extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - @EnableCassandraRepositories(basePackageClasses = DateThingRepo.class) - public static class Config extends IntegrationTestConfig { - - @Override - public String[] getEntityBasePackages() { - return new String[] { Employee.class.getPackage().getName() }; - } - - @Override - public SchemaAction getSchemaAction() { - return SchemaAction.RECREATE_DROP_UNUSED; - } - - @Override - public CustomConversions customConversions() { - - List> converters = new ArrayList>(); - converters.add(new PersonReadConverter()); - converters.add(new PersonWriteConverter()); - - return new CustomConversions(converters); - } - } - - @Autowired CassandraOperations cassandraOperations; + CassandraTemplate cassandraOperations; @Before public void setUp() { - cassandraOperations.deleteAll(Employee.class); + + List> converters = new ArrayList>(); + converters.add(new PersonReadConverter()); + converters.add(new PersonWriteConverter()); + CustomConversions customConversions = new CustomConversions(converters); + + BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext(); + mappingContext.setCustomConversions(customConversions); + mappingContext.afterPropertiesSet(); + + MappingCassandraConverter converter = new MappingCassandraConverter(mappingContext); + converter.setCustomConversions(customConversions); + converter.afterPropertiesSet(); + + cassandraOperations = new CassandraTemplate(session, converter); + + SchemaTestUtils.potentiallyCreateTableFor(Employee.class, cassandraOperations); + SchemaTestUtils.truncate(Employee.class, cassandraOperations); } /** diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/mapid/proxy/CassandraTemplateMapIdProxyDelegateIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/mapid/proxy/CassandraTemplateMapIdProxyDelegateIntegrationTests.java index 2ac30418b..e7fa6b3b7 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/mapid/proxy/CassandraTemplateMapIdProxyDelegateIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/mapid/proxy/CassandraTemplateMapIdProxyDelegateIntegrationTests.java @@ -20,44 +20,36 @@ import static org.springframework.data.cassandra.repository.support.MapIdFactory import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cassandra.core.PrimaryKeyType; -import org.springframework.context.annotation.Configuration; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.mapping.Column; import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; import org.springframework.data.cassandra.mapping.Table; import org.springframework.data.cassandra.repository.MapId; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; /** * Integration tests for {@link org.springframework.data.cassandra.core.CassandraTemplate} using {@link MapId}. * * @author Matthew T. Adams + * @author Mark Paluch */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CassandraTemplateMapIdProxyDelegateIntegrationTests - extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class CassandraTemplateMapIdProxyDelegateIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - public static class Config extends IntegrationTestConfig { - - @Override - public String[] getEntityBasePackages() { - return new String[] { SinglePkc.class.getPackage().getName() }; - } - } - - @Autowired CassandraOperations template; + CassandraOperations operations; @Before public void before() { - assertNotNull(template); + + operations = new CassandraTemplate(session); + + SchemaTestUtils.potentiallyCreateTableFor(SinglePkc.class, operations); + SchemaTestUtils.potentiallyCreateTableFor(MultiPkc.class, operations); + + SchemaTestUtils.truncate(SinglePkc.class, operations); + SchemaTestUtils.truncate(MultiPkc.class, operations); } @Test @@ -66,28 +58,28 @@ public class CassandraTemplateMapIdProxyDelegateIntegrationTests // insert SinglePkc inserted = new SinglePkc(uuid()); inserted.setValue(uuid()); - SinglePkc saved = template.insert(inserted); + SinglePkc saved = operations.insert(inserted); assertSame(saved, inserted); // select SinglePkcId id = id(SinglePkcId.class).key(saved.getKey()); - SinglePkc selected = template.selectOneById(SinglePkc.class, id); + SinglePkc selected = operations.selectOneById(SinglePkc.class, id); assertNotSame(selected, saved); assertEquals(saved.getKey(), selected.getKey()); assertEquals(saved.getValue(), selected.getValue()); // update selected.setValue(uuid()); - SinglePkc updated = template.update(selected); + SinglePkc updated = operations.update(selected); assertSame(updated, selected); - selected = template.selectOneById(SinglePkc.class, id); + selected = operations.selectOneById(SinglePkc.class, id); assertNotSame(selected, updated); assertEquals(updated.getValue(), selected.getValue()); // delete - template.delete(selected); - assertNull(template.selectOneById(SinglePkc.class, id)); + operations.delete(selected); + assertNull(operations.selectOneById(SinglePkc.class, id)); } public interface SinglePkcId { @@ -137,12 +129,12 @@ public class CassandraTemplateMapIdProxyDelegateIntegrationTests // insert MultiPkc inserted = new MultiPkc(uuid(), uuid()); inserted.setValue(uuid()); - MultiPkc saved = template.insert(inserted); + MultiPkc saved = operations.insert(inserted); assertSame(saved, inserted); // select MultiPkcId id = id(MultiPkcId.class).key0(saved.getKey0()).key1(saved.getKey1()); - MultiPkc selected = template.selectOneById(MultiPkc.class, id); + MultiPkc selected = operations.selectOneById(MultiPkc.class, id); assertNotSame(selected, saved); assertEquals(saved.getKey0(), selected.getKey0()); assertEquals(saved.getKey1(), selected.getKey1()); @@ -150,16 +142,16 @@ public class CassandraTemplateMapIdProxyDelegateIntegrationTests // update selected.setValue(uuid()); - MultiPkc updated = template.update(selected); + MultiPkc updated = operations.update(selected); assertSame(updated, selected); - selected = template.selectOneById(MultiPkc.class, id); + selected = operations.selectOneById(MultiPkc.class, id); assertNotSame(selected, updated); assertEquals(updated.getValue(), selected.getValue()); // delete - template.delete(selected); - assertNull(template.selectOneById(MultiPkc.class, id)); + operations.delete(selected); + assertNull(operations.selectOneById(MultiPkc.class, id)); } public interface MultiPkcId { diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/mapid/template/CassandraTemplateMapIdIntegrationTest.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/mapid/template/CassandraTemplateMapIdIntegrationTest.java index cfd626d9c..dadf645ae 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/mapid/template/CassandraTemplateMapIdIntegrationTest.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/mapid/template/CassandraTemplateMapIdIntegrationTest.java @@ -21,43 +21,35 @@ import static org.springframework.data.cassandra.repository.support.BasicMapId.* import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cassandra.core.PrimaryKeyType; -import org.springframework.context.annotation.Configuration; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.mapping.Column; import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; import org.springframework.data.cassandra.mapping.Table; import org.springframework.data.cassandra.repository.MapId; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; /** * Integration tests for {@link org.springframework.data.cassandra.core.CassandraTemplate} with {@link MapId}. * * @author Matthew T. Adams */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CassandraTemplateMapIdIntegrationTest extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class CassandraTemplateMapIdIntegrationTest extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - public static class Config extends IntegrationTestConfig { - - @Override - public String[] getEntityBasePackages() { - return new String[] { SinglePkc.class.getPackage().getName() }; - } - } - - @Autowired CassandraOperations template; + CassandraOperations operations; @Before public void before() { - assertNotNull(template); + + operations = new CassandraTemplate(session); + + SchemaTestUtils.potentiallyCreateTableFor(SinglePkc.class, operations); + SchemaTestUtils.potentiallyCreateTableFor(MultiPkc.class, operations); + + SchemaTestUtils.truncate(SinglePkc.class, operations); + SchemaTestUtils.truncate(MultiPkc.class, operations); } @Test @@ -66,28 +58,28 @@ public class CassandraTemplateMapIdIntegrationTest extends AbstractSpringDataEmb // insert SinglePkc inserted = new SinglePkc(uuid()); inserted.setValue(uuid()); - SinglePkc saved = template.insert(inserted); + SinglePkc saved = operations.insert(inserted); assertSame(saved, inserted); // select MapId id = id("key", saved.getKey()); - SinglePkc selected = template.selectOneById(SinglePkc.class, id); + SinglePkc selected = operations.selectOneById(SinglePkc.class, id); assertNotSame(selected, saved); assertEquals(saved.getKey(), selected.getKey()); assertEquals(saved.getValue(), selected.getValue()); // update selected.setValue(uuid()); - SinglePkc updated = template.update(selected); + SinglePkc updated = operations.update(selected); assertSame(updated, selected); - selected = template.selectOneById(SinglePkc.class, id); + selected = operations.selectOneById(SinglePkc.class, id); assertNotSame(selected, updated); assertEquals(updated.getValue(), selected.getValue()); // delete - template.delete(selected); - assertNull(template.selectOneById(SinglePkc.class, id)); + operations.delete(selected); + assertNull(operations.selectOneById(SinglePkc.class, id)); } @Table @@ -131,12 +123,12 @@ public class CassandraTemplateMapIdIntegrationTest extends AbstractSpringDataEmb // insert MultiPkc inserted = new MultiPkc(uuid(), uuid()); inserted.setValue(uuid()); - MultiPkc saved = template.insert(inserted); + MultiPkc saved = operations.insert(inserted); assertSame(saved, inserted); // select MapId id = id("key0", saved.getKey0()).with("key1", saved.getKey1()); - MultiPkc selected = template.selectOneById(MultiPkc.class, id); + MultiPkc selected = operations.selectOneById(MultiPkc.class, id); assertNotSame(selected, saved); assertEquals(saved.getKey0(), selected.getKey0()); assertEquals(saved.getKey1(), selected.getKey1()); @@ -144,16 +136,16 @@ public class CassandraTemplateMapIdIntegrationTest extends AbstractSpringDataEmb // update selected.setValue(uuid()); - MultiPkc updated = template.update(selected); + MultiPkc updated = operations.update(selected); assertSame(updated, selected); - selected = template.selectOneById(MultiPkc.class, id); + selected = operations.selectOneById(MultiPkc.class, id); assertNotSame(selected, updated); assertEquals(updated.getValue(), selected.getValue()); // delete - template.delete(selected); - assertNull(template.selectOneById(MultiPkc.class, id)); + operations.delete(selected); + assertNull(operations.selectOneById(MultiPkc.class, id)); } @Table diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/types/CassandraTypeMappingIntegrationTest.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/types/CassandraTypeMappingIntegrationTest.java index 13a2c38a7..4d3f964e4 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/types/CassandraTypeMappingIntegrationTest.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/types/CassandraTypeMappingIntegrationTest.java @@ -13,7 +13,6 @@ * see the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.cassandra.test.integration.mapping.types; import static org.hamcrest.CoreMatchers.*; @@ -33,15 +32,11 @@ import java.util.UUID; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.domain.AllPossibleTypes; -import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.cassandra.test.integration.support.SchemaTestUtils; import com.datastax.driver.core.BoundStatement; import com.datastax.driver.core.LocalDate; @@ -57,24 +52,20 @@ import com.datastax.driver.core.querybuilder.QueryBuilder; * @soundtrack DJ THT meets Scarlet - Live 2 Dance (Extended Mix) (Zgin Remix) */ @SuppressWarnings("Since15") -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbeddedCassandraIntegrationTest { +public class CassandraTypeMappingIntegrationTest extends AbstractKeyspaceCreatingIntegrationTest { - @Configuration - public static class Config extends IntegrationTestConfig { - - @Override - public String[] getEntityBasePackages() { - return new String[] { AllPossibleTypes.class.getPackage().getName(), CounterEntity.class.getPackage().getName() }; - } - } - - @Autowired CassandraOperations cassandraOperations; + CassandraOperations operations; @Before - public void setUp() { - cassandraOperations.deleteAll(AllPossibleTypes.class); + public void before() { + + operations = new CassandraTemplate(session); + + SchemaTestUtils.potentiallyCreateTableFor(AllPossibleTypes.class, operations); + SchemaTestUtils.potentiallyCreateTableFor(TimeEntity.class, operations); + + SchemaTestUtils.truncate(AllPossibleTypes.class, operations); + SchemaTestUtils.truncate(TimeEntity.class, operations); } /** @@ -86,8 +77,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setInet(InetAddress.getByName("127.0.0.1")); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getInet(), is(equalTo(entity.getInet()))); } @@ -101,8 +92,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setUuid(UUID.randomUUID()); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getUuid(), is(equalTo(entity.getUuid()))); } @@ -116,8 +107,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBoxedShort(Short.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBoxedShort(), is(equalTo(entity.getBoxedShort()))); } @@ -131,8 +122,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setPrimitiveShort(Short.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getPrimitiveShort(), is(equalTo(entity.getPrimitiveShort()))); } @@ -146,8 +137,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBoxedByte(Byte.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBoxedByte(), is(equalTo(entity.getBoxedByte()))); } @@ -161,8 +152,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setPrimitiveByte(Byte.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getPrimitiveByte(), is(equalTo(entity.getPrimitiveByte()))); } @@ -176,8 +167,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBoxedLong(Long.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBoxedLong(), is(equalTo(entity.getBoxedLong()))); } @@ -191,8 +182,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setPrimitiveLong(Long.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getPrimitiveLong(), is(equalTo(entity.getPrimitiveLong()))); } @@ -206,8 +197,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBoxedInteger(Integer.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBoxedInteger(), is(equalTo(entity.getBoxedInteger()))); } @@ -221,8 +212,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setPrimitiveInteger(Integer.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getPrimitiveInteger(), is(equalTo(entity.getPrimitiveInteger()))); } @@ -236,8 +227,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBoxedFloat(Float.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBoxedFloat(), is(equalTo(entity.getBoxedFloat()))); } @@ -251,8 +242,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setPrimitiveFloat(Float.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getPrimitiveFloat(), is(equalTo(entity.getPrimitiveFloat()))); } @@ -266,8 +257,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBoxedDouble(Double.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBoxedDouble(), is(equalTo(entity.getBoxedDouble()))); } @@ -281,8 +272,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setPrimitiveDouble(Double.MAX_VALUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getPrimitiveDouble(), is(equalTo(entity.getPrimitiveDouble()))); } @@ -296,8 +287,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBoxedBoolean(Boolean.TRUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBoxedBoolean(), is(equalTo(entity.getBoxedBoolean()))); } @@ -311,8 +302,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setPrimitiveBoolean(Boolean.TRUE); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.isPrimitiveBoolean(), is(equalTo(entity.isPrimitiveBoolean()))); } @@ -327,8 +318,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setTimestamp(new Date(1)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getTimestamp(), is(equalTo(entity.getTimestamp()))); } @@ -342,8 +333,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setDate(LocalDate.fromDaysSinceEpoch(1)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getDate(), is(equalTo(entity.getDate()))); } @@ -357,8 +348,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBigInteger(new BigInteger("123456")); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBigInteger(), is(equalTo(entity.getBigInteger()))); } @@ -372,8 +363,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBigDecimal(new BigDecimal("123456.7890123")); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBigDecimal(), is(equalTo(entity.getBigDecimal()))); } @@ -387,8 +378,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBlob(ByteBuffer.wrap("Hello".getBytes())); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); ByteBuffer blob = loaded.getBlob(); byte[] bytes = new byte[blob.remaining()]; @@ -405,8 +396,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setSetOfString(Collections.singleton("hello")); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getSetOfString(), is(equalTo(entity.getSetOfString()))); } @@ -420,8 +411,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setSetOfString(new HashSet()); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getSetOfString(), is(nullValue())); } @@ -435,8 +426,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setListOfString(Collections.singletonList("hello")); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getListOfString(), is(equalTo(entity.getListOfString()))); } @@ -450,8 +441,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setListOfString(new ArrayList()); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getListOfString(), is(nullValue())); } @@ -465,8 +456,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setMapOfString(Collections.singletonMap("hello", "world")); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getMapOfString(), is(equalTo(entity.getMapOfString()))); } @@ -480,8 +471,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setMapOfString(new HashMap()); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getMapOfString(), is(nullValue())); } @@ -495,8 +486,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setAnEnum(Condition.MINT); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getAnEnum(), is(equalTo(entity.getAnEnum()))); } @@ -512,12 +503,12 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed String id = "1"; long time = 21312214L; - PreparedStatement prepare = cassandraOperations.getSession() + PreparedStatement prepare = operations.getSession() .prepare("INSERT INTO timeentity (id, time) values(?,?)"); BoundStatement boundStatement = prepare.bind(id, time); - cassandraOperations.execute(boundStatement); + operations.execute(boundStatement); - TimeEntity loaded = cassandraOperations.selectOneById(TimeEntity.class, id); + TimeEntity loaded = operations.selectOneById(TimeEntity.class, id); assertThat(loaded.getTime(), is(equalTo(time))); } @@ -531,8 +522,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setLocalDate(java.time.LocalDate.of(2010, 7, 4)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getLocalDate(), is(equalTo(entity.getLocalDate()))); } @@ -546,8 +537,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setLocalDateTime(java.time.LocalDateTime.of(2010, 7, 4, 1, 2, 3)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getLocalDateTime(), is(equalTo(entity.getLocalDateTime()))); } @@ -561,8 +552,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setLocalTime(java.time.LocalTime.of(1, 2, 3)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getLocalTime(), is(equalTo(entity.getLocalTime()))); } @@ -576,8 +567,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setInstant(java.time.Instant.now()); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getInstant(), is(equalTo(entity.getInstant()))); } @@ -591,8 +582,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setZoneId(java.time.ZoneId.of("Europe/Paris")); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getZoneId(), is(equalTo(entity.getZoneId()))); } @@ -606,8 +597,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setJodaLocalDate(new org.joda.time.LocalDate(2010, 7, 4)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getJodaLocalDate(), is(equalTo(entity.getJodaLocalDate()))); } @@ -621,8 +612,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setJodaDateMidnight(new org.joda.time.DateMidnight(2010, 7, 4)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getJodaDateMidnight(), is(equalTo(entity.getJodaDateMidnight()))); } @@ -636,8 +627,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setJodaDateTime(new org.joda.time.DateTime(2010, 7, 4, 1, 2, 3)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getJodaDateTime(), is(equalTo(entity.getJodaDateTime()))); } @@ -651,8 +642,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBpLocalDate(org.threeten.bp.LocalDate.of(2010, 7, 4)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBpLocalDate(), is(equalTo(entity.getBpLocalDate()))); } @@ -666,8 +657,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBpLocalDateTime(org.threeten.bp.LocalDateTime.of(2010, 7, 4, 1, 2, 3)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBpLocalDateTime(), is(equalTo(entity.getBpLocalDateTime()))); } @@ -681,8 +672,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBpLocalTime(org.threeten.bp.LocalTime.of(1, 2, 3)); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBpLocalTime(), is(equalTo(entity.getBpLocalTime()))); } @@ -696,8 +687,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBpInstant(org.threeten.bp.Instant.now()); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBpZoneId(), is(equalTo(entity.getBpZoneId()))); } @@ -711,8 +702,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed AllPossibleTypes entity = new AllPossibleTypes("1"); entity.setBpZoneId(org.threeten.bp.ZoneId.of("Europe/Paris")); - cassandraOperations.insert(entity); - AllPossibleTypes loaded = cassandraOperations.selectOneById(AllPossibleTypes.class, entity.getId()); + operations.insert(entity); + AllPossibleTypes loaded = operations.selectOneById(AllPossibleTypes.class, entity.getId()); assertThat(loaded.getBpZoneId(), is(equalTo(entity.getBpZoneId()))); } @@ -727,8 +718,8 @@ public class CassandraTypeMappingIntegrationTest extends AbstractSpringDataEmbed CounterEntity entity = new CounterEntity("1"); entity.setCount(1); - cassandraOperations.update(entity); - CounterEntity loaded = cassandraOperations.selectOneById(CounterEntity.class, entity.getId()); + operations.update(entity); + CounterEntity loaded = operations.selectOneById(CounterEntity.class, entity.getId()); assertThat(loaded.getCount(), is(equalTo(entity.getCount()))); } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/SchemaTestUtils.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/SchemaTestUtils.java new file mode 100644 index 000000000..536ccb073 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/SchemaTestUtils.java @@ -0,0 +1,68 @@ +/* + * Copyright 2016 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.cassandra.test.integration.support; + +import org.springframework.cassandra.core.cql.generator.CreateTableCqlGenerator; +import org.springframework.cassandra.core.keyspace.CreateTableSpecification; +import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.mapping.CassandraMappingContext; +import org.springframework.data.cassandra.mapping.CassandraPersistentEntity; + +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.querybuilder.QueryBuilder; + +/** + * {@link SchemaTestUtils} is a collection of reflection-based utility methods for use in unit and integration testing + * scenarios. + * + * @author Mark Paluch + */ +public class SchemaTestUtils { + + /** + * Create a table for {@code entityClass} if it not exists. + * + * @param entityClass must not be {@literal null}. + * @param operations must not be {@literal null}. + */ + public static void potentiallyCreateTableFor(Class entityClass, CassandraOperations operations) { + + CassandraMappingContext mappingContext = operations.getConverter().getMappingContext(); + CassandraPersistentEntity persistentEntity = mappingContext.getPersistentEntity(entityClass); + Session session = operations.getSession(); + + KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace()); + if (keyspace.getTable(persistentEntity.getTableName().toCql()) == null) { + CreateTableSpecification tableSpecification = mappingContext.getCreateTableSpecificationFor(persistentEntity); + operations.execute(new CreateTableCqlGenerator(tableSpecification).toCql()); + } + } + + /** + * Truncate table for {@code entityClass}. + * + * @param entityClass must not be {@literal null}. + * @param operations must not be {@literal null}. + */ + public static void truncate(Class entityClass, CassandraOperations operations) { + + CassandraMappingContext mappingContext = operations.getConverter().getMappingContext(); + CassandraPersistentEntity persistentEntity = mappingContext.getPersistentEntity(entityClass); + + operations.execute(QueryBuilder.truncate(persistentEntity.getTableName().toCql())); + } +} diff --git a/spring-data-cassandra/src/test/resources/config/cassandra-connection.properties b/spring-data-cassandra/src/test/resources/config/cassandra-connection.properties index be62cb0ef..c28cb2327 100644 --- a/spring-data-cassandra/src/test/resources/config/cassandra-connection.properties +++ b/spring-data-cassandra/src/test/resources/config/cassandra-connection.properties @@ -6,3 +6,4 @@ build.cassandra.storage_port=@build.cassandra.storage_port@ build.cassandra.ssl_storage_port=@build.cassandra.ssl_storage_port@ build.cassandra.mode=@build.cassandra.mode@ build.cassandra.host=@build.cassandra.host@ +build.cassandra.reuse-cluster=true