Java tutorial
/* * Copyright 2013 EK3 Technologies, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ei.ne.ke.cassandra.cql3; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.List; import java.util.UUID; import org.apache.commons.lang.RandomStringUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import ei.ne.ke.cassandra.cql3.example.CompositeEntity; import ei.ne.ke.cassandra.cql3.example.CompositeEntityRepository; import ei.ne.ke.cassandra.cql3.example.EmbeddedEntity; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.netflix.astyanax.AstyanaxContext; import com.netflix.astyanax.Keyspace; import com.netflix.astyanax.util.TimeUUIDUtils; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:beans/astyanax-cql3-composite-repository-test.xml") public class AstyanaxCql3CompositeEntityRepositoryTest { private static final Logger LOGGER = LoggerFactory.getLogger(AstyanaxCql3CompositeEntityRepositoryTest.class); private static final EmbeddedEntity ID1 = new EmbeddedEntity(RandomStringUtils.randomAlphabetic(4), 0L, 0.0f); private static final EmbeddedEntity ID2 = new EmbeddedEntity(RandomStringUtils.randomAlphabetic(4), 1L, 1.0f); @Autowired private AstyanaxContext<Keyspace> keyspaceContext; @Autowired private CompositeEntityRepository repository; private CompositeEntity getEntity1() { CompositeEntity e = new CompositeEntity(); e.setKey(ID1); e.setaString("alpha"); e.setaUuid(UUID.randomUUID()); return e; } private CompositeEntity getEntity2() { CompositeEntity e = new CompositeEntity(); e.setKey(ID2); e.setaString("beta"); e.setaUuid(TimeUUIDUtils.getUniqueTimeUUIDinMillis()); return e; } @Before public void initializeContext() throws Exception { Cassandra.initialize(keyspaceContext.getClient()); } @After public void truncateTables() throws Exception { Cassandra.truncateTables(keyspaceContext.getClient()); } @Test public void testSave() { CompositeEntity entity = getEntity1(); repository.save(entity); assertTrue(repository.exists(ID1)); } @Test public void testFindOne() { CompositeEntity entity = getEntity1(); repository.save(entity); assertTrue(repository.exists(ID1)); CompositeEntity findOne = repository.findOne(ID1); assertNotNull(findOne); } @Test public void testDeleteByID() throws InterruptedException { CompositeEntity entity = getEntity1(); repository.save(entity); assertTrue(repository.exists(ID1)); repository.delete(ID1); assertFalse(repository.exists(ID1)); } @Test public void testDelete() throws InterruptedException { CompositeEntity entity = getEntity1(); repository.save(entity); assertTrue(repository.exists(ID1)); repository.delete(ID1); assertFalse(repository.exists(ID1)); } @Test public void testDeleteSet() throws InterruptedException { CompositeEntity entity1 = getEntity1(); CompositeEntity entity2 = getEntity2(); repository.save(entity1); repository.save(entity2); assertTrue(repository.exists(ID1)); assertTrue(repository.exists(ID2)); List<CompositeEntity> entities = Lists.newArrayList(); entities.add(entity1); entities.add(entity2); repository.delete(entities); assertFalse(repository.exists(ID1)); assertFalse(repository.exists(ID2)); } @Test public void testCount() throws InterruptedException { CompositeEntity entity1 = getEntity1(); CompositeEntity entity2 = getEntity2(); repository.save(entity1); repository.save(entity2); assertTrue(repository.exists(ID1)); assertTrue(repository.exists(ID2)); List<CompositeEntity> entities = Lists.newArrayList(); entities.add(entity1); entities.add(entity2); assertEquals(2, repository.count()); repository.delete(entities); assertFalse(repository.exists(ID1)); assertFalse(repository.exists(ID2)); } @Test public void testDeleteAll() throws InterruptedException { CompositeEntity entity1 = getEntity1(); CompositeEntity entity2 = getEntity2(); repository.save(entity1); repository.save(entity2); assertTrue(repository.exists(ID1)); assertTrue(repository.exists(ID2)); List<CompositeEntity> entities = Lists.newArrayList(); entities.add(entity1); entities.add(entity2); assertEquals(2, repository.count()); repository.deleteAll(); assertEquals(0, repository.count()); } @Test public void testFindAll() throws InterruptedException { List<EmbeddedEntity> keys = Lists.newArrayList(); Iterable<CompositeEntity> result = null; CompositeEntity entity1 = new CompositeEntity(); entity1.setKey(new EmbeddedEntity("sharedPrimaryKey", 0L, 0.0f)); entity1.setaString("alpha"); entity1.setaUuid(UUID.randomUUID()); repository.save(entity1); assertTrue(repository.exists(new EmbeddedEntity("sharedPrimaryKey", 0L, 0.0f))); keys.clear(); keys.add(new EmbeddedEntity("sharedPrimaryKey")); result = repository.findAll(keys); assertEquals(1, Iterables.size(result)); keys.clear(); keys.add(new EmbeddedEntity("sharedPrimaryKey", 0L)); result = repository.findAll(keys); assertEquals(1, Iterables.size(result)); keys.clear(); keys.add(new EmbeddedEntity("sharedPrimaryKey", 0L, 0.0f)); result = repository.findAll(keys); assertEquals(1, Iterables.size(result)); CompositeEntity entity2 = new CompositeEntity(); entity2.setKey(new EmbeddedEntity("sharedPrimaryKey", 0L, 1.0f)); entity2.setaString("beta"); entity2.setaUuid(UUID.randomUUID()); repository.save(entity2); assertTrue(repository.exists(new EmbeddedEntity("sharedPrimaryKey", 0L, 1.0f))); keys.clear(); keys.add(new EmbeddedEntity("sharedPrimaryKey")); result = repository.findAll(keys); assertEquals(2, Iterables.size(result)); keys.clear(); keys.add(new EmbeddedEntity("sharedPrimaryKey", 0L)); result = repository.findAll(keys); assertEquals(2, Iterables.size(result)); keys.clear(); keys.add(new EmbeddedEntity("sharedPrimaryKey", 0L, 1.0f)); result = repository.findAll(keys); assertEquals(1, Iterables.size(result)); } }