Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

48 test apartment delete #71

Merged
merged 13 commits into from
Oct 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface ApartmentRepository extends CrudRepository<Apartment, Long>, Pa
List<Apartment> findByCity(@Param("city") String city);
List<Apartment> findByRegistrationDate(@Param("registrationDate") Date registrationDate);
List<Apartment> findByName(@Param("name") String name);
List<Apartment> findByAddress(@Param("address") String address);
}
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());
}*/
}
11 changes: 11 additions & 0 deletions src/test/resources/features/DeleteApartment.feature
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
Loading