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

http://sigma-h.googlecode.com/ · Java · 248 lines · 182 code · 56 blank · 10 comment · 4 complexity · 87817574a6265a3389ed0e8a6af3c043 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 java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import org.hamcrest.CoreMatchers;
  10. import org.junit.Assert;
  11. import org.junit.Test;
  12. import org.junit.runner.RunWith;
  13. import org.sigmah.server.dao.OnDataSet;
  14. import org.sigmah.shared.command.CreateEntity;
  15. import org.sigmah.shared.command.GetProject;
  16. import org.sigmah.shared.command.GetProjects;
  17. import org.sigmah.shared.command.GetValue;
  18. import org.sigmah.shared.command.result.CreateResult;
  19. import org.sigmah.shared.command.result.ProjectListResult;
  20. import org.sigmah.shared.command.result.ValueResult;
  21. import org.sigmah.shared.dto.PhaseDTO;
  22. import org.sigmah.shared.dto.PhaseModelDTO;
  23. import org.sigmah.shared.dto.ProjectDTO;
  24. import org.sigmah.shared.dto.ProjectModelDTO;
  25. import org.sigmah.shared.dto.element.FlexibleElementDTO;
  26. import org.sigmah.shared.dto.element.QuestionChoiceElementDTO;
  27. import org.sigmah.shared.dto.element.QuestionElementDTO;
  28. import org.sigmah.shared.dto.layout.LayoutConstraintDTO;
  29. import org.sigmah.shared.dto.layout.LayoutDTO;
  30. import org.sigmah.shared.dto.layout.LayoutGroupDTO;
  31. import org.sigmah.shared.exception.CommandException;
  32. import org.sigmah.test.InjectionSupport;
  33. @RunWith(InjectionSupport.class)
  34. @OnDataSet("/dbunit/projects.db.xml")
  35. public class ProjectTest extends CommandTestCase {
  36. @Test
  37. public void createProject() throws CommandException {
  38. setUser(1);
  39. Map<String, Object> properties = new HashMap<String, Object>();
  40. properties.put("name", "1234567890123456");
  41. properties.put("fullName", "First p full");
  42. properties.put("budget", 150000.0);
  43. properties.put("modelId", 1L);
  44. properties.put("orgUnitId", 1);
  45. properties.put("calendarName", "events");
  46. CreateEntity cmd = new CreateEntity("Project", properties);
  47. CreateResult createResult = execute(cmd);
  48. Assert.assertThat(createResult.getEntity(), CoreMatchers.not(CoreMatchers.nullValue()));
  49. }
  50. @Test
  51. public void getProjects() throws CommandException {
  52. setUser(1);
  53. GetProjects cmd = new GetProjects();
  54. ProjectListResult result = execute(cmd);
  55. Assert.assertThat(result.getListProjectsLightDTO().size(), CoreMatchers.equalTo(3));
  56. }
  57. @Test
  58. public void projectsWithOrgRightsShouldBeVisible() throws CommandException {
  59. setUser(3);
  60. GetProjects cmd = new GetProjects();
  61. ProjectListResult result = execute(cmd);
  62. Assert.assertThat(result.getListProjectsLightDTO().size(), CoreMatchers.equalTo(2));
  63. }
  64. @Test
  65. public void phaseModelDisplayOrderTest() throws CommandException {
  66. setUser(1);
  67. final GetProject cmd = new GetProject(1);
  68. final ProjectDTO project = execute(cmd);
  69. final ProjectModelDTO projectModel = project.getProjectModelDTO();
  70. Assert.assertThat(projectModel, CoreMatchers.notNullValue());
  71. final List<PhaseModelDTO> phaseModels = projectModel.getPhaseModelsDTO();
  72. Assert.assertThat(phaseModels, CoreMatchers.notNullValue());
  73. // Verifying the phases display order
  74. Assert.assertThat(phaseModels.get(2).getName(), CoreMatchers.equalTo("Step 2"));
  75. }
  76. @Test
  77. public void flexibleElementsTest() throws CommandException {
  78. final int projectId = 1;
  79. setUser(1);
  80. // Flexible elements defined in the test database
  81. final ElementDefinition[] definitions = new ElementDefinition[] {
  82. new ElementDefinition("Success", false, "element.MessageElement"),
  83. new ElementDefinition("Check this", true, "element.CheckboxElement", new CustomVerificator() {
  84. @Override
  85. public void verify(FlexibleElementDTO element, ValueResult valueResult) {
  86. Assert.assertThat(element.isCorrectRequiredValue(valueResult), CoreMatchers.equalTo(true));
  87. }
  88. }), new ElementDefinition("Question 1", false, "element.QuestionElement", new CustomVerificator() {
  89. @Override
  90. public void verify(FlexibleElementDTO element, ValueResult valueResult) {
  91. final QuestionElementDTO questionElementDTO = (QuestionElementDTO) element;
  92. final List<QuestionChoiceElementDTO> choices = questionElementDTO.getChoicesDTO();
  93. Assert.assertThat(choices, CoreMatchers.notNullValue());
  94. Assert.assertThat(choices.size(), CoreMatchers.equalTo(2));
  95. }
  96. }), new ElementDefinition("Question 2", true, "element.QuestionElement", new CustomVerificator() {
  97. @Override
  98. public void verify(FlexibleElementDTO element, ValueResult valueResult) {
  99. final QuestionElementDTO questionElementDTO = (QuestionElementDTO) element;
  100. final List<QuestionChoiceElementDTO> choices = questionElementDTO.getChoicesDTO();
  101. Assert.assertThat(choices, CoreMatchers.notNullValue());
  102. Assert.assertThat(choices.size(), CoreMatchers.equalTo(3));
  103. // Sort order
  104. final QuestionChoiceElementDTO third = choices.get(2);
  105. Assert.assertThat(third, CoreMatchers.notNullValue());
  106. Assert.assertThat(third.getLabel(), CoreMatchers.equalTo("Answer 3"));
  107. }
  108. }), new ElementDefinition("Comments", false, "element.TextAreaElement", new CustomVerificator() {
  109. @Override
  110. public void verify(FlexibleElementDTO element, ValueResult valueResult) {
  111. Assert.assertThat((String) valueResult.getValueObject(), CoreMatchers.equalTo("Something"));
  112. }
  113. }) };
  114. // Retrieving the project
  115. final GetProject cmd = new GetProject(projectId);
  116. final ProjectDTO project = execute(cmd);
  117. Assert.assertThat(project.getId(), CoreMatchers.equalTo(projectId));
  118. // Retrieving the layout groups
  119. final ProjectModelDTO projectModel = project.getProjectModelDTO();
  120. Assert.assertThat(projectModel, CoreMatchers.notNullValue());
  121. final List<PhaseModelDTO> phaseModels = projectModel.getPhaseModelsDTO();
  122. Assert.assertThat(phaseModels, CoreMatchers.notNullValue());
  123. Assert.assertThat(phaseModels.size(), CoreMatchers.equalTo(4));
  124. final List<PhaseDTO> phases = project.getPhasesDTO();
  125. Assert.assertThat(phases, CoreMatchers.notNullValue());
  126. Assert.assertThat(phases.size(), CoreMatchers.equalTo(4));
  127. final PhaseDTO phase = phases.get(0);
  128. Assert.assertThat(phase, CoreMatchers.notNullValue());
  129. final PhaseModelDTO phaseModel = phase.getPhaseModelDTO();
  130. final LayoutDTO layout = phaseModel.getLayoutDTO();
  131. Assert.assertThat(layout, CoreMatchers.notNullValue());
  132. final List<LayoutGroupDTO> groups = layout.getLayoutGroupsDTO();
  133. Assert.assertThat(groups, CoreMatchers.notNullValue());
  134. // Testing the flexible elements
  135. int index = 0;
  136. for (final LayoutGroupDTO group : groups) {
  137. Assert.assertThat(group, CoreMatchers.notNullValue());
  138. final List<LayoutConstraintDTO> constraints = group.getLayoutConstraintsDTO();
  139. Assert.assertThat(constraints, CoreMatchers.notNullValue());
  140. for (final LayoutConstraintDTO constraint : constraints) {
  141. Assert.assertThat(constraint, CoreMatchers.notNullValue());
  142. final FlexibleElementDTO element = constraint.getFlexibleElementDTO();
  143. Assert.assertThat(element, CoreMatchers.notNullValue());
  144. final GetValue command = new GetValue(projectId, element.getId(), element.getEntityName());
  145. final ValueResult result = execute(command);
  146. element.setCurrentContainerDTO(project);
  147. element.assignValue(result);
  148. final ElementDefinition definition = definitions[index];
  149. Assert.assertThat(element.getLabel(), CoreMatchers.equalTo(definition.getLabel()));
  150. Assert.assertThat(element.getValidates(), CoreMatchers.equalTo(definition.isValidates()));
  151. Assert.assertThat(element.getEntityName(), CoreMatchers.equalTo(definition.getEntityName()));
  152. if (definition.getCustomVerificator() != null)
  153. definition.getCustomVerificator().verify(element, result);
  154. index++;
  155. }
  156. }
  157. Assert.assertThat(index, CoreMatchers.equalTo(definitions.length));
  158. }
  159. private static interface CustomVerificator {
  160. void verify(FlexibleElementDTO element, ValueResult valueResult);
  161. }
  162. private static class ElementDefinition {
  163. private final String label;
  164. private final boolean validates;
  165. private final String entityName;
  166. private final CustomVerificator customVerificator;
  167. public ElementDefinition(String label, boolean validates, String entityName) {
  168. this.label = label;
  169. this.validates = validates;
  170. this.entityName = entityName;
  171. this.customVerificator = null;
  172. }
  173. public ElementDefinition(String label, boolean validates, String entityName, CustomVerificator customVerificator) {
  174. this.label = label;
  175. this.validates = validates;
  176. this.entityName = entityName;
  177. this.customVerificator = customVerificator;
  178. }
  179. public String getEntityName() {
  180. return entityName;
  181. }
  182. public String getLabel() {
  183. return label;
  184. }
  185. public boolean isValidates() {
  186. return validates;
  187. }
  188. public CustomVerificator getCustomVerificator() {
  189. return customVerificator;
  190. }
  191. }
  192. }