SimpleCassandraRepository integration tests for User

This commit is contained in:
Alex Shvid
2013-11-27 14:04:33 -08:00
parent 21b30a0dc1
commit d5b57d759d
4 changed files with 119 additions and 14 deletions

View File

@@ -64,6 +64,7 @@
<name>Alex Shvid</name>
<email>a at shvid.com</email>
<roles>
<role>Project Lead</role>
<role>Developer</role>
</roles>
<timezone>-8</timezone>

View File

@@ -989,6 +989,8 @@ public class CassandraDataTemplate extends CassandraTemplate implements Cassandr
*/
private <T> T doSelectOne(final String query, ReadRowCallback<T> readRowCallback) {
logger.info(query);
/*
* Run the Query
*/

View File

@@ -15,13 +15,21 @@
*/
package org.springframework.data.cassandra.test.integration.repository;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -32,6 +40,8 @@ import org.springframework.data.cassandra.test.integration.table.User;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.google.common.collect.Lists;
/**
* Base class for tests for {@link UserRepository}.
*
@@ -48,7 +58,9 @@ public class UserRepositoryIntegrationTests {
@Autowired
protected CassandraDataOperations dataOperations;
User alex;
User tom, bob, alice, scott;
List<User> all;
@BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
@@ -61,31 +73,96 @@ public class UserRepositoryIntegrationTests {
repository.deleteAll();
alex = new User();
alex.setUsername("alex");
alex.setFirstName("Alex");
alex.setLastName("Shvid");
alex.setPassword("123");
alex.setPlace("SF");
tom = new User();
tom.setUsername("tom");
tom.setFirstName("Tom");
tom.setLastName("Ron");
tom.setPassword("123");
tom.setPlace("SF");
dataOperations.insert(alex);
bob = new User();
bob.setUsername("bob");
bob.setFirstName("Bob");
bob.setLastName("White");
bob.setPassword("555");
bob.setPlace("NY");
alice = new User();
alice.setUsername("alice");
alice.setFirstName("Alice");
alice.setLastName("Red");
alice.setPassword("777");
alice.setPlace("LA");
scott = new User();
scott.setUsername("scott");
scott.setFirstName("Scott");
scott.setLastName("Van");
scott.setPassword("444");
scott.setPlace("Boston");
all = dataOperations.insert(Arrays.asList(tom, bob, alice, scott));
}
@Test
public void findsUserById() throws Exception {
User user = repository.findOne(bob.getUsername());
Assert.assertNotNull(user);
assertEquals(bob, user);
}
@Test
public void findsProfileById() throws Exception {
public void findsAll() throws Exception {
List<User> result = Lists.newArrayList(repository.findAll());
assertThat(result.size(), is(all.size()));
assertThat(result.containsAll(all), is(true));
System.out.println("find profile by id");
}
@After
public void clearCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
@Test
public void findsAllWithGivenIds() {
Iterable<User> result = repository.findAll(Arrays.asList(bob.getUsername(), tom.getUsername()));
assertThat(result, hasItems(bob, tom));
assertThat(result, not(hasItems(alice, scott)));
}
@Test
public void deletesUserCorrectly() throws Exception {
repository.delete(tom);
List<User> result = Lists.newArrayList(repository.findAll());
assertThat(result.size(), is(all.size() - 1));
assertThat(result, not(hasItem(tom)));
}
@Test
public void deletesUserByIdCorrectly() {
repository.delete(tom.getUsername().toString());
List<User> result = Lists.newArrayList(repository.findAll());
assertThat(result.size(), is(all.size() - 1));
assertThat(result, not(hasItem(tom)));
}
@AfterClass
public static void stopCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
EmbeddedCassandraServerHelper.stopEmbeddedCassandra();
}
private static void assertEquals(User user1, User user2) {
Assert.assertEquals(user1.getUsername(), user2.getUsername());
Assert.assertEquals(user1.getFirstName(), user2.getFirstName());
Assert.assertEquals(user1.getLastName(), user2.getLastName());
Assert.assertEquals(user1.getPlace(), user2.getPlace());
Assert.assertEquals(user1.getPassword(), user2.getPassword());
}
}

View File

@@ -155,4 +155,29 @@ public class User {
this.birthYear = birthYear;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}