Skip to content

Commit

Permalink
Merge pull request #96 from UdL-EPS-SoftArch/Feature-modifyFavourite_v2
Browse files Browse the repository at this point in the history
Feature-modify favourite
  • Loading branch information
elskater98 authored May 8, 2024
2 parents 325af7f + 9574bf8 commit 038d1c5
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cat.udl.eps.softarch.demo.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -11,9 +13,11 @@
@Data
public class FavouritedPets extends UriEntity<Long> {
@Id
@NotNull
@GeneratedValue
Long id;

@NotBlank
String userId;
@NotNull
Long petId;
}
2 changes: 2 additions & 0 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cat.udl.eps.softarch.demo.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand All @@ -9,6 +10,7 @@
@Data
public class Pet extends UriEntity<Long> {
@Id
@NotNull
@GeneratedValue
Long id;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.FavouritedPets;
import cat.udl.eps.softarch.demo.domain.Pet;
import cat.udl.eps.softarch.demo.repository.FavouritedPetsRepository;
import cat.udl.eps.softarch.demo.repository.PetRepository;
import cat.udl.eps.softarch.demo.repository.UserRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

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

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;

public class modifyFavouriteStepDefs {
@Autowired
private UserRepository userRepository;
@Autowired
private PetRepository petRepository;
@Autowired
private FavouritedPetsRepository favouriteRepository;
@Autowired
private StepDefs stepDefs;
private Long petId;
private int petListSize;

@Given("^There are two registered pets$")
public void thereAreRegisteredPets() {
Pet pet1 = new Pet();
//pet.setId(petToCreate);
petRepository.save(pet1);

Pet pet2 = new Pet();
//pet.setId(petToFavourite);
petRepository.save(pet2);
}

@Given("^User \"([^\"]*)\" has pet \"([^\"]*)\" set as favourite$")
public void userHasPetSetAsFavourite(String userId, Long petId) throws Throwable {
FavouritedPets newPet;
if (petRepository.existsById(petId)) {
newPet = new FavouritedPets();
newPet.setPetId(petId);
newPet.setUserId(userId);
} else {
newPet = null;
}
stepDefs.mockMvc.perform(
post("/favouritedPetses")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(newPet))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}

@When("^I press the favouritePet button for the pet with id \"([^\"]*)\"$")
public void iPressTheFavouriteButton(Long favouritedPet) throws Throwable {
List<FavouritedPets> petList = favouriteRepository.findByUserId(AuthenticationStepDefs.currentUsername);
//Needed later for checking correct operation
petListSize = petList.size();
petId = favouritedPet;

boolean found = false;

if (!petList.isEmpty()) {
for (FavouritedPets value : petList) {
if (favouritedPet.equals(value.getPetId())) {
found = true;
Long entryId = favouriteRepository.findByUserIdAndPetId(AuthenticationStepDefs.currentUsername, petId).get(0).getId();
stepDefs.result = stepDefs.mockMvc.perform(delete(String.format("/favouritedPetses/%s", entryId))
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}
}
}
if (!found) {
FavouritedPets newPet = new FavouritedPets();
newPet.setPetId(favouritedPet);
newPet.setUserId(AuthenticationStepDefs.currentUsername);
stepDefs.result = stepDefs.mockMvc.perform(
post("/favouritedPetses")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(newPet))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}
}

@And("The entry on the relation \"favourites\" is created")
public void theEntryIsCreated() {
Assert.assertEquals(favouriteRepository.findByUserId(AuthenticationStepDefs.currentUsername).size(), (petListSize + 1));
}

@And("The entry on the relation \"favourites\" is deleted")
public void theEntryIsDeleted() {
Assert.assertEquals(favouriteRepository.findByUserId(AuthenticationStepDefs.currentUsername).size(), (petListSize - 1));
}

private boolean favouritedByUser() {
List<FavouritedPets> petList = favouriteRepository.findByUserId(AuthenticationStepDefs.currentUsername);
boolean found = false;
System.out.println("Size of petList (last step):");
System.out.println(petList.size());
for (FavouritedPets value : petList) {
if (petId.equals(value.getPetId())) {
found = true;
break;
}
}
return found;
}
}
37 changes: 37 additions & 0 deletions src/test/resources/features/modifyFavourite.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Feature: Modify Favourite
In order to keep a register of pets i liked
As a user
I want to mark or unmark a pet as favourite

Background:
Given There is a registered user with username "testuser" and password "password" and email "[email protected]"
Given There are two registered pets


Scenario: Mark as favourite
Given I login as "testuser" with password "password"
When I press the favouritePet button for the pet with id "1"
Then The entry on the relation "favourites" is created
And The response code is 201

Scenario: Unmark as favourite
Given I login as "testuser" with password "password"
Given User "testuser" has pet "2" set as favourite
When I press the favouritePet button for the pet with id "2"
Then The response code is 200
And The entry on the relation "favourites" is deleted

Scenario: Mark as favourite while i'm not logged in
Given I'm not logged in
When I press the favouritePet button for the pet with id "12345678"
Then The response code is 401
And The error message is "Unauthorized"

# This should not happen in real implementation, so we'll just let the entry be created since:
# 1. This won't happen on a real test case
# 2. The "wrong" entry will just never get accessed
Scenario: Mark as favourite a non existing pet
Given I login as "testuser" with password "password"
When I press the favouritePet button for the pet with id "87654321"
Then The entry on the relation "favourites" is created
And The response code is 201

0 comments on commit 038d1c5

Please sign in to comment.