Search in sources :

Example 26 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project product-is by wso2.

the class SCIMServiceProviderGroupTestCase method patchGroup.

@Test(alwaysRun = true, description = "Add new SCIM user member to group testeng2 without removing existing users", dependsOnMethods = { "updateGroup" })
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.ALL })
public void patchGroup() throws Exception {
    SCIMResponseHandler responseHandler = new SCIMResponseHandler();
    responseHandler.setSCIMClient(scimClient);
    // set the handler in wink client config
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.handlers(new ClientHandler[] { responseHandler });
    // create a wink rest client with the above config
    RestClient restClient = new RestClient(clientConfig);
    BasicAuthInfo encodedBasicAuthInfo = SCIMUtils.getBasicAuthInfo(userInfo);
    // create resource endpoint to access a known user resource.
    Resource groupResource = restClient.resource(scim_url + "Groups/" + scimGroupId);
    String response = groupResource.header(SCIMConstants.AUTHORIZATION_HEADER, encodedBasicAuthInfo.getAuthorizationHeader()).contentType(SCIMConstants.APPLICATION_JSON).accept(SCIMConstants.APPLICATION_JSON).get(String.class);
    log.info("Retrieved group: " + response);
    // decode retrieved group
    Group decodedGroup = (Group) scimClient.decodeSCIMResponse(response.replace("PRIMARY/", ""), SCIMConstants.JSON, 2);
    decodedGroup.setUserMember(scimUserId2, USERNAME2);
    String updatedGroupString = scimClient.encodeSCIMObject(decodedGroup, SCIMConstants.JSON);
    Resource updateGroupResource = restClient.resource(scim_url + "Groups/" + scimGroupId);
    String responseUpdated = updateGroupResource.header(SCIMConstants.AUTHORIZATION_HEADER, encodedBasicAuthInfo.getAuthorizationHeader()).contentType(SCIMConstants.APPLICATION_JSON).header("X-HTTP-Method-Override", "PATCH").accept(SCIMConstants.APPLICATION_JSON).post(String.class, updatedGroupString);
    log.info("Updated group: " + responseUpdated);
    Assert.assertTrue(userMgtClient.userNameExists("testeng2", USERNAME) && userMgtClient.userNameExists("testeng2", USERNAME2));
}
Also used : Group(org.wso2.charon.core.objects.Group) RestClient(org.apache.wink.client.RestClient) Resource(org.apache.wink.client.Resource) BasicAuthInfo(org.wso2.identity.integration.test.utils.BasicAuthInfo) SCIMResponseHandler(org.wso2.identity.integration.test.scim.utils.SCIMResponseHandler) ClientConfig(org.apache.wink.client.ClientConfig) SetEnvironment(org.wso2.carbon.automation.engine.annotations.SetEnvironment) Test(org.testng.annotations.Test)

Example 27 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project product-is by wso2.

the class ServerChallengeTestCase method updateChallengeQuestionPATCHTest.

/**
 * This test method validate update challenge question api response.
 *
 * @throws JSONException thrown to indicate a problem with the JSON.
 */
@Test(dependsOnMethods = { "addsNewChallengeQuestionSetTest" })
public void updateChallengeQuestionPATCHTest() throws JSONException {
    String operation = "add";
    String locale = "en_US";
    String questionID = "question6";
    String challengeQuestion = "What is the name of your first school?";
    ServerChallengeModel.Questions question = new ServerChallengeModel.Questions(locale, challengeQuestion, questionID);
    ServerChallengeModel.ChallengeQuestionOperation challengeQuestionOperation = new ServerChallengeModel.ChallengeQuestionOperation(question, operation);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(challengeQuestionOperation);
    given().auth().preemptive().basic(USER_NAME, PASSWORD).contentType(ContentType.JSON).header(HttpHeaders.ACCEPT, ContentType.JSON).body(json).log().ifValidationFails().when().patch(RESOURCE_PATH + "/{challenge-set-id}", QUESTION_SET_ID).then().log().ifValidationFails().assertThat().statusCode(HttpStatus.SC_CREATED).log().ifValidationFails();
    getsChallengeQuestionTest(QUESTION_SET_ID, challengeQuestion, questionID);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ServerChallengeModel(org.wso2.identity.integration.test.rest.api.server.challenge.v1.model.ServerChallengeModel) Gson(com.google.gson.Gson) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 28 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class MeResource method patchUser.

@PATCH
public Response patchUser(@HeaderParam(SCIMProviderConstants.CONTENT_TYPE) String inputFormat, @HeaderParam(SCIMProviderConstants.ACCEPT_HEADER) String outputFormat, @QueryParam(SCIMProviderConstants.ATTRIBUTES) String attribute, @QueryParam(SCIMProviderConstants.EXCLUDE_ATTRIBUTES) String excludedAttributes, String resourceString) {
    String userId = SupportUtils.getAuthenticatedUserId();
    try {
        // content-type header is compulsory in post request.
        if (inputFormat == null) {
            String error = SCIMProviderConstants.CONTENT_TYPE + " not present in the request header";
            throw new FormatNotSupportedException(error);
        }
        if (!isValidInputFormat(inputFormat)) {
            String error = inputFormat + " is not supported.";
            throw new FormatNotSupportedException(error);
        }
        if (!isValidOutputFormat(outputFormat)) {
            String error = outputFormat + " is not supported.";
            throw new FormatNotSupportedException(error);
        }
        // obtain the user store manager
        UserManager userManager = IdentitySCIMManager.getInstance().getUserManager();
        // Build Custom schema
        buildCustomSchema(userManager, getTenantId());
        // create charon-SCIM me resource manager and hand-over the request.
        MeResourceManager meResourceManager = new MeResourceManager();
        SCIMResponse response = meResourceManager.updateWithPATCH(userId, resourceString, userManager, attribute, excludedAttributes);
        return SupportUtils.buildResponse(response);
    } catch (CharonException e) {
        return handleCharonException(e);
    } catch (FormatNotSupportedException e) {
        return handleFormatNotSupportedException(e);
    }
}
Also used : FormatNotSupportedException(org.wso2.charon3.core.exceptions.FormatNotSupportedException) UserManager(org.wso2.charon3.core.extensions.UserManager) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse) MeResourceManager(org.wso2.charon3.core.protocol.endpoints.MeResourceManager) PATCH(org.wso2.carbon.identity.jaxrs.designator.PATCH)

