11package org .baeldung .spring .data .cassandra .repository ;
22
3- import com .datastax .driver .core .Cluster ;
4- import com .datastax .driver .core .Session ;
5- import com .datastax .driver .core .utils .UUIDs ;
6- import com .google .common .collect .ImmutableSet ;
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertNotEquals ;
5+
6+ import java .io .IOException ;
7+ import java .util .HashMap ;
8+
79import org .apache .cassandra .exceptions .ConfigurationException ;
810import org .apache .commons .logging .Log ;
911import org .apache .commons .logging .LogFactory ;
1012import org .apache .thrift .transport .TTransportException ;
1113import org .baeldung .spring .data .cassandra .config .CassandraConfig ;
1214import org .baeldung .spring .data .cassandra .model .Book ;
1315import org .cassandraunit .utils .EmbeddedCassandraServerHelper ;
14- import org .junit .*;
16+ import org .junit .After ;
17+ import org .junit .AfterClass ;
18+ import org .junit .Before ;
19+ import org .junit .BeforeClass ;
20+ import org .junit .Test ;
1521import org .junit .runner .RunWith ;
1622import org .springframework .beans .factory .annotation .Autowired ;
1723import org .springframework .cassandra .core .cql .CqlIdentifier ;
1824import org .springframework .data .cassandra .core .CassandraAdminOperations ;
1925import org .springframework .test .context .ContextConfiguration ;
2026import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
2127
22- import java .io .IOException ;
23- import java .util .HashMap ;
24-
25- import static org .junit .Assert .assertEquals ;
26- import static org .junit .Assert .assertNotEquals ;
28+ import com .datastax .driver .core .Cluster ;
29+ import com .datastax .driver .core .Session ;
30+ import com .datastax .driver .core .utils .UUIDs ;
31+ import com .google .common .collect .ImmutableSet ;
2732
2833@ RunWith (SpringJUnit4ClassRunner .class )
2934@ ContextConfiguration (classes = CassandraConfig .class )
3035public class BookRepositoryIntegrationTest {
31-
3236 private static final Log LOGGER = LogFactory .getLog (BookRepositoryIntegrationTest .class );
3337
3438 public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };" ;
@@ -43,13 +47,14 @@ public class BookRepositoryIntegrationTest {
4347 @ Autowired
4448 private CassandraAdminOperations adminTemplate ;
4549
50+ //
51+
4652 @ BeforeClass
4753 public static void startCassandraEmbedded () throws InterruptedException , TTransportException , ConfigurationException , IOException {
4854 EmbeddedCassandraServerHelper .startEmbeddedCassandra ();
49- Cluster cluster = Cluster .builder ().addContactPoints ("127.0.0.1" )
50- .withPort (9142 ).build ();
55+ final Cluster cluster = Cluster .builder ().addContactPoints ("127.0.0.1" ).withPort (9142 ).build ();
5156 LOGGER .info ("Server Started at 127.0.0.1:9142... " );
52- Session session = cluster .connect ();
57+ final Session session = cluster .connect ();
5358 session .execute (KEYSPACE_CREATION_QUERY );
5459 session .execute (KEYSPACE_ACTIVATE_QUERY );
5560 LOGGER .info ("KeySpace created and activated." );
@@ -63,54 +68,54 @@ public void createTable() throws InterruptedException, TTransportException, Conf
6368
6469 @ Test
6570 public void whenSavingBook_thenAvailableOnRetrieval () {
66- Book javaBook = new Book (UUIDs .timeBased (), "Head First Java" ,
67- "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
71+ final Book javaBook = new Book (UUIDs .timeBased (), "Head First Java" , "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
6872 bookRepository .save (ImmutableSet .of (javaBook ));
69- Iterable <Book > books = bookRepository .findByTitleAndPublisher ("Head First Java" , "O'Reilly Media" );
73+ final Iterable <Book > books = bookRepository .findByTitleAndPublisher ("Head First Java" , "O'Reilly Media" );
7074 assertEquals (javaBook .getId (), books .iterator ().next ().getId ());
7175 }
7276
7377 @ Test
7478 public void whenUpdatingBooks_thenAvailableOnRetrieval () {
75- Book javaBook = new Book (UUIDs .timeBased (), "Head First Java" , "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
79+ final Book javaBook = new Book (UUIDs .timeBased (), "Head First Java" , "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
7680 bookRepository .save (ImmutableSet .of (javaBook ));
77- Iterable <Book > books = bookRepository .findByTitleAndPublisher ("Head First Java" , "O'Reilly Media" );
81+ final Iterable <Book > books = bookRepository .findByTitleAndPublisher ("Head First Java" , "O'Reilly Media" );
7882 javaBook .setTitle ("Head First Java Second Edition" );
7983 bookRepository .save (ImmutableSet .of (javaBook ));
80- Iterable <Book > updateBooks = bookRepository .findByTitleAndPublisher ("Head First Java Second Edition" , "O'Reilly Media" );
84+ final Iterable <Book > updateBooks = bookRepository .findByTitleAndPublisher ("Head First Java Second Edition" , "O'Reilly Media" );
8185 assertEquals (javaBook .getTitle (), updateBooks .iterator ().next ().getTitle ());
8286 }
8387
8488 @ Test (expected = java .util .NoSuchElementException .class )
8589 public void whenDeletingExistingBooks_thenNotAvailableOnRetrieval () {
86- Book javaBook = new Book (UUIDs .timeBased (), "Head First Java" , "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
90+ final Book javaBook = new Book (UUIDs .timeBased (), "Head First Java" , "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
8791 bookRepository .save (ImmutableSet .of (javaBook ));
8892 bookRepository .delete (javaBook );
89- Iterable <Book > books = bookRepository .findByTitleAndPublisher ("Head First Java" , "O'Reilly Media" );
93+ final Iterable <Book > books = bookRepository .findByTitleAndPublisher ("Head First Java" , "O'Reilly Media" );
9094 assertNotEquals (javaBook .getId (), books .iterator ().next ().getId ());
9195 }
9296
9397 @ Test
9498 public void whenSavingBooks_thenAllShouldAvailableOnRetrieval () {
95- Book javaBook = new Book (UUIDs .timeBased (), "Head First Java" ,
96- "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
97- Book dPatternBook = new Book (UUIDs .timeBased (), "Head Design Patterns" ,
98- "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
99+ final Book javaBook = new Book (UUIDs .timeBased (), "Head First Java" , "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
100+ final Book dPatternBook = new Book (UUIDs .timeBased (), "Head Design Patterns" , "O'Reilly Media" , ImmutableSet .of ("Computer" , "Software" ));
99101 bookRepository .save (ImmutableSet .of (javaBook ));
100102 bookRepository .save (ImmutableSet .of (dPatternBook ));
101- Iterable <Book > books = bookRepository .findAll ();
103+ final Iterable <Book > books = bookRepository .findAll ();
102104 int bookCount = 0 ;
103- for (Book book : books ) bookCount ++;
105+ for (final Book book : books ) {
106+ bookCount ++;
107+ }
104108 assertEquals (bookCount , 2 );
105109 }
106110
107111 @ After
108112 public void dropTable () {
109- adminTemplate .dropTable (CqlIdentifier .cqlId (DATA_TABLE_NAME ));
113+ adminTemplate .dropTable (CqlIdentifier .cqlId (DATA_TABLE_NAME ));
110114 }
111115
112116 @ AfterClass
113117 public static void stopCassandraEmbedded () {
114118 EmbeddedCassandraServerHelper .cleanEmbeddedCassandra ();
115119 }
120+
116121}
0 commit comments