diff --git a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java index ebb619c456..6c099d99bc 100644 --- a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java +++ b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java @@ -12,12 +12,12 @@ import java.util.UUID; @Table public class Book { - @PrimaryKeyColumn(name = "isbn", ordinal = 2, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING) + @PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING) private UUID id; - @PrimaryKeyColumn(name = "title", ordinal = 0, type = PrimaryKeyType.PARTITIONED) + @PrimaryKeyColumn(name = "title", ordinal = 1, type = PrimaryKeyType.PARTITIONED) private String title; - @PrimaryKeyColumn(name = "publisher", ordinal = 1, type = PrimaryKeyType.PARTITIONED) + @PrimaryKeyColumn(name = "publisher", ordinal = 2, type = PrimaryKeyType.PARTITIONED) private String publisher; @Column private Set tags = new HashSet<>(); diff --git a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java index 4b2893252a..e5a7237145 100644 --- a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java +++ b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java @@ -52,8 +52,8 @@ public class BookRepositoryIntegrationTest { Session session = cluster.connect(); session.execute(KEYSPACE_CREATION_QUERY); session.execute(KEYSPACE_ACTIVATE_QUERY); - Thread.sleep(5000); LOGGER.info("KeySpace created and activated."); + Thread.sleep(5000); } @Before diff --git a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CQLQueriesIntegrationTest.java b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CQLQueriesIntegrationTest.java index fd60b9a7f9..f4354a66e2 100644 --- a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CQLQueriesIntegrationTest.java +++ b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CQLQueriesIntegrationTest.java @@ -57,8 +57,8 @@ public class CQLQueriesIntegrationTest { Session session = cluster.connect(); session.execute(KEYSPACE_CREATION_QUERY); session.execute(KEYSPACE_ACTIVATE_QUERY); - Thread.sleep(2000); LOGGER.info("KeySpace created and activated."); + Thread.sleep(5000); } @Before @@ -69,7 +69,7 @@ public class CQLQueriesIntegrationTest { @Test public void whenSavingBook_thenAvailableOnRetrieval_usingQueryBuilder() { UUID uuid = UUIDs.timeBased(); - Insert insert = QueryBuilder.insertInto(DATA_TABLE_NAME).value("isbn", uuid).value("title", "Head First Java").value("publisher", "OReilly Media").value("tags", ImmutableSet.of("Software")); + Insert insert = QueryBuilder.insertInto(DATA_TABLE_NAME).value("id", uuid).value("title", "Head First Java").value("publisher", "OReilly Media").value("tags", ImmutableSet.of("Software")); cassandraTemplate.execute(insert); Select select = QueryBuilder.select().from("book").limit(10); Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); @@ -79,7 +79,7 @@ public class CQLQueriesIntegrationTest { @Test public void whenSavingBook_thenAvailableOnRetrieval_usingCQLStatements() { UUID uuid = UUIDs.timeBased(); - String insertCql = "insert into book (isbn, title, publisher, tags) values " + "(" + uuid + ", 'Head First Java', 'OReilly Media', {'Software'})"; + String insertCql = "insert into book (id, title, publisher, tags) values " + "(" + uuid + ", 'Head First Java', 'OReilly Media', {'Software'})"; cassandraTemplate.execute(insertCql); Select select = QueryBuilder.select().from("book").limit(10); Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); @@ -89,16 +89,16 @@ public class CQLQueriesIntegrationTest { @Test public void whenSavingBook_thenAvailableOnRetrieval_usingPreparedStatements() { UUID uuid = UUIDs.timeBased(); - String insertPreparedCql = "insert into book (isbn, title, publisher, tags) values (?, ?, ?, ?)"; - List bookList = new ArrayList<>(); - List> bookListOfList = new ArrayList<>(); - bookList.add(uuid); - bookList.add("Head First Java"); - bookList.add("OReilly Media"); - bookList.add(ImmutableSet.of("Software")); - bookListOfList.add(bookList); - cassandraTemplate.ingest(insertPreparedCql, bookListOfList); - Select select = QueryBuilder.select().from("book").limit(10); + String insertPreparedCql = "insert into book (id, title, publisher, tags) values (?, ?, ?, ?)"; + List singleBookArgsList = new ArrayList<>(); + List> bookList = new ArrayList<>(); + singleBookArgsList.add(uuid); + singleBookArgsList.add("Head First Java"); + singleBookArgsList.add("OReilly Media"); + singleBookArgsList.add(ImmutableSet.of("Software")); + bookList.add(singleBookArgsList); + cassandraTemplate.ingest(insertPreparedCql, bookList); + Select select = QueryBuilder.select().from("book"); Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); assertEquals(uuid, retrievedBook.getId()); } diff --git a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java index 3108658c0e..35de508641 100644 --- a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java +++ b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java @@ -58,8 +58,8 @@ public class CassandraTemplateIntegrationTest { Session session = cluster.connect(); session.execute(KEYSPACE_CREATION_QUERY); session.execute(KEYSPACE_ACTIVATE_QUERY); - Thread.sleep(2000); LOGGER.info("KeySpace created and activated."); + Thread.sleep(5000); } @Before @@ -98,16 +98,28 @@ public class CassandraTemplateIntegrationTest { cassandraTemplate.insert(javaBook); Select select = QueryBuilder.select().from("book").limit(10); Book retrievedBook = cassandraTemplate.selectOne(select, Book.class); - retrievedBook.setTags(ImmutableSet.of("Computer", "Software", "Java")); + retrievedBook.setTags(ImmutableSet.of("Java", "Programming")); cassandraTemplate.update(retrievedBook); Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class); assertEquals(retrievedBook.getTags(), retrievedUpdatedBook.getTags()); } @Test - public void whenDeletingExistingBooks_thenNotAvailableOnRetrieval() { + public void whenDeletingASelectedBook_thenNotAvailableOnRetrieval() throws InterruptedException { + Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "OReilly Media", ImmutableSet.of("Computer", "Software")); + cassandraTemplate.insert(javaBook); + cassandraTemplate.delete(javaBook); + Select select = QueryBuilder.select().from("book").limit(10); + Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class); + assertNull(retrievedUpdatedBook); + } + + @Test + public void whenDeletingAllBooks_thenNotAvailableOnRetrieval() { Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); - Book insertedBook = cassandraTemplate.insert(javaBook); + Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software")); + cassandraTemplate.insert(javaBook); + cassandraTemplate.insert(dPatternBook); cassandraTemplate.deleteAll(Book.class); Select select = QueryBuilder.select().from("book").limit(10); Book retrievedUpdatedBook = cassandraTemplate.selectOne(select, Book.class);