PageRenderTime 50ms CodeModel.GetById 19ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

/sigmah/src/test/java/org/sigmah/server/endpoint/gwtrpc/GetSitesTest.java

http://sigma-h.googlecode.com/
Java | 217 lines | 138 code | 71 blank | 8 comment | 0 complexity | dcf4fe1167246c09147d5b3501cfd4dd 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;
  7
  8import com.extjs.gxt.ui.client.Style.SortDir;
  9import com.extjs.gxt.ui.client.data.PagingLoadResult;
 10import com.extjs.gxt.ui.client.data.SortInfo;
 11import junit.framework.Assert;
 12import org.junit.Test;
 13import org.junit.runner.RunWith;
 14import org.sigmah.server.dao.OnDataSet;
 15import org.sigmah.shared.command.GetSitePoints;
 16import org.sigmah.shared.command.GetSites;
 17import org.sigmah.shared.command.result.SitePointList;
 18import org.sigmah.shared.dao.Filter;
 19import org.sigmah.shared.dto.IndicatorDTO;
 20import org.sigmah.shared.dto.SiteDTO;
 21import org.sigmah.shared.exception.CommandException;
 22import org.sigmah.shared.report.model.DimensionType;
 23import org.sigmah.test.InjectionSupport;
 24
 25@RunWith(InjectionSupport.class)
 26@OnDataSet("/dbunit/sites-simple1.db.xml")
 27public class GetSitesTest extends CommandTestCase {
 28    private static final int DATABASE_OWNER = 1;
 29                
 30
 31    @Test
 32    public void testActivityQueryBasic() throws CommandException {
 33
 34
 35        setUser(DATABASE_OWNER);
 36
 37        GetSites cmd = new GetSites();
 38        cmd.filter().onActivity(1);
 39        cmd.setSortInfo(new SortInfo("date2", SortDir.DESC));
 40
 41        PagingLoadResult<SiteDTO> result = execute(cmd);    
 42
 43        Assert.assertEquals("totalLength", 3, result.getData().size());
 44        Assert.assertEquals("totalLength", 3, result.getTotalLength());
 45        Assert.assertEquals("offset", 0, result.getOffset());
 46        //Assert.assertNull("row(0).activity", result.getData().get(0).getActivity());
 47
 48        // assure sorted
 49        Assert.assertEquals("sorted", 2, result.getData().get(0).getId());
 50        Assert.assertEquals("sorted", 1, result.getData().get(1).getId());
 51        Assert.assertEquals("sorted", 3, result.getData().get(2).getId());
 52
 53        // assure indicators are present (site id=3)
 54        SiteDTO s = result.getData().get(2);
 55
 56        Assert.assertEquals("entityName", "Ituri", s.getAdminEntity(1).getName());
 57        Assert.assertNotNull("admin bounds", s.getAdminEntity(1).getBounds());
 58        Assert.assertEquals("indicator", 10000.0, s.getIndicatorValue(1));
 59    }
 60
 61    @Test
 62    public void testIndicatorSort() throws CommandException {
 63
 64        setUser(DATABASE_OWNER);
 65
 66        GetSites cmd = new GetSites();
 67        cmd.filter().onActivity(1);
 68        cmd.setSortInfo(new SortInfo(IndicatorDTO.getPropertyName(1), SortDir.DESC));
 69
 70        PagingLoadResult<SiteDTO> result = execute(cmd);
 71
 72        // assure sorted
 73        Assert.assertEquals("sorted", 10000.0, result.getData().get(0).getIndicatorValue(1));
 74        Assert.assertEquals("sorted", 3600.0, result.getData().get(1).getIndicatorValue(1));
 75        Assert.assertEquals("sorted", 1500.0, result.getData().get(2).getIndicatorValue(1));
 76
 77        Assert.assertNotNull("activityId", result.getData().get(0).getActivityId());
 78    }
 79
 80
 81    @Test
 82    public void testActivityQueryPaged() throws CommandException {
 83
 84        setUser(DATABASE_OWNER);
 85
 86        GetSites cmd = new GetSites();
 87        cmd.filter().onActivity(1);
 88        cmd.setSortInfo(new SortInfo(IndicatorDTO.getPropertyName(1), SortDir.DESC));
 89        cmd.setLimit(2);
 90        cmd.setOffset(0);
 91
 92        PagingLoadResult<SiteDTO> result = execute(cmd);
 93
 94        Assert.assertEquals("rows retrieved [0,2]", 2, result.getData().size());
 95        Assert.assertEquals("total rows [0,2]", 3, result.getTotalLength());
 96
 97        cmd.setOffset(1);
 98        cmd.setLimit(2);
 99
100        result = execute(cmd);
101
102        Assert.assertEquals("offset [1,2]", 1, result.getOffset());
103        Assert.assertEquals("rows retrieved [1,2]", 2, result.getData().size());
104        Assert.assertEquals("total rows [1,2]", 3, result.getTotalLength());
105
106        cmd.setOffset(0);
107        cmd.setLimit(50);
108
109        result = execute(cmd);
110
111        Assert.assertEquals("offset [0,50]", 0, result.getOffset());
112        Assert.assertEquals("rows retrieved [0,50]", 3, result.getData().size());
113        Assert.assertEquals("total rows [0,50]", 3, result.getTotalLength());
114
115    }
116
117
118    @Test
119    public void testDatabase() throws CommandException {
120
121        setUser(DATABASE_OWNER);
122
123        GetSites cmd = new GetSites();
124        cmd.filter().addRestriction(DimensionType.Database, 2);
125
126        PagingLoadResult<SiteDTO> result = execute(cmd);
127
128        Assert.assertEquals("rows", 3, result.getData().size());
129        Assert.assertNotNull("activityId", result.getData().get(0).getActivityId());
130
131    }
132
133    @Test
134    public void testDatabasePaged() throws CommandException {
135
136        setUser(DATABASE_OWNER);
137
138        GetSites cmd = new GetSites();
139        cmd.getFilter().addRestriction(DimensionType.Database, 1);
140        cmd.setLimit(2);
141
142        PagingLoadResult<SiteDTO> result = execute(cmd);
143
144
145        Assert.assertEquals("rows", 2, result.getData().size());
146
147    }
148
149    @Test
150    public void testDatabasePartner2PartnerVisibility() throws CommandException {
151
152        setUser(2); // BAVON (can't see other partner's stuff)
153
154        GetSites cmd = new GetSites();
155        cmd.getFilter().addRestriction(DimensionType.Database, 1);
156
157        PagingLoadResult<SiteDTO> result = execute(cmd);
158
159        Assert.assertEquals("rows", 3, result.getData().size());
160    }
161
162    @Test
163    public void testAll() throws CommandException {
164
165        setUser(DATABASE_OWNER);
166
167        GetSites cmd = new GetSites();
168
169        PagingLoadResult<SiteDTO> result = execute(cmd);
170
171        Assert.assertEquals("rows", 8, result.getData().size());
172        Assert.assertNotNull("activityId", result.getData().get(0).getActivityId());
173
174    }
175
176    @Test
177    public void testAllWithRemovedUser() throws CommandException {
178
179        setUser(5); // Christian (Bad guy!)
180
181        PagingLoadResult<SiteDTO> result = execute(new GetSites());
182
183        Assert.assertEquals("rows", 0, result.getData().size());
184
185    }
186
187    @Test
188    public void testSeekSite() throws Exception {
189
190        setUser(DATABASE_OWNER);
191
192        GetSites cmd = new GetSites();
193        cmd.filter().onActivity(1);
194        cmd.setSortInfo(new SortInfo(IndicatorDTO.getPropertyName(1), SortDir.DESC));
195        cmd.setLimit(2);
196        cmd.setSeekToSiteId(1);
197
198        PagingLoadResult<SiteDTO> result = execute(cmd);
199
200        Assert.assertEquals("second page returned", 2, result.getOffset());
201        Assert.assertEquals("rows on this page", 1, result.getData().size());
202        Assert.assertEquals("correct site returned", 1, result.getData().get(0).getId());
203    }
204    
205    @Test
206    public void testSitePointsForIndicator() throws Exception {
207    	setUser(DATABASE_OWNER);
208    	
209    	Filter filter = new Filter();
210    	filter.addRestriction(DimensionType.Indicator, 1);
211    	
212    	GetSitePoints cmd = new GetSitePoints(filter);
213    	
214    	SitePointList list = execute(cmd);
215    	Assert.assertEquals(3, list.getPoints().size());
216    }
217}