Skip to content

Commit

Permalink
Merge pull request #91 from UdL-EPS-SoftArch/Test-upgradeadvertisemen…
Browse files Browse the repository at this point in the history
…ttest

Test upgradeadvertisement
  • Loading branch information
rogargon authored Nov 5, 2024
2 parents 23f219f + 5f20110 commit fe2d483
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.*;
import cat.udl.eps.softarch.demo.repository.AdvertisementRepository;
import cat.udl.eps.softarch.demo.repository.AdvertisementStatusRepository;
import cat.udl.eps.softarch.demo.repository.ApartmentRepository;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

public class UpdateAdvertisementStepDefs {

@Autowired
private AdvertisementStatusRepository advertisementStatusRepository;

@Autowired
private AdvertisementRepository advertisementRepository;

@Autowired
private ApartmentRepository apartmentRepository;

@Autowired
private StepDefs stepDefs;


@When("I update an advertisement with title {string} to have name {string} and address {string}")
public void iUpdateAdvertisement(String title, String newTitle, String newAddress) throws Exception {
List<Advertisement> ads = advertisementRepository.findByTitle(title);

if (ads.isEmpty()) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

Advertisement advertisement = ads.get(0);
advertisement.setTitle(newTitle);
advertisement.setAddress(newAddress);
advertisementRepository.save(advertisement);

stepDefs.result = stepDefs.mockMvc.perform(put("/advertisements/" + advertisement.getId())
.with(AuthenticationStepDefs.authenticate())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(advertisement))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}



@Then("The advertisement with title {string} should have name {string} and address {string}")
public void theAdvertisementWithTitleShouldHaveNameAndAddress(String title, String expectedName, String expectedAddress) throws Exception {
Advertisement updatedAdvertisement = advertisementRepository.findByTitle(title).stream().findFirst().orElse(null);

if (updatedAdvertisement == null) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

assertEquals(expectedName, updatedAdvertisement.getTitle());
assertEquals(expectedAddress, updatedAdvertisement.getAddress());
}

@When("I update the advertisement with title {string} to have price {string}")
public void iUpdateAdvertisementPrice(String title, String newPrice) throws Exception {
List<Advertisement> ads = advertisementRepository.findByTitle(title);

if (ads.isEmpty()) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

Advertisement advertisement = ads.get(0);
advertisement.setPrice(new BigDecimal(newPrice));

stepDefs.result = stepDefs.mockMvc.perform(put("/advertisements/" + advertisement.getId())
.with(AuthenticationStepDefs.authenticate())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(advertisement))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}

@Then("The advertisement with title {string} should have price {string}")
public void theAdvertisementWithTitleShouldHavePrice(String title, String expectedPrice) throws Exception {
Advertisement updatedAdvertisement = advertisementRepository.findByTitle(title).stream().findFirst().orElse(null);

if (updatedAdvertisement == null) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

assertEquals(new BigDecimal(expectedPrice), updatedAdvertisement.getPrice());
}

@When("I update the advertisement with title {string} to have zipCode {string}")
public void iUpdateAdvertisementZipCode(String title, String newZipCode) throws Exception {
List<Advertisement> ads = advertisementRepository.findByTitle(title);

if (ads.isEmpty()) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

Advertisement advertisement = ads.get(0);
advertisement.setZipCode(newZipCode);
advertisementRepository.save(advertisement);

stepDefs.result = stepDefs.mockMvc.perform(put("/advertisements/" + advertisement.getId())
.with(AuthenticationStepDefs.authenticate())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(advertisement))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}

@Then("The advertisement with title {string} should have zipCode {string}")
public void theAdvertisementWithTitleShouldHaveZipCode(String title, String expectedZipCode) throws Exception {
Advertisement updatedAdvertisement = advertisementRepository.findByTitle(title).stream().findFirst().orElse(null);

if (updatedAdvertisement == null) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

assertEquals(expectedZipCode, updatedAdvertisement.getZipCode());
}

@When("I update the advertisement with title {string} to have country {string}")
public void iUpdateAdvertisementCountry(String title, String newCountry) throws Exception {
List<Advertisement> ads = advertisementRepository.findByTitle(title);

if (ads.isEmpty()) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

Advertisement advertisement = ads.get(0);
advertisement.setCountry(newCountry);
advertisementRepository.save(advertisement);

stepDefs.result = stepDefs.mockMvc.perform(put("/advertisements/" + advertisement.getId())
.with(AuthenticationStepDefs.authenticate())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(advertisement))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}

@Then("The advertisement with title {string} should have country {string}")
public void theAdvertisementWithTitleShouldHaveCountry(String title, String expectedCountry) throws Exception {
Advertisement updatedAdvertisement = advertisementRepository.findByTitle(title).stream().findFirst().orElse(null);

if (updatedAdvertisement == null) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

assertEquals(expectedCountry, updatedAdvertisement.getCountry());
}

@When("I update the advertisement with title {string} to have description {string}")
public void iUpdateAdvertisementDescription(String title, String newDescription) throws Exception {
List<Advertisement> ads = advertisementRepository.findByTitle(title);

if (ads.isEmpty()) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

Advertisement advertisement = ads.get(0);
advertisement.setDescription(newDescription);
advertisementRepository.save(advertisement);

stepDefs.result = stepDefs.mockMvc.perform(put("/advertisements/" + advertisement.getId())
.with(AuthenticationStepDefs.authenticate())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(advertisement))
.characterEncoding(StandardCharsets.UTF_8))
.andDo(print());
}

