From 7871f962ee79276bc692ec929147a451916d55bb Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Thu, 17 Oct 2024 21:37:34 +0200 Subject: [PATCH 01/12] feat: delete apartment step definition and feature --- .../demo/steps/DeleteApartmentStepDefs.java | 74 +++++++++++++++++++ .../features/DeleteApartment.feature | 11 +++ 2 files changed, 85 insertions(+) create mode 100644 src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java create mode 100644 src/test/resources/features/DeleteApartment.feature diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java new file mode 100644 index 0000000..5ae4a40 --- /dev/null +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java @@ -0,0 +1,74 @@ +package cat.udl.eps.softarch.demo.steps; + +import static org.junit.Assert.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import cat.udl.eps.softarch.demo.domain.Apartment; +import cat.udl.eps.softarch.demo.domain.Owner; +import cat.udl.eps.softarch.demo.repository.ApartmentRepository; +import cat.udl.eps.softarch.demo.repository.OwnerRepository; +import cat.udl.eps.softarch.demo.repository.UserRepository; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.When; +import org.springframework.beans.factory.annotation.Autowired; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +public class DeleteApartmentStepDefs { + + @Autowired + private StepDefs stepDefs; + + @Autowired + private ApartmentRepository apartmentRepository; + + @Autowired + private OwnerRepository ownerRepository; + + @Autowired + private UserRepository userRepository; + + @Given("^There is an apartment registered with the name \"([^\"]*)\"$") + public void thereIsAnApartmentRegisteredWithTheName(String name) { + List apartments = apartmentRepository.findByName(name); + assertFalse("Apartment with name \"" + name + "\" should exist", apartments.isEmpty()); + } + + @When("^I delete the apartment with name \"([^\"]*)\"$") + public void iDeleteTheApartmentWithName(String name) throws Exception { + List apartments = apartmentRepository.findByName(name); + assertFalse("Apartment with name \"" + name + "\" should exist", apartments.isEmpty()); + Apartment apartment = apartments.get(0); + + stepDefs.result = stepDefs.mockMvc.perform( + delete("/apartments/" + apartment.getId()) + .characterEncoding(StandardCharsets.UTF_8) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()) + .andExpect(status().isNoContent()); + } + + @And("^The apartment with name \"([^\"]*)\" no longer exists$") + public void theApartmentWithNameNoLongerExists(String name) { + List apartments = apartmentRepository.findByName(name); + assertTrue("Apartment with name \"" + name + "\" should no longer exist", apartments.isEmpty()); + } + + @Given("^There is a registered owner with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") + public void thereIsARegisteredOwnerWithUsernameAndPasswordAndEmail(String username, String password, String email) { + if (!userRepository.existsById(username)) { + Owner owner = new Owner(); + owner.setId(username); + owner.setPassword(password); + owner.setEmail(email); + owner.setName("Owner Name"); + owner.setPhoneNumber("123456789"); + owner.encodePassword(); + ownerRepository.save(owner); + } + } +} \ No newline at end of file diff --git a/src/test/resources/features/DeleteApartment.feature b/src/test/resources/features/DeleteApartment.feature new file mode 100644 index 0000000..90fbbac --- /dev/null +++ b/src/test/resources/features/DeleteApartment.feature @@ -0,0 +1,11 @@ +Feature: Delete Apartment + In order to manage apartments + As an Owner + I want to delete apartments + + Scenario: Delete an apartment as Owner + Given There is a registered owner with username "owner" and password "password" and email "owner@example.com" + And I login as "owner" with password "password" + When I delete an apartment with name "Cozy Loft", floor "3", address "Carrer de les Flors 10", postal code "08001", city "Barcelona", country "Spain", description "A cozy loft in the center of Barcelona" + Then The response code is 201 + And The apartment has been deleted with name "Cozy Loft", floor "3", address "Carrer de les Flors 10", postal code "08001", city "Barcelona", country "Spain", description "A cozy loft in the center of Barcelona" \ No newline at end of file From c116c73b6d7b575812d6cedeb0496846b0bf878c Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Thu, 17 Oct 2024 21:38:05 +0200 Subject: [PATCH 02/12] feat: find and delete apartment by address --- .../demo/repository/ApartmentRepository.java | 1 + .../demo/steps/DeleteApartmentStepDefs.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/ApartmentRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/ApartmentRepository.java index 37845d4..6cc250b 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/repository/ApartmentRepository.java +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/ApartmentRepository.java @@ -16,4 +16,5 @@ public interface ApartmentRepository extends CrudRepository, Pa List findByCity(@Param("city") String city); List findByRegistrationDate(@Param("registrationDate") Date registrationDate); List findByName(@Param("name") String name); + List findByAddress(@Param("address") String address); } diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java index 5ae4a40..cc8dc0d 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java @@ -58,6 +58,32 @@ public void theApartmentWithNameNoLongerExists(String name) { assertTrue("Apartment with name \"" + name + "\" should no longer exist", apartments.isEmpty()); } + @Given("^There is an apartment registered with the address \"([^\"]*)\"$") + public void thereIsAnApartmentRegisteredWithTheAddress(String address) { + List apartments = apartmentRepository.findByAddress(address); + assertFalse("Apartment with address \"" + address + "\" should exist", apartments.isEmpty()); + } + + @When("^I delete the apartment with address \"([^\"]*)\"$") + public void iDeleteTheApartmentWithAddress(String address) throws Exception { + List apartments = apartmentRepository.findByAddress(address); + assertFalse("Apartment with address \"" + address + "\" should exist", apartments.isEmpty()); + Apartment apartment = apartments.get(0); + + stepDefs.result = stepDefs.mockMvc.perform( + delete("/apartments/" + apartment.getId()) + .characterEncoding(StandardCharsets.UTF_8) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()) + .andExpect(status().isNoContent()); + } + + @And("^The apartment with address \"([^\"]*)\" no longer exists$") + public void theApartmentWithAddressNoLongerExists(String address) { + List apartments = apartmentRepository.findByAddress(address); + assertTrue("Apartment with address \"" + address + "\" should no longer exist", apartments.isEmpty()); + } + @Given("^There is a registered owner with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") public void thereIsARegisteredOwnerWithUsernameAndPasswordAndEmail(String username, String password, String email) { if (!userRepository.existsById(username)) { From 3fc57ad28b9200d3c55cb81b0bdbf3ab2b6d4426 Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Thu, 17 Oct 2024 23:13:55 +0200 Subject: [PATCH 03/12] fix: duplicated method --- .../demo/steps/DeleteApartmentStepDefs.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java index cc8dc0d..279735c 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java @@ -83,18 +83,4 @@ public void theApartmentWithAddressNoLongerExists(String address) { List apartments = apartmentRepository.findByAddress(address); assertTrue("Apartment with address \"" + address + "\" should no longer exist", apartments.isEmpty()); } - - @Given("^There is a registered owner with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") - public void thereIsARegisteredOwnerWithUsernameAndPasswordAndEmail(String username, String password, String email) { - if (!userRepository.existsById(username)) { - Owner owner = new Owner(); - owner.setId(username); - owner.setPassword(password); - owner.setEmail(email); - owner.setName("Owner Name"); - owner.setPhoneNumber("123456789"); - owner.encodePassword(); - ownerRepository.save(owner); - } - } } \ No newline at end of file From 88cafa92f358ffbbf8e52217cba36dbf825dc065 Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Thu, 17 Oct 2024 23:17:13 +0200 Subject: [PATCH 04/12] fix: delete scenario definitions --- src/test/resources/features/DeleteApartment.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/features/DeleteApartment.feature b/src/test/resources/features/DeleteApartment.feature index 90fbbac..a804f1d 100644 --- a/src/test/resources/features/DeleteApartment.feature +++ b/src/test/resources/features/DeleteApartment.feature @@ -6,6 +6,6 @@ Feature: Delete Apartment Scenario: Delete an apartment as Owner Given There is a registered owner with username "owner" and password "password" and email "owner@example.com" And I login as "owner" with password "password" - When I delete an apartment with name "Cozy Loft", floor "3", address "Carrer de les Flors 10", postal code "08001", city "Barcelona", country "Spain", description "A cozy loft in the center of Barcelona" + When I delete an apartment with name "Cozy Loft" Then The response code is 201 - And The apartment has been deleted with name "Cozy Loft", floor "3", address "Carrer de les Flors 10", postal code "08001", city "Barcelona", country "Spain", description "A cozy loft in the center of Barcelona" \ No newline at end of file + And The apartment has been deleted with name "Cozy Loft" \ No newline at end of file From 0ad636e6ee360194dc195f99723e51f8511eb705 Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Thu, 17 Oct 2024 23:21:26 +0200 Subject: [PATCH 05/12] fix: scenario definitions --- src/test/resources/features/DeleteApartment.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/features/DeleteApartment.feature b/src/test/resources/features/DeleteApartment.feature index a804f1d..e42fcf9 100644 --- a/src/test/resources/features/DeleteApartment.feature +++ b/src/test/resources/features/DeleteApartment.feature @@ -6,6 +6,6 @@ Feature: Delete Apartment Scenario: Delete an apartment as Owner Given There is a registered owner with username "owner" and password "password" and email "owner@example.com" And I login as "owner" with password "password" - When I delete an apartment with name "Cozy Loft" + When I delete the apartment with name "Cozy Loft" Then The response code is 201 - And The apartment has been deleted with name "Cozy Loft" \ No newline at end of file + And The apartment with name "Cozy Loft" no longer exists \ No newline at end of file From b8aca4241fc3ee0afd0b11551c5a0d22de6bc44b Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Thu, 17 Oct 2024 23:32:47 +0200 Subject: [PATCH 06/12] fix: apartment creation before deletion --- .../demo/steps/DeleteApartmentStepDefs.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java index 279735c..c667a08 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java @@ -2,6 +2,7 @@ import static org.junit.Assert.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -14,9 +15,12 @@ import io.cucumber.java.en.Given; import io.cucumber.java.en.When; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import java.nio.charset.StandardCharsets; +import java.time.ZonedDateTime; import java.util.List; +import java.util.Optional; public class DeleteApartmentStepDefs { @@ -33,8 +37,29 @@ public class DeleteApartmentStepDefs { private UserRepository userRepository; @Given("^There is an apartment registered with the name \"([^\"]*)\"$") - public void thereIsAnApartmentRegisteredWithTheName(String name) { + public void thereIsAnApartmentRegisteredWithTheName(String name) throws Exception { List apartments = apartmentRepository.findByName(name); + if(apartments.isEmpty()) { + Apartment apartment = new Apartment(); + apartment.setName(name); + apartment.setRegistrationDate(ZonedDateTime.now()); + + Optional ownerOptional = ownerRepository.findById(AuthenticationStepDefs.currentUsername); + + if (ownerOptional.isPresent()) { + apartment.setOwner(ownerOptional.get()); + } + + stepDefs.result = stepDefs.mockMvc.perform( + post("/apartments") + .contentType(MediaType.APPLICATION_JSON) + .content(stepDefs.mapper.writeValueAsString(apartment)) + .characterEncoding(StandardCharsets.UTF_8) + .with(AuthenticationStepDefs.authenticate())) + .andDo(print()) + .andExpect(status().isCreated()); + } + apartments = apartmentRepository.findByName(name); assertFalse("Apartment with name \"" + name + "\" should exist", apartments.isEmpty()); } From 14d132e7c4a4c5aeb03003f4ee8d1679ebc66fd8 Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Thu, 17 Oct 2024 23:42:28 +0200 Subject: [PATCH 07/12] fix: objectMapper --- .../eps/softarch/demo/steps/DeleteApartmentStepDefs.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java index c667a08..0ff173e 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java @@ -11,6 +11,7 @@ import cat.udl.eps.softarch.demo.repository.ApartmentRepository; import cat.udl.eps.softarch.demo.repository.OwnerRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; +import com.fasterxml.jackson.databind.ObjectMapper; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.When; @@ -36,6 +37,9 @@ public class DeleteApartmentStepDefs { @Autowired private UserRepository userRepository; + @Autowired + private ObjectMapper objectMapper; + @Given("^There is an apartment registered with the name \"([^\"]*)\"$") public void thereIsAnApartmentRegisteredWithTheName(String name) throws Exception { List apartments = apartmentRepository.findByName(name); @@ -53,7 +57,7 @@ public void thereIsAnApartmentRegisteredWithTheName(String name) throws Exceptio stepDefs.result = stepDefs.mockMvc.perform( post("/apartments") .contentType(MediaType.APPLICATION_JSON) - .content(stepDefs.mapper.writeValueAsString(apartment)) + .content(objectMapper.writeValueAsString(apartment)) .characterEncoding(StandardCharsets.UTF_8) .with(AuthenticationStepDefs.authenticate())) .andDo(print()) From d22d06ab68bb9540ac656d8c677da2e7deedcbc0 Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Thu, 17 Oct 2024 23:48:50 +0200 Subject: [PATCH 08/12] fix: apartmentRepository save --- .../udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java index 0ff173e..c3b001e 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java @@ -62,6 +62,8 @@ public void thereIsAnApartmentRegisteredWithTheName(String name) throws Exceptio .with(AuthenticationStepDefs.authenticate())) .andDo(print()) .andExpect(status().isCreated()); + + apartmentRepository.save(apartment); } apartments = apartmentRepository.findByName(name); assertFalse("Apartment with name \"" + name + "\" should exist", apartments.isEmpty()); From e1671c5e60ce29ff4930bfbd5e184a40495a6e4c Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Fri, 18 Oct 2024 16:37:39 +0200 Subject: [PATCH 09/12] fix: scenario given --- .../udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java | 4 ++-- src/test/resources/features/DeleteApartment.feature | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java index c3b001e..1f23abf 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java @@ -88,7 +88,7 @@ public void theApartmentWithNameNoLongerExists(String name) { List apartments = apartmentRepository.findByName(name); assertTrue("Apartment with name \"" + name + "\" should no longer exist", apartments.isEmpty()); } - +/* @Given("^There is an apartment registered with the address \"([^\"]*)\"$") public void thereIsAnApartmentRegisteredWithTheAddress(String address) { List apartments = apartmentRepository.findByAddress(address); @@ -113,5 +113,5 @@ public void iDeleteTheApartmentWithAddress(String address) throws Exception { public void theApartmentWithAddressNoLongerExists(String address) { List apartments = apartmentRepository.findByAddress(address); assertTrue("Apartment with address \"" + address + "\" should no longer exist", apartments.isEmpty()); - } + }*/ } \ No newline at end of file diff --git a/src/test/resources/features/DeleteApartment.feature b/src/test/resources/features/DeleteApartment.feature index e42fcf9..bed9214 100644 --- a/src/test/resources/features/DeleteApartment.feature +++ b/src/test/resources/features/DeleteApartment.feature @@ -4,7 +4,7 @@ Feature: Delete Apartment I want to delete apartments Scenario: Delete an apartment as Owner - Given There is a registered owner with username "owner" and password "password" and email "owner@example.com" + Given There is an apartment registered with the name "Cozy Loft" And I login as "owner" with password "password" When I delete the apartment with name "Cozy Loft" Then The response code is 201 From 54cd96b0ccb41287e54790d84658a8003d028b3d Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Tue, 22 Oct 2024 16:05:42 +0200 Subject: [PATCH 10/12] fix: login before checking --- src/test/resources/features/DeleteApartment.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/features/DeleteApartment.feature b/src/test/resources/features/DeleteApartment.feature index bed9214..4abe37b 100644 --- a/src/test/resources/features/DeleteApartment.feature +++ b/src/test/resources/features/DeleteApartment.feature @@ -4,8 +4,8 @@ Feature: Delete Apartment I want to delete apartments Scenario: Delete an apartment as Owner - Given There is an apartment registered with the name "Cozy Loft" - And I login as "owner" with password "password" + Given I login as "owner" with password "password" + And There is an apartment registered with the name "Cozy Loft" When I delete the apartment with name "Cozy Loft" Then The response code is 201 And The apartment with name "Cozy Loft" no longer exists \ No newline at end of file From ffd16b4de353b706ecfdfc770ffce755f432334e Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Tue, 22 Oct 2024 16:06:12 +0200 Subject: [PATCH 11/12] fix: success response code changed to 204 --- src/test/resources/features/DeleteApartment.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/features/DeleteApartment.feature b/src/test/resources/features/DeleteApartment.feature index 4abe37b..6d9ed9c 100644 --- a/src/test/resources/features/DeleteApartment.feature +++ b/src/test/resources/features/DeleteApartment.feature @@ -7,5 +7,5 @@ Feature: Delete Apartment Given I login as "owner" with password "password" And There is an apartment registered with the name "Cozy Loft" When I delete the apartment with name "Cozy Loft" - Then The response code is 201 + Then The response code is 204 And The apartment with name "Cozy Loft" no longer exists \ No newline at end of file From 98c7fabf51550d562c199900b38bdfba9662856e Mon Sep 17 00:00:00 2001 From: abelpesa03 Date: Tue, 22 Oct 2024 16:07:19 +0200 Subject: [PATCH 12/12] fix: double apartment creation --- .../udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java index 1f23abf..3a498a0 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java @@ -62,8 +62,6 @@ public void thereIsAnApartmentRegisteredWithTheName(String name) throws Exceptio .with(AuthenticationStepDefs.authenticate())) .andDo(print()) .andExpect(status().isCreated()); - - apartmentRepository.save(apartment); } apartments = apartmentRepository.findByName(name); assertFalse("Apartment with name \"" + name + "\" should exist", apartments.isEmpty());