提交 168972a5 编写于 作者: F Frederik Heremans

Added user-info create/get/update to REST

上级 c9996023
......@@ -58,6 +58,7 @@ import org.activiti.rest.api.history.HistoricDetailResponse;
import org.activiti.rest.api.history.HistoricProcessInstanceResponse;
import org.activiti.rest.api.history.HistoricTaskInstanceResponse;
import org.activiti.rest.api.history.HistoricVariableInstanceResponse;
import org.activiti.rest.api.identity.UserInfoResponse;
import org.activiti.rest.api.identity.UserResponse;
import org.activiti.rest.api.legacy.identity.LegacyRestIdentityLink;
import org.activiti.rest.api.management.JobResponse;
......@@ -561,6 +562,14 @@ public class RestResponseFactory {
return response;
}
public UserInfoResponse createUserInfoResponse(SecuredResource securedResource, String key, String value, String userId) {
UserInfoResponse response = new UserInfoResponse();
response.setKey(key);
response.setValue(value);
response.setUrl(securedResource.createFullResourceUrl(RestUrls.URL_USER_INFO, userId, key));
return response;
}
/**
* Called once when the converters need to be initialized. Override of custom conversion
......@@ -575,5 +584,4 @@ public class RestResponseFactory {
variableConverters.add(new BooleanRestVariableConverter());
variableConverters.add(new DateRestVariableConverter());
}
}
......@@ -62,6 +62,7 @@ public final class RestUrls {
public static final String SEGMENT_USERS = "users";
public static final String SEGMENT_GROUPS = "groups";
public static final String SEGMENT_PICTURE = "picture";
public static final String SEGMENT_INFO = "info";
/**
* URL template for the deployment collection: <i>repository/deployments</i>
......@@ -356,6 +357,16 @@ public final class RestUrls {
* URL template for the picture for a single user: <i>identity/users/{0:userId}/picture</i>
*/
public static final String[] URL_USER_PICTURE = {SEGMENT_IDENTITY_RESOURCES, SEGMENT_USERS, "{0}", SEGMENT_PICTURE};
/**
* URL template for an info entry for a single user: <i>identity/users/{0:userId}/info/{1:key}</i>
*/
public static final String[] URL_USER_INFO = {SEGMENT_IDENTITY_RESOURCES, SEGMENT_USERS, "{0}", SEGMENT_INFO, "{1}"};
/**
* URL template for the info collection for a single user: <i>identity/users/{0:userId}/info</i>
*/
public static final String[] URL_USER_INFO_COLLECTION = {SEGMENT_IDENTITY_RESOURCES, SEGMENT_USERS, "{0}", SEGMENT_INFO};
/**
* Creates an url based on the passed fragments and replaces any placeholders with the given arguments. The
......
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.identity;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.identity.User;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.api.SecuredResource;
/**
* @author Frederik Heremans
*/
public class BaseUserResource extends SecuredResource {
protected User getUserFromRequest() {
String userId = getAttribute("userId");
if (userId == null) {
throw new ActivitiIllegalArgumentException("The userId cannot be null");
}
User user = ActivitiUtil.getIdentityService().createUserQuery().userId(userId).singleResult();
if (user == null) {
throw new ActivitiObjectNotFoundException("Could not find a user with id '" + userId + "'.", User.class);
}
return user;
}
}
......@@ -88,7 +88,7 @@ public class UserCollectionResource extends SecuredResource {
@Post
public UserResponse createUser(UserRequest request) {
if(request.getId() == null) {
throw new ActivitiIllegalArgumentException("The id for the new user cannot be null");
throw new ActivitiIllegalArgumentException("Id cannot be null.");
}
// Check if a user with the given ID already exists so we return a CONFLICT
......@@ -103,6 +103,8 @@ public class UserCollectionResource extends SecuredResource {
created.setPassword(request.getPassword());
ActivitiUtil.getIdentityService().saveUser(created);
setStatus(Status.SUCCESS_CREATED);
return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
.createUserResponse(this, created, true);
}
......
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.identity;
import java.util.ArrayList;
import java.util.List;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.identity.User;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.api.RestResponseFactory;
import org.activiti.rest.application.ActivitiRestServicesApplication;
import org.restlet.data.Status;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ResourceException;
/**
* @author Frederik Heremans
*/
public class UserInfoCollectionResource extends BaseUserResource {
@Get
public List<UserInfoResponse> getUserInfo() {
if(!authenticate())
return null;
User user = getUserFromRequest();
RestResponseFactory factory = getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory();
List<UserInfoResponse> responses = new ArrayList<UserInfoResponse>();
// Create responses for all keys,not including value as this is exposed through the individual resource URL.
for(String key : ActivitiUtil.getIdentityService().getUserInfoKeys(user.getId())) {
responses.add(factory.createUserInfoResponse(this, key, null, user.getId()));
}
return responses;
}
@Post
public UserInfoResponse setUserInfo(UserInfoRequest request) {
if(!authenticate())
return null;
User user = getUserFromRequest();
if(request.getKey() == null) {
throw new ActivitiIllegalArgumentException("The key cannot be null.");
}
if(request.getValue() == null) {
throw new ActivitiIllegalArgumentException("The value cannot be null.");
}
String existingValue = ActivitiUtil.getIdentityService().getUserInfo(user.getId(), request.getKey());
if(existingValue != null) {
throw new ResourceException(Status.CLIENT_ERROR_CONFLICT.getCode(), "User info with key '" + request.getKey() + "' already exists for this user.", null, null);
}
ActivitiUtil.getIdentityService().setUserInfo(user.getId(), request.getKey(), request.getValue());
setStatus(Status.SUCCESS_CREATED);
return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
.createUserInfoResponse(this, request.getKey(), request.getValue(), user.getId());
}
}
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.identity;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
/**
* @author Frederik Heremans
*/
public class UserInfoRequest {
protected String key;
protected String value;
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
@JsonSerialize(include=Inclusion.NON_NULL)
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.identity;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.identity.User;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.application.ActivitiRestServicesApplication;
import org.restlet.data.Status;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;
/**
* @author Frederik Heremans
*/
public class UserInfoResource extends BaseUserResource {
@Get
public UserInfoResponse getUserInfo() {
if(!authenticate())
return null;
User user = getUserFromRequest();
String key = getAttribute("key");
if(key == null) {
throw new ActivitiIllegalArgumentException("Key cannot be null.");
}
String existingValue = ActivitiUtil.getIdentityService().getUserInfo(user.getId(), key);
if(existingValue == null) {
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND.getCode(), "User info with key '" + key + "' does not exists for user '" + user.getId() + "'.", null, null);
}
return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
.createUserInfoResponse(this, key, existingValue, user.getId());
}
@Put
public UserInfoResponse setUserInfo(UserInfoRequest request) {
if(!authenticate())
return null;
User user = getUserFromRequest();
String key = getValidKeyFromRequest(user);
if(request.getValue() == null) {
throw new ActivitiIllegalArgumentException("The value cannot be null.");
}
if(request.getKey() == null || key.equals(request.getKey())) {
ActivitiUtil.getIdentityService().setUserInfo(user.getId(), key, request.getValue());
} else {
throw new ActivitiIllegalArgumentException("Key provided in request body doesn't match the key in the resource URL.");
}
return getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory()
.createUserInfoResponse(this, key, request.getValue(), user.getId());
}
@Delete
public void deleteUserInfo() {
User user = getUserFromRequest();
String key = getValidKeyFromRequest(user);
ActivitiUtil.getIdentityService().setUserInfo(user.getId(), key, null);
setStatus(Status.SUCCESS_NO_CONTENT);
}
protected String getValidKeyFromRequest(User user) {
String key = getAttribute("key");
if(key == null) {
throw new ActivitiIllegalArgumentException("Key cannot be null.");
}
String existingValue = ActivitiUtil.getIdentityService().getUserInfo(user.getId(), key);
if(existingValue == null) {
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND.getCode(), "User info with key '" + key + "' does not exists for user '" + user.getId() + "'.", null, null);
}
return key;
}
}
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.identity;
/**
* @author Frederik Heremans
*/
public class UserInfoResponse extends UserInfoRequest {
protected String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
......@@ -23,7 +23,6 @@ import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.identity.Picture;
import org.activiti.engine.identity.User;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.api.SecuredResource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
......@@ -40,7 +39,7 @@ import org.restlet.resource.ResourceException;
/**
* @author Frederik Heremans
*/
public class UserPictureResource extends SecuredResource {
public class UserPictureResource extends BaseUserResource {
@Get
public InputRepresentation getUserPicture() {
......@@ -105,18 +104,4 @@ public class UserPictureResource extends SecuredResource {
throw new ActivitiException("Error while reading uploaded file: " + e.getMessage(), e);
}
}
protected User getUserFromRequest() {
String userId = getAttribute("userId");
if (userId == null) {
throw new ActivitiIllegalArgumentException("The userId cannot be null");
}
User user = ActivitiUtil.getIdentityService().createUserQuery().userId(userId).singleResult();
if (user == null) {
throw new ActivitiObjectNotFoundException("Could not find a user with id '" + userId + "'.", User.class);
}
return user;
}
}
......@@ -13,6 +13,8 @@
package org.activiti.rest.api.identity;
import org.codehaus.jackson.annotate.JsonIgnore;
/**
* @author Frederik Heremans
......@@ -48,18 +50,19 @@ public class UserRequest extends UserResponse {
passwordChanged = true;
}
@JsonIgnore
public boolean isEmailChanged() {
return emailChanged;
}
@JsonIgnore
public boolean isFirstNameChanged() {
return firstNameChanged;
}
@JsonIgnore
public boolean isLastNameChanged() {
return lastNameChanged;
}
@JsonIgnore
public boolean isPasswordChanged() {
return passwordChanged;
}
......
......@@ -13,11 +13,8 @@
package org.activiti.rest.api.identity;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.identity.User;
import org.activiti.rest.api.ActivitiUtil;
import org.activiti.rest.api.SecuredResource;
import org.activiti.rest.application.ActivitiRestServicesApplication;
import org.restlet.data.Status;
import org.restlet.resource.Delete;
......@@ -27,7 +24,7 @@ import org.restlet.resource.Put;
/**
* @author Frederik Heremans
*/
public class UserResource extends SecuredResource {
public class UserResource extends BaseUserResource {
@Get
public UserResponse getUser() {
......@@ -67,19 +64,4 @@ public class UserResource extends SecuredResource {
ActivitiUtil.getIdentityService().deleteUser(user.getId());
setStatus(Status.SUCCESS_NO_CONTENT);
}
protected User getUserFromRequest() {
String userId = getAttribute("userId");
if (userId == null) {
throw new ActivitiIllegalArgumentException("The userId cannot be null");
}
User user = ActivitiUtil.getIdentityService().createUserQuery().userId(userId).singleResult();
if (user == null) {
throw new ActivitiObjectNotFoundException("Could not find a user with id '" + userId + "'.", User.class);
}
return user;
}
}
......@@ -16,6 +16,8 @@ import org.activiti.rest.api.history.HistoricTaskInstanceResource;
import org.activiti.rest.api.history.HistoricVariableInstanceCollectionResource;
import org.activiti.rest.api.history.HistoricVariableInstanceQueryResource;
import org.activiti.rest.api.identity.UserCollectionResource;
import org.activiti.rest.api.identity.UserInfoCollectionResource;
import org.activiti.rest.api.identity.UserInfoResource;
import org.activiti.rest.api.identity.UserPictureResource;
import org.activiti.rest.api.identity.UserResource;
import org.activiti.rest.api.legacy.LegacyTaskAttachmentResource;
......@@ -171,6 +173,8 @@ public class RestServicesInit {
router.attach("/identity/users", UserCollectionResource.class);
router.attach("/identity/users/{userId}", UserResource.class);
router.attach("/identity/users/{userId}/picture", UserPictureResource.class);
router.attach("/identity/users/{userId}/info/{key}", UserInfoResource.class);
router.attach("/identity/users/{userId}/info", UserInfoCollectionResource.class);
router.attach("/query/tasks", TaskQueryResource.class);
......
......@@ -20,6 +20,12 @@ import org.activiti.engine.identity.User;
import org.activiti.engine.test.Deployment;
import org.activiti.rest.BaseRestTestCase;
import org.activiti.rest.api.RestUrls;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
/**
......@@ -106,4 +112,67 @@ public class UserCollectionResourceTest extends BaseRestTestCase {
}
}
}
public void testCreateUser() throws Exception {
try {
ObjectNode requestNode = objectMapper.createObjectNode();
requestNode.put("id", "testuser");
requestNode.put("firstName", "Frederik");
requestNode.put("lastName", "Heremans");
requestNode.put("email", "no-reply@activiti.org");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_COLLECTION, "testuser"));
Representation response = client.post(requestNode);
assertEquals(Status.SUCCESS_CREATED, client.getResponse().getStatus());
JsonNode responseNode = objectMapper.readTree(response.getStream());
assertNotNull(responseNode);
assertEquals("testuser", responseNode.get("id").getTextValue());
assertEquals("Frederik", responseNode.get("firstName").getTextValue());
assertEquals("Heremans", responseNode.get("lastName").getTextValue());
assertEquals("no-reply@activiti.org", responseNode.get("email").getTextValue());
assertTrue(responseNode.get("url").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, "testuser")));
} finally {
try {
identityService.deleteUser("testuser");
} catch(Throwable t) {
// Ignore, user might not have been created by test
}
}
}
public void testCreateUserExceptions() throws Exception {
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_COLLECTION, "unexisting"));
// Create without ID
ObjectNode requestNode = objectMapper.createObjectNode();
requestNode.put("firstName", "Frederik");
requestNode.put("lastName", "Heremans");
requestNode.put("email", "no-reply@activiti.org");
try {
client.post(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, expected.getStatus());
assertEquals("Id cannot be null.", expected.getStatus().getDescription());
}
// Create when user already exists
// Create without ID
requestNode = objectMapper.createObjectNode();
requestNode.put("id", "kermit");
requestNode.put("firstName", "Frederik");
requestNode.put("lastName", "Heremans");
requestNode.put("email", "no-reply@activiti.org");
try {
client.post(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_CONFLICT, expected.getStatus());
assertEquals("A user with id 'kermit' already exists.", expected.getStatus().getDescription());
}
}
}
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest.api.identity;
import org.activiti.engine.identity.User;
import org.activiti.rest.BaseRestTestCase;
import org.activiti.rest.api.RestUrls;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
/**
* @author Frederik Heremans
*/
public class UserInfoResourceTest extends BaseRestTestCase {
/**
* Test getting the collection of info for a user.
*/
public void testGetUserInfoCollection() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
identityService.setUserInfo(newUser.getId(), "key1", "Value 1");
identityService.setUserInfo(newUser.getId(), "key2", "Value 2");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO_COLLECTION,
newUser.getId()));
Representation response = client.get();
assertEquals(Status.SUCCESS_OK, client.getResponse().getStatus());
JsonNode responseNode = objectMapper.readTree(response.getStream());
assertNotNull(responseNode);
assertTrue(responseNode.isArray());
assertEquals(2, responseNode.size());
boolean foundFirst = false;
boolean foundSecond = false;
for(int i=0; i<responseNode.size(); i++) {
ObjectNode info = (ObjectNode) responseNode.get(i);
assertNotNull(info.get("key").getTextValue());
assertNotNull(info.get("url").getTextValue());
if(info.get("key").getTextValue().equals("key1")) {
foundFirst = true;
assertTrue(info.get("url").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_USER_INFO, newUser.getId(), "key1")));
} else if(info.get("key").getTextValue().equals("key2")) {
assertTrue(info.get("url").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_USER_INFO, newUser.getId(), "key2")));
foundSecond = true;
}
}
assertTrue(foundFirst);
assertTrue(foundSecond);
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
/**
* Test getting info for a user.
*/
public void testGetUserInfo() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
identityService.setUserInfo(newUser.getId(), "key1", "Value 1");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO,
newUser.getId(), "key1"));
Representation response = client.get();
assertEquals(Status.SUCCESS_OK, client.getResponse().getStatus());
JsonNode responseNode = objectMapper.readTree(response.getStream());
assertEquals("key1", responseNode.get("key").getTextValue());
assertEquals("Value 1", responseNode.get("value").getTextValue());
assertTrue(responseNode.get("url").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_USER_INFO, newUser.getId(), "key1")));
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
/**
* Test getting the info for an unexisting user.
*/
public void testGetInfoForUnexistingUser() throws Exception {
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO, "unexisting", "key1"));
try {
client.get();
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a user with id 'unexisting'.", expected.getStatus().getDescription());
}
}
/**
* Test getting the info for a user who doesn't have that info set
*/
public void testGetInfoForUserWithoutInfo() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO, "testuser", "key1"));
try {
client.get();
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("User info with key 'key1' does not exists for user 'testuser'.", expected.getStatus().getDescription());
}
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
/**
* Test deleting info for a user.
*/
public void testDeleteUserInfo() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
identityService.setUserInfo(newUser.getId(), "key1", "Value 1");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO,
newUser.getId(), "key1"));
Representation response = client.delete();
assertEquals(Status.SUCCESS_NO_CONTENT, client.getResponse().getStatus());
assertEquals(0, response.getSize());
// Check if info is actually deleted
assertNull(identityService.getUserInfo(newUser.getId(), "key1"));
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
/**
* Test update info for a user.
*/
public void testUpdateUserInfo() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
identityService.setUserInfo(newUser.getId(), "key1", "Value 1");
ObjectNode requestNode = objectMapper.createObjectNode();
requestNode.put("value", "Updated value");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO,
newUser.getId(), "key1"));
Representation response = client.put(requestNode);
assertEquals(Status.SUCCESS_OK, client.getResponse().getStatus());
JsonNode responseNode = objectMapper.readTree(response.getStream());
assertEquals("key1", responseNode.get("key").getTextValue());
assertEquals("Updated value", responseNode.get("value").getTextValue());
assertTrue(responseNode.get("url").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_USER_INFO, newUser.getId(), "key1")));
// Check if info is actually updated
assertEquals("Updated value", identityService.getUserInfo(newUser.getId(), "key1"));
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
/**
* Test update the info for an unexisting user.
*/
public void testUpdateInfoForUnexistingUser() throws Exception {
ObjectNode requestNode = objectMapper.createObjectNode();
requestNode.put("value", "Updated value");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO, "unexisting", "key1"));
try {
client.put(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a user with id 'unexisting'.", expected.getStatus().getDescription());
}
}
/**
* Test deleting the info for a user who doesn't have that info set
*/
public void testUpdateUnexistingInfo() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
ObjectNode requestNode = objectMapper.createObjectNode();
requestNode.put("value", "Updated value");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO, "testuser", "key1"));
try {
client.put(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("User info with key 'key1' does not exists for user 'testuser'.", expected.getStatus().getDescription());
}
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
/**
* Test deleting the info for an unexisting user.
*/
public void testDeleteInfoForUnexistingUser() throws Exception {
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO, "unexisting", "key1"));
try {
client.delete();
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a user with id 'unexisting'.", expected.getStatus().getDescription());
}
}
/**
* Test deleting the info for a user who doesn't have that info set
*/
public void testDeleteInfoForUserWithoutInfo() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO, "testuser", "key1"));
try {
client.delete();
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("User info with key 'key1' does not exists for user 'testuser'.", expected.getStatus().getDescription());
}
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
public void testCreateUserInfo() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
ObjectNode requestNode = objectMapper.createObjectNode();
requestNode.put("value", "Value 1");
requestNode.put("key", "key1");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO_COLLECTION, "testuser"));
Representation response = client.post(requestNode);
assertEquals(Status.SUCCESS_CREATED, client.getResponse().getStatus());
JsonNode responseNode = objectMapper.readTree(response.getStream());
assertEquals("key1", responseNode.get("key").getTextValue());
assertEquals("Value 1", responseNode.get("value").getTextValue());
assertTrue(responseNode.get("url").getTextValue().endsWith(RestUrls.createRelativeResourceUrl(
RestUrls.URL_USER_INFO, newUser.getId(), "key1")));
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
public void testCreateUserInfoExceptions() throws Exception {
User savedUser = null;
try {
User newUser = identityService.newUser("testuser");
newUser.setFirstName("Fred");
newUser.setLastName("McDonald");
newUser.setEmail("no-reply@activiti.org");
identityService.saveUser(newUser);
savedUser = newUser;
// Test creating without value
ObjectNode requestNode = objectMapper.createObjectNode();
requestNode.put("key", "key1");
ClientResource client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO_COLLECTION, "testuser"));
try {
client.post(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, expected.getStatus());
assertEquals("The value cannot be null.", expected.getStatus().getDescription());
}
client.release();
// Test creating without key
requestNode = objectMapper.createObjectNode();
requestNode.put("value", "The value");
try {
client.post(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, expected.getStatus());
assertEquals("The key cannot be null.", expected.getStatus().getDescription());
}
client.release();
// Test creating an already existing info
identityService.setUserInfo(newUser.getId(), "key1", "The value");
requestNode = objectMapper.createObjectNode();
requestNode.put("key", "key1");
requestNode.put("value", "The value");
try {
client.post(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_CONFLICT, expected.getStatus());
assertEquals("User info with key 'key1' already exists for this user.", expected.getStatus().getDescription());
}
client.release();
// Test creating info for unexisting user
client = getAuthenticatedClient(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER_INFO_COLLECTION, "unexistinguser"));
try {
client.post(requestNode);
fail("Exception expected");
} catch(ResourceException expected) {
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, expected.getStatus());
assertEquals("Could not find a user with id 'unexistinguser'.", expected.getStatus().getDescription());
}
} finally {
// Delete user after test passes or fails
if(savedUser != null) {
identityService.deleteUser(savedUser.getId());
}
}
}
}
......@@ -4618,7 +4618,52 @@ Only the attachment name is required to create a new attachment.
</table>
</para>
<para>
<emphasis role="bold">Success response body:</emphasis> see response for <literal>runtime/tasks/{taskId}</literal>.
<emphasis role="bold">Success response body:</emphasis> see response for <literal>identity/users/{userId}</literal>.
</para>
</section>
<section>
<title>Create a user</title>
<para>
<programlisting>POST identity/users/</programlisting>
</para>
<para>
<emphasis role="bold">Body JSON:</emphasis>
<programlisting>
{
"id":"tijs",
"firstName":"Tijs",
"lastName":"Barrez",
"url":"http://localhost:8182/identity/users/anotherUser",
"email":"no-reply@alfresco.org"
}</programlisting>
All request values are optional. For example, you can only include the 'firstName' attribute in the request body JSON-object, only updating the firstName of the user, leaving all other fields unaffected. When an attribute is explicitly included and is set to null, the user-value will be updated to null. Example: <literal>{"firstName" : null}</literal> will clear the firstName of the user).
</para>
<para>
<table>
<title>Response codes</title>
<tgroup cols='2'>
<thead>
<row>
<entry>Response code</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>201</entry>
<entry>Indicates the user was created.</entry>
</row>
<row>
<entry>400</entry>
<entry>Indicates the id of the user was missing.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
<emphasis role="bold">Success response body:</emphasis> see response for <literal>identity/users/{userId}</literal>.
</para>
</section>
......@@ -4674,7 +4719,7 @@ Only the attachment name is required to create a new attachment.
</table>
</para>
</section>
<section>
<section>
<title>Get a user's picture</title>
<para>
<programlisting>GET identity/users/{userId}/picture</programlisting>
......@@ -4791,6 +4836,348 @@ Only the attachment name is required to create a new attachment.
</para>
</section>
<section>
<title>List a user's info</title>
<para>
<programlisting>GET identity/users/{userId}/info</programlisting>
</para>
<para>
<table>
<title>URL parameters</title>
<tgroup cols='3'>
<thead>
<row>
<entry>Parameter</entry>
<entry>Required</entry>
<entry>Value</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>userId</entry>
<entry>Yes</entry>
<entry>String</entry>
<entry>The id of the user to get the info for.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
<emphasis role="bold">Response Body:</emphasis>
<programlisting>
[
{
"key":"key1",
"url":"http://localhost:8182/identity/users/testuser/info/key1"
},
{
"key":"key2",
"url":"http://localhost:8182/identity/users/testuser/info/key2"
}
]</programlisting>
<table>
<title>Response codes</title>
<tgroup cols='2'>
<thead>
<row>
<entry>Response code</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>200</entry>
<entry>Indicates the user was found and list of info (key and url) is returned.</entry>
</row>
<row>
<entry>404</entry>
<entry>Indicates the requested user was not found.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
<section>
<title>Get a user's info</title>
<para>
<programlisting>GET identity/users/{userId}/info/{key}</programlisting>
</para>
<para>
<table>
<title>URL parameters</title>
<tgroup cols='3'>
<thead>
<row>
<entry>Parameter</entry>
<entry>Required</entry>
<entry>Value</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>userId</entry>
<entry>Yes</entry>
<entry>String</entry>
<entry>The id of the user to get the info for.</entry>
</row>
<row>
<entry>key</entry>
<entry>Yes</entry>
<entry>String</entry>
<entry>The key of the user info to get.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
<emphasis role="bold">Response Body:</emphasis>
<programlisting>
{
"key":"key1",
"value":"Value 1",
"url":"http://localhost:8182/identity/users/testuser/info/key1"
}</programlisting>
<table>
<title>Response codes</title>
<tgroup cols='2'>
<thead>
<row>
<entry>Response code</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>200</entry>
<entry>Indicates the user was found and the user has info for the given key..</entry>
</row>
<row>
<entry>404</entry>
<entry>Indicates the requested user was not found or the user doesn't have info for the given key. Status description contains additional information about the error.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
<section>
<title>Update a user's info</title>
<para>
<programlisting>PUT identity/users/{userId}/info/{key}</programlisting>
</para>
<para>
<table>
<title>URL parameters</title>
<tgroup cols='3'>
<thead>
<row>
<entry>Parameter</entry>
<entry>Required</entry>
<entry>Value</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>userId</entry>
<entry>Yes</entry>
<entry>String</entry>
<entry>The id of the user to update the info for.</entry>
</row>
<row>
<entry>key</entry>
<entry>Yes</entry>
<entry>String</entry>
<entry>The key of the user info to update.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
<emphasis role="bold">Request Body:</emphasis>
<programlisting>
{
"value":"The updated value"
}</programlisting>
</para>
<para>
<emphasis role="bold">Response Body:</emphasis>
<programlisting>
{
"key":"key1",
"value":"The updated value",
"url":"http://localhost:8182/identity/users/testuser/info/key1"
}</programlisting>
<table>
<title>Response codes</title>
<tgroup cols='2'>
<thead>
<row>
<entry>Response code</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>200</entry>
<entry>Indicates the user was found and the info has been updated.</entry>
</row>
<row>
<entry>400</entry>
<entry>Indicates the value was missing from the request body.</entry>
</row>
<row>
<entry>404</entry>
<entry>Indicates the requested user was not found or the user doesn't have info for the given key. Status description contains additional information about the error.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
<section>
<title>Create a new user's info entry</title>
<para>
<programlisting>POST identity/users/{userId}/info</programlisting>
</para>
<para>
<table>
<title>URL parameters</title>
<tgroup cols='3'>
<thead>
<row>
<entry>Parameter</entry>
<entry>Required</entry>
<entry>Value</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>userId</entry>
<entry>Yes</entry>
<entry>String</entry>
<entry>The id of the user to create the info for.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
<emphasis role="bold">Request Body:</emphasis>
<programlisting>
{
"key":"key1",
"value":"The value"
}</programlisting>
</para>
<para>
<emphasis role="bold">Response Body:</emphasis>
<programlisting>
{
"key":"key1",
"value":"The value",
"url":"http://localhost:8182/identity/users/testuser/info/key1"
}</programlisting>
<table>
<title>Response codes</title>
<tgroup cols='2'>
<thead>
<row>
<entry>Response code</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>201</entry>
<entry>Indicates the user was found and the info has been created.</entry>
</row>
<row>
<entry>400</entry>
<entry>Indicates the key or value was missing from the request body. Status description contains additional information about the error.</entry>
</row>
<row>
<entry>404</entry>
<entry>Indicates the requested user was not found.</entry>
</row>
<row>
<entry>409</entry>
<entry>Indicates there is already an info-entry with the given key for the user, update the resource instance (<literal>PUT</literal>).</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
<section>
<title>Delete a user's info</title>
<para>
<programlisting>DELETE identity/users/{userId}/info/{key}</programlisting>
</para>
<para>
<table>
<title>URL parameters</title>
<tgroup cols='3'>
<thead>
<row>
<entry>Parameter</entry>
<entry>Required</entry>
<entry>Value</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>userId</entry>
<entry>Yes</entry>
<entry>String</entry>
<entry>The id of the user to delete the info for.</entry>
</row>
<row>
<entry>key</entry>
<entry>Yes</entry>
<entry>String</entry>
<entry>The key of the user info to delete.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
<table>
<title>Response codes</title>
<tgroup cols='2'>
<thead>
<row>
<entry>Response code</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>204</entry>
<entry>Indicates the user was found and the info for the given key has been deleted. Response body is left empty intentionally.</entry>
</row>
<row>
<entry>404</entry>
<entry>Indicates the requested user was not found or the user doesn't have info for the given key. Status description contains additional information about the error.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
</section>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册