Skip to content

Commit

Permalink
added put test for adStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
Troter2 committed Nov 5, 2024
1 parent 4bbe429 commit ae89ed5
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.requestMatchers(HttpMethod.POST, "/apartments/*").hasAnyRole("OWNER")
.requestMatchers(HttpMethod.POST, "/advertisementStatuses").hasAnyRole("OWNER")
.requestMatchers(HttpMethod.DELETE, "/advertisementStatuses").hasAnyRole("OWNER")
.requestMatchers(HttpMethod.PUT, "/advertisementStatuses/*").hasAnyRole("OWNER")
.requestMatchers(HttpMethod.POST, "/users").anonymous()
.requestMatchers(HttpMethod.POST, "/users/*").denyAll()
.requestMatchers(HttpMethod.POST, "/rooms").authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,14 @@ public class DeleteAdvertisementStatusStepDefs {
private ObjectMapper objectMapper;


@And("^The advertisement status with name \"([^\"]*)\" no longer exists$")
public void theApartmentWithNameNoLongerExists(String name) {
List<AdvertisementStatus> apartments = advertisementStatusRepository.findByStatus(name);
assertTrue("Apartment with name \"" + name + "\" should no longer exist", apartments.isEmpty());
}

@When("I delete the advertisement status with status {string}")
public void iDeleteTheAdvertisementStatusWithStatus(String status) throws Exception {
AdvertisementStatus adStatus = advertisementStatusRepository.findByStatus(status).stream().findFirst().orElse(null);


if (adStatus == null) {
stepDefs.result = stepDefs.mockMvc.perform(
delete("/advertisements/{id}", 9999).accept(MediaType.APPLICATION_JSON)
delete("/advertisementStatuses/{id}", 9999).accept(MediaType.APPLICATION_JSON)
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.AdvertisementStatus;
import cat.udl.eps.softarch.demo.repository.AdvertisementStatusRepository;
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 static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

public class ModifyAdvertisementStatusStepDefs {
@Autowired
StepDefs stepDefs;

@Autowired
AdvertisementStatusRepository adStatusRepository;

private AdvertisementStatus adStatus;

private String previousStatus;

private long adStatusId;

@Given("^There is an advertisementStatus already created with status \"([^\"]*)\"$")
public void there_is_an_advertisement_status_already_created_with_description(String status) {
this.adStatus = new AdvertisementStatus();
this.adStatus.setStatus(status);
this.adStatusRepository.save(this.adStatus);
}

@When("^I modify an advertisementStatus with status \"([^\"]*)\" and change it to \"([^\"]*)\"$")
public void modify_advertisement_status(String status, String newStatus) throws Throwable {
AdvertisementStatus adStatus = adStatusRepository.findByStatus(status).stream().findFirst().orElse(null);
assertNotNull("Apartment with name \"" + status + "\" should exist", adStatus);
this.previousStatus = this.adStatus.getStatus();
this.adStatusId = adStatus.getId();
if (newStatus.equals("null")) {
adStatus.setStatus(null);
} else {
adStatus.setStatus(newStatus);
}

this.stepDefs.result = this.stepDefs.mockMvc.perform(put("/advertisementStatuses/" + this.adStatusId)
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(adStatus)) // Enviar el objeto actualizado
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());

}

@And("The advertisementStatus has not been modified to \"([^\"]*)\"$")
public void the_advertisement_status_has_not_been_modified(String descToCheck) throws Throwable {

AdvertisementStatus adStatusToCheck = this.adStatusRepository.findById(this.adStatusId).get();

assertNotEquals(adStatusToCheck.getStatus(), descToCheck);
assertEquals(adStatusToCheck.getStatus(), previousStatus);
}

@And("The advertisementStatus has been modified to \"([^\"]*)\"$")
public void the_advertisement_status_has_been_modified(String status) {

AdvertisementStatus adStatusToCheck = this.adStatusRepository.findById(this.adStatusId).get();

assertEquals(status, adStatusToCheck.getStatus());
}

@And("^The advertisementStatus has been modified with the description \"([^\"]*)\"$")
public void the_advertisement_status_has_been_modified_with_description(String newDescription) throws Throwable {

AdvertisementStatus adStatusToCheck = this.adStatusRepository.findById(this.adStatusId).get();

assertEquals(adStatusToCheck.getStatus(), newDescription);
assertNotEquals(adStatusToCheck.getStatus(), previousStatus);
}
}
40 changes: 40 additions & 0 deletions src/test/resources/features/ModifyAdvertisementStatus.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Feature: Modify an Advertisement Status
In order to make changes on a property
As an owner
I want to modify a property

Background:
Given There is a registered user with username "user" and password "password" and email "[email protected]"
Given There is a registered owner with username "owner" and password "password" and email "[email protected]"

Scenario: Modify an Advertisement Status when not logged in
Given I'm not logged in
And There is an advertisementStatus already created with status "Available"
When I modify an advertisementStatus with status "Available" and change it to "Occupied"
Then The response code is 401
And The error message is "Unauthorized"
And The advertisementStatus has not been modified to "Occupied"

Scenario: Modify a Advertisement Status when logged in as a user
Given I login as "user" with password "password"
And There is an advertisementStatus already created with status "Available"
When I modify an advertisementStatus with status "Available" and change it to "Occupied"
Then The response code is 403
And The error message is "Forbidden"
And The advertisementStatus has not been modified to "Occupied"

Scenario: Modify a Advertisement Status when logged in as a owner
Given I login as "owner" with password "password"
And There is an advertisementStatus already created with status "Available"
When I modify an advertisementStatus with status "Available" and change it to "Occupied"
Then The response code is 204
And The advertisementStatus has been modified to "Occupied"

Scenario: Modify a Advertisement Status when logged in as a owner and status null
Given I login as "owner" with password "password"
And There is an advertisementStatus already created with status "Available"
When I modify an advertisementStatus with status "Available" and change it to "null"
Then The response code is 400
And The advertisementStatus has not been modified to "null"


0 comments on commit ae89ed5

Please sign in to comment.