-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from UdL-EPS-SoftArch/Feature-modifyFavourite_v2
Feature-modify favourite
- Loading branch information
Showing
4 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/test/java/cat/udl/eps/softarch/demo/steps/modifyFavouriteStepDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |