001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/model/actions/personalization/ChangeServicePropertyActionTest.java,v 1.1.1.1 2004/03/25 12:08:40 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:40 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.portal.model.actions.personalization;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Map;
049:
050: import junit.framework.Assert;
051: import junit.framework.TestCase;
052: import es.udc.mypersonalizer.kernel.model.actions.ActionProcessorSingleton;
053: import es.udc.mypersonalizer.kernel.model.editors.DesktopLayoutEditor;
054: import es.udc.mypersonalizer.kernel.model.editors.EditorFactory;
055: import es.udc.mypersonalizer.kernel.model.editors.ServiceKeys;
056: import es.udc.mypersonalizer.kernel.model.editors.UserRegistrationInformationEditor;
057: import es.udc.mypersonalizer.kernel.model.editors.WorkspaceLayoutEditor;
058: import es.udc.mypersonalizer.kernel.model.metainfo.MetaProperty;
059: import es.udc.mypersonalizer.kernel.model.metainfo.MetaService;
060: import es.udc.mypersonalizer.kernel.model.metainfo.MetaServiceRegistrySingleton;
061: import es.udc.mypersonalizer.kernel.model.properties.Property;
062: import es.udc.mypersonalizer.kernel.model.properties.PropertyStructure;
063: import es.udc.mypersonalizer.kernel.model.repository.interfaces.DesktopLayout;
064: import es.udc.mypersonalizer.kernel.model.repository.interfaces.DesktopLayoutAccessor;
065: import es.udc.mypersonalizer.kernel.model.repository.interfaces.RepositoryAccessorFactory;
066: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceProperty;
067: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePropertyKey;
068: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserGroup;
069: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserGroupAccessor;
070: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserRegistrationInformation;
071: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserRegistrationInformationAccessor;
072: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceLayout;
073: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceLayoutAccessor;
074: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
075: import es.udc.mypersonalizer.kernel.util.exceptions.ModelException;
076: import es.udc.mypersonalizer.portal.model.permissions.UserCredentials;
077: import es.udc.mypersonalizer.portal.model.types.WizardEvent;
078: import es.udc.mypersonalizer.portal.model.types.personalization.FindServicePropertyEvent;
079:
080: /**
081: * Test suite for {@link ChangeServicePropertyAction}.
082: *
083: * @author Daniel Fernandez
084: * @since 1.0
085: */
086: public class ChangeServicePropertyActionTest extends TestCase {
087:
088: private static final String CHANGE_SERVICE_PROPERTY_ACTION = "ChangeServicePropertyAction";
089:
090: private static final String FIND_SERVICE_PROPERTY_ACTION = "FindServicePropertyAction";
091:
092: private static final String EDIT_USER_REGISTRATION_INFORMATION_ACTION = "FindUserRegistrationInformationAction";
093:
094: private static final String TEST_LOGIN_NAME = "scott";
095:
096: private static final String SERVICE_NAME = "MyBookmarks";
097:
098: private FindServicePropertyEvent findServicePropertyEvent;
099:
100: private WizardEvent changeServicePropertyEvent;
101:
102: private ServiceProperty serviceProperty = null;
103:
104: public ChangeServicePropertyActionTest(String testName) {
105: super (testName);
106: }
107:
108: protected void setUp() throws Exception {
109: super .setUp();
110:
111: }
112:
113: public void testChangeUserInfo() {
114: try {
115:
116: findServicePropertyEvent = createFindServicePropertyEvent();
117:
118: if (findServicePropertyEvent.getServicePropertyKey() != null) {
119: serviceProperty = findServiceProperty();
120:
121: changeServicePropertyEvent = createChangeServicePropertyEvent();
122:
123: changeServiceProperty();
124: }
125:
126: } catch (RuntimeException e) {
127: fail(e.getMessage());
128: }
129: Assert.assertEquals(true, true);
130: }
131:
132: public ServiceProperty findServiceProperty()
133: throws RuntimeException {
134:
135: try {
136: ActionProcessorSingleton actionProcessorSingleton = ActionProcessorSingleton
137: .getInstance();
138:
139: ServiceProperty serviceProperty = (ServiceProperty) actionProcessorSingleton
140: .execute(FIND_SERVICE_PROPERTY_ACTION,
141: findServicePropertyEvent);
142:
143: System.out
144: .println("Property correctly read from repository");
145:
146: return serviceProperty;
147:
148: } catch (InternalErrorException e) {
149: throw new RuntimeException(e.getMessage());
150: } catch (ModelException e) {
151: throw new RuntimeException(e.getMessage());
152: }
153:
154: }
155:
156: public void changeServiceProperty() throws RuntimeException {
157:
158: try {
159: ActionProcessorSingleton actionProcessorSingleton = ActionProcessorSingleton
160: .getInstance();
161:
162: ServiceProperty serviceProperty = (ServiceProperty) actionProcessorSingleton
163: .execute(CHANGE_SERVICE_PROPERTY_ACTION,
164: changeServicePropertyEvent);
165:
166: System.out
167: .println("Property correctly writen to repository");
168: } catch (InternalErrorException e) {
169: throw new RuntimeException(e.getMessage());
170: } catch (ModelException e) {
171: throw new RuntimeException(e.getMessage());
172: }
173:
174: }
175:
176: private FindServicePropertyEvent createFindServicePropertyEvent()
177: throws RuntimeException {
178:
179: try {
180:
181: RepositoryAccessorFactory repositoryAccessorFactory = RepositoryAccessorFactory
182: .getInstance();
183:
184: UserRegistrationInformationAccessor userRegInfoAccessor = repositoryAccessorFactory
185: .createUserRegistrationInformationAccessor();
186:
187: UserRegistrationInformation userRegistrationInformation = userRegInfoAccessor
188: .findUserRegistrationInformation(TEST_LOGIN_NAME);
189:
190: EditorFactory editorFactory = EditorFactory.getInstance();
191: UserRegistrationInformationEditor userRegInfoEditor = editorFactory
192: .createUserRegistrationInformationEditor(userRegistrationInformation
193: .getProperty());
194:
195: DesktopLayoutAccessor desktopLayoutAccessor = repositoryAccessorFactory
196: .createDesktopLayoutAccessor();
197:
198: DesktopLayout desktopLayout = desktopLayoutAccessor
199: .findDesktopLayout(userRegInfoEditor
200: .getDesktopLayoutPropertyIdentifier());
201:
202: DesktopLayoutEditor desktopLayoutEditor = editorFactory
203: .createDesktopLayoutEditor(desktopLayout
204: .getProperty());
205:
206: WorkspaceLayoutAccessor workspaceLayoutAccessor = repositoryAccessorFactory
207: .createWorkspaceLayoutAccessor();
208:
209: WorkspaceLayout workspaceLayout = workspaceLayoutAccessor
210: .findWorkspaceLayout(desktopLayoutEditor
211: .getDefaultWorkspaceLayoutIdentifier());
212:
213: WorkspaceLayoutEditor workspaceLayoutEditor = editorFactory
214: .createWorkspaceLayoutEditor(workspaceLayout
215: .getProperty());
216:
217: Iterator serviceKeysIter = workspaceLayoutEditor
218: .getServiceKeys().entrySet().iterator();
219: ServicePropertyKey servicePropertyKey = null;
220:
221: while ((serviceKeysIter.hasNext())
222: && (servicePropertyKey == null)) {
223: Map.Entry entry = (Map.Entry) serviceKeysIter.next();
224: List servicePropertyList = (List) entry.getValue();
225: Iterator servicePropertyIter = servicePropertyList
226: .iterator();
227: while ((servicePropertyIter.hasNext())
228: && (servicePropertyKey == null)) {
229: ServiceKeys serviceKeys = (ServiceKeys) servicePropertyIter
230: .next();
231:
232: if (serviceKeys.getServicePropertyKey()
233: .getServiceIdentifier().equals(
234: "MyBookmarks")) {
235:
236: servicePropertyKey = serviceKeys
237: .getServicePropertyKey();
238: }
239: }
240: }
241:
242: UserGroupAccessor userGroupAccessor = repositoryAccessorFactory
243: .createUserGroupAccessor();
244: Collection userGroupIdentifiers = new ArrayList();
245: Collection userGroups = userGroupAccessor
246: .findAllGroupsAssignedToUser(TEST_LOGIN_NAME);
247: Iterator userGroupsIterator = userGroups.iterator();
248: while (userGroupsIterator.hasNext()) {
249: UserGroup userGroup = (UserGroup) userGroupsIterator
250: .next();
251: userGroupIdentifiers.add(userGroup
252: .getUserGroupIdentifier());
253: }
254:
255: FindServicePropertyEvent event = new FindServicePropertyEvent(
256: new UserCredentials(TEST_LOGIN_NAME,
257: userGroupIdentifiers), servicePropertyKey);
258:
259: return event;
260:
261: } catch (Exception e) {
262: throw new RuntimeException(e.getMessage());
263: }
264: }
265:
266: private WizardEvent createChangeServicePropertyEvent()
267: throws RuntimeException {
268:
269: try {
270:
271: RepositoryAccessorFactory repositoryAccessorFactory = RepositoryAccessorFactory
272: .getInstance();
273:
274: PropertyStructure propertyStructure = (PropertyStructure) serviceProperty
275: .getProperty().getValuesAsObject()[0];
276:
277: Map properties = propertyStructure.getAsMap();
278: Map newProperties = new HashMap();
279: Iterator propertiesIter = properties.values().iterator();
280:
281: MetaServiceRegistrySingleton metaServiceRegistrySingleton = MetaServiceRegistrySingleton
282: .getInstance();
283:
284: MetaService userInformationService = metaServiceRegistrySingleton
285: .getMetaService(serviceProperty.getKey()
286: .getServiceIdentifier());
287:
288: MetaProperty userInformationMetaRootProperty = userInformationService
289: .getMetaRootProperty();
290:
291: while (propertiesIter.hasNext()) {
292: Property currentProperty = (Property) propertiesIter
293: .next();
294: String propertyName = currentProperty.getSimpleName();
295: String propertyValue = null;
296: String newPropertyValue = null;
297:
298: String[] values = currentProperty.getValuesAsString();
299:
300: MetaProperty currentMetaProperty = userInformationMetaRootProperty
301: .findMetaProperty(propertyName);
302:
303: boolean multiValued = currentMetaProperty
304: .isMultiValued();
305:
306: if (!multiValued) {
307: if (values.length == 0) {
308: propertyValue = "";
309: } else {
310: propertyValue = values[0];
311: if (propertyValue.length() > 0) {
312: char firstChar = propertyValue.charAt(0);
313: String rest = propertyValue.substring(1,
314: propertyValue.length());
315: if (Character.isLowerCase(firstChar)) {
316: firstChar = Character
317: .toUpperCase(firstChar);
318: } else {
319: firstChar = Character
320: .toLowerCase(firstChar);
321: }
322: newPropertyValue = (new String(
323: new char[] { firstChar }))
324: + rest;
325: } else {
326: newPropertyValue = propertyValue;
327: }
328: }
329: }
330:
331: // System.out.println("["+propertyName+":"+newPropertyValue+"]");
332: String[] newPropertyValues = new String[] { newPropertyValue };
333: newProperties.put(propertyName, newPropertyValues);
334:
335: }
336:
337: UserGroupAccessor userGroupAccessor = repositoryAccessorFactory
338: .createUserGroupAccessor();
339: Collection userGroupIdentifiers = new ArrayList();
340: Collection userGroups = userGroupAccessor
341: .findAllGroupsAssignedToUser(TEST_LOGIN_NAME);
342: Iterator userGroupsIterator = userGroups.iterator();
343: while (userGroupsIterator.hasNext()) {
344: UserGroup userGroup = (UserGroup) userGroupsIterator
345: .next();
346: userGroupIdentifiers.add(userGroup
347: .getUserGroupIdentifier());
348: }
349:
350: String loginName = "scott";
351: String propertySetterCommand = "updateSimpleProperties";
352: String serviceIdentifier = serviceProperty.getKey()
353: .getServiceIdentifier();
354: String propertyStructureName = "0";
355: Long propertyIdentifier = serviceProperty.getKey()
356: .getPropertyIdentifier();
357:
358: return new WizardEvent(new UserCredentials(loginName,
359: userGroupIdentifiers), newProperties,
360: serviceIdentifier, propertySetterCommand,
361: propertyStructureName, propertyIdentifier);
362:
363: } catch (Exception e) {
364: throw new RuntimeException(e.getMessage());
365: }
366:
367: }
368:
369: }
|