From a82b1cec82b1a4c2b0e8f269e3c09a150bb2fb52 Mon Sep 17 00:00:00 2001 From: Angel Baro Date: Thu, 4 Apr 2024 16:47:00 +0200 Subject: [PATCH] Location feature of deleting --- .../demo/steps/DeleteLocationStepsDefs.java | 63 +++++++++++++++++++ .../resources/features/DeleteLocation.feature | 41 ++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/test/java/cat/udl/eps/softarch/demo/steps/DeleteLocationStepsDefs.java create mode 100644 src/test/resources/features/DeleteLocation.feature diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteLocationStepsDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteLocationStepsDefs.java new file mode 100644 index 0000000..7b02fd4 --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteLocationStepsDefs.java @@ -0,0 +1,63 @@ +package cat.udl.eps.softarch.demo.steps; + + +import cat.udl.eps.softarch.demo.domain.Location; +import cat.udl.eps.softarch.demo.repository.LocationRepository; +import io.cucumber.java.en.And; +import io.cucumber.java.en.When; +import org.junit.Assert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.server.ResponseStatusException; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + + +public class DeleteLocationStepsDefs { + + @Autowired + private StepDefs stepDefs; + + @Autowired + private LocationRepository locationRepository; + + public static String newResourceUri; + @When("I delete the location with the details:$") + public void iDeleteTheLocationWithTheDetails(Map locationDetails) throws Throwable{ + Location existingLocation = locationRepository.findLocationByAddressAndProvinceAndCityAndPostalCode( + locationDetails.get("address"), + locationDetails.get("province"), + locationDetails.get("city"), + Integer.parseInt(locationDetails.get("postalCode")) + ); + + stepDefs.result = stepDefs.mockMvc.perform( + delete("/locations/{id}", (existingLocation != null) ? existingLocation.getId() : "999") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(existingLocation)) + .characterEncoding(StandardCharsets.UTF_8) + .accept(MediaType.APPLICATION_JSON) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()); + } + + @And("There is (\\d+) Location with the details:$") + public void thereIsOnlyLocationWithTheDeatils(int locationNum, Map locationDetails) { + Assert.assertEquals(locationNum, locationRepository.count()); + } + + +} diff --git a/src/test/resources/features/DeleteLocation.feature b/src/test/resources/features/DeleteLocation.feature new file mode 100644 index 0000000..783782b --- /dev/null +++ b/src/test/resources/features/DeleteLocation.feature @@ -0,0 +1,41 @@ +Feature: Delete Location + In order to delete an existing Location + As a user with appropriate permissions + I want to be able to delete the Location details of a shelter + + Background: + Given There is a registered user with username "username" and password "password" and email "user@email.com" + And There is already a Location with the following details: + | address | Major Street 3 | + | province | Lleida | + | city | Seròs | + | postalCode | 25183 | + + + Scenario: Delete an existing Location details + Given I can login with username "username" and password "password" + When I delete the location with the details: + | address | Major Street 3 | + | province | Lleida | + | city | Seròs | + | postalCode | 25183 | + Then The response code is 200 + And There is 0 Location with the details: + | address | Major Street 3 | + | province | Lleida | + | city | Seròs | + | postalCode | 25183 | + + Scenario: Delete Location when I am not logged in + Given I'm not logged in + When I delete the location with the details: + | address | Major Street 3 | + | province | Lleida | + | city | Seròs | + | postalCode | 25183 | + Then The response code is 401 + And There is 1 Location with the details: + | address | Major Street 3 | + | province | Lleida | + | city | Seròs | + | postalCode | 25183 | \ No newline at end of file