Example usage for com.squareup.okhttp Response code

List of usage examples for com.squareup.okhttp Response code

Introduction

In this page you can find the example usage for com.squareup.okhttp Response code.

Prototype

int code

To view the source code for com.squareup.okhttp Response code.

Click Source Link

Usage

From source file:io.kodokojo.bdd.MarathonIsPresent.java

License:Open Source License

public boolean marathonIsRunning() {
    Request request = new Request.Builder().url(getMarathonUrl() + "/ping").get().build();
    Response response = null;
    try {/*  ww w  .  jav  a  2s . co m*/
        response = httpClient.newCall(request).execute();
        return response.code() == 200;
    } catch (IOException e) {
        LOGGER.error("unable to done request {} due to following error", e);
    } finally {
        if (response != null) {
            try {
                response.body().close();
            } catch (IOException e) {
                fail(e.getMessage());
            }
        }
    }
    return false;
}

From source file:io.kodokojo.bdd.stage.AccessRestWhen.java

License:Open Source License

private SELF try_to_access_to_call_$_url_$(@Quoted String methodName, @Quoted String url) {

    Request.Builder builder = new Request.Builder().get().url(getBaseUrl() + url);
    if (currentUserLogin != null) {
        UserInfo requesterUserInfo = currentUsers.get(currentUserLogin);
        builder = HttpUserSupport.addBasicAuthentification(requesterUserInfo, builder);
    }/*from w  ww  . j av  a  2s  .  co  m*/
    Request request = builder.build();
    Response response = null;
    try {
        response = httpClient.newCall(request).execute();
        responseHttpStatusCode = response.code();
        responseHttpStatusBody = response.body().string();
        response.body().close();
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        if (response != null) {
            try {
                response.body().close();
            } catch (IOException e) {
                fail(e.getMessage());
            }
        }
    }
    return self();
}

From source file:io.kodokojo.bdd.stage.ApplicationThen.java

License:Open Source License

