1   /**
2    * Copyright 2005-2007 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.businessobjects.environment.persistence;
16  
17  import org.figure8.join.util.SpringTestCase;
18  import org.figure8.join.businessobjects.environment.Deployment;
19  import org.figure8.join.businessobjects.environment.LogicalEnvironment;
20  import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
21  import org.figure8.join.businessobjects.environment.EnvironmentMapping;
22  import org.figure8.join.businessobjects.commons.Step;
23  import org.figure8.join.businessobjects.commons.Release;
24  import org.figure8.join.businessobjects.commons.Target;
25  import org.figure8.join.businessobjects.commons.persistence.StepDao;
26  import org.figure8.join.businessobjects.commons.persistence.TargetDao;
27  import org.figure8.join.businessobjects.commons.persistence.ReleaseDao;
28  import org.figure8.join.businessobjects.artifact.Assembly;
29  import org.figure8.join.businessobjects.artifact.persistence.AssemblyDao;
30  
31  import java.util.Date;
32  import java.util.List;
33  /**
34   * JUnit test case for testing DeploymentDao implementation.
35   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
36   * @version $Revision: 1.1 $
37   */
38  public class DeploymentDaoTest extends SpringTestCase{
39  
40     // Static -------------------------------------------------------------------
41  
42     /** Spring configuration files */
43     private String[] configLocations = new String[]{
44        "classpath:/org/figure8/join/businessobjects/environment/persistence/spring.xml"};
45  
46  
47     // Attributes ---------------------------------------------------------------
48  
49     /** The needed StepDao impl */
50     protected StepDao stepDao = null;
51     /** The needed TargetDao impl. */
52     protected TargetDao targetDao = null;
53     /** The needed ReleaseDao impl */
54     protected ReleaseDao releaseDao = null;
55     /** The needed AssemblyDao impl. */
56     protected AssemblyDao assemblyDao = null;
57     /** The needed logical environment dao impl. */
58     protected LogicalEnvironmentDao logicalEnvDao = null;
59     /** The needed physical environment dao impl. */
60     protected PhysicalEnvironmentDao physicalEnvDao = null;
61     /** The needed environment mapping dao impl. */
62     protected EnvironmentMappingDao envMappingDao = null;
63  
64     /** The DeploymentDao implementation instance to test. */
65     protected DeploymentDao dao = null;
66  
67  
68     // Override of SpringTestCase -----------------------------------------------
69  
70     /** Retrieve the daos we neef after having initialized context */
71     public void setUp(){
72        super.setUp();
73        // Get required daos.
74        stepDao = (StepDao)context.getBean("stepDao");
75        targetDao = (TargetDao)context.getBean("targetDao");
76        releaseDao = (ReleaseDao)context.getBean("releaseDao");
77        assemblyDao = (AssemblyDao)context.getBean("assemblyDao");
78        logicalEnvDao = (LogicalEnvironmentDao)context.getBean("logicalEnvironmentDao");
79        physicalEnvDao = (PhysicalEnvironmentDao)context.getBean("physicalEnvironmentDao");
80        envMappingDao = (EnvironmentMappingDao)context.getBean("environmentMappingDao");
81        // Get the dao implementation to test.
82        dao = (DeploymentDao)context.getBean("deploymentDao");
83     }
84  
85  
86     // Public -------------------------------------------------------------------
87  
88     /** Test the creation of a Deployment */
89     public void testDeploymentCreation(){
90        // Save a deployment and required items.
91        Step step = new Step("deploymentCreation", 1);
92        Target target = new Target("full", "description");
93        Release release = new Release("99.0", 99, 0, new Date());
94        Assembly assembly = new Assembly("01", "comments", "composerId", release);
95        LogicalEnvironment logical = new LogicalEnvironment("depCreation", "label", "description", "managerId", step, release);
96        PhysicalEnvironment physical = new PhysicalEnvironment("depCreation", "name");
97        EnvironmentMapping envMapping = new EnvironmentMapping(logical, physical);
98        Deployment deployment = new Deployment("applicantId", "comments", envMapping, assembly, target);
99        int size = getDeploymentListSize();
100       stepDao.save(step);
101       targetDao.save(target);
102       releaseDao.save(release);
103       assemblyDao.save(assembly);
104       logicalEnvDao.save(logical);
105       physicalEnvDao.save(physical);
106       envMappingDao.save(envMapping);
107       dao.save(deployment);
108       // Assert there's one deployment created.
109       assertEquals("Deployment is successfully created", size + 1, getDeploymentListSize());
110    }
111 
112    /** Test the retrieval of Deployments using the environment criterion */
113    public void testFindByEnvironments(){
114       // Save a deployment and required items.
115       Step step = new Step("depFindByEnv", 1);
116       Target target = new Target("fullByEnv", "description");
117       Release release = new Release("99.1", 99, 1, new Date());
118       Assembly assembly = new Assembly("01", "comments", "composerId", release);
119       LogicalEnvironment logical = new LogicalEnvironment("depFindByEnv", "labelByEnv", "description", "managerId", step, release);
120       PhysicalEnvironment physical = new PhysicalEnvironment("depFindByEnv1", "nameByEnv1");
121       PhysicalEnvironment physical2 = new PhysicalEnvironment("depFindByEnv2", "nameByEnv2");
122       EnvironmentMapping envMapping = new EnvironmentMapping(logical, physical);
123       Deployment deployment = new Deployment("applicantId", "comments", envMapping, assembly, target);
124       stepDao.save(step);
125       targetDao.save(target);
126       releaseDao.save(release);
127       assemblyDao.save(assembly);
128       logicalEnvDao.save(logical);
129       physicalEnvDao.save(physical);
130       physicalEnvDao.save(physical2);
131       envMappingDao.save(envMapping);
132       dao.save(deployment);
133       // Find by environments and assert.
134       List deployments = dao.getDeploymentsByLogicalEnvironment(logical);
135       assertNotNull("Deployments list is not null", deployments);
136       assertEquals("Deployments list has correct size", 1, deployments.size());
137       deployments = dao.getDeploymentsByPhysicalEnvironment(physical);
138       assertNotNull("Deployments list is not null", deployments);
139       assertEquals("Deployments list has correct size", 1, deployments.size());
140       deployments = dao.getDeploymentsByPhysicalEnvironment(physical2);
141       assertNotNull("Deployments list is not null", deployments);
142       assertEquals("Deployments list has correct size", 0, deployments.size());
143       // Close existing mapping, create another one and associated deployment.
144       envMapping.close();
145       // TODO: solve why next call to save() makes the test crash (lazy intialization exception)
146       // envMappingDao.save(envMapping);
147       EnvironmentMapping envMapping2 = new EnvironmentMapping(logical, physical2);
148       Deployment deployment2 = new Deployment("applicantId", "comments", envMapping2, assembly, target);
149       envMappingDao.save(envMapping2);
150       dao.save(deployment2);
151       // Find by environments and assert.
152       deployments = dao.getDeploymentsByLogicalEnvironment(logical);
153       assertNotNull("Deployments list is not null", deployments);
154       assertEquals("Deployments list has correct size", 2, deployments.size());
155       deployments = dao.getDeploymentsByPhysicalEnvironment(physical);
156       assertNotNull("Deployments list is not null", deployments);
157       assertEquals("Deployments list has correct size", 1, deployments.size());
158       deployments = dao.getDeploymentsByPhysicalEnvironment(physical2);
159       assertNotNull("Deployments list is not null", deployments);
160       assertEquals("Deployments list has correct size", 1, deployments.size());
161    }
162 
163 
164    // Protected ----------------------------------------------------------------
165 
166    /** Test the findAll on Deployment */
167    protected int getDeploymentListSize(){
168       return dao.findAll().size();
169    }
170 
171 
172    // Implementation of SpringTestCase -----------------------------------------
173 
174    /** @return configLocations inner field */
175    public String[] getConfigLocations(){
176       return configLocations;
177    }
178 }