PageRenderTime 29ms CodeModel.GetById 4ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/src/test/java/com/google/ie/business/dao/impl/ShardedCounterDaoImplTest.java

http://thoughtsite.googlecode.com/
Java | 108 lines | 83 code | 20 blank | 5 comment | 6 complexity | b2d22a1dcd114a4a7ddc9cc2343bc3bf MD5 | raw file
  1package com.google.ie.business.dao.impl;
  2
  3import static org.junit.Assert.assertEquals;
  4
  5import com.google.appengine.api.datastore.DatastoreServiceFactory;
  6import com.google.appengine.api.datastore.Query;
  7import com.google.ie.business.domain.Idea;
  8import com.google.ie.business.domain.ShardedCounter;
  9import com.google.ie.business.domain.User;
 10import com.google.ie.test.DatastoreTest;
 11
 12import org.junit.Before;
 13import org.junit.Test;
 14
 15import java.util.List;
 16
 17/**
 18 * Test cases for IdeaDaoImpl class
 19 * 
 20 * @author gmaurya
 21 */
 22public class ShardedCounterDaoImplTest extends DatastoreTest {
 23
 24    private ShardedCounterDaoImpl shardedCounterDao;
 25    private UserDaoImpl userDao;
 26    private IdeaDaoImpl ideaDao;
 27
 28    private Idea idea;
 29    private User user;
 30    private ShardedCounter counter;
 31    private static final int shardNumber = 2;
 32
 33    @Before
 34    public void setUp() {
 35        super.setUp();
 36        if (shardedCounterDao == null) {
 37            shardedCounterDao = new ShardedCounterDaoImpl();
 38            shardedCounterDao.setPersistenceManagerFactory(pmf);
 39        }
 40        if (userDao == null) {
 41            userDao = new UserDaoImpl();
 42            userDao.setPersistenceManagerFactory(pmf);
 43        }
 44        if (ideaDao == null) {
 45            ideaDao = new IdeaDaoImpl();
 46            ideaDao.setPersistenceManagerFactory(pmf);
 47        }
 48
 49        user = new User();
 50        user.setDisplayName("test user");
 51        user = userDao.saveUser(user);
 52
 53        idea = new Idea();
 54        idea.setTitle("Title");
 55        idea.setCreatorKey(user.getUserKey());
 56        idea.setDescription("Idea Description");
 57
 58        idea = ideaDao.saveIdea(idea);
 59    }
 60
 61    @Test
 62    public void createOrUpdateShardedCounter() {
 63        counter = new ShardedCounter(idea.getKey());
 64        counter.setPositivePoint(0);
 65        counter.setTotalPoint(15);
 66        counter.setShardNumber(shardNumber);
 67        counter = shardedCounterDao.createOrUpdateShardedCounter(counter);
 68
 69        Query query = new Query(ShardedCounter.class.getSimpleName());
 70        assertEquals(1,
 71                        DatastoreServiceFactory.getDatastoreService().prepare(query)
 72                        .countEntities());
 73        counter.setPositivePoint(12);
 74        counter.setTotalPoint(30);
 75        counter = shardedCounterDao.createOrUpdateShardedCounter(counter);
 76        counter = shardedCounterDao.findEntityByPrimaryKey(ShardedCounter.class, counter.getKey());
 77        assertEquals(12, counter.getPositivePoint());
 78
 79    }
 80
 81    @Test
 82    public void getShardByParentKey() {
 83        counter = new ShardedCounter(idea.getKey());
 84        counter.setPositivePoint(0);
 85        counter.setTotalPoint(15);
 86        counter.setShardNumber(shardNumber);
 87        counter = shardedCounterDao.createOrUpdateShardedCounter(counter);
 88
 89        List<ShardedCounter> shards = shardedCounterDao.getShardsByParentKey(idea.getKey());
 90        assertEquals(1, shards.size());
 91
 92    }
 93
 94    @Test
 95    public void getShardByParentKeyAndShardNumber() {
 96        counter = new ShardedCounter(idea.getKey());
 97        counter.setPositivePoint(0);
 98        counter.setTotalPoint(15);
 99        counter.setShardNumber(shardNumber);
100        counter = shardedCounterDao.createOrUpdateShardedCounter(counter);
101        List<ShardedCounter> shards =
102                        shardedCounterDao.getShardsByParentKeyAndShardNum(idea
103                        .getKey(), shardNumber);
104        assertEquals(1, shards.size());
105
106    }
107
108}