PageRenderTime 29ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/server/endpoint/gwtrpc/handler/GetAdminEntitiesHandlerTest.java

http://sigma-h.googlecode.com/
Java | 134 lines | 95 code | 35 blank | 4 comment | 0 complexity | f15ed84024582fea7b1bf90d5537a35f MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.server.endpoint.gwtrpc.handler;
  7
  8import org.dozer.Mapper;
  9import org.junit.Before;
 10import org.junit.Test;
 11import org.sigmah.shared.dao.AdminDAO;
 12import org.sigmah.shared.domain.*;
 13import org.sigmah.shared.command.GetAdminEntities;
 14import org.sigmah.shared.command.result.AdminEntityResult;
 15import org.sigmah.shared.dto.AdminEntityDTO;
 16import org.sigmah.shared.exception.CommandException;
 17
 18import java.util.Collections;
 19import java.util.List;
 20
 21import static org.easymock.EasyMock.*;
 22import static org.junit.Assert.assertEquals;
 23
 24public class GetAdminEntitiesHandlerTest {
 25
 26    private User user;
 27    private AdminDAO adminDAO;
 28    private Mapper mapper;
 29    private static final int LEVEL_ID = 1;
 30    private static final int ACTIVITY_ID = 4;
 31    private static final int PARENT_ID = ACTIVITY_ID;
 32    private AdminDAO.Query query;
 33
 34    @Test
 35    public void testRootLevelQuery() throws Exception {
 36
 37        GetAdminEntities cmd = new GetAdminEntities(LEVEL_ID);
 38
 39        expect(query.level(LEVEL_ID)).andReturn(query);
 40        expect(query.execute()).andReturn(nCopies(15));
 41        replay(query);
 42
 43        AdminEntityResult result = execute(cmd);
 44
 45        assertEquals(15, result.getData().size());
 46    }
 47
 48    @Test
 49    public void testEmptyRootLevelQuery() throws Exception {
 50
 51        GetAdminEntities cmd = new GetAdminEntities(LEVEL_ID);
 52
 53        expect(query.level(LEVEL_ID)).andReturn(query);
 54        expect(query.execute()).andReturn(nCopies(0));
 55        replay(query);
 56
 57        AdminEntityResult result = execute(cmd);
 58
 59        assertEquals(0, result.getData().size());
 60    }
 61
 62    @Test
 63    public void testChildQuery() throws Exception {
 64
 65        GetAdminEntities cmd = new GetAdminEntities(LEVEL_ID, PARENT_ID);
 66
 67        expect(query.level(LEVEL_ID)).andReturn(query);
 68        expect(query.withParentEntityId(PARENT_ID)).andReturn(query);
 69        expect(query.execute()).andReturn(nCopies(3));
 70        replay(query);
 71
 72        AdminEntityResult result = execute(cmd);
 73
 74        assertEquals(3, result.getData().size());
 75    }
 76
 77    @Test
 78    public void testSiteQuery() throws Exception {
 79
 80        int expectedCount = 3;
 81
 82        GetAdminEntities cmd = new GetAdminEntities(LEVEL_ID, PARENT_ID);
 83        cmd.setActivityId(ACTIVITY_ID);
 84
 85        expect(query.level(LEVEL_ID)).andReturn(query);
 86        expect(query.withParentEntityId(PARENT_ID)).andReturn(query);
 87        expect(query.withSitesOfActivityId(ACTIVITY_ID)).andReturn(query);
 88        expect(query.execute()).andReturn(nCopies(expectedCount));
 89        replay(query);
 90
 91        AdminEntityResult result = execute(cmd);
 92
 93        assertEquals(expectedCount, result.getData().size());
 94
 95    }
 96
 97    @Before
 98    public void setupUser() {
 99        user = new User();
100        user.setId(LEVEL_ID);
101        user.setEmail("alex@bertram");
102    }
103
104    @Before
105    public void setupMapper() {
106        mapper = createMock(Mapper.class);
107        expect(mapper.map(isA(AdminEntity.class), eq(AdminEntityDTO.class)))
108                .andReturn(new AdminEntityDTO())
109                .anyTimes();
110        replay(mapper);
111    }
112
113    @Before
114    public void setupAdminDAO() {
115        adminDAO = createMock(AdminDAO.class);
116        expect(adminDAO.query()).andReturn(query);
117        replay(adminDAO);
118    }
119
120    @Before
121    public void createQueryMock() {
122        query = createMock(AdminDAO.Query.class);
123    }
124
125    public AdminEntityResult execute(GetAdminEntities cmd) throws CommandException {
126        GetAdminEntitiesHandler handler = new GetAdminEntitiesHandler(adminDAO, mapper);
127        return (AdminEntityResult) handler.execute(cmd, user);
128    }
129
130    private List<AdminEntity> nCopies(int entitiesToReturn) {
131        return Collections.nCopies(entitiesToReturn, new AdminEntity());
132    }
133
134}