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 update #108

Merged
merged 13 commits into from
Nov 13, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package cat.udl.eps.softarch.demo.steps;

import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
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.Then;
import io.cucumber.java.en.When;
import jakarta.transaction.Transactional;
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.Objects;
import java.util.Optional;

public class UpdateApartmentStepDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private ApartmentRepository apartmentRepository;

@Autowired
private OwnerRepository ownerRepository;

@Autowired
private ObjectMapper objectMapper;

@Given("Exists an apartment registered as {string}")
public void existsAnApartmentRegisteredWithTheName(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);

ownerOptional.ifPresent(apartment::setOwner);

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());
}

@Given("Exists an apartment registered as {string} with description {string}")
public void existsAnApartmentRegisteredWithTheDescription(String name, String description) throws Exception {
List<Apartment> apartments = apartmentRepository.findByName(name);
if (apartments.isEmpty()) {
Apartment apartment = new Apartment();
apartment.setName(name);
apartment.setRegistrationDate(ZonedDateTime.now());
apartment.setDescription(description);

Optional<Owner> ownerOptional = ownerRepository.findById(AuthenticationStepDefs.currentUsername);

ownerOptional.ifPresent(apartment::setOwner);

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());
}

@Transactional
@When("I update it from name {string} to {string}")
public void iUpdateTheApartmentsName(String name, String new_name) throws Exception {
List<Apartment> apartments = apartmentRepository.findByName(name);
assertFalse("Apartment with name \"" + name + "\" should exist", apartments.isEmpty());

Apartment apartment = apartments.get(0);
apartment.setName(new_name);

stepDefs.result = stepDefs.mockMvc.perform(
put("/apartments/" + apartment.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(apartment))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print())
.andExpect(status().isNoContent());
}

@Transactional
@When("I update {string} from description {string} to {string}")
public void iUpdateTheApartmentsDescription(String name, String description, String new_description) throws Exception {
List<Apartment> apartments = apartmentRepository.findByName(name);
assertFalse("Apartment with name \"" + name + "\" should exist", apartments.isEmpty());

Apartment apartment = apartments.get(0);

assertEquals(apartment.getDescription(), description);

apartment.setDescription(new_description);

stepDefs.result = stepDefs.mockMvc.perform(
put("/apartments/" + apartment.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(apartment))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print())
.andExpect(status().isNoContent());
}

@Then("The apartment {string} name is {string}")
public void theApartmentHasUpdatedNameTo(String name, String new_name) {
List<Apartment> oldApartments = apartmentRepository.findByName(name);
assertTrue("Apartment with name \"" + name + "\" should not exist", oldApartments.isEmpty());

List<Apartment> newApartments = apartmentRepository.findByName(new_name);
assertFalse("Apartment with name \"" + new_name + "\" should exist", newApartments.isEmpty());
}

@Then("The apartment {string} description is {string}")
public void theApartmentHasUpdatedDescriptionTo(String name, String new_description) {
List<Apartment> apartments = apartmentRepository.findByName(name);
assertFalse("Apartment with name \"" + name + "\" should exist", apartments.isEmpty());

Apartment apartment = apartments.get(0);
assertEquals(apartment.getDescription(), new_description);
}
}
18 changes: 18 additions & 0 deletions src/test/resources/features/UpdateApartment.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: Update Apartment
In order to manage apartments as an Owner
I want to update an apartment's feature

Scenario: Update an apartment's name as Owner
Given I login as "owner" with password "password"
And Exists an apartment registered as "Cozy Loft"
When I update it from name "Cozy Loft" to "Super Cozy Loft"
Then The response code is 204
And The apartment "Cozy Loft" name is "Super Cozy Loft"


Scenario: Update an apartment's description as Owner
Given I login as "owner" with password "password"
And Exists an apartment registered as "Cozy Loft" with description "A cozy loft in the center of Barcelona"
When I update "Cozy Loft" from description "A cozy loft in the center of Barcelona" to "A super cozy loft in the center of Barcelona!"
Then The response code is 204
And The apartment "Cozy Loft" description is "A super cozy loft in the center of Barcelona!"
Loading