From c5273c9eb28653bc7d02c54f1511a2acd2e656b4 Mon Sep 17 00:00:00 2001 From: David Webb Date: Wed, 27 Nov 2013 16:19:21 -0500 Subject: [PATCH] DATACASS-35: Unit Tests for CassandraOperations - Completed. --- .../template/CassandraOperationsTest.java | 691 ++++++++++++++++-- 1 file changed, 644 insertions(+), 47 deletions(-) diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java index f4a363d88..7b763431d 100644 --- a/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertTrue; import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.UUID; @@ -36,14 +37,18 @@ import org.springframework.cassandra.core.CassandraOperations; import org.springframework.cassandra.core.CassandraTemplate; import org.springframework.cassandra.core.HostMapper; import org.springframework.cassandra.core.PreparedStatementBinder; +import org.springframework.cassandra.core.PreparedStatementCallback; +import org.springframework.cassandra.core.PreparedStatementCreator; import org.springframework.cassandra.core.ResultSetExtractor; import org.springframework.cassandra.core.ResultSetFutureExtractor; import org.springframework.cassandra.core.RingMember; import org.springframework.cassandra.core.RowCallbackHandler; import org.springframework.cassandra.core.RowIterator; +import org.springframework.cassandra.core.RowMapper; import org.springframework.cassandra.core.SessionCallback; import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest; import org.springframework.dao.DataAccessException; +import org.springframework.util.CollectionUtils; import com.datastax.driver.core.BoundStatement; import com.datastax.driver.core.Host; @@ -69,6 +74,8 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio /* * Objects used for test data */ + final String ISBN_NINES = "999999999"; + final String TITLE_NINES = "Book of Nines"; final Object[] o1 = new Object[] { "1234", "Moby Dick", "Herman Manville", new Integer(456) }; final Object[] o2 = new Object[] { "2345", "War and Peace", "Russian Dude", new Integer(456) }; final Object[] o3 = new Object[] { "3456", "Jane Ayre", "Charlotte", new Integer(456) }; @@ -139,32 +146,20 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio List> values = new LinkedList>(); - List l1 = new LinkedList(); - l1.add("1234"); - l1.add("Moby Dick"); - l1.add("Herman Manville"); - l1.add(new Integer(456)); - - values.add(l1); - - List l2 = new LinkedList(); - l2.add("2345"); - l2.add("War and Peace"); - l2.add("Russian Dude"); - l2.add(new Integer(456)); - - values.add(l2); - - // values.add(new Object[] { "3456", "Jane Ayre", "Charlotte", new Integer(456) }); + values.add(new LinkedList(CollectionUtils.arrayToList(o1))); + values.add(new LinkedList(CollectionUtils.arrayToList(o2))); + values.add(new LinkedList(CollectionUtils.arrayToList(o3))); cassandraTemplate.ingest(cql, values); // Assert that the rows were inserted into Cassandra - Book b1 = getBook("1234"); - Book b2 = getBook("2345"); + Book b1 = getBook((String) o1[0]); + Book b2 = getBook((String) o2[0]); + Book b3 = getBook((String) o3[0]); - assertBook(b1, listToBook(l1)); - assertBook(b2, listToBook(l2)); + assertBook(b1, objectToBook(o1)); + assertBook(b2, objectToBook(o2)); + assertBook(b3, objectToBook(o3)); } @Test @@ -184,9 +179,9 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio Book b2 = getBook("2345"); Book b3 = getBook("3456"); - assertBook(b1, objectToBook(values[0])); - assertBook(b2, objectToBook(values[1])); - assertBook(b3, objectToBook(values[2])); + assertBook(b1, objectToBook(o1)); + assertBook(b2, objectToBook(o2)); + assertBook(b3, objectToBook(o3)); } /** @@ -240,9 +235,9 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio Book b2 = getBook("2345"); Book b3 = getBook("3456"); - assertBook(b1, objectToBook(v[0])); - assertBook(b2, objectToBook(v[1])); - assertBook(b3, objectToBook(v[2])); + assertBook(b1, objectToBook(o1)); + assertBook(b2, objectToBook(o2)); + assertBook(b3, objectToBook(o3)); } @@ -329,11 +324,7 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio Row r = rs.one(); assertNotNull(r); - Book b = new Book(); - b.setIsbn(r.getString("isbn")); - b.setTitle(r.getString("title")); - b.setAuthor(r.getString("author")); - b.setPages(r.getInt("pages")); + Book b = rowToBook(r); return b; } @@ -360,11 +351,7 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio Row r = frs.one(); assertNotNull(r); - Book b = new Book(); - b.setIsbn(r.getString("isbn")); - b.setTitle(r.getString("title")); - b.setAuthor(r.getString("author")); - b.setPages(r.getInt("pages")); + Book b = rowToBook(r); return b; } @@ -390,11 +377,7 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio assertNotNull(row); - Book b = new Book(); - b.setIsbn(row.getString("isbn")); - b.setTitle(row.getString("title")); - b.setAuthor(row.getString("author")); - b.setPages(row.getInt("pages")); + Book b = rowToBook(row); assertBook(b1, b); @@ -430,19 +413,624 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio assertNotNull(row); - Book b = new Book(); - b.setIsbn(row.getString("isbn")); - b.setTitle(row.getString("title")); - b.setAuthor(row.getString("author")); - b.setPages(row.getInt("pages")); + Book b = rowToBook(row); assertBook(b1, b); } + }); } + @Test + public void queryTestCqlStringRowMapper() { + + // Insert our 3 test books. + ingestionTestObjectArray(); + + List books = cassandraTemplate.query("select * from book where isbn in ('1234','2345','3456')", + new RowMapper() { + + @Override + public Book mapRow(Row row, int rowNum) throws DriverException { + Book b = rowToBook(row); + return b; + } + }); + + log.debug("Size of Book List -> " + books.size()); + assertEquals(books.size(), 3); + assertBook(books.get(0), getBook(books.get(0).getIsbn())); + assertBook(books.get(1), getBook(books.get(1).getIsbn())); + assertBook(books.get(2), getBook(books.get(2).getIsbn())); + } + + @Test + public void processTestResultSetRowMapper() { + + // Insert our 3 test books. + ingestionTestObjectArray(); + + ResultSet rs = cassandraTemplate.queryAsynchronously("select * from book where isbn in ('1234','2345','3456')", + new ResultSetFutureExtractor() { + + @Override + public ResultSet extractData(ResultSetFuture rs) throws DriverException, DataAccessException { + + ResultSet frs = rs.getUninterruptibly(); + return frs; + } + }); + + assertNotNull(rs); + + List books = cassandraTemplate.process(rs, new RowMapper() { + + @Override + public Book mapRow(Row row, int rowNum) throws DriverException { + Book b = rowToBook(row); + return b; + } + }); + + log.debug("Size of Book List -> " + books.size()); + assertEquals(books.size(), 3); + assertBook(books.get(0), getBook(books.get(0).getIsbn())); + assertBook(books.get(1), getBook(books.get(1).getIsbn())); + assertBook(books.get(2), getBook(books.get(2).getIsbn())); + + } + + @Test + public void queryForObjectTestCqlStringRowMapper() { + + Book book = cassandraTemplate.queryForObject("select * from book where isbn in ('" + ISBN_NINES + "')", + new RowMapper() { + @Override + public Book mapRow(Row row, int rowNum) throws DriverException { + Book b = rowToBook(row); + return b; + } + }); + + assertNotNull(book); + assertBook(book, getBook(ISBN_NINES)); + } + + /** + * Test that CQL for QueryForObject must only return 1 row or an IllegalArgumentException is thrown. + */ + @Test(expected = IllegalArgumentException.class) + public void queryForObjectTestCqlStringRowMapperNotOneRowReturned() { + + // Insert our 3 test books. + ingestionTestObjectArray(); + + Book book = cassandraTemplate.queryForObject("select * from book where isbn in ('1234','2345','3456')", + new RowMapper() { + @Override + public Book mapRow(Row row, int rowNum) throws DriverException { + Book b = rowToBook(row); + return b; + } + }); + } + + @Test + public void processOneTestResultSetRowMapper() { + + // Insert our 3 test books. + ingestionTestObjectArray(); + + ResultSet rs = cassandraTemplate.queryAsynchronously("select * from book where isbn in ('" + ISBN_NINES + "')", + new ResultSetFutureExtractor() { + + @Override + public ResultSet extractData(ResultSetFuture rs) throws DriverException, DataAccessException { + + ResultSet frs = rs.getUninterruptibly(); + return frs; + } + }); + + assertNotNull(rs); + + Book book = cassandraTemplate.processOne(rs, new RowMapper() { + @Override + public Book mapRow(Row row, int rowNum) throws DriverException { + Book b = rowToBook(row); + return b; + } + }); + + assertNotNull(book); + assertBook(book, getBook(ISBN_NINES)); + } + + @Test + public void quertForObjectTestCqlStringRequiredType() { + + String title = cassandraTemplate.queryForObject("select title from book where isbn in ('" + ISBN_NINES + "')", + String.class); + + assertEquals(title, TITLE_NINES); + + } + + @Test(expected = ClassCastException.class) + public void queryForObjectTestCqlStringRequiredTypeInvalid() { + + Float title = cassandraTemplate.queryForObject("select title from book where isbn in ('" + ISBN_NINES + "')", + Float.class); + + } + + @Test + public void processOneTestResultSetType() { + + ResultSet rs = cassandraTemplate.queryAsynchronously("select title from book where isbn in ('" + ISBN_NINES + "')", + new ResultSetFutureExtractor() { + + @Override + public ResultSet extractData(ResultSetFuture rs) throws DriverException, DataAccessException { + + ResultSet frs = rs.getUninterruptibly(); + return frs; + } + }); + + assertNotNull(rs); + + String title = cassandraTemplate.processOne(rs, String.class); + + assertNotNull(title); + assertEquals(title, TITLE_NINES); + } + + @Test + public void queryForMapTestCqlString() { + + Map rsMap = cassandraTemplate + .queryForMap("select * from book where isbn in ('" + ISBN_NINES + "')"); + + log.debug(rsMap.toString()); + + Book b1 = objectToBook(rsMap.get("isbn"), rsMap.get("title"), rsMap.get("author"), rsMap.get("pages")); + + Book b2 = getBook(ISBN_NINES); + + assertBook(b1, b2); + + } + + @Test + public void processMapTestResultSet() { + + ResultSet rs = cassandraTemplate.queryAsynchronously("select * from book where isbn in ('" + ISBN_NINES + "')", + new ResultSetFutureExtractor() { + + @Override + public ResultSet extractData(ResultSetFuture rs) throws DriverException, DataAccessException { + + ResultSet frs = rs.getUninterruptibly(); + return frs; + } + }); + + assertNotNull(rs); + + Map rsMap = cassandraTemplate.processMap(rs); + + log.debug("Size of Book List -> " + rsMap.size()); + + Book b1 = objectToBook(rsMap.get("isbn"), rsMap.get("title"), rsMap.get("author"), rsMap.get("pages")); + + Book b2 = getBook(ISBN_NINES); + + assertBook(b1, b2); + + } + + @Test + public void queryForListTestCqlStringType() { + + // Insert our 3 test books. + ingestionTestObjectArray(); + + List titles = cassandraTemplate.queryForList("select title from book where isbn in ('1234','2345','3456')", + String.class); + + log.debug(titles.toString()); + + assertNotNull(titles); + assertEquals(titles.size(), 3); + + } + + @Test + public void processListTestResultSetType() { + + // Insert our 3 test books. + ingestionTestObjectArray(); + + ResultSet rs = cassandraTemplate.queryAsynchronously("select * from book where isbn in ('1234','2345','3456')", + new ResultSetFutureExtractor() { + + @Override + public ResultSet extractData(ResultSetFuture rs) throws DriverException, DataAccessException { + + ResultSet frs = rs.getUninterruptibly(); + return frs; + } + }); + + assertNotNull(rs); + + List titles = cassandraTemplate.processList(rs, String.class); + + log.debug(titles.toString()); + + assertNotNull(titles); + assertEquals(titles.size(), 3); + } + + @Test + public void queryForListOfMapCqlString() { + + // Insert our 3 test books. + ingestionTestObjectArray(); + + List> results = cassandraTemplate + .queryForListOfMap("select * from book where isbn in ('1234','2345','3456')"); + + log.debug(results.toString()); + + assertEquals(results.size(), 3); + + } + + @Test + public void processListOfMapTestResultSet() { + + // Insert our 3 test books. + ingestionTestObjectArray(); + + ResultSet rs = cassandraTemplate.queryAsynchronously("select * from book where isbn in ('1234','2345','3456')", + new ResultSetFutureExtractor() { + + @Override + public ResultSet extractData(ResultSetFuture rs) throws DriverException, DataAccessException { + + ResultSet frs = rs.getUninterruptibly(); + return frs; + } + }); + + assertNotNull(rs); + + List> results = cassandraTemplate.processListOfMap(rs); + + log.debug(results.toString()); + + assertEquals(results.size(), 3); + + } + + @Test + public void executeTestCqlStringPreparedStatementCallback() { + + String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)"; + + BoundStatement statement = cassandraTemplate.execute(cql, new PreparedStatementCallback() { + + @Override + public BoundStatement doInPreparedStatement(PreparedStatement ps) throws DriverException, DataAccessException { + BoundStatement bs = ps.bind(); + return bs; + } + }); + + assertNotNull(statement); + + } + + @Test + public void executeTestPreparedStatementCreatorPreparedStatementCallback() { + + final String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)"; + + BoundStatement statement = cassandraTemplate.execute(new PreparedStatementCreator() { + + @Override + public PreparedStatement createPreparedStatement(Session session) throws DriverException { + return session.prepare(cql); + } + }, new PreparedStatementCallback() { + + @Override + public BoundStatement doInPreparedStatement(PreparedStatement ps) throws DriverException, DataAccessException { + BoundStatement bs = ps.bind(); + return bs; + } + }); + + assertNotNull(statement); + + } + + @Test + public void queryTestCqlStringPreparedStatementBinderResultSetExtractor() { + + final String cql = "select * from book where isbn = ?"; + final String isbn = "999999999"; + + Book b1 = cassandraTemplate.query(cql, new PreparedStatementBinder() { + + @Override + public BoundStatement bindValues(PreparedStatement ps) throws DriverException { + return ps.bind(isbn); + } + }, new ResultSetExtractor() { + + @Override + public Book extractData(ResultSet rs) throws DriverException, DataAccessException { + Row r = rs.one(); + assertNotNull(r); + + Book b = rowToBook(r); + + return b; + } + }); + + Book b2 = getBook(isbn); + + assertBook(b1, b2); + } + + @Test + public void queryTestCqlStringPreparedStatementBinderRowCallbackHandler() { + + final String cql = "select * from book where isbn = ?"; + final String isbn = "999999999"; + + cassandraTemplate.query(cql, new PreparedStatementBinder() { + + @Override + public BoundStatement bindValues(PreparedStatement ps) throws DriverException { + return ps.bind(isbn); + } + }, new RowCallbackHandler() { + + @Override + public void processRow(Row row) throws DriverException { + + Book b = rowToBook(row); + + Book b2 = getBook(isbn); + + assertBook(b, b2); + + } + }); + + } + + @Test + public void queryTestCqlStringPreparedStatementBinderRowMapper() { + + final String cql = "select * from book where isbn = ?"; + final String isbn = "999999999"; + + List books = cassandraTemplate.query(cql, new PreparedStatementBinder() { + + @Override + public BoundStatement bindValues(PreparedStatement ps) throws DriverException { + return ps.bind(isbn); + } + }, new RowMapper() { + + @Override + public Book mapRow(Row row, int rowNum) throws DriverException { + return rowToBook(row); + } + }); + + Book b2 = getBook(isbn); + + assertEquals(books.size(), 1); + assertBook(books.get(0), b2); + } + + @Test + public void queryTestPreparedStatementCreatorResultSetExtractor() { + + ingestionTestObjectArray(); + + final String cql = "select * from book"; + + List books = cassandraTemplate.query(new PreparedStatementCreator() { + + @Override + public PreparedStatement createPreparedStatement(Session session) throws DriverException { + return session.prepare(cql); + } + }, new ResultSetExtractor>() { + + @Override + public List extractData(ResultSet rs) throws DriverException, DataAccessException { + + List books = new LinkedList(); + + for (Row row : rs.all()) { + books.add(rowToBook(row)); + } + + return books; + } + }); + + log.debug("Size of all Books -> " + books.size()); + + assertTrue(books.size() > 0); + } + + @Test + public void queryTestPreparedStatementCreatorRowCallbackHandler() { + + ingestionTestObjectArray(); + + final String cql = "select * from book"; + + cassandraTemplate.query(new PreparedStatementCreator() { + + @Override + public PreparedStatement createPreparedStatement(Session session) throws DriverException { + return session.prepare(cql); + } + }, new RowCallbackHandler() { + + @Override + public void processRow(Row row) throws DriverException { + + Book b = rowToBook(row); + + log.debug("Title -> " + b.getTitle()); + + } + }); + + } + + @Test + public void queryTestPreparedStatementCreatorRowMapper() { + + ingestionTestObjectArray(); + + final String cql = "select * from book"; + + List books = cassandraTemplate.query(new PreparedStatementCreator() { + + @Override + public PreparedStatement createPreparedStatement(Session session) throws DriverException { + return session.prepare(cql); + } + }, new RowMapper() { + + @Override + public Book mapRow(Row row, int rowNum) throws DriverException { + return rowToBook(row); + } + }); + + log.debug("Size of all Books -> " + books.size()); + + assertTrue(books.size() > 0); + } + + @Test + public void queryTestPreparedStatementCreatorPreparedStatementBinderResultSetExtractor() { + + final String cql = "select * from book where isbn = ?"; + final String isbn = "999999999"; + + List books = cassandraTemplate.query(new PreparedStatementCreator() { + + @Override + public PreparedStatement createPreparedStatement(Session session) throws DriverException { + return session.prepare(cql); + } + }, new PreparedStatementBinder() { + + @Override + public BoundStatement bindValues(PreparedStatement ps) throws DriverException { + return ps.bind(isbn); + } + }, new ResultSetExtractor>() { + + @Override + public List extractData(ResultSet rs) throws DriverException, DataAccessException { + List books = new LinkedList(); + + for (Row row : rs.all()) { + books.add(rowToBook(row)); + } + + return books; + } + }); + + Book b2 = getBook(isbn); + + log.debug("Book list Size -> " + books.size()); + + assertEquals(books.size(), 1); + assertBook(books.get(0), b2); + } + + @Test + public void queryTestPreparedStatementCreatorPreparedStatementBinderRowCallbackHandler() { + + final String cql = "select * from book where isbn = ?"; + final String isbn = "999999999"; + + cassandraTemplate.query(new PreparedStatementCreator() { + + @Override + public PreparedStatement createPreparedStatement(Session session) throws DriverException { + return session.prepare(cql); + } + }, new PreparedStatementBinder() { + + @Override + public BoundStatement bindValues(PreparedStatement ps) throws DriverException { + return ps.bind(isbn); + } + }, new RowCallbackHandler() { + + @Override + public void processRow(Row row) throws DriverException { + Book b = rowToBook(row); + Book b2 = getBook(isbn); + assertBook(b, b2); + } + }); + + } + + @Test + public void queryTestPreparedStatementCreatorPreparedStatementBinderRowMapper() { + + final String cql = "select * from book where isbn = ?"; + final String isbn = "999999999"; + + List books = cassandraTemplate.query(new PreparedStatementCreator() { + + @Override + public PreparedStatement createPreparedStatement(Session session) throws DriverException { + return session.prepare(cql); + } + }, new PreparedStatementBinder() { + + @Override + public BoundStatement bindValues(PreparedStatement ps) throws DriverException { + return ps.bind(isbn); + } + }, new RowMapper() { + + @Override + public Book mapRow(Row row, int rowNum) throws DriverException { + return rowToBook(row); + } + }); + + Book b2 = getBook(isbn); + + assertEquals(books.size(), 1); + assertBook(books.get(0), b2); + } + /** * Assert that a Book matches the arguments expected * @@ -458,6 +1046,15 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio } + private Book rowToBook(Row row) { + Book b = new Book(); + b.setIsbn(row.getString("isbn")); + b.setTitle(row.getString("title")); + b.setAuthor(row.getString("author")); + b.setPages(row.getInt("pages")); + return b; + } + /** * Convert Object[] to a Book *