Skip to content

Commit 68c7e86

Browse files
committed
Updated test cases
1 parent d76fb0f commit 68c7e86

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class Book {
1616
private UUID id;
1717
@PrimaryKeyColumn(name = "title", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
1818
private String title;
19+
1920
@PrimaryKeyColumn(name = "publisher", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
2021
private String publisher;
2122
@Column
@@ -43,4 +44,20 @@ public String getPublisher() {
4344
public Set getTags() {
4445
return tags;
4546
}
47+
48+
public void setId(UUID id) {
49+
this.id = id;
50+
}
51+
52+
public void setTitle(String title) {
53+
this.title = title;
54+
}
55+
56+
public void setPublisher(String publisher) {
57+
this.publisher = publisher;
58+
}
59+
60+
public void setTags(Set<String> tags) {
61+
this.tags = tags;
62+
}
4663
}

spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,68 @@ public class BookRepositoryIntegrationTest {
4646
@BeforeClass
4747
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
4848
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
49-
Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
49+
Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1")
50+
.withPort(9142).build();
5051
LOGGER.info("Server Started at 127.0.0.1:9142... ");
5152
Session session = cluster.connect();
5253
session.execute(KEYSPACE_CREATION_QUERY);
5354
session.execute(KEYSPACE_ACTIVATE_QUERY);
55+
LOGGER.info(session.execute("Select * from Book").all().toArray());
56+
Thread.sleep(5000);
5457
LOGGER.info("KeySpace created and activated.");
5558
}
5659

5760
@Before
58-
public void resetKeySpace() throws InterruptedException, TTransportException, ConfigurationException, IOException {
61+
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
5962
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
6063
}
6164

6265
@Test
63-
public void whenSavingBooks_thenAvailableOnRetrieval() {
64-
Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
66+
public void whenSavingBook_thenAvailableOnRetrieval() {
67+
Book javaBook = new Book(UUIDs.timeBased(), "Head First Java",
68+
"O'Reilly Media", ImmutableSet.of("Computer", "Software"));
6569
bookRepository.save(ImmutableSet.of(javaBook));
6670
Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
6771
assertEquals(javaBook.getId(), books.iterator().next().getId());
6872
}
6973

74+
@Test
75+
public void whenUpdatingBooks_thenAvailableOnRetrieval() {
76+
Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
77+
bookRepository.save(ImmutableSet.of(javaBook));
78+
Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
79+
javaBook.setTitle("Head First Java Second Edition");
80+
bookRepository.save(ImmutableSet.of(javaBook));
81+
Iterable<Book> updateBooks = bookRepository.findByTitleAndPublisher("Head First Java Second Edition", "O'Reilly Media");
82+
assertEquals(javaBook.getTitle(), updateBooks.iterator().next().getTitle());
83+
}
84+
7085
@Test(expected = java.util.NoSuchElementException.class)
7186
public void whenDeletingExistingBooks_thenNotAvailableOnRetrieval() {
7287
Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
7388
bookRepository.save(ImmutableSet.of(javaBook));
7489
bookRepository.delete(javaBook);
75-
Iterable<Book> books = bookRepository.findByTitleAndPublisher("type1", "O'Reilly Media");
90+
Iterable<Book> books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
7691
assertNotEquals(javaBook.getId(), books.iterator().next().getId());
7792
}
7893

94+
@Test
95+
public void whenSavingBooks_thenAllShouldAvailableOnRetrieval() {
96+
Book javaBook = new Book(UUIDs.timeBased(), "Head First Java",
97+
"O'Reilly Media", ImmutableSet.of("Computer", "Software"));
98+
Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns",
99+
"O'Reilly Media", ImmutableSet.of("Computer", "Software"));
100+
bookRepository.save(ImmutableSet.of(javaBook));
101+
bookRepository.save(ImmutableSet.of(dPatternBook));
102+
Iterable<Book> books = bookRepository.findAll();
103+
int bookCount = 0;
104+
for (Book book : books) bookCount++;
105+
assertEquals(bookCount, 2);
106+
}
107+
79108
@After
80-
public void cleanTable() {
81-
adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
109+
public void dropTable() {
110+
adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
82111
}
83112

84113
@AfterClass

0 commit comments

Comments
 (0)