use of io.jans.scim.model.scim2.patch.PatchOperation in project jans by JanssenProject.
the class PatchGroupTest method patch2.
@Test(dependsOnMethods = "patch1")
public void patch2() throws Exception {
List<UserResource> users = getTestUsers("aaa");
assertTrue(users.size() > 0);
// Define one "add" operation to insert the users retrieved in the created group
PatchOperation operation = new PatchOperation();
operation.setOperation("add");
operation.setPath("members");
List<Member> memberList = new ArrayList<>();
users.stream().forEach(u -> {
Member m = new Member();
m.setType(ScimResourceUtil.getType(usrClass));
m.setValue(u.getId());
m.setDisplay(u.getDisplayName());
m.setRef("/scim/v2/Users/" + u.getId());
memberList.add(m);
});
operation.setValue(memberList);
// Apply the patch to the group
PatchRequest pr = new PatchRequest();
pr.setOperations(Collections.singletonList(operation));
Response response = client.patchGroup(pr, group.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
group = response.readEntity(groupCls);
// Verify the new users are there
Set<Member> members = group.getMembers();
assertNotNull(members);
assertTrue(members.stream().allMatch(m -> m.getDisplay() != null && m.getType() != null && m.getValue() != null && m.getRef() != null));
// Verify the Ids are the same (both provided and returned)
Set<String> userIds = users.stream().map(UserResource::getId).collect(Collectors.toSet());
assertTrue(members.stream().map(Member::getValue).collect(Collectors.toSet()).equals(userIds));
}
use of io.jans.scim.model.scim2.patch.PatchOperation in project jans by JanssenProject.
the class PatchGroupTest method patch3.
@Test(dependsOnMethods = "patch2")
public void patch3() {
Member[] members = group.getMembers().toArray(new Member[0]);
// Try modifying one of the members. This should fail because of mutability
PatchOperation operation = new PatchOperation();
operation.setOperation("replace");
operation.setPath(String.format("members[value eq \"%s\"].value", members[0].getValue()));
operation.setValue(members[1].getValue());
PatchRequest pr = new PatchRequest();
pr.setOperations(Collections.singletonList(operation));
Response response = client.patchGroup(pr, group.getId(), null, null);
assertEquals(response.getStatus(), BAD_REQUEST.getStatusCode());
// Try modifying one of the members. This should not fail ...
operation.setValue(members[0].getValue());
response = client.patchGroup(pr, group.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
// Try deleting value subattribute. This should fail ...
operation.setOperation("remove");
response = client.patchGroup(pr, group.getId(), null, null);
assertEquals(response.getStatus(), BAD_REQUEST.getStatusCode());
// Try removing one of the members. This should not fail ...
operation.setPath(String.format("members[value eq \"%s\"]", members[0].getValue()));
response = client.patchGroup(pr, group.getId(), null, null);
group = response.readEntity(GroupResource.class);
assertEquals(response.getStatus(), OK.getStatusCode());
assertEquals(members.length - 1, group.getMembers().size());
}
use of io.jans.scim.model.scim2.patch.PatchOperation in project jans by JanssenProject.
the class PatchValueFilterUserTest method objectPatch.
@Test(dependsOnMethods = "patch")
public void objectPatch() {
PatchRequest request = new PatchRequest();
request.setOperations(new ArrayList<>());
PatchOperation del = new PatchOperation();
del.setOperation("remove");
del.setPath("emails[type sw \"hobby\"]");
request.getOperations().add(del);
del = new PatchOperation();
del.setOperation("remove");
del.setPath("phoneNumbers[primary pr or value co \" \"].type");
request.getOperations().add(del);
del = new PatchOperation();
del.setOperation("remove");
del.setPath("addresses[region eq \"somewhere\" and primary ne true].locality");
request.getOperations().add(del);
Response response = client.patchUser(request, user.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
user = response.readEntity(usrClass);
assertNull(user.getEmails());
assertTrue(user.getPhoneNumbers().stream().allMatch(ph -> ph.getType() == null));
// No change in addresses
assertEquals(user.getAddresses().size(), 1);
assertNull(user.getAddresses().get(0).getLocality());
}
use of io.jans.scim.model.scim2.patch.PatchOperation in project oxTrust by GluuFederation.
the class UserWebService method patchUser.
@Path("{id}")
@PATCH
@Consumes({ MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@Produces({ MEDIA_TYPE_SCIM_JSON + UTF8_CHARSET_FRAGMENT, MediaType.APPLICATION_JSON + UTF8_CHARSET_FRAGMENT })
@HeaderParam("Accept")
@DefaultValue(MEDIA_TYPE_SCIM_JSON)
@ProtectedApi
@RefAdjusted
@ApiOperation(value = "PATCH operation", notes = "https://tools.ietf.org/html/rfc7644#section-3.5.2", response = UserResource.class)
public Response patchUser(PatchRequest request, @PathParam("id") String id, @QueryParam(QUERY_PARAM_ATTRIBUTES) String attrsList, @QueryParam(QUERY_PARAM_EXCLUDED_ATTRS) String excludedAttrsList) {
Response response;
try {
log.debug("Executing web service method. patchUser");
UserResource user = new UserResource();
// person is not null (check associated decorator method)
GluuCustomPerson person = personService.getPersonByInum(id);
// Fill user instance with all info from person
scim2UserService.transferAttributesToUserResource(person, user, endpointUrl);
// Apply patches one by one in sequence
for (PatchOperation po : request.getOperations()) {
// Handle special case: https://github.com/GluuFederation/oxTrust/issues/800
if (po.getType().equals(REMOVE) && po.getPath().equals("pairwiseIdentitifers")) {
// If this block weren't here, the implementation will throw error because read-only attribute cannot be altered
// Note the path is intentionally mistyped, see class member in UserResource
person.setOxPPID(null);
user.setPairwiseIdentitifers(null);
scim2UserService.removePPIDsBranch(person.getDn());
} else
user = (UserResource) scim2PatchService.applyPatchOperation(user, po);
}
// Throws exception if final representation does not pass overall validation
log.debug("patchUser. Revising final resource representation still passes validations");
executeDefaultValidation(user);
ScimResourceUtil.adjustPrimarySubAttributes(user);
// Update timestamp
String now = ISODateTimeFormat.dateTime().withZoneUTC().print(System.currentTimeMillis());
user.getMeta().setLastModified(now);
// Replaces the information found in person with the contents of user
scim2UserService.replacePersonInfo(person, user, endpointUrl);
String json = resourceSerializer.serialize(user, attrsList, excludedAttrsList);
response = Response.ok(new URI(user.getMeta().getLocation())).entity(json).build();
} catch (InvalidAttributeValueException e) {
log.error(e.getMessage(), e);
response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.MUTABILITY, e.getMessage());
} catch (SCIMException e) {
response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_SYNTAX, e.getMessage());
} catch (Exception e) {
log.error("Failure at patchUser method", e);
response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
}
return response;
}
use of io.jans.scim.model.scim2.patch.PatchOperation in project jans by JanssenProject.
the class PatchUserExtTest method patchObject.
@Test(dependsOnMethods = "patchJson")
public void patchObject() {
PatchOperation operation = new PatchOperation();
operation.setOperation("replace");
operation.setPath("urn:ietf:params:scim:schemas:extension:gluu:2.0:User:scimCustomSecond");
long now = System.currentTimeMillis();
List<String> someDates = Arrays.asList(now, now + 1000, now + 2000, now + 3000).stream().map(DateUtil::millisToISOString).collect(Collectors.toList());
operation.setValue(someDates);
PatchRequest pr = new PatchRequest();
pr.setOperations(Collections.singletonList(operation));
Response response = client.patchUser(pr, user.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
UserResource other = response.readEntity(usrClass);
CustomAttributes custAttrs = other.getCustomAttributes(USER_EXT_SCHEMA_ID);
// Verify different dates appeared in scimCustomSecond
List<Date> scimCustomSecond = custAttrs.getValues("scimCustomSecond", Date.class);
assertEquals(scimCustomSecond.size(), someDates.size());
assertEquals(now, scimCustomSecond.get(0).getTime());
}
Aggregations