public SELF add_user_$_to_project_configuration_as_user_$_will_fail(@Quoted String usernameToAdd,
        @Quoted String usernameOwner) {
    UserInfo currentUser = currentUsers.get(usernameOwner);
    UserInfo userToAdd = currentUsers.get(usernameToAdd);

    String json = "[\n" + "    \"" + userToAdd.getIdentifier() + "\"\n" + "  ]";

    OkHttpClient httpClient = new OkHttpClient();

    RequestBody body = RequestBody.create(com.squareup.okhttp.MediaType.parse("application/json"),
            json.getBytes());/*from   ww  w.j  a  v a  2  s.c o m*/
    Request.Builder builder = new Request.Builder()
            .url(getApiBaseUrl() + "/projectconfig/" + projectConfigurationId + "/user").put(body);
    Request request = HttpUserSupport.addBasicAuthentification(currentUser, builder).build();
    Response response = null;
    try {
        response = httpClient.newCall(request).execute();
        assertThat(response.code()).isEqualTo(403);
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return self();
}

From source file:io.kodokojo.bdd.stage.ApplicationThen.java

License:Open Source License

private void getUserDetails(String username, boolean complete) {
    OkHttpClient httpClient = new OkHttpClient();
    UserInfo requesterUserInfo = currentUsers.get(currentUserLogin);
    UserInfo targetUserInfo = currentUsers.get(username);
    String url = getApiBaseUrl() + "/user";
    if (!requesterUserInfo.getIdentifier().equals(targetUserInfo.getIdentifier())) {
        url += "/" + targetUserInfo.getIdentifier();
    }//from w w w .  j a  v  a  2 s .  co m
    Request.Builder builder = new Request.Builder().get().url(url);
    Request request = HttpUserSupport.addBasicAuthentification(requesterUserInfo, builder).build();
    Response response = null;
    try {
        response = httpClient.newCall(request).execute();
        assertThat(response.code()).isEqualTo(200);

        JsonParser parser = new JsonParser();
        String body = response.body().string();
        JsonObject json = (JsonObject) parser.parse(body);

        assertThat(json.getAsJsonPrimitive("name").getAsString()).isNotEmpty();
        if (complete) {
            assertThat(json.getAsJsonPrimitive("password").getAsString()).isNotEmpty();
            assertThat(json.getAsJsonPrimitive("email").getAsString()).isNotEmpty();
            assertThat(json.getAsJsonPrimitive("sshPublicKey").getAsString()).isNotEmpty();

        } else {
            assertThat(json.getAsJsonPrimitive("password").getAsString()).isEmpty();
        }
        if (StringUtils.isNotBlank(projectConfigurationId)) {
            assertThat(json.has("projectConfigurationIds"));
            JsonArray projectConfigurationIds = json.getAsJsonArray("projectConfigurationIds");
            assertThat(projectConfigurationIds).isNotEmpty();
            boolean foundCurrentProjectConfigurationId = false;
            Iterator<JsonElement> projectConfigIt = projectConfigurationIds.iterator();

            while (projectConfigIt.hasNext()) {
                JsonObject projectConfig = (JsonObject) projectConfigIt.next();
                assertThat(projectConfig.has("projectConfigurationId"));
                foundCurrentProjectConfigurationId = projectConfigurationId
                        .equals(projectConfig.getAsJsonPrimitive("projectConfigurationId").getAsString());
            }
            assertThat(foundCurrentProjectConfigurationId).isTrue();
        }
    } catch (IOException e) {
        fail("Unable to get User details on Url " + url, e);
    } finally {
        if (response != null) {
            try {
                response.body().close();
            } catch (IOException e) {
                fail("Fail to close Http response.", e);
            }
        }
    }
}

From source file:io.kodokojo.bdd.stage.ApplicationThen.java

License:Open Source License

private ProjectConfigDto getProjectConfigurationFromCurrentProjectConfigId() {
    OkHttpClient httpClient = new OkHttpClient();
    UserInfo requesterUserInfo = currentUsers.get(currentUserLogin);
    Request.Builder builder = new Request.Builder()
            .url(getApiBaseUrl() + "/projectconfig/" + projectConfigurationId).get();
    Request request = HttpUserSupport.addBasicAuthentification(requesterUserInfo, builder).build();
    Response response = null;
    try {/*from w  w  w .  ja  v a2 s . c  om*/
        response = httpClient.newCall(request).execute();
        assertThat(response.code()).isEqualTo(200);
        String bodyResponse = response.body().string();
        Gson gson = new GsonBuilder().create();
        ProjectConfigDto projectConfigDto = gson.fromJson(bodyResponse, ProjectConfigDto.class);
        assertThat(projectConfigDto).isNotNull();
        assertThat(projectConfigDto.getAdmins().get(0).getUsername())
                .isEqualTo(requesterUserInfo.getUsername());
        assertThat(projectConfigDto.getUsers()).isNotEmpty();
        assertThat(projectConfigDto.getStackConfigs()).isNotEmpty();

        JsonParser parser = new JsonParser();
        JsonObject json = (JsonObject) parser.parse(bodyResponse);
        assertThat(json.has("name")).isTrue();
        assertThat(json.has("identifier")).isTrue();
        assertThat(json.has("admins")).isTrue();
        JsonArray admins = json.getAsJsonArray("admins");
        assertThat(admins).isNotEmpty();
        jsonUserAreValid(admins.iterator());
        JsonArray users = json.getAsJsonArray("users");
        jsonUserAreValid(users.iterator());
        assertThat(users).isNotEmpty();
        JsonArray stackConfigs = json.getAsJsonArray("stackConfigs");
        assertThat(stackConfigs).isNotEmpty();
        Iterator<JsonElement> stackConfigurationIt = stackConfigs.iterator();
        while (stackConfigurationIt.hasNext()) {
            JsonObject stackConf = (JsonObject) stackConfigurationIt.next();
            jsonStackConfigIsValid(stackConf);
        }

        return projectConfigDto;
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        if (response != null) {
            closeQuietly(response.body());
        }
    }
    return null;
}

From source file:io.kodokojo.bdd.stage.ApplicationWhen.java

License:Open Source License

public SELF retrive_a_new_id() {
    OkHttpClient httpClient = new OkHttpClient();
    RequestBody emptyBody = RequestBody.create(null, new byte[0]);
    String baseUrl = getBaseUrl();
    Request request = new Request.Builder().post(emptyBody).url(baseUrl + "/api/v1/user").build();
    Response response = null;
    try {/*from  ww w  .  ja  v a  2  s  .com*/
        response = httpClient.newCall(request).execute();
        if (response.code() != 200) {
            fail("Invalid HTTP code status " + response.code() + " expected 200");
        }
        newUserId = response.body().string();

    } catch (IOException e) {
        fail(e.getMessage(), e);
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return self();
}

From source file:io.kodokojo.bdd.stage.ApplicationWhen.java

License:Open Source License

private SELF create_user(String email, boolean success) {
    if (isBlank(email)) {
        throw new IllegalArgumentException("email must be defined.");
    }/*from   w ww. j  a  va 2 s  .  co m*/
    if (success && StringUtils.isBlank(newUserId)) {
        retrive_a_new_id();
    }

    OkHttpClient httpClient = new OkHttpClient();
    RequestBody body = RequestBody.create(MediaType.parse("application/json"),
            ("{\"email\": \"" + email + "\"}").getBytes());
    String baseUrl = getBaseUrl();

    Request.Builder builder = new Request.Builder().post(body)
            .url(baseUrl + "/api/v1/user" + (newUserId != null ? "/" + newUserId : ""));
    if (isNotBlank(currentUserLogin)) {
        UserInfo currentUser = currentUsers.get(currentUserLogin);
        if (currentUser != null) {
            builder = HttpUserSupport.addBasicAuthentification(currentUser, builder);
        }
    }
    Request request = builder.build();
    Response response = null;
    try {
        response = httpClient.newCall(request).execute();
        if (success) {
            assertThat(response.code()).isEqualTo(201);
            JsonParser parser = new JsonParser();
            String bodyResponse = response.body().string();
            JsonObject json = (JsonObject) parser.parse(bodyResponse);
            String currentUsername = json.getAsJsonPrimitive("username").getAsString();
            String currentUserPassword = json.getAsJsonPrimitive("password").getAsString();
            String currentUserEmail = json.getAsJsonPrimitive("email").getAsString();
            String currentUserIdentifier = json.getAsJsonPrimitive("identifier").getAsString();
            String currentUserEntityIdentifier = json.getAsJsonPrimitive("entityIdentifier").getAsString();
            currentUsers.put(currentUsername, new UserInfo(currentUsername, currentUserIdentifier,
                    currentUserEntityIdentifier, currentUserPassword, currentUserEmail));
            if (isBlank(currentUserLogin)) {
                currentUserLogin = currentUsername;
            }
            Attachment privateKey = Attachment.plainText(bodyResponse).withTitle(currentUsername + " response");
            currentStep.addAttachment(privateKey);
        } else {
            assertThat(response.code()).isNotEqualTo(201);
        }
        response.body().close();
    } catch (IOException e) {
        if (success) {
            fail(e.getMessage(), e);
        }
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return self();
}

From source file:io.kodokojo.bdd.stage.ApplicationWhen.java

License:Open Source License

private String getProjectConfiguration(String username, String projectConfigurationId) {
    UserInfo userInfo = currentUsers.get(username);
    Request.Builder builder = new Request.Builder().get()
            .url(getApiBaseUrl() + "/projectconfig/" + projectConfigurationId);
    Request request = HttpUserSupport.addBasicAuthentification(userInfo, builder).build();
    Response response = null;
    try {/*  www .j  ava 2 s.  com*/
        OkHttpClient httpClient = new OkHttpClient();
        response = httpClient.newCall(request).execute();
        assertThat(response.code()).isEqualTo(200);
        String body = response.body().string();
        return body;
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return null;

}

From source file:io.kodokojo.bdd.stage.brickauthenticator.JenkinsUserAuthenticator.java

License:Open Source License

@Override
public boolean authenticate(OkHttpClient httpClient, String url, UserInfo userInfo) {

    Request.Builder builder = new Request.Builder().url(url + "/me/configure");
    builder = HttpUserSupport.addBasicAuthentification(userInfo, builder);
    Request request = builder.build();
    Response response = null;
    try {//from   w  w w  .j  av a 2s.c  o  m
        response = httpClient.newCall(request).execute();
        assertThat(response.code()).isBetween(200, 299);
        return true;
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return false;
}

From source file:io.kodokojo.bdd.stage.brickauthenticator.NexusUserAuthenticator.java

License:Open Source License

@Override
public boolean authenticate(OkHttpClient httpClient, String url, UserInfo userInfo) {

    Request.Builder builder = new Request.Builder().url(url + "/service/local/users");
    builder = HttpUserSupport.addBasicAuthentification(userInfo, builder);
    Request request = builder.build();
    Response response = null;
    try {/*w  w w  . j av a  2s  .c o  m*/
        response = httpClient.newCall(request).execute();
        assertThat(response.code()).isBetween(200, 299);
        return true;
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return false;
}