PageRenderTime 30ms CodeModel.GetById 10ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://sigma-h.googlecode.com/
Java | 327 lines | 208 code | 107 blank | 12 comment | 2 complexity | 02f1d8479279ca9ef4786dd1bb5c7d01 MD5 | raw file
  1package org.sigmah.server.endpoint.gwtrpc;
  2
  3import static org.hamcrest.CoreMatchers.equalTo;
  4import static org.hamcrest.CoreMatchers.not;
  5import static org.junit.Assert.assertThat;
  6
  7import java.util.List;
  8
  9import org.dozer.Mapper;
 10import org.junit.Test;
 11import org.junit.runner.RunWith;
 12import org.sigmah.server.dao.OnDataSet;
 13import org.sigmah.shared.command.CopyLogFrame;
 14import org.sigmah.shared.command.GetIndicators;
 15import org.sigmah.shared.command.GetProject;
 16import org.sigmah.shared.command.UpdateLogFrame;
 17import org.sigmah.shared.command.result.IndicatorListResult;
 18import org.sigmah.shared.command.result.LogFrameResult;
 19import org.sigmah.shared.domain.Indicator;
 20import org.sigmah.shared.domain.logframe.IndicatorCopyStrategy;
 21import org.sigmah.shared.domain.logframe.LogFrame;
 22import org.sigmah.shared.domain.logframe.LogFrameGroupType;
 23import org.sigmah.shared.dto.IndicatorDTO;
 24import org.sigmah.shared.dto.ProjectDTO;
 25import org.sigmah.shared.dto.logframe.ExpectedResultDTO;
 26import org.sigmah.shared.dto.logframe.LogFrameActivityDTO;
 27import org.sigmah.shared.dto.logframe.LogFrameDTO;
 28import org.sigmah.shared.dto.logframe.LogFrameGroupDTO;
 29import org.sigmah.shared.dto.logframe.LogFrameModelDTO;
 30import org.sigmah.shared.dto.logframe.SpecificObjectiveDTO;
 31import org.sigmah.shared.exception.CommandException;
 32import org.sigmah.test.InjectionSupport;
 33
 34import com.google.inject.Inject;
 35
 36
 37@RunWith(InjectionSupport.class)
 38@OnDataSet("/dbunit/project-indicator.db.xml")
 39public class LogFrameIntegrationTest extends CommandTestCase {
 40
 41    @Test
 42    public void logFrame() throws CommandException {
 43    	
 44    	createNewLogFrameForProject(1);
 45    	
 46    	// now verify that we can reload
 47    	ProjectDTO project = execute(new GetProject(1));
 48    	
 49    	
 50    }
 51    
 52    @Test
 53    public void deleteActivity() throws CommandException {
 54    	
 55    	createNewLogFrameForProject(1);
 56    	
 57    	// now verify that we can reload
 58    	ProjectDTO project = execute(new GetProject(1));
 59    	
 60    	project.getLogFrameDTO()
 61    		.getSpecificObjectives().get(0)
 62    		.getExpectedResults().get(0)
 63    		.getActivities().remove(1);
 64    		 
 65    	
 66    	execute(new UpdateLogFrame(project.getLogFrameDTO(), 1));
 67
 68    	// reload project
 69    	project = execute(new GetProject(1));
 70    	
 71    	assertThat("number of activies", project.getLogFrameDTO()
 72    						.getSpecificObjectives().get(0)
 73    						.getExpectedResults().get(0)
 74    						.getActivities().size(), equalTo(1));
 75    	    	
 76    }
 77    
 78    @Test
 79    public void saveIndicators() throws CommandException {
 80    	
 81    	createNewLogFrameForProject(1);
 82    	
 83    	// now retrieve the new log frame and the list of indicators
 84    	ProjectDTO project = execute(new GetProject(1));
 85    	IndicatorListResult indicators = execute(GetIndicators.forDatabase(1));
 86    	
 87    	project.getLogFrameDTO()
 88    		.getSpecificObjectives().get(0)
 89    		.getIndicators().add( indicators.getData().get(0));
 90    	
 91    	
 92    	LogFrameResult saved = execute(new UpdateLogFrame(project.getLogFrameDTO(), project.getId()));
 93    	
 94    	// validate the indicator was properly added
 95    	
 96    	project = execute(new GetProject(1));
 97
 98    	assertThat( project.getLogFrameDTO()
 99    					.getSpecificObjectives().get(0)
100    					.getIndicators().get(0)
101    						.getName(), equalTo( indicators.getData().get(0).getName()) );
102    
103    	
104   
105    	
106    }
107    
108    @Test
109    public void newIndicator() throws CommandException {
110    	createNewLogFrameForProject(1);
111    	
112    	// retrieve the new logframe
113    	ProjectDTO project = execute(new GetProject(1));
114    	
115    	IndicatorDTO newInd = addNewIndicatorToLogFrame(project);
116    	
117    	
118    	// validate the indicator was properly added
119    	
120    	project = execute(new GetProject(1));
121
122    	assertThat( project.getLogFrameDTO()
123    					.getSpecificObjectives().get(0)
124    					.getIndicators().get(0)
125    						.getName(), equalTo( newInd.getName()) );
126    	
127    	// .. and validate that the indicator is linked to the database
128    	IndicatorListResult indicators = execute(GetIndicators.forDatabase(1));
129    	IndicatorDTO theNewIndicator = findByName(indicators, newInd.getName());
130        	
131    	// now try resaving to make sure that the indicator preserves its relationship with UserDatabase
132    	LogFrameResult afterReSave = execute(new UpdateLogFrame(project.getLogFrameDTO(), project.getId()));
133    	
134    	IndicatorDTO theNewIndicatorAfterReSave = afterReSave.getLogFrame()
135    		.getSpecificObjectives().get(0).getIndicators().get(0);
136    	
137    	assertThat(theNewIndicatorAfterReSave.getId(), equalTo(theNewIndicator.getId()));
138    	
139    	
140    }
141
142
143    @Test
144    public void copyLogFrame() throws CommandException {
145    	
146    	createNewLogFrameForProject(1);    	
147    	
148    	// retrieve the new logframe
149    	ProjectDTO project = execute(new GetProject(1));
150
151    	IndicatorDTO newInd = addNewIndicatorToLogFrame(project);
152
153    	LogFrameDTO copiedLogFrame = execute(new CopyLogFrame(project.getLogFrameDTO().getId(), 2));
154
155    	SpecificObjectiveDTO soCopy = copiedLogFrame.getSpecificObjectives().get(0);
156    	SpecificObjectiveDTO soOriginal = project.getLogFrameDTO().getSpecificObjectives().get(0);
157    	
158		assertThat(soCopy.getCode(), equalTo(soOriginal.getCode()));
159		assertThat(soCopy.getId(), not(equalTo(soOriginal.getId())));
160		assertThat(soCopy.getRisksAndAssumptions(), equalTo(soOriginal.getRisksAndAssumptions()));
161		assertThat(soCopy.getExpectedResults().size(), equalTo(soOriginal.getExpectedResults().size()));
162		
163		ExpectedResultDTO erCopy = soCopy.getExpectedResults().get(0);
164		ExpectedResultDTO erOriginal = soOriginal.getExpectedResults().get(0);
165    	
166		assertThat(erCopy.getCode(), equalTo(erOriginal.getCode()));
167		assertThat(erCopy.getId(), not(equalTo(erOriginal.getId())));
168		assertThat(erCopy.getActivities().size(), equalTo(erOriginal.getActivities().size()));
169		
170		LogFrameActivityDTO aCopy = erCopy.getActivities().get(0);
171		LogFrameActivityDTO aOriginal = erOriginal.getActivities().get(0);
172		
173		assertThat(aCopy.getId(), not(equalTo(aOriginal.getId())));
174		assertThat(aCopy.getCode(), equalTo(aOriginal.getCode()));
175		assertThat(aCopy.getTitle(), equalTo(aOriginal.getTitle()));
176		    	
177    	List<IndicatorDTO> indicators = copiedLogFrame
178    		.getSpecificObjectives().get(0)
179    		.getIndicators();
180    	
181		assertThat( indicators.size(), equalTo(1));
182    	assertThat( indicators.get(0).getDatabaseId(), equalTo(2));    	
183    	
184    	// assure that the original is unmolested!
185    	IndicatorListResult reloadedIndicators = execute(GetIndicators.forDatabase(1));
186    	
187    	assertThat( findByName(reloadedIndicators, newInd.getName()).getDatabaseId(), equalTo(1));
188    }
189    
190    @Test
191    public void replaceLogFrame() throws CommandException {
192    	
193    	LogFrameResult result1 = createNewLogFrameForProject(1);
194    	LogFrameResult result2 = createNewLogFrameForProject(2);
195    	
196    	execute(new CopyLogFrame(result1.getLogFrame().getId(), 2));
197    	    	
198    }
199    
200    @Test
201    @OnDataSet("/dbunit/project-indicator.db.xml")
202    public void replaceLogFrameWithIndicatorLinking() throws CommandException {
203    	
204    	LogFrameResult result1 = createNewLogFrameForProject(1);
205    	addNewIndicatorToLogFrame(execute(new GetProject(1)));
206    	
207    	
208    	LogFrameResult result2 = createNewLogFrameForProject(2);
209    	
210    	execute(CopyLogFrame.from(result1.getLogFrame().getId())
211    			.to(2)
212    			.with(IndicatorCopyStrategy.DUPLICATE));
213    	
214    	
215    	   	    	    	
216    }
217    
218    
219
220	private IndicatorDTO findByName(IndicatorListResult indicators, String name) {
221		for(IndicatorDTO indicator : indicators.getData()) {
222			if(indicator.getName().equals(name)) {
223				return indicator;
224			}
225		}
226		throw new AssertionError("indicator with name '" + name + "' not found");
227	}
228
229	private LogFrameResult createNewLogFrameForProject(int projectId) throws CommandException {
230		
231    	LogFrameModelDTO model = new LogFrameModelDTO();
232    	model.setName("Generic Model");
233		
234    	LogFrameDTO logFrame = new LogFrameDTO();
235    	logFrame.setMainObjective("Reduce child mortalite");
236    	logFrame.setLogFrameModel(model);
237		
238    	LogFrameGroupDTO soGroup = logFrame.addGroup("S.O. 1", LogFrameGroupType.SPECIFIC_OBJECTIVE);
239    	
240    	SpecificObjectiveDTO so1 = logFrame.addSpecificObjective();
241    	so1.setCode(1);
242    	so1.setRisksAndAssumptions("The community is open to vaccinating their children");
243    	so1.setInterventionLogic("Assure that all children are vaccinated");
244    	so1.setParentLogFrame(logFrame);
245    	so1.setGroup(soGroup);
246    	
247    	LogFrameGroupDTO resultGroup = logFrame.addGroup("R. 1", LogFrameGroupType.EXPECTED_RESULT);
248    		
249    	ExpectedResultDTO result1 = so1.addExpectedResult();
250    	result1.setCode(1);
251    	result1.setLabel("R1");
252    	result1.setInterventionLogic("95% of children are vaccinated");
253    	result1.setGroup(resultGroup);
254    	
255    	
256		LogFrameGroupDTO activityGroup = logFrame.addGroup("Community-based", LogFrameGroupType.ACTIVITY);
257		
258		LogFrameActivityDTO activity = result1.addActivity();
259    	activity.setCode(1);
260    	activity.setLabel("Vaccination");
261    	activity.setAdvancement(0);
262    	activity.setGroup(activityGroup);
263
264    	LogFrameActivityDTO activity2 = result1.addActivity();
265    	activity2.setCode(2);
266    	activity2.setLabel("Awareness raising");
267    	activity2.setAdvancement(0);
268    	activity2.setGroup(activityGroup);
269    	    	
270    	
271    	// verify that is saved without error
272    	return execute(new UpdateLogFrame(logFrame, projectId));
273	}
274
275	private IndicatorDTO addNewIndicatorToLogFrame(ProjectDTO project) throws CommandException {
276		IndicatorDTO newInd = new IndicatorDTO();
277    	newInd.setDatabaseId(1);
278    	newInd.setName("Number of children vaccinated");
279    	newInd.setAggregation(IndicatorDTO.AGGREGATE_SUM);
280    	newInd.setUnits("children");
281    	newInd.setObjective(1000.0d);
282    	
283    	project.getLogFrameDTO()
284    		.getSpecificObjectives().get(0)
285    		.getIndicators().add( newInd );
286    	
287    	execute(new UpdateLogFrame(project.getLogFrameDTO(), project.getId()));
288    	
289		return newInd;
290	}
291	
292	@Inject
293	private Mapper mapper;
294	
295	@Test
296	public void indicatorIdIsMapped() throws CommandException {
297		
298		LogFrameModelDTO model = new LogFrameModelDTO();
299    	model.setName("Generic Model");
300		
301    	LogFrameDTO logFrame = new LogFrameDTO();
302    	logFrame.setMainObjective("Reduce child mortalite");
303    	logFrame.setLogFrameModel(model);
304		
305    	LogFrameGroupDTO soGroup = logFrame.addGroup("S.O. 1", LogFrameGroupType.SPECIFIC_OBJECTIVE);
306    	
307    	SpecificObjectiveDTO so1 = logFrame.addSpecificObjective();
308    	so1.setCode(1);
309    	so1.setRisksAndAssumptions("The community is open to vaccinating their children");
310    	so1.setInterventionLogic("Assure that all children are vaccinated");
311    	so1.setParentLogFrame(logFrame);
312    	so1.setGroup(soGroup);
313    	
314		IndicatorListResult indicators = execute(GetIndicators.forDatabase(1));
315		IndicatorDTO anIndicator = indicators.getData().get(0);
316		
317		so1.getIndicators().add(anIndicator);
318		
319		LogFrame entity = mapper.map(logFrame, LogFrame.class);
320		Indicator anIndicatorMapped = entity.getSpecificObjectives().get(0).getIndicators().iterator().next();
321		
322		assertThat( anIndicatorMapped.getId(), equalTo( anIndicator.getId() ));
323		
324	}
325    
326	
327}