1   /**
2    * Copyright 2005-2006 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.Service;
19  import org.figure8.join.businessobjects.environment.Gateway;
20  import org.figure8.join.businessobjects.environment.Resource;
21  import org.figure8.join.businessobjects.environment.ServiceType;
22  import org.figure8.join.businessobjects.environment.GatewayType;
23  import org.figure8.join.businessobjects.environment.ResourceType;
24  import org.figure8.join.businessobjects.environment.VersionedResource;
25  import org.figure8.join.businessobjects.environment.VersionedResourceType;
26  
27  import java.util.List;
28  /**
29   * JUnit test case for testing ResourceDao implementation.
30   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31   * @version $Revision: 1.1 $
32   */
33  public class ResourceDaoTest extends SpringTestCase{
34  
35     // Static -------------------------------------------------------------------
36  
37     /** Spring configuration files */
38     private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/environment/persistence/spring.xml"};
39  
40  
41     // Attributes ---------------------------------------------------------------
42  
43     /** The dao implementation to test */
44     protected ResourceDao dao = null;
45     /** The ResourceType dao implementation we need */
46     protected ResourceTypeDao typeDao = null;
47  
48  
49     // Override of SpringTestCase -----------------------------------------------
50  
51     /** Retrieve the service type dao after having initialized context */
52     public void setUp(){
53        super.setUp();
54        // Get ResourceDao and daos we need.
55        dao = (ResourceDao)context.getBean("resourceDao");
56        typeDao = (ResourceTypeDao)context.getBean("resourceTypeDao");
57     }
58  
59  
60     // Public -------------------------------------------------------------------
61  
62     /** Test the creation of a Resource */
63     public void testResourceCreation(){
64        // Create service and associated service type.
65        ServiceType servType = new ServiceType("servCreationKey", "servCreationType");
66        Service service = new Service("serviceCreation", servType);
67        int size = getResourceListSize();
68        typeDao.save(servType);
69        dao.save(service);
70        // Assert there's one more resource.
71        assertEquals("Service is successfully created", size + 1, dao.findAll().size());
72        
73        // Create gateway and associated gateway type.
74        GatewayType gateType = new GatewayType("gateCreationKey", "gateCreationType");
75        Gateway gateway = new Gateway("gatewayCreation", gateType);
76        typeDao.save(gateType);
77        dao.save(gateway);
78        // Assert there's one more resource.
79        assertEquals("Gateway is successfully created", size + 2, dao.findAll().size());
80  
81        // Remove all the created resources.
82        dao.remove(service);
83        dao.remove(gateway);
84        // Assert they have been removed.
85        assertEquals("Resources have been successfully removed", size, dao.findAll().size());
86     }
87  
88     /** Test the retrieval of resources */
89     public void testResourceRetrieval(){
90        // Save a bunch of new resource and associated types.
91        ServiceType type1 = new ServiceType("servKey1", "servLabel1");
92        Service serv = new Service("serviceRetrieval", type1);
93        GatewayType type2 = new GatewayType("gateKey2", "gateLabel2");
94        GatewayType type3 = new GatewayType("gateKey3", "gateLabel3");
95        Gateway gate1 = new Gateway("gatewayRetrieval1", type2);
96        Gateway gate2 = new Gateway("gatewayRetrieval2", type3);
97        VersionedResourceType type4 = new VersionedResourceType("reseKey4", "reseLabel4");
98        VersionedResource rese = new VersionedResource("resourceRetrieval", type4);
99        int size = getResourceListSize();
100       typeDao.save(type1);
101       dao.save(serv);
102       typeDao.save(type2);
103       typeDao.save(type3);
104       dao.save(gate1);
105       dao.save(gate2);
106       typeDao.save(type4);
107       dao.save(rese);
108       // Assert there's 4 more resources.
109       assertEquals("Resources are successfully created", size + 4, dao.findAll().size());
110       // Retrieve a resource using its name.
111       Resource foundResource = dao.getResource("resourceRetrieval");
112       assertNotNull("Found a Resource", foundResource);
113       assertEquals("Found the correct Resource", "resourceRetrieval", foundResource.getName());
114       assertEquals("Found the correct Resource", "reseKey4", foundResource.getResourceType().getKey());
115       // Retrieve resources using category.
116       List foundResources = dao.getResourcesByCategory("Gateway");
117       assertNotNull("Found some Resources", foundResources);
118       assertEquals("Found the correct number of Resources", 2, foundResources.size());
119       // Retrieve resources using type.
120       foundResources = dao.getResourcesByType(type2);
121       assertNotNull("Found some Resources", foundResources);
122       assertEquals("Found the correct number of Resources", 1, foundResources.size());
123       assertEquals("Found the correct Resource", "gatewayRetrieval1", ((Resource)foundResources.get(0)).getName());
124       // Check another category.
125       foundResources = dao.getResourcesByCategory("Service");
126       assertNotNull("Found some Resources", foundResources);
127       assertEquals("Found the correct number of Resources", 1, foundResources.size());
128       assertEquals("Found the correct Resource", "serviceRetrieval", ((Resource)foundResources.get(0)).getName());
129    }
130    
131    
132    // Protected ----------------------------------------------------------------
133 
134    /** Test the findAll() on dao */
135    protected int getResourceListSize(){
136       return dao.findAll().size();
137    }
138    
139    
140    // Implementation of SpringTestCase -----------------------------------------
141 
142    /** @return configLocations inner field */
143    public String[] getConfigLocations(){
144       return configLocations;
145    }
146 }
147