-
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.
- Loading branch information
1 parent
0efda3c
commit 03b945c
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/test/java/cat/udl/eps/softarch/demo/steps/StepUpdateAdoption.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,91 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Adoptions; | ||
import cat.udl.eps.softarch.demo.repository.AdoptionsRepository; | ||
import cat.udl.eps.softarch.demo.repository.PetRepository; | ||
import cat.udl.eps.softarch.demo.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import io.cucumber.java.en.And; | ||
import io.cucumber.java.en.Given; | ||
import io.cucumber.java.en.Then; | ||
import io.cucumber.java.en.When; | ||
import org.springframework.http.MediaType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
|
||
public class StepUpdateAdoption { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@Autowired | ||
private AdoptionsRepository adoptionsRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@When("^Update the adoption with id (\\d+) User with username \"([^\"]*)\"$") | ||
public void updateAdoptionUsername(int adoption_id, String final_username) { | ||
Long id_adoption = (long) adoption_id; | ||
Optional<Adoptions> opt = adoptionsRepository.findById(id_adoption); | ||
|
||
opt.ifPresent(adoptions -> { | ||
|
||
userRepository.findById(final_username).ifPresent(user -> { | ||
adoptions.setUser(user); | ||
adoptions.setDateOfAdoption(LocalDateTime.now()); | ||
}); | ||
try { | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
patch("/adoptions/{id}", adoptions.getId()) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(adoptions)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
|
||
@Then("^The adoption with id (\\d+) should have been updated to username \"([^\"]*)\"") | ||
public void theAdoptionUserForThePetWithIdShouldBeUpdatedToUsername(int id_int, String username) { | ||
Long id = (long) id_int; | ||
Optional<Adoptions> opt = adoptionsRepository.findById(id); | ||
|
||
opt.ifPresent(adoptions -> { | ||
userRepository.findById(username).ifPresent(user -> assertEquals(user, adoptions.getUser())); | ||
}); | ||
} | ||
|
||
|
||
@And("^The adoption with id (\\d+) dateofAdoption is updated") | ||
public void theAdoptionDateofAdoptionIsUpdated(int adoption_id) { | ||
Long id = (long) adoption_id; | ||
Optional<Adoptions> opt = adoptionsRepository.findById(id); | ||
|
||
opt.ifPresent(adoptions -> { | ||
assertEquals(adoptions.getDateOfAdoption().getDayOfMonth(), LocalDateTime.now().getDayOfMonth()); | ||
assertEquals(adoptions.getDateOfAdoption().getMonth(), LocalDateTime.now().getMonth()); | ||
assertEquals(adoptions.getDateOfAdoption().getYear(), LocalDateTime.now().getYear()); | ||
}); | ||
} | ||
|
||
@Then("^The system should display an error message indicating the username \"([^\"]*)\" does not exist") | ||
public void theSystemShouldDisplayAnErrorMessageIndicatingTheUsernameDoesNotExist(String username) { | ||
assertThat(userRepository.findById(username)).isEmpty(); | ||
} | ||
} |
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,24 @@ | ||
Feature: Update an Adoption | ||
In order to manage pet adoptions | ||
As a User | ||
I want to be able to update the adoption status of a Pet | ||
|
||
Background: | ||
Given There is a registered user with username "username" and password "password" and email "[email protected]" | ||
And There is a registered user with username "username2" and password "password2" and email "[email protected]" | ||
And There is a registered user with username "username3" and password "password3" and email "[email protected]" | ||
|
||
Scenario: User successfully updates the adoption User of a Pet | ||
And I can login with username "username" and password "password" | ||
Given The Pet with id 1 exists | ||
Given The Pet with id 1 is adopted by user "username2" | ||
When Update the adoption with id 1 User with username "username3" | ||
Then The adoption with id 1 should have been updated to username "username3" | ||
And The adoption with id 1 dateofAdoption is updated | ||
|
||
Scenario: User successfully updates the adoption User of a Pet | ||
And I can login with username "username" and password "password" | ||
Given The Pet with id 1 exists | ||
Given The Pet with id 1 is adopted by user "username2" | ||
When Update the adoption with id 1 User with username "username4" | ||
Then The system should display an error message indicating the username "username4" does not exist |