Example usage for com.google.gson JsonArray contains

List of usage examples for com.google.gson JsonArray contains

Introduction

In this page you can find the example usage for com.google.gson JsonArray contains.

Prototype

public boolean contains(JsonElement element) 

Source Link

Document

Returns true if this array contains the specified element.

Usage

From source file:org.wso2.am.integration.tests.restapi.GIT_1628_OAuthAppUpdateViaRestApiTestCase.java

License:Open Source License

@Test(description = "Updating grant types and callback url", dependsOnMethods = "testKeyGenerationResponseContainsGrantTypesAndCallback")
public void testUpdateGrantTypesAndCallback() throws Exception {
    String url = storeRestApiBaseUrl + "applications/" + applicationId + "/keys/PRODUCTION";
    String payload = "{\n" + "  \"callbackUrl\": \"http://GIT_1628.com/prod_updated\",\n"
            + "  \"supportedGrantTypes\":    [\n" + "      \"password\",\n" + "      \"iwa:ntlm\"\n" + "  ]\n"
            + '}';
    HttpResponse response = HTTPSClientUtils.doPut(url, headers, payload);
    if (response.getResponseCode() != 200) {
        Assert.fail("Grant type/callback update failed: Response code is " + response.getResponseCode()
                + " Response message is '" + response.getData() + '\'');
    }//from   w  w w. ja  v  a 2  s  . co m

    String json = response.getData();
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);

    String callbackUrl = jsonObject.getAsJsonPrimitive("callbackUrl").getAsString();
    Assert.assertEquals(callbackUrl, "http://GIT_1628.com/prod_updated");

    JsonArray grantTypes = jsonObject.getAsJsonArray("supportedGrantTypes");
    Assert.assertNotNull(grantTypes,
            "Grant Types are not available in the response. Response: " + response.getData());

    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("password")),
            "password is not available " + "in grant type list. Response: " + response.getData());
    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("iwa:ntlm")),
            "iwa:ntlm is not available " + "in grant type list. Response: " + response.getData());

    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("client_credentials")),
            "client_credentials is available in grant type list. Response: " + response.getData());
    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("refresh_token")),
            "refresh_token is available " + "in grant type list. Response: " + response.getData());
    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("urn:ietf:params:oauth:grant-type:saml2-bearer")),
            "urn:ietf:params:oauth:grant-type:saml2-bearer is available in grant type list. Response: "
                    + response.getData());

    //test updated grant types
    HashMap<String, String> tokenApiHeaders = new HashMap<String, String>();
    String authenticationHeader = consumerKey + ":" + consumerSecret;
    byte[] encodedBytes = Base64.encodeBase64(authenticationHeader.getBytes("UTF-8"));
    tokenApiHeaders.put(RESTAPITestConstants.AUTHORIZATION_KEY, "Basic " + new String(encodedBytes, "UTF-8"));

    //password
    payload = "grant_type=password&username=" + user.getUserName() + "&password=" + user.getPassword();

    response = HTTPSClientUtils.doPost(tokenApiUrl, payload, tokenApiHeaders);
    Assert.assertEquals(response.getResponseCode(), 200,
            "Password grant token call is not successful: " + "Response code is " + response.getResponseCode()
                    + " Response message is '" + response.getData() + '\'');

    //client credentials
    payload = "grant_type=client_credentials";

    response = HTTPSClientUtils.doPost(tokenApiUrl, payload, tokenApiHeaders);
    Assert.assertEquals(response.getResponseCode(), 400,
            "Client Credentials grant type is not disabled properly: " + "Response code is "
                    + response.getResponseCode() + " Response message is '" + response.getData() + '\'');
    Assert.assertTrue(
            response.getData().contains(
                    "The authenticated client is not authorized to use this " + "authorization grant type"),
            "Wrong error message found.");
}

From source file:org.wso2.am.integration.tests.restapi.GIT_1628_OAuthAppUpdateViaRestApiTestCase.java

License:Open Source License

@Test(description = "Retrieving grant types and callback url", dependsOnMethods = "testUpdateGrantTypesAndCallback")
public void testGetGrantTypesAndCallback() throws Exception {
    String url = storeRestApiBaseUrl + "applications/" + applicationId + "/keys/PRODUCTION";
    HttpResponse response = HTTPSClientUtils.doGet(url, headers);
    if (response.getResponseCode() != 200) {
        Assert.fail("Grant type/callback retrieval failed: Response code is " + response.getResponseCode()
                + " Response message is '" + response.getData() + '\'');
    }/*from  w w w .j  a  v a 2 s  .c  o  m*/

    String json = response.getData();
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);

    String callbackUrl = jsonObject.getAsJsonPrimitive("callbackUrl").getAsString();
    Assert.assertEquals(callbackUrl, "http://GIT_1628.com/prod_updated");

    JsonArray grantTypes = jsonObject.getAsJsonArray("supportedGrantTypes");
    Assert.assertNotNull(grantTypes,
            "Grant Types are not available in the response. Response: " + response.getData());

    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("password")),
            "password is not available " + "in grant type list. Response: " + response.getData());
    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("iwa:ntlm")),
            "iwa:ntlm is not available " + "in grant type list. Response: " + response.getData());

    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("refresh_token")),
            "refresh_token is available " + "in grant type list. Response: " + response.getData());
    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("client_credentials")),
            "client_credentials is available in grant type list");
    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("urn:ietf:params:oauth:grant-type:saml2-bearer")),
            "urn:ietf:params:oauth:grant-type:saml2-bearer is available in grant type list. Response: "
                    + response.getData());
}

