|
| 1 | +package com.baeldung.rest.jbehave; |
| 2 | + |
| 3 | +import org.apache.http.HttpResponse; |
| 4 | +import org.apache.http.client.methods.HttpGet; |
| 5 | +import org.apache.http.client.methods.HttpUriRequest; |
| 6 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 7 | +import org.jbehave.core.annotations.Given; |
| 8 | +import org.jbehave.core.annotations.Then; |
| 9 | +import org.jbehave.core.annotations.When; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | + |
| 13 | +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 14 | +import static org.apache.http.HttpStatus.SC_NOT_FOUND; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
| 17 | +public class GithubUserNotFoundSteps { |
| 18 | + |
| 19 | + private String api; |
| 20 | + private String nonExistentUser; |
| 21 | + private int githubResponseCode; |
| 22 | + |
| 23 | + @Given("github user profile api") |
| 24 | + public void givenGithubUserProfileApi() { |
| 25 | + api = "https://api.github.com/users/%s"; |
| 26 | + } |
| 27 | + |
| 28 | + @Given("a random non-existent username") |
| 29 | + public void givenANonexistentUsername() { |
| 30 | + nonExistentUser = randomAlphabetic(8); |
| 31 | + } |
| 32 | + |
| 33 | + @When("I look for the random user via the api") |
| 34 | + public void whenILookForTheUserViaTheApi() throws IOException { |
| 35 | + githubResponseCode = getGithubUserProfile(api, nonExistentUser) |
| 36 | + .getStatusLine() |
| 37 | + .getStatusCode(); |
| 38 | + } |
| 39 | + |
| 40 | + @When("I look for $user via the api") |
| 41 | + public void whenILookForSomeNonExistentUserViaTheApi(String user) throws IOException { |
| 42 | + githubResponseCode = getGithubUserProfile(api, user) |
| 43 | + .getStatusLine() |
| 44 | + .getStatusCode(); |
| 45 | + } |
| 46 | + |
| 47 | + static HttpResponse getGithubUserProfile(String api, String username) throws IOException { |
| 48 | + HttpUriRequest request = new HttpGet(String.format(api, username)); |
| 49 | + return HttpClientBuilder |
| 50 | + .create() |
| 51 | + .build() |
| 52 | + .execute(request); |
| 53 | + } |
| 54 | + |
| 55 | + @Then("github respond: 404 not found") |
| 56 | + public void thenGithubRespond404NotFound() { |
| 57 | + assertTrue(SC_NOT_FOUND == githubResponseCode); |
| 58 | + } |
| 59 | +} |
0 commit comments