PageRenderTime 54ms CodeModel.GetById 10ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://thoughtsite.googlecode.com/
Java | 202 lines | 174 code | 23 blank | 5 comment | 2 complexity | 151314ea214a888b3cf7d2218135e6e4 MD5 | raw file
  1package com.google.ie.business.dao.impl;
  2
  3import static org.junit.Assert.assertEquals;
  4import static org.junit.Assert.assertNotNull;
  5
  6import com.google.appengine.api.datastore.DatastoreServiceFactory;
  7import com.google.appengine.api.datastore.Query;
  8import com.google.ie.business.domain.Idea;
  9import com.google.ie.business.domain.Tag;
 10import com.google.ie.business.domain.User;
 11import com.google.ie.business.service.ServiceConstants;
 12import com.google.ie.dto.RetrievalInfo;
 13import com.google.ie.test.DatastoreTest;
 14
 15import org.junit.Before;
 16import org.junit.Test;
 17
 18import java.util.HashSet;
 19import java.util.List;
 20import java.util.Set;
 21
 22/**
 23 * Test cases for IdeaDaoImpl class
 24 * 
 25 * @author abraina
 26 */
 27public class IdeaDaoImplTest extends DatastoreTest {
 28
 29    private IdeaDaoImpl ideaDao;
 30    private UserDaoImpl userDao;
 31    private TagDaoImpl tagDao;
 32
 33    @Before
 34    public void setUp() {
 35        super.setUp();
 36        if (ideaDao == null) {
 37            ideaDao = new IdeaDaoImpl();
 38            ideaDao.setPersistenceManagerFactory(pmf);
 39            userDao = new UserDaoImpl();
 40            userDao.setPersistenceManagerFactory(pmf);
 41            tagDao = new TagDaoImpl();
 42            tagDao.setPersistenceManagerFactory(pmf);
 43        }
 44    }
 45
 46    @Test
 47    public void save() {
 48        Idea idea = new Idea();
 49        idea.setTitle("Title");
 50        ideaDao.saveIdea(idea);
 51        Query query = new Query(Idea.class.getSimpleName());
 52        assertEquals(1,
 53                        DatastoreServiceFactory.getDatastoreService().prepare(query)
 54                        .countEntities());
 55    }
 56
 57    @Test
 58    public void save_existingIdea() {
 59        Idea idea = new Idea();
 60        idea.setTitle("Title");
 61        idea = ideaDao.saveIdea(idea);
 62        idea.setTitle("Title1234");
 63        idea = ideaDao.saveIdea(idea);
 64        Idea newIdea = ideaDao.getIdea(idea);
 65        assertEquals("Title1234", newIdea.getTitle());
 66        Query query = new Query(Idea.class.getSimpleName());
 67        assertEquals(1,
 68                        DatastoreServiceFactory.getDatastoreService().prepare(query)
 69                        .countEntities());
 70    }
 71
 72    @Test
 73    public void getUserIdeas() {
 74        User user = new User();
 75        user.setUserKey("UserTestKey");
 76        String key = user.getUserKey();
 77
 78        Idea idea1 = new Idea();
 79        idea1.setTitle("title1");
 80        idea1.setCreatorKey(key);
 81        ideaDao.saveIdea(idea1);
 82        RetrievalInfo dummyInfo = createDummyRetrievalParam(0, 1,
 83                        DaoConstants.DEFAULT_IDEA_ORDERING_FIELD,
 84                        DaoConstants.DEFAULT_IDEA_ORDERING_TYPE);
 85        Set<String> statusOfIdeas = new HashSet<String>();
 86        statusOfIdeas.add("published");
 87        List<Idea> ideas = ideaDao.getUserIdeas(user, statusOfIdeas, dummyInfo);
 88        assertNotNull(ideas);
 89    }
 90
 91    @Test
 92    public void getUserIdeas_checkNumberOfIdeasReturned() {
 93        User user = new User();
 94        user.setReputationPoints(100);
 95        user = userDao.saveUser(user);
 96
 97        String key = user.getUserKey();
 98        Idea idea1 = new Idea();
 99        idea1.setTitle("title1");
100        idea1.setCreatorKey(key);
101        idea1.setStatus("Objectionable");
102        ideaDao.saveIdea(idea1);
103
104        Idea idea2 = new Idea();
105        idea2.setTitle("title2");
106        idea2.setCreatorKey(key);
107        idea2.setStatus("Saved");
108        ideaDao.saveIdea(idea2);
109
110        RetrievalInfo dummyInfo = createDummyRetrievalParam(0, 2,
111                        DaoConstants.DEFAULT_IDEA_ORDERING_FIELD,
112                        DaoConstants.DEFAULT_IDEA_ORDERING_TYPE);
113        Set<String> statusOfIdeas = new HashSet<String>();
114        statusOfIdeas.add("Published");
115        statusOfIdeas.add("Objectionable");
116        statusOfIdeas.add("Saved");
117        List<Idea> ideas = ideaDao.getUserIdeas(user, statusOfIdeas, dummyInfo);
118        assertEquals(2, ideas.size());
119    }
120
121    @Test
122    public void getUserIdeas_checkRetrievalInfoParam() {
123        User user = new User();
124        user.setReputationPoints(100);
125        user = userDao.saveUser(user);
126
127        RetrievalInfo retrievalParam = createDummyRetrievalParam(0, 1,
128                        DaoConstants.DEFAULT_IDEA_ORDERING_FIELD,
129                        DaoConstants.DEFAULT_IDEA_ORDERING_TYPE);
130        String key = user.getUserKey();
131        Idea idea1 = new Idea();
132        idea1.setTitle("title1");
133        idea1.setCreatorKey(key);
134        ideaDao.saveIdea(idea1);
135
136        Idea idea2 = new Idea();
137        idea2.setTitle("title2");
138        idea2.setCreatorKey(key);
139        idea2.setStatus("Published");
140        ideaDao.saveIdea(idea2);
141
142        Set<String> statusOfIdeas = new HashSet<String>();
143        statusOfIdeas.add("Published");
144        List<Idea> ideas = ideaDao.getUserIdeas(user, statusOfIdeas,
145                        retrievalParam);
146        assertEquals(1, ideas.size());
147    }
148
149    @Test
150    public void getIdea() {
151        Idea newIdea;
152        Idea ideaOld = new Idea();
153        ideaOld.setTitle("TestTitle");
154        ideaOld.setCreatorKey("userKey");
155        ideaOld.setStatus(Idea.STATUS_PUBLISHED);
156        newIdea = ideaDao.saveIdea(ideaOld);
157        assertNotNull(ideaDao.getIdea(newIdea));
158    }
159
160    @Test
161    public void getIdeas() {
162        Idea newIdea;
163        Idea ideaOld = new Idea();
164        ideaOld.setTitle("TestTitle");
165        ideaOld.setCreatorKey("userKey");
166        ideaOld.setStatus(Idea.STATUS_PUBLISHED);
167        newIdea = ideaDao.saveIdea(ideaOld);
168
169        RetrievalInfo retrievalParam = createDummyRetrievalParam(0, 1,
170                        DaoConstants.IDEA_ORDERING_FIELD_PUBLISH_DATE,
171                        DaoConstants.DEFAULT_IDEA_ORDERING_TYPE);
172        Set<String> statusOfIdeas = new HashSet<String>();
173        statusOfIdeas.add("Published");
174        assertNotNull(ideaDao.getIdeas(retrievalParam, statusOfIdeas));
175        assertEquals(newIdea.getTitle(),
176                        ideaDao.getIdeas(retrievalParam, statusOfIdeas).get(0).getTitle());
177    }
178
179    @Test
180    public void getIdeasByTagKey() {
181        Idea expectedIdea = new Idea();
182        Tag tag = new Tag();
183        tag.setTitle("tag1");
184        tag = tagDao.saveTag(tag);
185        System.out.println("Tag key ::" + tag.getKey());
186        expectedIdea.setTitle("Test Idea");
187        expectedIdea.setCreatorKey("userKey");
188        expectedIdea.setStatus(Idea.STATUS_PUBLISHED);
189        Set<String> tagKeys = new HashSet<String>();
190        tagKeys.add(tag.getKey());
191        expectedIdea.setTagKeys(tagKeys);
192        expectedIdea = ideaDao.saveIdea(expectedIdea);
193        Set<String> setOfStatus = new HashSet<String>();
194        setOfStatus.add(Idea.STATUS_PUBLISHED);
195        List<Idea> listOfIdeas = ideaDao.getIdeasByTagKey(tag.getKey(), setOfStatus, new
196                        RetrievalInfo(0, 10, ServiceConstants.DEFAULT_IDEA_ORDERING_FIELD,
197                        ServiceConstants.DEFAULT_IDEA_ORDERING_TYPE));
198        assertNotNull(listOfIdeas);
199        Idea actualIdea = listOfIdeas.iterator().next();
200        assertEquals(expectedIdea.getTitle(), actualIdea.getTitle());
201    }
202}