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

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

http://sigma-h.googlecode.com/
Java | 230 lines | 125 code | 75 blank | 30 comment | 0 complexity | e113495faea96d31b90ef2e687d70d9c 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.data.PagingLoadResult;
  9import org.junit.Assert;
 10import org.junit.Test;
 11import org.junit.runner.RunWith;
 12import org.sigmah.server.dao.OnDataSet;
 13import org.sigmah.shared.command.CreateEntity;
 14import org.sigmah.shared.command.GetSites;
 15import org.sigmah.shared.command.result.CreateResult;
 16import org.sigmah.shared.dto.AdminEntityDTO;
 17import org.sigmah.shared.dto.PartnerDTO;
 18import org.sigmah.shared.dto.SiteDTO;
 19import org.sigmah.shared.exception.CommandException;
 20import org.sigmah.test.InjectionSupport;
 21
 22import java.util.GregorianCalendar;
 23
 24@RunWith(InjectionSupport.class)
 25@OnDataSet("/dbunit/sites-simple1.db.xml")
 26public class CreateSiteTest extends CommandTestCase {
 27
 28
 29    @Test
 30    public void test() throws CommandException {
 31        // create a new detached, client model
 32        SiteDTO newSite = new SiteDTO();
 33
 34        newSite.setActivityId(1);
 35        newSite.setPartner(new PartnerDTO(1, "Foobar"));
 36        newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
 37        newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
 38        newSite.setLocationName("Virunga");
 39        newSite.setLocationAxe("Goma - Rutshuru");
 40        newSite.setX(27.432);
 41        newSite.setY(1.23);
 42        newSite.setIndicatorValue(1, 996.0);
 43        newSite.setIndicatorValue(2, null);
 44        newSite.setAttributeValue(1, true);
 45        newSite.setAttributeValue(2, false);
 46        newSite.setComments("huba huba");
 47
 48        // create command
 49
 50        CreateEntity cmd = CreateEntity.Site(newSite);
 51
 52        // execute the command
 53
 54        setUser(1);
 55
 56        CreateResult result = (CreateResult) execute(cmd);
 57
 58
 59        // let the client know the command has succeeded
 60        newSite.setId(result.getNewId());
 61        //cmd.onCompleted(result);
 62
 63
 64        // try to retrieve what we've created
 65
 66        PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
 67
 68        Assert.assertEquals(1, loadResult.getData().size());
 69
 70        SiteDTO secondRead = loadResult.getData().get(0);
 71
 72
 73        // confirm that the changes are there
 74        Assert.assertEquals("site.location.name", newSite.getLocationName(), secondRead.getLocationName());
 75        Assert.assertEquals("site.attribute[1]", true, secondRead.getAttributeValue(1));
 76        Assert.assertEquals("site.reportingPeriod[0].indicatorValue[0]", 996.0, secondRead.getIndicatorValue(1), 0.1);
 77        Assert.assertEquals("site.comments", newSite.getComments(), secondRead.getComments());
 78        Assert.assertEquals("site.partner", newSite.getPartner().getId(), secondRead.getPartner().getId());
 79    }
 80
 81
 82    @Test
 83    public void testAdminBoundCreate() throws CommandException {
 84        // create a new detached, client model
 85        SiteDTO newSite = new SiteDTO();
 86
 87        newSite.setActivityId(4);
 88        newSite.setPartner(new PartnerDTO(1, "Foobar"));
 89        newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
 90        newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
 91        newSite.setAdminEntity(1, new AdminEntityDTO(1, 2, "Sud Kivu"));
 92        newSite.setAdminEntity(2, new AdminEntityDTO(2, 11, "Walungu"));
 93        newSite.setAdminEntity(3, null);
 94        newSite.setX(27.432);
 95        newSite.setY(1.23);
 96        newSite.setComments("huba huba");
 97
 98        // create command
 99
100        CreateEntity cmd = CreateEntity.Site(newSite);
101
102        // execute the command
103
104        setUser(1);
105
106        CreateResult result = (CreateResult) execute(cmd);
107        newSite.setId(result.getNewId());
108
109
110        // try to retrieve what we've created
111
112        PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
113
114        Assert.assertEquals(1, loadResult.getData().size());
115
116        SiteDTO secondRead = loadResult.getData().get(0);
117
118
119        // confirm that the changes are there
120        Assert.assertEquals("site.location.name", "Walungu", secondRead.getLocationName());
121    }
122
123
124    @Test
125    public void testAllAttribsFalse() throws CommandException {
126        // create a new detached, client model
127        SiteDTO newSite = new SiteDTO();
128
129        newSite.setActivityId(1);
130        newSite.setPartner(new PartnerDTO(1, "Foobar"));
131        newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
132        newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
133        newSite.setLocationName("Virunga");
134        newSite.setAttributeValue(1, false);
135        newSite.setAttributeValue(2, false);
136
137        // create command
138
139        CreateEntity cmd = CreateEntity.Site(newSite);
140
141        // execute the command
142
143        setUser(1);
144
145        CreateResult result = (CreateResult) execute(cmd);
146
147
148        // let the client know the command has succeeded
149        newSite.setId(result.getNewId());
150        //cmd.onCompleted(result);
151
152
153        // try to retrieve what we've created
154
155        PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
156
157        Assert.assertEquals(1, loadResult.getData().size());
158
159        SiteDTO secondRead = loadResult.getData().get(0);
160
161
162        // confirm that the changes are there
163        Assert.assertEquals("site.attribute[2]", false, secondRead.getAttributeValue(1));
164        Assert.assertEquals("site.attribute[2]", false, secondRead.getAttributeValue(2));
165    }
166    
167    @Test
168    public void testAddByDatabase() throws CommandException {
169        // create a new detached, client model
170        SiteDTO newSite = new SiteDTO();
171
172        newSite.setDatabaseId(1);
173        newSite.setLocationName("Walungu Centre");
174        newSite.setPartner(new PartnerDTO(1, "Foobar"));
175        newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
176        newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
177        newSite.setAdminEntity(1, new AdminEntityDTO(1, 2, "Sud Kivu"));
178        newSite.setAdminEntity(2, new AdminEntityDTO(2, 11, "Walungu"));
179        newSite.setAdminEntity(3, null);
180        newSite.setX(27.432);
181        newSite.setY(1.23);
182        newSite.setComments("huba huba");
183
184        // create command
185
186        CreateEntity cmd = CreateEntity.Site(newSite);
187
188        // execute the command
189
190        setUser(1);
191        
192        execute(cmd);
193    }
194    
195    @Test
196    @OnDataSet("/dbunit/project-indicator.db.xml")
197    public void testAddByDatabaseWithoutAdminDetails() throws CommandException {
198        setUser(1);
199
200        SiteDTO newSite = new SiteDTO();
201
202        newSite.setDatabaseId(4);
203        newSite.setLocationName("Paris");
204        newSite.setPartner(new PartnerDTO(4, "Foobar"));
205
206        // create command
207
208        CreateEntity cmd = CreateEntity.Site(newSite);
209
210        // execute the command      
211        execute(cmd);
212        
213        // add another
214        SiteDTO newSite2 = new SiteDTO();
215
216        newSite.setDatabaseId(4);
217        newSite.setLocationName("Lyon");
218        newSite.setPartner(new PartnerDTO(4, "Foobar"));
219
220        // create command
221
222        CreateEntity cmd2 = CreateEntity.Site(newSite);
223    
224        execute(cmd);
225
226    }
227
228
229
230}