Search in sources :

Example 16 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

the class PatchReplaceUserTest method jsonNoPathPatch2.

@Parameters({ "user_patchreplace_2" })
@Test(dependsOnMethods = "jsonNoPathPatch1")
public void jsonNoPathPatch2(String patchRequest) {
    Response response = client.patchUser(patchRequest, user.getId(), null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    UserResource other = response.readEntity(usrClass);
    assertNotEquals(user.getName().getHonorificPrefix(), other.getName().getHonorificPrefix());
    assertNotEquals(user.getName().getHonorificSuffix(), other.getName().getHonorificSuffix());
    assertNotEquals(user.getName().getFormatted(), other.getName().getFormatted());
    // Verify change in the streetAddress
    Address adr = other.getAddresses().get(0);
    assertNotEquals(user.getAddresses().get(0).getStreetAddress(), adr.getStreetAddress());
    // Verify other attributes were nulled
    assertNull(adr.getPostalCode());
    assertNull(adr.getRegion());
    // Verify change in number of phone numbers
    assertNotEquals(user.getPhoneNumbers().size(), other.getPhoneNumbers().size());
    // Verify new user has different phone numbers
    String phone = user.getPhoneNumbers().get(0).getValue();
    assertTrue(other.getPhoneNumbers().stream().map(PhoneNumber::getValue).noneMatch(phone::equals));
    // Verify x509Certs disappeared
    assertNull(other.getX509Certificates());
    // Verify "roles" are still there intact
    assertEquals(user.getRoles().size(), other.getRoles().size());
    assertEquals(user.getRoles().get(0).getValue(), other.getRoles().get(0).getValue());
    user = other;
}
Also used : Response(javax.ws.rs.core.Response) Address(io.jans.scim.model.scim2.user.Address) InstantMessagingAddress(io.jans.scim.model.scim2.user.InstantMessagingAddress) UserResource(io.jans.scim.model.scim2.user.UserResource) PhoneNumber(io.jans.scim.model.scim2.user.PhoneNumber) Parameters(org.testng.annotations.Parameters) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Example 17 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

the class GroupsBulkTest method bulkObject.

@Test(dependsOnMethods = "bulkJson")
public void bulkObject() {
    logger.info("Sending a bulk with a patch to insert admin user into group");
    // Creates a patch request consisting of adding the admin user to the group created
    PatchOperation po = new PatchOperation();
    po.setOperation("add");
    po.setPath("members.value");
    po.setValue(getAdminId());
    PatchRequest pr = new PatchRequest();
    pr.setOperations(Collections.singletonList(po));
    // Creates the bulk operation associated to the patch request
    BulkOperation bop = new BulkOperation();
    bop.setMethod("PATCH");
    bop.setPath("/Groups/" + groupId);
    bop.setData(mapper.convertValue(pr, new TypeReference<Map<String, Object>>() {
    }));
    BulkRequest breq = new BulkRequest();
    breq.setOperations(Collections.singletonList(bop));
    // Send bulk and check success of processing
    Response response = client.processBulkOperations(breq);
    assertEquals(response.getStatus(), Status.OK.getStatusCode());
    BulkResponse bres = response.readEntity(BulkResponse.class);
    assertSuccessfulOps(bres.getOperations());
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) BulkOperation(io.jans.scim.model.scim2.bulk.BulkOperation) BulkRequest(io.jans.scim.model.scim2.bulk.BulkRequest) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 18 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

the class PairwiseIdentifiersTest method queryAndRemoval.

@Test
public void queryAndRemoval() throws Exception {
    // Get a list (of at most 1 user) who has a persisted pairwise identifier
    Response response = client.searchUsers("pairwiseIdentifiers pr", null, 1, null, null, "pairwiseIdentifiers, id", null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    ListResponse lr = response.readEntity(ListResponse.class);
    // If the list is empty do nothing (successful test)
    if (lr.getItemsPerPage() > 0) {
        UserResource user = (UserResource) lr.getResources().get(0);
        assertNotNull(user.getPairwiseIdentifiers());
        // Prepare the removal of the user's PPIDs
        PatchOperation operation = new PatchOperation();
        operation.setOperation("remove");
        operation.setPath("pairwiseIdentitifers");
        PatchRequest pr = new PatchRequest();
        pr.setOperations(Collections.singletonList(operation));
        response = client.patchUser(pr, user.getId(), "pairwiseIdentifiers", null);
        assertEquals(response.getStatus(), OK.getStatusCode());
        // Ensure they are not there anymore.
        user = response.readEntity(UserResource.class);
        assertNull(user.getPairwiseIdentifiers());
    // This test does not guarantee the ou=pairwiseIdentifiers sub-branch disappears... only the jsPPID LDAP attribute
    }
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) ListResponse(io.jans.scim.model.scim2.ListResponse) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 19 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

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(scopes = { "https://jans.io/scim/users.write" })
@RefAdjusted
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");
        response = inspectPatchRequest(request, UserResource.class);
        if (response != null)
            return response;
        ScimCustomPerson person = userPersistenceHelper.getPersonByInum(id);
        if (person == null)
            return notFoundResponse(id, userResourceType);
        response = externalConstraintsService.applyEntityCheck(person, request, httpHeaders, uriInfo, HttpMethod.PATCH, userResourceType);
        if (response != null)
            return response;
        UserResource user = new UserResource();
        // 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("pairwiseIdentifiers")) {
                // If this block weren't here, the implementation will throw error because read-only attribute cannot be altered
                person.setPpid(null);
                user.setPairwiseIdentifiers(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");
        executeValidation(user);
        ScimResourceUtil.adjustPrimarySubAttributes(user);
        // Update timestamp
        user.getMeta().setLastModified(DateUtil.millisToISOString(System.currentTimeMillis()));
        // 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 : Response(javax.ws.rs.core.Response) SCIMException(io.jans.scim.model.exception.SCIMException) ScimCustomPerson(io.jans.scim.model.scim.ScimCustomPerson) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) URI(java.net.URI) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) URISyntaxException(java.net.URISyntaxException) SCIMException(io.jans.scim.model.exception.SCIMException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Path(javax.ws.rs.Path) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) RefAdjusted(io.jans.scim.service.scim2.interceptor.RefAdjusted) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ProtectedApi(io.jans.scim.service.filter.ProtectedApi) PATCH(io.jans.scim.ws.rs.scim2.PATCH)

Aggregations

Response (javax.ws.rs.core.Response)19 UserResource (io.jans.scim.model.scim2.user.UserResource)14 Test (org.testng.annotations.Test)14 PatchOperation (io.jans.scim.model.scim2.patch.PatchOperation)10 UserBaseTest (io.jans.scim2.client.UserBaseTest)10 PatchRequest (io.jans.scim.model.scim2.patch.PatchRequest)9 Parameters (org.testng.annotations.Parameters)9 GroupResource (io.jans.scim.model.scim2.group.GroupResource)4 BaseTest (io.jans.scim2.client.BaseTest)4 ListResponse (io.jans.scim.model.scim2.ListResponse)3 InstantMessagingAddress (io.jans.scim.model.scim2.user.InstantMessagingAddress)3 PhoneNumber (io.jans.scim.model.scim2.user.PhoneNumber)3 DuplicateEntryException (io.jans.orm.exception.operation.DuplicateEntryException)2 SCIMException (io.jans.scim.model.exception.SCIMException)2 CustomAttributes (io.jans.scim.model.scim2.CustomAttributes)2 BulkResponse (io.jans.scim.model.scim2.bulk.BulkResponse)2 Member (io.jans.scim.model.scim2.group.Member)2 Address (io.jans.scim.model.scim2.user.Address)2 ScimResourceUtil (io.jans.scim.model.scim2.util.ScimResourceUtil)2 ArrayList (java.util.ArrayList)2