@Then("The advertisement with title {string} should have description {string}")
public void theAdvertisementWithTitleShouldHaveDescription(String title, String expectedDescription) throws Exception {
Advertisement updatedAdvertisement = advertisementRepository.findByTitle(title).stream().findFirst().orElse(null);

if (updatedAdvertisement == null) {
throw new IllegalArgumentException("Advertisement with title " + title + " not found");
}

assertEquals(expectedDescription, updatedAdvertisement.getDescription());
}


}
60 changes: 60 additions & 0 deletions src/test/resources/features/UpdateAdvertisement.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Feature: Update Advertisement
In order to update advertisements in the system
As a user
I want to be able to create and update advertisements

Background:
Given There is a registered user with username "owner" and password "password" and email "[email protected]"
Given There is an existing apartment with id "1" named "Luxury Suite"
And There is an existing advertisement status "Active"

Scenario: Create and update an advertisement without logged in
Given I'm not logged in
Given There is an advertisement with title "Luxury Suite Ad", description "A beautiful suite", price "2000", zipCode "12345", address "High Street 123", country "Wonderland", status "Active", apartment title "Luxury Suite"
When I update an advertisement with title "Luxury Suite Ad" to have name "Updated Suite Ad" and address "Updated Street 123"
Then The advertisement with title "Updated Suite Ad" should have name "Updated Suite Ad" and address "Updated Street 123"
And The response code is 401

Scenario: Create and update an advertisement
Given I login as "owner" with password "password"
Given There is an advertisement with title "Luxury Suite Ad", description "A beautiful suite", price "2000", zipCode "12345", address "High Street 123", country "Wonderland", status "Active", apartment title "Luxury Suite"
When I update an advertisement with title "Luxury Suite Ad" to have name "Updated Suite Ad" and address "Updated Street 123"
Then The advertisement with title "Updated Suite Ad" should have name "Updated Suite Ad" and address "Updated Street 123"
And The response code is 200

Scenario: Update advertisement price
Given I login as "owner" with password "password"
Given There is an advertisement with title "Luxury Suite Ad", description "A beautiful suite", price "2000", zipCode "12345", address "High Street 123", country "Wonderland", status "Active", apartment title "Luxury Suite"
When I update the advertisement with title "Luxury Suite Ad" to have price "2500"
Then The advertisement with title "Luxury Suite Ad" should have price "2500.00"
And The response code is 200

Scenario: Attempt to update advertisement with invalid data
Given I login as "owner" with password "password"
Given There is an advertisement with title "Luxury Suite Ad", description "A beautiful suite", price "2000", zipCode "12345", address "High Street 123", country "Wonderland", status "Active", apartment title "Luxury Suite"
When I update the advertisement with title "Luxury Suite Ad" to have price "-100.00"
Then The response code is 400
And The error message is "must be greater than or equal to 0.01"

Scenario: Update advertisement zip code
Given I login as "owner" with password "password"
Given There is an advertisement with title "Luxury Suite Ad", description "A beautiful suite", price "2000", zipCode "12345", address "High Street 123", country "Wonderland", status "Active", apartment title "Luxury Suite"
When I update the advertisement with title "Luxury Suite Ad" to have zipCode "54321"
Then The advertisement with title "Luxury Suite Ad" should have zipCode "54321"
And The response code is 200

Scenario: Update advertisement country
Given I login as "owner" with password "password"
Given There is an advertisement with title "Luxury Suite Ad", description "A beautiful suite", price "2000", zipCode "12345", address "High Street 123", country "Wonderland", status "Active", apartment title "Luxury Suite"
When I update the advertisement with title "Luxury Suite Ad" to have country "Dreamland"
Then The advertisement with title "Luxury Suite Ad" should have country "Dreamland"
And The response code is 200

Scenario: Update advertisement description
Given I login as "owner" with password "password"
Given There is an advertisement with title "Luxury Suite Ad", description "A beautiful suite", price "2000", zipCode "12345", address "High Street 123", country "Wonderland", status "Active", apartment title "Luxury Suite"
When I update the advertisement with title "Luxury Suite Ad" to have description "An updated beautiful suite"
Then The advertisement with title "Luxury Suite Ad" should have description "An updated beautiful suite"
And The response code is 200


0 comments on commit fe2d483

Please sign in to comment.