1   /**
2    * Copyright 2005-2008 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.commons.Target;
19  import org.figure8.join.businessobjects.commons.persistence.TargetDao;
20  import org.figure8.join.businessobjects.environment.Parameter;
21  import org.figure8.join.businessobjects.environment.ParameterValue;
22  import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
23  
24  import java.util.List;
25  /**
26   * JUnit test case for testing ParameterValueDao implementation.
27   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28   * @version $Revision: 1.2 $
29   */
30  public class ParameterValueDaoTest extends SpringTestCase{
31  
32     // Static -------------------------------------------------------------------
33  
34     /** Spring configuration files */
35     private String[] configLocations = new String[]{
36        "classpath:/org/figure8/join/businessobjects/environment/persistence/spring.xml"};
37  
38  
39     // Attributes ---------------------------------------------------------------
40  
41     /** The TargetDao we need for test fixture */
42     protected TargetDao targetDao = null;
43     /** The ParameterDao we need for test fixture */
44     protected ParameterDao parameterDao = null;
45     /** The PhysicalEnvDao we need for test fixture */
46     protected PhysicalEnvironmentDao environmentDao = null;
47  
48     /** The ParameterValueDao implementation to test. */
49     protected ParameterValueDao dao = null;
50  
51  
52     // Override of SpringTestCase -----------------------------------------------
53  
54     /** Retrieve the daos we need after having initialized context */
55     public void setUp(){
56        super.setUp();
57        // Get the DAOs we need.
58        targetDao = (TargetDao)context.getBean("targetDao");
59        parameterDao = (ParameterDao)context.getBean("parameterDao");
60        environmentDao = (PhysicalEnvironmentDao)context.getBean("physicalEnvironmentDao");
61        dao = (ParameterValueDao)context.getBean("parameterValueDao");
62     }
63  
64  
65     // Public -------------------------------------------------------------------
66  
67     /** Test the ParameterValue creation */
68     public void testParameterValueCreation(){
69        // Create the objects we need.
70        Target target = new Target("PVCreation", "description");
71        Parameter param = new Parameter("PVCreation", "description");
72        PhysicalEnvironment env = new PhysicalEnvironment("PVCreation", "PVCreation Env Name");
73        targetDao.save(target);
74        parameterDao.save(param);
75        environmentDao.save(env);
76        // Save a new ParameterValue.
77        ParameterValue value = new ParameterValue("creation", param, target, env);
78        int size = getParameterValueListSize();
79        dao.save(value);
80        // Assert there's one value.
81        assertEquals("ParameterValue is successfully created", size + 1, dao.findAll().size());
82     }
83  
84     /** Test the retrieval of current parameter values */
85     public void testFindCurrentParameterValues(){
86        // Create the objects we need.
87        Target target = new Target("PVFindCurrent", "description");
88        Parameter param1 = new Parameter("PVFindCurrent1", "description");
89        Parameter param2 = new Parameter("PVFindCurrent2", "description");
90        PhysicalEnvironment env = new PhysicalEnvironment("PVFindCurrent", "PVFindCurrent Env Name");
91        targetDao.save(target);
92        parameterDao.save(param1);
93        parameterDao.save(param2);
94        environmentDao.save(env);
95        // Save some new ParameterValues.
96        ParameterValue value1 = new ParameterValue("findCurrent1", param1, target, env);
97        ParameterValue value2 = new ParameterValue("findCurrent2", param2, target, env);
98        int size = getParameterValueListSize();
99        dao.save(value1);
100       dao.save(value2);
101       // Assert there's two more values.
102       assertEquals("ParameterValue are successfully created", size + 2, dao.findAll().size());
103       // Retrieve current values.
104       List values = dao.getCurrentParameterValues(env.getKey(), target.getName());
105       assertNotNull("Found some current values", values);
106       assertFalse("Found some non empty values", values.isEmpty());
107       assertEquals("Found the two current values", 2, values.size());
108 
109       // Update a value and check currents.
110       value2.setValue("findCurrent2-updated");
111       dao.save(value2);
112 
113       // Retrieve current values.
114       values = dao.getCurrentParameterValues(env.getKey(), target.getName());
115       assertNotNull("Found some current values", values);
116       assertFalse("Found some non empty values", values.isEmpty());
117       assertEquals("Found the two current values", 2, values.size());
118       for (int i=0; i < values.size(); i++){
119          ParameterValue value = (ParameterValue)values.get(i);
120          assertTrue("Value is one of current ones",
121                  "findCurrent1".equals(value.getValue()) || "findCurrent2-updated".equals(value.getValue()));
122       }
123    }
124 
125    /** Test the retrieval of historical parameter values */
126    public void testFindHistoricalParameterValues(){
127       // Create the objects we need.
128       Target target = new Target("PVFindHistorical", "description");
129       Parameter param = new Parameter("PVFindHistorical", "description");
130       PhysicalEnvironment env = new PhysicalEnvironment("PVFindHistorical", "PVFindHistorical Env Name");
131       targetDao.save(target);
132       parameterDao.save(param);
133       environmentDao.save(env);
134       // Save a new ParameterValue.
135       ParameterValue value = new ParameterValue("findHistorical", param, target, env);
136       int size = getParameterValueListSize();
137       dao.save(value);
138       // Assert there's one more values.
139       assertEquals("ParameterValue is successfully created", size + 1, dao.findAll().size());
140 
141       // Simulate the update of the value and check historical.
142       value.deprecate();
143       dao.save(value);
144       ParameterValue value2 = new ParameterValue("findHistorical-updated", param, target, env);
145       dao.save(value2);
146 
147       // Retrieve current values.
148       List values = dao.getHistoricalParameterValues(param, env.getKey());
149       assertNotNull("Found some historical values", values);
150       assertFalse("Found some non empty values", values.isEmpty());
151       assertEquals("Found the historical values", 2, values.size());
152       for (int i=0; i < values.size(); i++){
153          ParameterValue foundValue = (ParameterValue)values.get(i);
154          assertTrue("Value is one of historical ones", "findHistorical".equals(foundValue.getValue())
155                                                          || "findHistorical-updated".equals(foundValue.getValue()));
156       }
157    }
158 
159    /** Test the removal of a ParameterValue */
160    public void testParameterValueRemoval(){
161 
162    }
163 
164 
165    // Protected ----------------------------------------------------------------
166 
167    /** Test the findAll on parameter values */
168    protected int getParameterValueListSize(){
169       return dao.findAll().size();
170    }
171 
172 
173    // Implementation of SpringTestCase -----------------------------------------
174 
175    /** @return An array of Spring configuration files location. */
176    public String[] getConfigLocations(){
177       return configLocations;
178    }
179 }