diff --git a/src/test/java/feature/SpringIntegrationTest.java b/src/test/java/feature/SpringIntegrationTest.java index 838fbce..ecfe69b 100644 --- a/src/test/java/feature/SpringIntegrationTest.java +++ b/src/test/java/feature/SpringIntegrationTest.java @@ -52,4 +52,12 @@ protected void executeDelete(String path) { String url = "http://localhost:" + port + path; latestResponse = restTemplate.exchange(url, HttpMethod.DELETE, HttpEntity.EMPTY, String.class); } + + protected void executePut(String path, Object payload) { + String url = "http://localhost:" + port + path; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity request = new HttpEntity<>(payload, headers); + latestResponse = restTemplate.exchange(url, HttpMethod.PUT, request, String.class); + } } diff --git a/src/test/java/feature/StepDefinition.java b/src/test/java/feature/StepDefinition.java index fbb2966..afb3443 100644 --- a/src/test/java/feature/StepDefinition.java +++ b/src/test/java/feature/StepDefinition.java @@ -100,6 +100,31 @@ public void theClientCallToDeleteTheCreatedUser() { executeDelete("/random-users/" + createdUserId); } + @Given("a valid user payload for update") + public void aValidUserPayloadForUpdate() { + payload = new UserRequest( + "male", + "John", + "Doe", + "Mr", + "john.doe@example.com", + "0611111111", + "john.jpg", + "US" + ); + } + + @When("the client call to PUT the created user") + public void theClientCallToPutTheCreatedUser() { + assertNotNull(createdUserId, "No user was created before this step"); + executePut("/random-users/" + createdUserId, payload); + } + + @When("the client call to PUT \\/random-users\\/{int}") + public void theClientCallToPutRandomUser(int id) { + executePut("/random-users/" + id, payload); + } + @And("the response contains a list of users") public void theResponseContainsAListOfUsers() throws Exception { JsonNode body = objectMapper.readTree(latestResponse.getBody()); diff --git a/src/test/resources/features/update_user.feature b/src/test/resources/features/update_user.feature new file mode 100644 index 0000000..2c86034 --- /dev/null +++ b/src/test/resources/features/update_user.feature @@ -0,0 +1,21 @@ +Feature: Update user endpoint + + Scenario: Update a user by ID after creation + Given a valid user payload for creation + When the client call to POST /random-users + Then the response status should be 201 + And the user profile + | id | | + | firstname | Emma | + Given a valid user payload for update + When the client call to PUT the created user + Then the response status should be 200 + And the user profile + | firstname | John | + | lastname | Doe | + + Scenario: Update a user that does not exist + Given a valid user payload for update + When the client call to PUT /random-users/999999 + Then the response status should be 404 +