Example 29 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class GroupResource method decodePatchOperation.

/**
 * Decode patch operation resource string and get the permissions.
 *
 * @param jsonResourceString string that should decode.
 * @return Map of permissions to add and remove.
 * @throws BadRequestException
 */
private HashMap<String, String[]> decodePatchOperation(String jsonResourceString) throws BadRequestException {
    JSONDecoder decode = new JSONDecoder();
    ArrayList<PatchOperation> listOperations;
    List<String> permissionsToAdd;
    List<String> permissionsToRemove;
    HashMap<String, String[]> permissionMap = new HashMap<>();
    // Decode the JSON string and get the permissions based on operations.
    listOperations = decode.decodeRequest(jsonResourceString);
    if (!listOperations.isEmpty()) {
        for (PatchOperation op : listOperations) {
            if ((SCIMProviderConstants.ADD).equals(op.getOperation())) {
                JSONArray permissions = new JSONArray(op.getValues().toString());
                permissionsToAdd = IntStream.range(0, permissions.length()).mapToObj(permissions::getString).collect(Collectors.toList());
                if (permissionsToAdd.isEmpty()) {
                    permissionMap.put(SCIMProviderConstants.ADD, null);
                } else {
                    permissionMap.put(SCIMProviderConstants.ADD, permissionsToAdd.toArray(new String[0]));
                }
            } else if (SCIMProviderConstants.REMOVE.equals(op.getOperation())) {
                JSONArray permissions = new JSONArray(op.getValues().toString());
                permissionsToRemove = IntStream.range(0, permissions.length()).mapToObj(permissions::getString).collect(Collectors.toList());
                if (permissionsToRemove.isEmpty()) {
                    permissionMap.put(SCIMProviderConstants.REMOVE, null);
                } else {
                    permissionMap.put(SCIMProviderConstants.REMOVE, permissionsToRemove.toArray(new String[0]));
                }
            }
        }
    }
    return permissionMap;
}
Also used : JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) HashMap(java.util.HashMap) PatchOperation(org.wso2.charon3.core.utils.codeutils.PatchOperation) JSONArray(org.json.JSONArray)

Example 30 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class GroupResource method patchPermissionForGroup.

@PATCH
@Path("{id}/permissions")
@Produces({ MediaType.APPLICATION_JSON, SCIMProviderConstants.APPLICATION_SCIM_JSON })
public Response patchPermissionForGroup(@PathParam(SCIMConstants.CommonSchemaConstants.ID) String id, @HeaderParam(SCIMProviderConstants.CONTENT_TYPE) String inputFormat, @HeaderParam(SCIMProviderConstants.ACCEPT_HEADER) String outputFormat, @QueryParam(SCIMProviderConstants.ATTRIBUTES) String attribute, @QueryParam(SCIMProviderConstants.EXCLUDE_ATTRIBUTES) String excludedAttributes, String resourceString) {
    // content-type header is compulsory in post request.
    if (inputFormat == null) {
        return handleFormatNotSupportedException(new FormatNotSupportedException("Content type: " + SCIMProviderConstants.CONTENT_TYPE + "  not present in the request header"));
    }
    if (!isValidInputFormat(inputFormat)) {
        return handleFormatNotSupportedException(new FormatNotSupportedException("Input format: " + inputFormat + " is not supported."));
    }
    if (!isValidOutputFormat(outputFormat)) {
        return handleFormatNotSupportedException(new FormatNotSupportedException("Output format: " + outputFormat + " is not supported."));
    }
    Map<String, String> requestAttributes = new HashMap<>();
    requestAttributes.put(SCIMProviderConstants.ID, id);
    requestAttributes.put(SCIMProviderConstants.HTTP_VERB, PATCH.class.getSimpleName());
    requestAttributes.put(SCIMProviderConstants.RESOURCE_STRING, resourceString);
    requestAttributes.put(SCIMProviderConstants.ATTRIBUTES, PERMISSIONS);
    requestAttributes.put(SCIMProviderConstants.EXCLUDE_ATTRIBUTES, excludedAttributes);
    return processRequest(requestAttributes);
}
Also used : FormatNotSupportedException(org.wso2.charon3.core.exceptions.FormatNotSupportedException) HashMap(java.util.HashMap) PATCH(org.wso2.carbon.identity.jaxrs.designator.PATCH) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PATCH(org.wso2.carbon.identity.jaxrs.designator.PATCH)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)19 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 Test (org.testng.annotations.Test)11 JSONArray (org.json.JSONArray)9 JSONObject (org.json.JSONObject)9 Attribute (org.wso2.charon3.core.attributes.Attribute)9 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)9 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)9 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)9 CharonException (org.wso2.charon3.core.exceptions.CharonException)9 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)8 List (java.util.List)7 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)7 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 JSONException (org.json.JSONException)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)6 ExtractableResponse (io.restassured.response.ExtractableResponse)5