-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from UdL-EPS-SoftArch/48-test-apartment-delete
48 test apartment delete
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/test/java/cat/udl/eps/softarch/demo/steps/DeleteApartmentStepDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
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.request.MockMvcRequestBuilders.post; | ||
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 com.fasterxml.jackson.databind.ObjectMapper; | ||
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 org.springframework.http.MediaType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class DeleteApartmentStepDefs { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private ApartmentRepository apartmentRepository; | ||
|
||
@Autowired | ||
private OwnerRepository ownerRepository; | ||
|
||
@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<Apartment> apartments = apartmentRepository.findByName(name); | ||
if(apartments.isEmpty()) { | ||
Apartment apartment = new Apartment(); | ||
apartment.setName(name); | ||
apartment.setRegistrationDate(ZonedDateTime.now()); | ||
|
||
Optional<Owner> ownerOptional = ownerRepository.findById(AuthenticationStepDefs.currentUsername); | ||
|
||
if (ownerOptional.isPresent()) { | ||
apartment.setOwner(ownerOptional.get()); | ||
} | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/apartments") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.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()); | ||
} | ||
|
||
@When("^I delete the apartment with name \"([^\"]*)\"$") | ||
public void iDeleteTheApartmentWithName(String name) throws Exception { | ||
List<Apartment> 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<Apartment> 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<Apartment> 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<Apartment> 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<Apartment> apartments = apartmentRepository.findByAddress(address); | ||
assertTrue("Apartment with address \"" + address + "\" should no longer exist", apartments.isEmpty()); | ||
}*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 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 204 | ||
And The apartment with name "Cozy Loft" no longer exists |