|
| 1 | +package org.baeldung.spring.data.cassandra.repository; |
| 2 | + |
| 3 | +import com.datastax.driver.core.utils.UUIDs; |
| 4 | +import com.google.common.collect.ImmutableSet; |
| 5 | +import org.apache.cassandra.exceptions.ConfigurationException; |
| 6 | +import org.apache.thrift.transport.TTransportException; |
| 7 | +import org.baeldung.spring.data.cassandra.config.CassandraConfig; |
| 8 | +import org.baeldung.spring.data.cassandra.model.Event; |
| 9 | +import org.cassandraunit.spring.CassandraUnitTestExecutionListener; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.cassandra.core.cql.CqlIdentifier; |
| 14 | +import org.springframework.data.cassandra.core.CassandraAdminOperations; |
| 15 | +import org.springframework.test.context.ContextConfiguration; |
| 16 | +import org.springframework.test.context.TestExecutionListeners; |
| 17 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 18 | +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.HashMap; |
| 22 | + |
| 23 | +import static org.hamcrest.CoreMatchers.hasItem; |
| 24 | +import static org.hamcrest.CoreMatchers.not; |
| 25 | +import static org.junit.Assert.assertThat; |
| 26 | + |
| 27 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 28 | +@ContextConfiguration(classes = {CassandraConfig.class}) |
| 29 | +@TestExecutionListeners({CassandraUnitTestExecutionListener.class, DependencyInjectionTestExecutionListener.class}) |
| 30 | +public class EventRepositoryIntegrationTest { |
| 31 | + |
| 32 | + public static final String TIME_BUCKET = "2014-01-01"; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private EventRepository eventRepository; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private CassandraAdminOperations adminTemplate; |
| 39 | + |
| 40 | + @Test |
| 41 | + public void repositoryStoresAndRetrievesEvents() throws InterruptedException, TTransportException, ConfigurationException, IOException { |
| 42 | + adminTemplate.execute("CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = {" |
| 43 | + + " 'class': 'SimpleStrategy', " |
| 44 | + + " 'replication_factor': '3' " |
| 45 | + + "};" ); |
| 46 | + adminTemplate.dropTable(CqlIdentifier.cqlId("event")); |
| 47 | + adminTemplate.createTable(true, CqlIdentifier.cqlId("event"), Event.class, new HashMap<String, Object>()); |
| 48 | + Event event1 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag1", "tag2")); |
| 49 | + Event event2 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag3")); |
| 50 | + eventRepository.save(ImmutableSet.of(event1, event2)); |
| 51 | + |
| 52 | + Iterable<Event> events = eventRepository.findByTypeAndBucket("type1", TIME_BUCKET); |
| 53 | + |
| 54 | + assertThat(events, hasItem(event1)); |
| 55 | + assertThat(events, hasItem(event2)); |
| 56 | + } |
| 57 | + |
| 58 | + // @Test |
| 59 | + public void repositoryDeletesStoredEvents() { |
| 60 | + Event event1 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag1", "tag2")); |
| 61 | + Event event2 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag3")); |
| 62 | + eventRepository.save(ImmutableSet.of(event1, event2)); |
| 63 | + |
| 64 | + eventRepository.delete(event1); |
| 65 | + eventRepository.delete(event2); |
| 66 | + |
| 67 | + Iterable<Event> events = eventRepository.findByTypeAndBucket("type1", TIME_BUCKET); |
| 68 | + |
| 69 | + assertThat(events, not(hasItem(event1))); |
| 70 | + assertThat(events, not(hasItem(event2))); |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments