1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.astrogrid.community.common.policy.manager ;
34
35 import org.apache.commons.logging.Log ;
36 import org.apache.commons.logging.LogFactory ;
37
38 import java.util.Map ;
39 import java.util.HashMap ;
40
41 import org.astrogrid.community.common.policy.data.ResourceData ;
42 import org.astrogrid.community.common.service.CommunityServiceMock ;
43
44 import org.astrogrid.community.common.identifier.ResourceIdentifier ;
45
46 import org.astrogrid.community.common.exception.CommunityServiceException ;
47 import org.astrogrid.community.common.exception.CommunityResourceException ;
48 import org.astrogrid.community.common.exception.CommunityIdentifierException ;
49
50 /***
51 * Mock implementation of our ResourceManager service.
52 *
53 */
54 public class ResourceManagerMock
55 extends CommunityServiceMock
56 implements ResourceManager
57 {
58 /***
59 * Our debug logger.
60 *
61 */
62 private static Log log = LogFactory.getLog(ResourceManagerMock.class);
63
64 /***
65 * Switch for testing service exceptions.
66 * Set this to true, and the service calls will throw CommunityServiceExceptions.
67 *
68 */
69 public static boolean SERVICE_EXCEPTIONS = false ;
70
71 /***
72 * Public constructor.
73 *
74 */
75 public ResourceManagerMock()
76 {
77 super() ;
78 }
79
80 /***
81 * Our hash table of values.
82 *
83 */
84 private static Map map = new HashMap() ;
85
86 /***
87 * Reset our map.
88 *
89 */
90 public static void reset()
91 {
92 log.debug("") ;
93 log.debug("----\"----") ;
94 log.debug("ResourceManagerMock.reset()") ;
95 map.clear() ;
96 }
97
98 /***
99 * Register a new Resource.
100 * @return A new ResourceData object to represent the resource.
101 * @throws CommunityServiceException If there is an internal error in the service.
102 *
103 */
104 public ResourceData addResource()
105 throws CommunityServiceException
106 {
107 log.debug("") ;
108 log.debug("----\"----") ;
109 log.debug("ResourceManagerMock.addResource()") ;
110
111
112 if (SERVICE_EXCEPTIONS)
113 {
114 throw new CommunityServiceException("Mock exception test") ;
115 }
116
117
118 ResourceData resource = new ResourceData(
119 new ResourceIdentifier()
120 ) ;
121
122
123 map.put(resource.getIdent(), resource) ;
124
125
126 return resource ;
127 }
128
129 /***
130 * Request a Resource details.
131 * @param The resource identifier.
132 * @return The requested ResourceData object.
133 * @throws CommunityIdentifierException If the identifier is not valid.
134 * @throws CommunityResourceException If unable to locate the resource.
135 * @throws CommunityServiceException If there is an internal error in the service.
136 *
137 */
138 public ResourceData getResource(String ident)
139 throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
140 {
141 log.debug("") ;
142 log.debug("----\"----") ;
143 log.debug("ResourceManagerMock.getResource()") ;
144 log.debug(" Ident : " + ident) ;
145
146
147 if (SERVICE_EXCEPTIONS)
148 {
149 throw new CommunityServiceException("Mock exception test") ;
150 }
151
152
153 if (null == ident)
154 {
155 throw new CommunityIdentifierException(
156 "Null identifier"
157 ) ;
158 }
159
160
161 ResourceData resource = (ResourceData) map.get(ident) ;
162
163
164 if (null != resource)
165 {
166 return resource ;
167 }
168
169
170 else {
171 throw new CommunityResourceException(
172 "Unable to locate resource",
173 ident
174 ) ;
175 }
176 }
177
178 /***
179 * Request a Resource details.
180 * @param The resource identifier.
181 * @return The requested ResourceData object.
182 * @throws CommunityIdentifierException If the identifier is not valid.
183 * @throws CommunityResourceException If unable to locate the resource.
184 * @throws CommunityServiceException If there is an internal error in the service.
185 *
186 */
187 public Object[] getResources() {
188 log.debug("") ;
189 log.debug("----\"----") ;
190 log.debug("ResourceManagerMock.getResources()") ;
191 return null;
192
193 }
194
195
196 /***
197 * Update a Resource details.
198 * @param The ResourceData to update.
199 * @return The updated ResourceData.
200 * @throws CommunityResourceException If unable to locate the resource.
201 * @throws CommunityServiceException If there is an internal error in the service.
202 * @throws CommunityIdentifierException If the resource identifier is not valid.
203 *
204 */
205 public ResourceData setResource(ResourceData resource)
206 throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
207 {
208 log.debug("") ;
209 log.debug("----\"----") ;
210 log.debug("ResourceManagerMock.setResource()") ;
211 log.debug(" Resource : " + resource) ;
212
213
214 if (SERVICE_EXCEPTIONS)
215 {
216 throw new CommunityServiceException("Mock exception test") ;
217 }
218
219
220 if (null == resource)
221 {
222 throw new CommunityResourceException(
223 "Null resource"
224 ) ;
225 }
226
227
228 if (null == resource.getIdent())
229 {
230 throw new CommunityIdentifierException(
231 "Null identifier"
232 ) ;
233 }
234
235
236 if (map.containsKey(resource.getIdent()))
237 {
238
239
240 map.put(resource.getIdent(), resource) ;
241
242
243 return resource ;
244 }
245
246
247 else {
248 throw new CommunityResourceException(
249 "Unable to locate resource",
250 resource.getIdent()
251 ) ;
252 }
253 }
254
255 /***
256 * Delete a Resource object.
257 * @param The resource identifier.
258 * @return The original ResourceData.
259 * @throws CommunityResourceException If unable to locate the resource.
260 * @throws CommunityServiceException If there is an internal error in the service.
261 * @throws CommunityIdentifierException If the resource identifier is not valid.
262 *
263 */
264 public ResourceData delResource(String ident)
265 throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
266 {
267 log.debug("") ;
268 log.debug("----\"----") ;
269 log.debug("ResourceManagerMock.delResource()") ;
270 log.debug(" Ident : " + ident) ;
271
272
273 if (SERVICE_EXCEPTIONS)
274 {
275 throw new CommunityServiceException("Mock exception test") ;
276 }
277
278
279 if (null == ident)
280 {
281 throw new CommunityIdentifierException(
282 "Null identifier"
283 ) ;
284 }
285
286
287 ResourceData original = (ResourceData) map.get(ident) ;
288
289
290 if (null != original)
291 {
292
293
294 map.remove(ident) ;
295
296
297 return original ;
298 }
299
300
301 else {
302 throw new CommunityResourceException(
303 "Unable to locate resource",
304 ident
305 ) ;
306 }
307 }
308 }