From source file:org.wso2.am.integration.tests.restapi.GIT_1628_OAuthAppUpdateViaRestApiTestCase.java

License:Open Source License

@Test(description = "Retrieving application and check keys", dependsOnMethods = "testGetGrantTypesAndCallback")
public void testGetApplicationAndCheckKeys() throws Exception {
    String url = storeRestApiBaseUrl + "applications/" + applicationId;
    HttpResponse response = HTTPSClientUtils.doGet(url, headers);
    if (response.getResponseCode() != 200) {
        Assert.fail("Grant type/callback retrieval failed: Response code is " + response.getResponseCode()
                + " Response message is '" + response.getData() + '\'');
    }//from w  w  w.  ja  v  a2 s.c o  m

    String json = response.getData();
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);

    JsonArray keys = jsonObject.getAsJsonArray("keys");
    Assert.assertNotNull(keys, "Keys are not available in the response. Response: " + response.getData());

    JsonObject key = (JsonObject) keys.get(0);

    String callbackUrl = key.getAsJsonPrimitive("callbackUrl").getAsString();
    Assert.assertEquals(callbackUrl, "http://GIT_1628.com/prod_updated");

    JsonArray grantTypes = key.getAsJsonArray("supportedGrantTypes");
    Assert.assertNotNull(grantTypes,
            "Grant Types are not available in the response. Response: " + response.getData());

    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("password")),
            "password is not available " + "in grant type list. Response: " + response.getData());
    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("iwa:ntlm")),
            "iwa:ntlm is not available " + "in grant type list. Response: " + response.getData());

    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("refresh_token")),
            "refresh_token is available " + "in grant type list. Response: " + response.getData());
    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("client_credentials")),
            "client_credentials is available in grant type list");
    Assert.assertFalse(grantTypes.contains(new JsonPrimitive("urn:ietf:params:oauth:grant-type:saml2-bearer")),
            "urn:ietf:params:oauth:grant-type:saml2-bearer is available in grant type list. Response: "
                    + response.getData());
}

From source file:org.wso2.am.integration.tests.restapi.GIT_1628_OAuthAppUpdateViaRestApiTestCase.java

License:Open Source License

@Test(description = "Key generation response should contain all grant types even when request does not have "
        + "grant types. (for backward compatibility)", dependsOnMethods = "testGetApplicationAndCheckKeys")
public void testKeyGenerationWithoutGrantTypesNorCallback() throws Exception {
    String url = storeRestApiBaseUrl + "applications/generate-keys?applicationId=" + applicationId;
    String payload = "{\n" + "  \"validityTime\": \"3600\",\n" + "  \"keyType\": \"SANDBOX\",\n"
            + "  \"accessAllowDomains\": [\"ALL\" ]\n" + '}';
    HttpResponse response = HTTPSClientUtils.doPost(url, headers, payload);
    if (response.getResponseCode() != 200) {
        Assert.fail("Key generation failed: Response code is " + response.getResponseCode()
                + " Response message is '" + response.getData() + '\'');
    }//from   w  w w .jav a  2s .  co  m

    String json = response.getData();
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);

    JsonArray grantTypes = jsonObject.getAsJsonArray("supportedGrantTypes");
    Assert.assertNotNull(grantTypes, "Grant Types are not available in the response:" + response.getData());

    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("refresh_token")),
            "refresh_token is not available " + "in grant type list. Response: " + response.getData());
    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("client_credentials")),
            "client_credentials is not available " + "in grant type list. Response: " + response.getData());
    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("password")),
            "password is not available " + "in grant type list. Response: " + response.getData());
    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("iwa:ntlm")),
            "iwa:ntlm is not available " + "in grant type list. Response: " + response.getData());
    Assert.assertTrue(grantTypes.contains(new JsonPrimitive("urn:ietf:params:oauth:grant-type:saml2-bearer")),
            "urn:ietf:params:oauth:grant-type:saml2-bearer is not available " + "in grant type list. Response: "
                    + response.getData());
}