Search in sources :

Example 6 with PatchOperation

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));
}
Also used : Response(javax.ws.rs.core.Response) Member(io.jans.scim.model.scim2.group.Member) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Set(java.util.Set) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest) Collectors(java.util.stream.Collectors) ScimResourceUtil(io.jans.scim.model.scim2.util.ScimResourceUtil) ArrayList(java.util.ArrayList) GroupResource(io.jans.scim.model.scim2.group.GroupResource) List(java.util.List) Response(javax.ws.rs.core.Response) Assert(org.testng.Assert) Parameters(org.testng.annotations.Parameters) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) io.jans.scim.model.scim2(io.jans.scim.model.scim2) Collections(java.util.Collections) Status(javax.ws.rs.core.Response.Status) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) ArrayList(java.util.ArrayList) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Member(io.jans.scim.model.scim2.group.Member) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 7 with PatchOperation

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());
}
Also used : Response(javax.ws.rs.core.Response) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Member(io.jans.scim.model.scim2.group.Member) GroupResource(io.jans.scim.model.scim2.group.GroupResource) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 8 with PatchOperation

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());
}
Also used : Response(javax.ws.rs.core.Response) UserBaseTest(io.jans.scim2.client.UserBaseTest) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Email(io.jans.scim.model.scim2.user.Email) Address(io.jans.scim.model.scim2.user.Address) PhoneNumber(io.jans.scim.model.scim2.user.PhoneNumber) Test(org.testng.annotations.Test) ArrayList(java.util.ArrayList) Response(javax.ws.rs.core.Response) Assert(org.testng.Assert) Parameters(org.testng.annotations.Parameters) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) Collections(java.util.Collections) Status(javax.ws.rs.core.Response.Status) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Example 9 with PatchOperation

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;
}
Also used : ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) Response(javax.ws.rs.core.Response) ListViewResponse(org.gluu.persist.model.ListViewResponse) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) UserResource(org.gluu.oxtrust.model.scim2.user.UserResource) PatchOperation(org.gluu.oxtrust.model.scim2.patch.PatchOperation) URI(java.net.URI) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Path(javax.ws.rs.Path) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) RefAdjusted(org.gluu.oxtrust.service.scim2.interceptor.RefAdjusted) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ProtectedApi(org.gluu.oxtrust.service.filter.ProtectedApi)

Example 10 with PatchOperation

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());
}
Also used : Response(javax.ws.rs.core.Response) CustomAttributes(io.jans.scim.model.scim2.CustomAttributes) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Aggregations

Response (javax.ws.rs.core.Response)13 PatchOperation (io.jans.scim.model.scim2.patch.PatchOperation)10 PatchRequest (io.jans.scim.model.scim2.patch.PatchRequest)8 Test (org.testng.annotations.Test)7 UserResource (io.jans.scim.model.scim2.user.UserResource)6 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)5 SCIMException (io.jans.scim.model.exception.SCIMException)4 BaseTest (io.jans.scim2.client.BaseTest)4 URI (java.net.URI)4 Consumes (javax.ws.rs.Consumes)4 DefaultValue (javax.ws.rs.DefaultValue)4 HeaderParam (javax.ws.rs.HeaderParam)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 GroupResource (io.jans.scim.model.scim2.group.GroupResource)3 ArrayList (java.util.ArrayList)3 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 DuplicateEntryException (io.jans.orm.exception.operation.DuplicateEntryException)2