PageRenderTime 38ms CodeModel.GetById 2ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

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