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

45 feature test visit entity #109

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Advertisement;
import cat.udl.eps.softarch.demo.domain.Visit;
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository;
import cat.udl.eps.softarch.demo.repository.VisitRepository;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;

import java.time.ZonedDateTime;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class ModifyVisitSteps {

@Autowired
private StepDefs stepDefs;

@Autowired
private VisitRepository visitRepository;

@Autowired
private AdvertisementRepository advertisementRepository;

private MvcResult result;

@When("I modify the visit request to the advertisement with title {string} with new date {string}")
public void iModifyTheVisitRequestToTheAdvertisementWithTitleWithNewDate(String title, String newDate) throws Exception {
Advertisement advertisement = advertisementRepository.findByTitle(title).get(0);
Visit visit = visitRepository.findByAdvertisement(advertisement).get(0);
visit.setVisitDateTime(ZonedDateTime.parse(newDate));

result = stepDefs.mockMvc.perform(put("/visits/" + visit.getId())
.contentType(MediaType.APPLICATION_JSON)
.content("{\"when\": \"" + newDate + "\"}")
.with(AuthenticationStepDefs.authenticate()))
.andDo(print())
.andExpect(status().isNoContent())
.andReturn();
}

@Then("The visit is successfully modified")
public void theVisitIsSuccessfullyModified() {
assertNotNull(result, "Result should not be null after visit modification");
Assertions.assertEquals(204, result.getResponse().getStatus(), "Expected HTTP status 200 OK");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@
import cat.udl.eps.softarch.demo.repository.AdvertisementStatusRepository;
import cat.udl.eps.softarch.demo.repository.ApartmentRepository;
import cat.udl.eps.softarch.demo.repository.VisitRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertNotNull;
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;
Expand Down Expand Up @@ -57,6 +52,7 @@ public void createAdvertisementStatus(String status) {
advertisementStatusRepository.save(advertisementStatus);

}

@Given("There is an advertisement with title {string} and address {string}")
public void thereIsAnAdvertisementWithTitleAndAddress(String title, String address) {
advertisement = new Advertisement();
Expand All @@ -83,10 +79,10 @@ public void iRequestAVisitToTheAdvertisementWithTitle(String title) throws Excep

Advertisement advertisement;

if(title.equals("Invalid Advertisement"))
advertisement = null;
else {
advertisement = advertisementRepository.findByTitle(title).get(0);
if (title.equals("Invalid Advertisement"))
advertisement = null;
else {
advertisement = advertisementRepository.findByTitle(title).get(0);
}

String visitDateTime = ZonedDateTime.now().plusDays(30).toString();
Expand All @@ -101,7 +97,7 @@ public void iRequestAVisitToTheAdvertisementWithTitle(String title) throws Excep

String jsonContent = objectMapper.writeValueAsString(visit);

if(advertisement != null) {
if (advertisement != null) {
result = stepDefs.mockMvc.perform(post("/visits")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonContent)
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/features/ModifyVisit.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Created by abdee at 10/11/24
Feature: Modify Visit
As a user
I want to modify a visit to an advertisement
So that I can change the visit details

Scenario: Modify visit request
Given There is an advertisement with title "Cozy Loft" and address "Carrer de les Flors 10"
And I login as "demo" with password "password"
And I request a visit to the advertisement with title "Cozy Loft"
When I modify the visit request to the advertisement with title "Cozy Loft" with new date "2025-08-01T10:00:00Z"
Then The visit is successfully modified
Loading