ei.ne.ke.cassandra.cql3.AstyanaxCql3SimpleEntityRepositoryTest.java Source code

Java tutorial

Introduction

Here is the source code for ei.ne.ke.cassandra.cql3.AstyanaxCql3SimpleEntityRepositoryTest.java

Source

/*
 * 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.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.SimpleEntity;
import ei.ne.ke.cassandra.cql3.example.SimpleEntityRepository;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.netflix.astyanax.AstyanaxContext;
import com.netflix.astyanax.Keyspace;
import com.netflix.astyanax.util.TimeUUIDUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans/astyanax-cql3-simple-repository-test.xml")
public class AstyanaxCql3SimpleEntityRepositoryTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(AstyanaxCql3SimpleEntityRepositoryTest.class);

    private static final String ENTITY1_ID = RandomStringUtils.randomAlphabetic(4);
    private static final String ENTITY2_ID = RandomStringUtils.randomAlphabetic(4);

    @Autowired
    private AstyanaxContext<Keyspace> keyspaceContext;

    @Autowired
    private SimpleEntityRepository repository;

    private SimpleEntity getEntity1() {
        SimpleEntity e = new SimpleEntity();
        e.setTheKey(ENTITY1_ID);
        e.setaBigInt(0L);
        e.setaBoolean(Boolean.FALSE);
        e.setaDecimal(BigDecimal.ZERO);
        e.setaDouble(1.0d);
        e.setaFloat(1.0f);
        e.setAnInt(23);
        List<String> stringList = Lists.newArrayList("alpha", "beta");
        e.setaListOfString(stringList);
        List<Integer> intList = Lists.newArrayList(23, 42);
        e.setaListOfInt(intList);
        Map<String, String> mapStringString = Maps.newHashMap();
        mapStringString.put("super", "cali");
        mapStringString.put("fragi", "listic");
        e.setaMapOfStringToString(mapStringString);
        Map<Integer, String> mapIntString = Maps.newHashMap();
        mapIntString.put(1, "one");
        mapIntString.put(2, "two");
        e.setaMapOfIntToString(mapIntString);
        Set<String> stringSet = Sets.newHashSet();
        stringSet.add("foo");
        stringSet.add("bar");
        e.setaSetOfString(stringSet);
        Set<Integer> intSet = Sets.newHashSet();
        intSet.add(1234);
        intSet.add(5678);
        e.setaSetOfInt(intSet);
        e.setaString("Pirate");
        e.setaUuid(UUID.randomUUID());
        return e;
    }

    private SimpleEntity getEntity2() {
        SimpleEntity e = new SimpleEntity();
        e.setTheKey(ENTITY2_ID);
        e.setaBigInt(1L);
        e.setaBoolean(Boolean.TRUE);
        e.setaDecimal(BigDecimal.ONE);
        e.setaDouble(2.0d);
        e.setaFloat(2.0f);
        e.setAnInt(42);
        List<String> stringList = Lists.newArrayList("gamma", "delta");
        e.setaListOfString(stringList);
        List<Integer> intList = Lists.newArrayList(27, 666);
        e.setaListOfInt(intList);
        Map<String, String> mapStringString = Maps.newHashMap();
        mapStringString.put("hello", "world");
        mapStringString.put("what's", "up");
        e.setaMapOfStringToString(mapStringString);
        Map<Integer, String> mapIntString = Maps.newHashMap();
        mapIntString.put(3, "three");
        mapIntString.put(4, "four");
        e.setaMapOfIntToString(mapIntString);
        Set<String> stringSet = Sets.newHashSet();
        stringSet.add("baz");
        stringSet.add("bat");
        e.setaSetOfString(stringSet);
        Set<Integer> intSet = Sets.newHashSet();
        intSet.add(4321);
        intSet.add(8765);
        e.setaSetOfInt(intSet);
        e.setaString("Ninja");
        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() {
        SimpleEntity entity = getEntity1();
        repository.save(entity);
        assertTrue(repository.exists(ENTITY1_ID));
    }

    @Test
    public void testFindOne() {
        SimpleEntity entity = getEntity1();
        repository.save(entity);
        assertTrue(repository.exists(ENTITY1_ID));
        SimpleEntity findOne = repository.findOne(ENTITY1_ID);
        assertNotNull(findOne);
    }

    @Test
    public void testDeleteByID() throws InterruptedException {
        SimpleEntity entity = getEntity1();
        repository.save(entity);
        assertTrue(repository.exists(ENTITY1_ID));
        repository.delete(ENTITY1_ID);
        assertFalse(repository.exists(ENTITY1_ID));
    }

    @Test
    public void testDelete() throws InterruptedException {
        SimpleEntity entity = getEntity1();
        repository.save(entity);
        assertTrue(repository.exists(ENTITY1_ID));
        repository.delete(ENTITY1_ID);
        assertFalse(repository.exists(ENTITY1_ID));
    }

    @Test
    public void testDeleteSet() throws InterruptedException {
        SimpleEntity user1 = getEntity1();
        SimpleEntity user2 = getEntity2();
        repository.save(user1);
        repository.save(user2);
        assertTrue(repository.exists(ENTITY1_ID));
        assertTrue(repository.exists(ENTITY2_ID));

        List<SimpleEntity> users = Lists.newArrayList();
        users.add(user1);
        users.add(user2);
        repository.delete(users);
        assertFalse(repository.exists(ENTITY1_ID));
        assertFalse(repository.exists(ENTITY2_ID));
    }

    @Test
    public void testCount() throws InterruptedException {
        SimpleEntity user1 = getEntity1();
        SimpleEntity user2 = getEntity2();
        repository.save(user1);
        repository.save(user2);
        assertTrue(repository.exists(ENTITY1_ID));
        assertTrue(repository.exists(ENTITY2_ID));

        List<SimpleEntity> users = Lists.newArrayList();
        users.add(user1);
        users.add(user2);
        assertEquals(2, repository.count());

        repository.delete(users);
        assertFalse(repository.exists(ENTITY1_ID));
        assertFalse(repository.exists(ENTITY2_ID));
    }

    @Test
    public void testDeleteAll() throws InterruptedException {
        SimpleEntity user1 = getEntity1();
        SimpleEntity user2 = getEntity2();
        repository.save(user1);
        repository.save(user2);
        assertTrue(repository.exists(ENTITY1_ID));
        assertTrue(repository.exists(ENTITY2_ID));

        List<SimpleEntity> users = Lists.newArrayList();
        users.add(user1);
        users.add(user2);
        assertEquals(2, repository.count());

        repository.deleteAll();
        assertEquals(0, repository.count());
    }

}