View Javadoc
1   package org.itracker.services;
2   
3   import junit.framework.Assert;
4   import org.itracker.model.*;
5   import org.itracker.persistence.dao.ProjectDAO;
6   import org.itracker.persistence.dao.ProjectScriptDAO;
7   import org.itracker.persistence.dao.VersionDAO;
8   import org.itracker.persistence.dao.WorkflowScriptDAO;
9   import org.junit.Ignore;
10  import org.junit.Test;
11  import org.springframework.dao.DataAccessException;
12  
13  import java.text.ParseException;
14  import java.util.*;
15  
16  import static org.itracker.Assert.*;
17  public class ProjectServiceImplIT extends AbstractServicesIntegrationTest {
18  
19      private ProjectDAO projectDAO;
20      private ProjectService projectService;
21      private VersionDAO versionDAO;
22      private ProjectScriptDAO projectScriptDAO;
23      private WorkflowScriptDAO workflowScriptDAO;
24  
25  
26      @Override
27      public void onSetUp() throws Exception {
28  
29          super.onSetUp();
30          projectService = (ProjectService) applicationContext
31                  .getBean("projectService");
32          projectDAO = (ProjectDAO) applicationContext.getBean("projectDAO");
33  
34          versionDAO = (VersionDAO) applicationContext.getBean("versionDAO");
35          projectScriptDAO = (ProjectScriptDAO) applicationContext
36                  .getBean("projectScriptDAO");
37          workflowScriptDAO = (WorkflowScriptDAO) applicationContext
38                  .getBean("workflowScriptDAO");
39  
40      }
41  
42      @Test
43      public void testGetProject() {
44          Project project = projectService.getProject(2);
45          assertNotNull("project #2", project);
46          Assert.assertEquals("name", "test_name", project.getName());
47          Assert.assertEquals("description", "test_description", project
48                  .getDescription());
49  
50          project = projectService.getProject(2);
51          assertNotNull("project #3", project);
52          Assert.assertEquals("name", "test_name", project.getName());
53          Assert.assertEquals("description", "test_description", project
54                  .getDescription());
55  
56      }
57  
58      @Test
59      public void testGetAllProjects() {
60          List<Project> projects = projectService.getAllProjects();
61          assertNotNull("allProjects", projects);
62          assertEquals("allProjects", 2, projects.size());
63      }
64  
65      @Test
66      public void testGetAllAvailableProjects() {
67          List<Project> projects = projectService.getAllAvailableProjects();
68          assertNotNull("allAvailableProjects", projects);
69          assertEquals("allAvailableProjects", 2, projects.size());
70      }
71  
72  
73      @Test
74      public void testProjectComponents() {
75  
76          Project project = projectService.getProject(2);
77          Component component = new Component(project, "new_component");
78          component.setDescription("new_decription");
79          assertNull("addProjectComponent", component.getId());
80          Date then = new Date();
81  
82          int numberOfComponents = project.getComponents().size();
83  
84          projectService.addProjectComponent(project.getId(), component);
85          assertNotNull("addProjectComponent", component.getId());
86          assertEquals("component size", numberOfComponents + 1, project
87                  .getComponents().size());
88  
89          // refresh project bean
90          project = projectService.getProject(project.getId());
91          assertEquals("component size", numberOfComponents + 1, project
92                  .getComponents().size());
93  
94  
95          assertTrue("date created", project.getCreateDate().compareTo(then) >= 0);
96  
97          // refresh saved component
98          Component savedComponent = projectService.getProjectComponent(component
99                  .getId());
100         assertNotNull("addProjectComponent", savedComponent);
101         Assert.assertEquals("addProjectComponent", savedComponent.getName(),
102                 component.getName());
103         Assert.assertEquals("addProjectComponent", savedComponent
104                 .getDescription(), component.getDescription());
105 
106 
107         assertTrue("date modified", savedComponent.getLastModifiedDate().compareTo(then) >= 0);
108         assertTrue("date created", savedComponent.getCreateDate().compareTo(then) >= 0);
109         assertEquals("parent project", project, savedComponent.getProject());
110 
111         then = new Date();
112         savedComponent.setName("new_name");
113         projectService.updateProjectComponent(savedComponent);
114 
115         Component updatedComponent = projectService
116                 .getProjectComponent(savedComponent.getId());
117         assertEquals(savedComponent.getName(), updatedComponent.getName());
118         assertEquals(savedComponent.getDescription(), updatedComponent
119                 .getDescription());
120 
121         assertTrue("date modified", updatedComponent.getLastModifiedDate()
122                 .compareTo(then) >= 0);
123 
124         assertFalse("date created", updatedComponent.getCreateDate()
125                 .compareTo(then) >= 0);
126 
127 
128     }
129 
130     @Test
131     public void testRemoveProjectComponent() {
132 
133         Project project = projectService.getProject(2);
134         assertNotNull("project not found", project.getId());
135 
136         assertTrue(projectService.removeProjectComponent(project.getId(), 2));
137     }
138 
139     @Test
140     public void testTryRemoveInvalidComponent() {
141 
142         Project project = projectService.getProject(2);
143         assertNotNull("project not found", project.getId());
144 
145         assertFalse(projectService.removeProjectComponent(project.getId(), 89));
146     }
147 
148     @Test
149     public void testProjectVersions() {
150 
151         Version version = projectService.getProjectVersion(1);
152         assertNotNull("version not found", version);
153 
154         Project project = projectService.getProject(2);
155         assertNotNull("project not found", project.getId());
156 
157         assertTrue("versions don't match", project.getVersions().size() == 1);
158 
159         version = new Version(project, "2.0");
160 
161 
162         version.setDescription("");
163         projectService.addProjectVersion(project.getId(), version);
164 
165         project = projectService.getProject(2);
166         assertTrue("versions dont' match", project.getVersions().size() == 2);
167 
168         versionDAO.detach(version);
169         version.setMinor(2);
170         projectService.updateProjectVersion(version);
171 
172         version = projectService.getProjectVersion(version.getId());
173         assertEquals("version not updated", version.getMinor(), 2);
174 
175         projectDAO.detach(project);
176         projectService.removeProjectVersion(project.getId(), version.getId());
177         assertNull("version not removed", projectService
178                 .getProjectVersion(version.getId()));
179 
180     }
181 
182     @Test
183     public void removeProjectVersion() {
184 
185         Version version = projectService.getProjectVersion(1);
186         assertNotNull("version not found", version);
187 
188         Project project = projectService.getProject(2);
189         assertNotNull("project not found", project.getId());
190 
191         projectDAO.detach(project);
192         assertTrue(projectService.removeProjectVersion(project.getId(), 1));
193         assertNull("version not removed", projectService
194                 .getProjectVersion(1));
195 
196 
197     }
198 
199     @Test
200     public void tryInsertDuplicateVersion() {
201 
202         Project project = projectService.getProject(2);
203         assertNotNull("project not found", project.getId());
204 
205         assertTrue("versions don't match", project.getVersions().size() == 1);
206         Version version = project.getVersions().get(0);
207         versionDAO.detach(version);
208         version.setId(null);
209         try {
210             projectService.addProjectVersion(project.getId(), version);
211             fail();
212         } catch (DataAccessException e) {
213             // ok
214 
215         }
216     }
217 
218     @Test
219     public void testProjectOwner() {
220         Project project = projectService.getProject(2);
221         assertNotNull("project not found", project.getId());
222 
223         assertEquals(0, project.getOwners().size());
224         assertEquals(projectService.getProjectOwners(project.getId()).size(),
225                 project.getOwners().size());
226 
227         Set<Integer> newOwners = new HashSet<Integer>();
228         newOwners.add(1);
229         projectService.setProjectOwners(project, newOwners);
230         assertEquals(1, projectService.getProjectOwners(project.getId()).size());
231 
232         projectService.setProjectOwners(project, new HashSet<Integer>());
233         assertEquals(0, projectService.getProjectOwners(project.getId()).size());
234 
235     }
236 
237     @Test
238     public void testGetTotalNumberIssuesByProject() {
239         Long issuesByProject = projectService.getTotalNumberIssuesByProject(2);
240         assertEquals(Long.valueOf(4), issuesByProject);
241     }
242 
243     @Test
244     public void testCountIssuesByVersion() {
245         Long issuesByVersion = projectService.countIssuesByVersion(2);
246         assertEquals(Long.valueOf(0), issuesByVersion);
247 
248     }
249 
250     @Test
251     public void testCountIssuesByComponent() {
252         Long issuesByComponent = projectService.countIssuesByComponent(2);
253         assertEquals(Long.valueOf(0), issuesByComponent);
254     }
255 
256     @Test
257     public void testGetTotalNumberOpenIssuesByProject() {
258         Long issues = projectService.countIssuesByComponent(2);
259         assertEquals(Long.valueOf(0), issues);
260     }
261 
262     @Test
263     public void testGetTotalNumberResolvedIssuesByProject() {
264         Long number = projectService.getTotalNumberResolvedIssuesByProject(2);
265         assertEquals(Long.valueOf(0), number);
266     }
267 
268     @Test
269     public void getgetTotalNumberOpenIssuesByProject() {
270         Long number = projectService.getTotalNumberOpenIssuesByProject(2);
271         assertEquals(Long.valueOf(4), number);
272     }
273 
274     @Test
275     public void getLatestIssueUpdatedDateByProjectId() throws ParseException {
276         Date date = projectService.getLatestIssueUpdatedDateByProjectId(2);
277         assertNotNull(date);
278         Calendar cal = Calendar.getInstance();
279         cal.setTime(date);
280         assertEquals(2008, cal.get(Calendar.YEAR));
281         assertEquals(0, cal.get(Calendar.MONTH));
282         assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
283     }
284 
285     @Test
286     public void testProjectScripts() {
287         ProjectScript projectScript = projectService.getProjectScript(1);
288         assertNotNull("project script not found", projectScript);
289 
290         Project project = projectService.getProject(2);
291         assertNotNull("project not found", project);
292 
293         assertEquals("versions don't match", 1, project.getScripts().size());
294         assertEquals("versions don't match", 1, projectService
295                 .getProjectScripts().size());
296 
297         projectScript = new ProjectScript();
298         projectScript.setProject(project);
299         projectScript.setPriority(1);
300         projectScript.setFieldId(2);
301         projectScript.setFieldType(Configuration.Type.customfield);
302 
303         WorkflowScript script = workflowScriptDAO.findByPrimaryKey(2);
304         assertNotNull("workflow script not found", script);
305         projectScript.setScript(script);
306 
307         projectScript = projectService.addProjectScript(project.getId(),
308                 projectScript);
309 
310         projectDAO.refresh(project);
311         assertEquals("project scripts don't match", 2, project.getScripts()
312                 .size());
313 
314         projectDAO.detach(project);
315         projectScriptDAO.detach(projectScript);
316         projectScript.setPriority(2);
317         projectService.updateProjectScript(projectScript);
318 
319         projectScript = projectService.getProjectScript(projectScript.getId());
320         assertEquals("version not updated", projectScript.getPriority(), 2);
321 
322         projectService.removeProjectScript(project.getId(), projectScript
323                 .getId());
324 
325         project = projectService.getProject(2);
326         assertEquals("project scripts don't match", 1, project.getScripts()
327                 .size());
328         assertNull("project script not removed", projectService
329                 .getProjectScript(projectScript.getId()));
330 
331     }
332 
333     @Test
334     public void testProjectCustomFields() {
335 
336         Project project = projectService.getProject(2);
337         assertNotNull("project not found", project);
338 
339         List<CustomField> fields = projectService.getProjectFields(project
340                 .getId());
341         assertEquals("custom field count", 2, fields.size());
342 
343         Set<Integer> Ids = new HashSet<Integer>(2);
344         projectService.setProjectFields(project, Ids);
345 
346         assertEquals("custom field count", 0, projectService.getProjectFields(
347                 project.getId()).size());
348         Ids.add(1);
349         Ids.add(2);
350         projectService.setProjectFields(project, Ids);
351 
352         assertEquals("custom field count", 2, projectService.getProjectFields(
353                 project.getId()).size());
354 
355     }
356 
357     //TODO
358     @Test
359     @Ignore
360     public void testGetListOfProjectFields() {
361         fail("not implemented");
362     }
363 
364     //TODO
365     @Test
366     @Ignore
367     public void getListOfProjectOwners() {
368         fail("not implemented");
369     }
370 
371     @Test
372     public void testProjectCreateUpdate() {
373         Project project = new Project("New Project");
374         project.setDescription("desc");
375 
376         Project createProject = projectService.createProject(project, 1);
377         assertNotNull(createProject);
378         project = projectService.getProject(createProject.getId());
379         assertNotNull(project);
380 
381         project = new Project("New Project2");
382         project.setDescription("desc");
383         List<Component> components = new ArrayList<Component>();
384         Component component = new Component(project, "comp");
385         component.setDescription("comp desc");
386         components.add(component);
387         project.setComponents(components);
388         createProject = projectService.createProject(project, 1);
389         assertNotNull(createProject);
390         project = projectService.getProject(createProject.getId());
391         assertNotNull(project);
392 
393         Version version = new Version();
394         version.setProject(project);
395         version.setVersionInfo("2.0");
396         version.setDescription("version one");
397         version.setStatus(Status.ACTIVE);
398         project.setVersions(Arrays.asList(version));
399         projectService.updateProject(project, 1);
400 
401         project = projectService.getProject(project.getId());
402         assertNotNull(project);
403         assertEquals("version not updated", 1, project.getVersions().size());
404 
405     }
406 
407     @Test
408     public void testIsNameUniqueProjectName() throws Exception {
409 
410         List<Project> projects = projectDAO.findAll();
411         String existingName = projects.get(0).getName();
412 
413         assertTrue("existing name", projectService.isUniqueProjectName(existingName, projects.get(0).getId()));
414         assertFalse("existing name", projectService.isUniqueProjectName(existingName, projects.get(1).getId()));
415         assertFalse("existing name", projectService.isUniqueProjectName(existingName, null));
416 
417     }
418 
419     @Override
420     protected String[] getDataSetFiles() {
421         return new String[]{
422 
423                 "dataset/userpreferencesbean_dataset.xml",
424                 "dataset/userbean_dataset.xml",
425                 "dataset/customfieldbean_dataset.xml",
426                 "dataset/customfieldvaluebean_dataset.xml",
427                 "dataset/projectbean_dataset.xml",
428                 "dataset/project_owner_rel_dataset.xml",
429                 "dataset/projectbean_field_rel_dataset.xml",
430                 "dataset/workflowscriptbean_dataset.xml",
431                 "dataset/projectscriptbean_dataset.xml",
432                 "dataset/componentbean_dataset.xml",
433                 "dataset/versionbean_dataset.xml",
434                 "dataset/permissionbean_dataset.xml",
435                 "dataset/issuebean_dataset.xml",
436                 "dataset/issuefieldbean_dataset.xml",
437                 "dataset/issueattachmentbean_dataset.xml",
438                 "dataset/issueactivitybean_dataset.xml",
439                 "dataset/issuehistorybean_dataset.xml",
440                 "dataset/notificationbean_dataset.xml"};
441 
442     }
443 
444 }