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 package org.astrogrid.community.common.policy.manager ;
29
30 import org.apache.commons.logging.Log ;
31 import org.apache.commons.logging.LogFactory ;
32
33 import java.util.Map ;
34 import java.util.HashMap ;
35
36 import org.astrogrid.community.common.exception.CommunityPolicyException ;
37 import org.astrogrid.community.common.exception.CommunityIdentifierException ;
38
39 import org.astrogrid.community.common.policy.data.AccountData ;
40 import org.astrogrid.community.common.service.CommunityServiceMock ;
41
42 /***
43 * Mock implementation of our AccountManager service.
44 *
45 */
46 public class AccountManagerMock
47 extends CommunityServiceMock
48 implements AccountManager
49 {
50 /***
51 * Our debug logger.
52 *
53 */
54 private static Log log = LogFactory.getLog(AccountManagerMock.class);
55
56 /***
57 * Public constructor.
58 *
59 */
60 public AccountManagerMock()
61 {
62 super() ;
63 log.debug("") ;
64 log.debug("----\"----") ;
65 log.debug("AccountManagerMock()") ;
66 }
67
68 /***
69 * Our hash table of values.
70 *
71 */
72 private static Map map = new HashMap() ;
73
74 /***
75 * Reset our map.
76 *
77 */
78 public static void reset()
79 {
80 log.debug("") ;
81 log.debug("----\"----") ;
82 log.debug("AccountManagerMock.reset()") ;
83 map.clear() ;
84 }
85
86 /***
87 * Add a new Account, given the Account ident.
88 * @param ident The Account identifier.
89 * @return An AccountData for the Account.
90 * @throws CommunityIdentifierException If the identifier is not valid.
91 * @throws CommunityPolicyException If the identifier is already in the database.
92 *
93 */
94 public AccountData addAccount(String ident)
95 throws CommunityIdentifierException, CommunityPolicyException
96 {
97 log.debug("") ;
98 log.debug("----\"----") ;
99 log.debug("AccountManagerMock.addAccount()") ;
100 log.debug(" Ident : " + ident) ;
101
102
103 if (null == ident)
104 {
105 throw new CommunityIdentifierException(
106 "Null identifier"
107 ) ;
108 }
109
110
111 if (map.containsKey(ident))
112 {
113 throw new CommunityPolicyException(
114 "Duplicate account",
115 ident
116 ) ;
117 }
118
119
120 AccountData account = new AccountData(ident) ;
121
122
123 map.put(account.getIdent(), account) ;
124
125
126 return account ;
127 }
128
129 /***
130 * Add a new Account, given the Account data.
131 * @param data The AccountData to add.
132 * @return A new AccountData for the Account.
133 * @throws CommunityIdentifierException If the identifier is not valid.
134 * @throws CommunityPolicyException If the identifier is already in the database.
135 *
136 */
137 public AccountData addAccount(AccountData account)
138 throws CommunityIdentifierException, CommunityPolicyException
139 {
140 log.debug("") ;
141 log.debug("----\"----") ;
142 log.debug("AccountManagerMock.addAccount()") ;
143 log.debug(" Account : " + ((null != account) ? account.getIdent() : null)) ;
144
145
146 if (null == account)
147 {
148 throw new CommunityIdentifierException(
149 "Null account"
150 ) ;
151 }
152
153
154 if (null == account.getIdent())
155 {
156 throw new CommunityIdentifierException(
157 "Null identifier"
158 ) ;
159 }
160
161
162 if (map.containsKey(account.getIdent()))
163 {
164 throw new CommunityPolicyException(
165 "Duplicate account",
166 account.getIdent()
167 ) ;
168 }
169
170
171 map.put(account.getIdent(), account) ;
172
173
174 return account ;
175 }
176
177 /***
178 * Request an Account details, given the Account ident.
179 * @param ident The Account identifier.
180 * @return An AccountData for the Account.
181 * @throws CommunityIdentifierException If the identifier is not valid.
182 * @throws CommunityPolicyException If the identifier is not in the database.
183 *
184 */
185 public AccountData getAccount(String ident)
186 throws CommunityIdentifierException, CommunityPolicyException
187 {
188 log.debug("") ;
189 log.debug("----\"----") ;
190 log.debug("AccountManagerMock.getAccount()") ;
191 log.debug(" Ident : " + ident) ;
192
193
194 if (null == ident)
195 {
196 throw new CommunityIdentifierException(
197 "Null identifier"
198 ) ;
199 }
200
201
202 AccountData account = (AccountData) map.get(ident) ;
203
204
205 if (null != account)
206 {
207 return account ;
208 }
209
210
211 else {
212 throw new CommunityPolicyException(
213 "Account not found",
214 ident
215 ) ;
216 }
217 }
218
219 /***
220 * Update an Account.
221 * @param update The new Account data to update.
222 * @return A new AccountData for the Account.
223 * @throws CommunityIdentifierException If the identifier is not valid.
224 * @throws CommunityPolicyException If the identifier is not in the database.
225 *
226 */
227 public AccountData setAccount(AccountData update)
228 throws CommunityIdentifierException, CommunityPolicyException
229 {
230 log.debug("") ;
231 log.debug("----\"----") ;
232 log.debug("AccountManagerMock.setAccount()") ;
233 log.debug(" Ident : " + ((null != update) ? update.getIdent() : null)) ;
234
235
236 if (null == update)
237 {
238 throw new CommunityIdentifierException(
239 "Null inout data"
240 ) ;
241 }
242
243
244 if (null == update.getIdent())
245 {
246 throw new CommunityIdentifierException(
247 "Null inout data"
248 ) ;
249 }
250
251
252 AccountData found = this.getAccount(update.getIdent()) ;
253
254
255 map.put(found.getIdent(), update) ;
256
257
258 return update ;
259 }
260
261 /***
262 * Delete an Account.
263 * @param ident The Account identifier.
264 * @return The AccountData for the old Account.
265 * @throws CommunityIdentifierException If the identifier is not valid.
266 * @throws CommunityPolicyException If the identifier is not in the database.
267 *
268 */
269 public AccountData delAccount(String ident)
270 throws CommunityIdentifierException, CommunityPolicyException
271 {
272 log.debug("") ;
273 log.debug("----\"----") ;
274 log.debug("AccountManagerMock.delAccount()") ;
275 log.debug(" Ident : " + ident) ;
276
277
278 if (null == ident)
279 {
280 throw new CommunityIdentifierException(
281 "Null identifier"
282 ) ;
283 }
284
285
286 AccountData account = this.getAccount(ident) ;
287
288
289 if (null != account)
290 {
291
292
293 map.remove(account.getIdent()) ;
294
295
296 return account ;
297 }
298
299
300 else {
301 throw new CommunityPolicyException(
302 "Account not found",
303 ident
304 ) ;
305 }
306 }
307
308 /***
309 * Request a list of local Accounts.
310 * @return An array of AccountData objects.
311 *
312 */
313 public Object[] getLocalAccounts()
314 {
315 log.debug("") ;
316 log.debug("----\"----") ;
317 log.debug("AccountManagerMock.getLocalAccounts()") ;
318 return map.values().toArray() ;
319 }
320
321 }