/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. package org.sigmah.server.endpoint.gwtrpc;
  6. import com.extjs.gxt.ui.client.data.PagingLoadResult;
  7. import org.junit.Assert;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.sigmah.server.dao.OnDataSet;
  11. import org.sigmah.shared.command.CreateEntity;
  12. import org.sigmah.shared.command.GetSites;
  13. import org.sigmah.shared.command.result.CreateResult;
  14. import org.sigmah.shared.dto.AdminEntityDTO;
  15. import org.sigmah.shared.dto.PartnerDTO;
  16. import org.sigmah.shared.dto.SiteDTO;
  17. import org.sigmah.shared.exception.CommandException;
  18. import org.sigmah.test.InjectionSupport;
  19. import java.util.GregorianCalendar;
  20. @RunWith(InjectionSupport.class)
  21. @OnDataSet("/dbunit/sites-simple1.db.xml")
  22. public class CreateSiteTest extends CommandTestCase {
  23. @Test
  24. public void test() throws CommandException {
  25. // create a new detached, client model
  26. SiteDTO newSite = new SiteDTO();
  27. newSite.setActivityId(1);
  28. newSite.setPartner(new PartnerDTO(1, "Foobar"));
  29. newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
  30. newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
  31. newSite.setLocationName("Virunga");
  32. newSite.setLocationAxe("Goma - Rutshuru");
  33. newSite.setX(27.432);
  34. newSite.setY(1.23);
  35. newSite.setIndicatorValue(1, 996.0);
  36. newSite.setIndicatorValue(2, null);
  37. newSite.setAttributeValue(1, true);
  38. newSite.setAttributeValue(2, false);
  39. newSite.setComments("huba huba");
  40. // create command
  41. CreateEntity cmd = CreateEntity.Site(newSite);
  42. // execute the command
  43. setUser(1);
  44. CreateResult result = (CreateResult) execute(cmd);
  45. // let the client know the command has succeeded
  46. newSite.setId(result.getNewId());
  47. //cmd.onCompleted(result);
  48. // try to retrieve what we've created
  49. PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
  50. Assert.assertEquals(1, loadResult.getData().size());
  51. SiteDTO secondRead = loadResult.getData().get(0);
  52. // confirm that the changes are there
  53. Assert.assertEquals("site.location.name", newSite.getLocationName(), secondRead.getLocationName());
  54. Assert.assertEquals("site.attribute[1]", true, secondRead.getAttributeValue(1));
  55. Assert.assertEquals("site.reportingPeriod[0].indicatorValue[0]", 996.0, secondRead.getIndicatorValue(1), 0.1);
  56. Assert.assertEquals("site.comments", newSite.getComments(), secondRead.getComments());
  57. Assert.assertEquals("site.partner", newSite.getPartner().getId(), secondRead.getPartner().getId());
  58. }
  59. @Test
  60. public void testAdminBoundCreate() throws CommandException {
  61. // create a new detached, client model
  62. SiteDTO newSite = new SiteDTO();
  63. newSite.setActivityId(4);
  64. newSite.setPartner(new PartnerDTO(1, "Foobar"));
  65. newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
  66. newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
  67. newSite.setAdminEntity(1, new AdminEntityDTO(1, 2, "Sud Kivu"));
  68. newSite.setAdminEntity(2, new AdminEntityDTO(2, 11, "Walungu"));
  69. newSite.setAdminEntity(3, null);
  70. newSite.setX(27.432);
  71. newSite.setY(1.23);
  72. newSite.setComments("huba huba");
  73. // create command
  74. CreateEntity cmd = CreateEntity.Site(newSite);
  75. // execute the command
  76. setUser(1);
  77. CreateResult result = (CreateResult) execute(cmd);
  78. newSite.setId(result.getNewId());
  79. // try to retrieve what we've created
  80. PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
  81. Assert.assertEquals(1, loadResult.getData().size());
  82. SiteDTO secondRead = loadResult.getData().get(0);
  83. // confirm that the changes are there
  84. Assert.assertEquals("site.location.name", "Walungu", secondRead.getLocationName());
  85. }
  86. @Test
  87. public void testAllAttribsFalse() throws CommandException {
  88. // create a new detached, client model
  89. SiteDTO newSite = new SiteDTO();
  90. newSite.setActivityId(1);
  91. newSite.setPartner(new PartnerDTO(1, "Foobar"));
  92. newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
  93. newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
  94. newSite.setLocationName("Virunga");
  95. newSite.setAttributeValue(1, false);
  96. newSite.setAttributeValue(2, false);
  97. // create command
  98. CreateEntity cmd = CreateEntity.Site(newSite);
  99. // execute the command
  100. setUser(1);
  101. CreateResult result = (CreateResult) execute(cmd);
  102. // let the client know the command has succeeded
  103. newSite.setId(result.getNewId());
  104. //cmd.onCompleted(result);
  105. // try to retrieve what we've created
  106. PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
  107. Assert.assertEquals(1, loadResult.getData().size());
  108. SiteDTO secondRead = loadResult.getData().get(0);
  109. // confirm that the changes are there
  110. Assert.assertEquals("site.attribute[2]", false, secondRead.getAttributeValue(1));
  111. Assert.assertEquals("site.attribute[2]", false, secondRead.getAttributeValue(2));
  112. }
  113. @Test
  114. public void testAddByDatabase() throws CommandException {
  115. // create a new detached, client model
  116. SiteDTO newSite = new SiteDTO();
  117. newSite.setDatabaseId(1);
  118. newSite.setLocationName("Walungu Centre");
  119. newSite.setPartner(new PartnerDTO(1, "Foobar"));
  120. newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
  121. newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
  122. newSite.setAdminEntity(1, new AdminEntityDTO(1, 2, "Sud Kivu"));
  123. newSite.setAdminEntity(2, new AdminEntityDTO(2, 11, "Walungu"));
  124. newSite.setAdminEntity(3, null);
  125. newSite.setX(27.432);
  126. newSite.setY(1.23);
  127. newSite.setComments("huba huba");
  128. // create command
  129. CreateEntity cmd = CreateEntity.Site(newSite);
  130. // execute the command
  131. setUser(1);
  132. execute(cmd);
  133. }
  134. @Test
  135. @OnDataSet("/dbunit/project-indicator.db.xml")
  136. public void testAddByDatabaseWithoutAdminDetails() throws CommandException {
  137. setUser(1);
  138. SiteDTO newSite = new SiteDTO();
  139. newSite.setDatabaseId(4);
  140. newSite.setLocationName("Paris");
  141. newSite.setPartner(new PartnerDTO(4, "Foobar"));
  142. // create command
  143. CreateEntity cmd = CreateEntity.Site(newSite);
  144. // execute the command
  145. execute(cmd);
  146. // add another
  147. SiteDTO newSite2 = new SiteDTO();
  148. newSite.setDatabaseId(4);
  149. newSite.setLocationName("Lyon");
  150. newSite.setPartner(new PartnerDTO(4, "Foobar"));
  151. // create command
  152. CreateEntity cmd2 = CreateEntity.Site(newSite);
  153. execute(cmd);
  154. }
  155. }