diff --git a/source/java/org/alfresco/repo/web/scripts/preference/PreferenceServiceTest.java b/source/java/org/alfresco/repo/web/scripts/preference/PreferenceServiceTest.java index 88f03f97c4..e3025532a5 100644 --- a/source/java/org/alfresco/repo/web/scripts/preference/PreferenceServiceTest.java +++ b/source/java/org/alfresco/repo/web/scripts/preference/PreferenceServiceTest.java @@ -25,12 +25,12 @@ import org.alfresco.repo.web.scripts.BaseWebScriptTest; import org.alfresco.service.cmr.security.MutableAuthenticationService; import org.alfresco.service.cmr.security.PersonService; import org.alfresco.util.PropertyMap; -import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; -import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; -import org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest; -import org.springframework.extensions.webscripts.TestWebScriptServer.Response; import org.json.JSONException; import org.json.JSONObject; +import org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.Response; /** * Unit test to test preference Web Script API @@ -38,148 +38,153 @@ import org.json.JSONObject; * @author Roy Wetherall */ public class PreferenceServiceTest extends BaseWebScriptTest -{ +{ private MutableAuthenticationService authenticationService; + private AuthenticationComponent authenticationComponent; + private PersonService personService; - + private static final String USER_ONE = "PreferenceTestOne" + System.currentTimeMillis(); + private static final String USER_BAD = "PreferenceTestBad" + System.currentTimeMillis(); - + private static final String URL = "/api/people/" + USER_ONE + "/preferences";; - + @Override protected void setUp() throws Exception { super.setUp(); - - this.authenticationService = (MutableAuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService"); - this.authenticationComponent = (AuthenticationComponent)getServer().getApplicationContext().getBean("authenticationComponent"); - this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService"); - + + this.authenticationService = (MutableAuthenticationService) getServer().getApplicationContext().getBean( + "AuthenticationService"); + this.authenticationComponent = (AuthenticationComponent) getServer().getApplicationContext().getBean( + "authenticationComponent"); + this.personService = (PersonService) getServer().getApplicationContext().getBean("PersonService"); + this.authenticationComponent.setSystemUserAsCurrentUser(); - + // Create users createUser(USER_ONE); createUser(USER_BAD); - + // Do tests as user one this.authenticationComponent.setCurrentUser(USER_ONE); } - + private void createUser(String userName) { if (this.authenticationService.authenticationExists(userName) == false) { this.authenticationService.createAuthentication(userName, "PWD".toCharArray()); - + PropertyMap ppOne = new PropertyMap(4); ppOne.put(ContentModel.PROP_USERNAME, userName); ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName"); ppOne.put(ContentModel.PROP_LASTNAME, "lastName"); ppOne.put(ContentModel.PROP_EMAIL, "email@email.com"); ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle"); - + this.personService.createPerson(ppOne); - } + } } - + @Override protected void tearDown() throws Exception { super.tearDown(); this.authenticationComponent.setCurrentUser(AuthenticationUtil.getAdminUserName()); - + } - + public void testPreferences() throws Exception { // Get the preferences before they have been set - + Response resp = sendRequest(new GetRequest(URL), 200); JSONObject jsonResult = new JSONObject(resp.getContentAsString()); - + assertNotNull(jsonResult); - assertFalse(jsonResult.keys().hasNext()); - + assertFalse(jsonResult.keys().hasNext()); + // Set some preferences - + JSONObject jsonObject = getPreferenceObj(); jsonObject.put("comp1", getPreferenceObj()); - + resp = sendRequest(new PostRequest(URL, jsonObject.toString(), "application/json"), 200); assertEquals(0, resp.getContentLength()); - + // Get the preferences - + resp = sendRequest(new GetRequest(URL), 200); jsonResult = new JSONObject(resp.getContentAsString()); assertNotNull(jsonResult); assertTrue(jsonResult.keys().hasNext()); - + checkJSONObject(jsonResult); checkJSONObject(jsonResult.getJSONObject("comp1")); - + // Update some of the preferences - + jsonObject.put("stringValue", "updated"); jsonObject.put("comp2", getPreferenceObj()); - + resp = sendRequest(new PostRequest(URL, jsonObject.toString(), "application/json"), 200); assertEquals(0, resp.getContentLength()); - + // Get the preferences - + resp = sendRequest(new GetRequest(URL), 200); jsonResult = new JSONObject(resp.getContentAsString()); assertNotNull(jsonResult); assertTrue(jsonResult.keys().hasNext()); - + jsonObject.put("stringValue", "updated"); jsonObject.put("numberValue", 10); jsonObject.put("numberValue2", 3.142); checkJSONObject(jsonResult.getJSONObject("comp1")); checkJSONObject(jsonResult.getJSONObject("comp2")); - + // Filter the preferences retrieved - + resp = sendRequest(new GetRequest(URL + "?pf=comp2"), 200); jsonResult = new JSONObject(resp.getContentAsString()); assertNotNull(jsonResult); assertTrue(jsonResult.keys().hasNext()); - + checkJSONObject(jsonResult.getJSONObject("comp2")); assertFalse(jsonResult.has("comp1")); assertFalse(jsonResult.has("stringValue")); - + // Clear some of the preferences sendRequest(new DeleteRequest(URL + "?pf=comp1"), 200); - + resp = sendRequest(new GetRequest(URL), 200); jsonResult = new JSONObject(resp.getContentAsString()); assertNotNull(jsonResult); assertTrue(jsonResult.keys().hasNext()); - + checkJSONObject(jsonResult.getJSONObject("comp2")); - assertFalse(jsonResult.has("comp1")); - + assertFalse(jsonResult.has("comp1")); + // Clear all the preferences sendRequest(new DeleteRequest(URL), 200); - + resp = sendRequest(new GetRequest(URL), 200); jsonResult = new JSONObject(resp.getContentAsString()); assertNotNull(jsonResult); assertFalse(jsonResult.keys().hasNext()); // Test trying to update another user's permissions - sendRequest(new PostRequest("/api/people/" + USER_BAD + "/preferences", jsonObject.toString(), "application/json"), 500); - + sendRequest(new PostRequest("/api/people/" + USER_BAD + "/preferences", jsonObject.toString(), + "application/json"), 401); + // Test error conditions sendRequest(new GetRequest("/api/people/noExistUser/preferences"), 404); } - - private JSONObject getPreferenceObj() - throws JSONException + + private JSONObject getPreferenceObj() throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put("stringValue", "value"); @@ -187,9 +192,8 @@ public class PreferenceServiceTest extends BaseWebScriptTest jsonObject.put("numberValue2", 3.142); return jsonObject; } - - private void checkJSONObject(JSONObject jsonObject) - throws JSONException + + private void checkJSONObject(JSONObject jsonObject) throws JSONException { assertEquals("value", jsonObject.get("stringValue")); assertEquals(10, jsonObject.get("numberValue"));