-
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 branch 'main' into Feature-validate-shelter-certificate
- Loading branch information
Showing
13 changed files
with
431 additions
and
6 deletions.
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
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
31 changes: 31 additions & 0 deletions
31
src/main/java/cat/udl/eps/softarch/demo/handler/ShelterEventHandler.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,31 @@ | ||
package cat.udl.eps.softarch.demo.handler; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Shelter; | ||
import cat.udl.eps.softarch.demo.repository.ShelterRepository; | ||
import org.springframework.data.rest.core.annotation.HandleAfterSave; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeCreate; | ||
import org.springframework.data.rest.core.annotation.RepositoryEventHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
@Component | ||
@RepositoryEventHandler | ||
public class ShelterEventHandler { | ||
final ShelterRepository shelterRepository; | ||
|
||
public ShelterEventHandler(ShelterRepository shelterRepository) { | ||
this.shelterRepository = shelterRepository; | ||
} | ||
|
||
@HandleBeforeCreate | ||
public void handleShelterPreCreate(Shelter shelter) { | ||
shelter.setCreatedAt(ZonedDateTime.now()); | ||
shelter.setUpdatedAt(ZonedDateTime.now()); | ||
|
||
} | ||
@HandleAfterSave | ||
public void handleShelterPostSave(Shelter shelter) { | ||
shelter.setUpdatedAt(ZonedDateTime.now()); | ||
} | ||
} |
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
96 changes: 96 additions & 0 deletions
96
src/test/java/cat/udl/eps/softarch/demo/steps/CreateShelterStepDefs.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,96 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import cat.udl.eps.softarch.demo.domain.*; | ||
import cat.udl.eps.softarch.demo.repository.AdminRepository; | ||
import cat.udl.eps.softarch.demo.repository.ShelterRepository; | ||
import cat.udl.eps.softarch.demo.repository.ShelterVolunteerRepository; | ||
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 jakarta.validation.constraints.AssertTrue; | ||
import org.json.JSONObject; | ||
import org.junit.Assert; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.ZonedDateTime; | ||
|
||
public class CreateShelterStepDefs { | ||
@Autowired | ||
|
||
private StepDefs stepDefs; | ||
@Autowired | ||
private ShelterRepository shelterRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private ShelterVolunteerRepository ShelterVolunteerRepository; | ||
|
||
@Autowired | ||
private AdminRepository adminRepository; | ||
|
||
@Given("^There is a registered admin with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") | ||
public void thereIsARegisteredAdminWithNameAndPasswordAndEmail(String username, String password, String email) throws Throwable { | ||
if (!adminRepository.existsById(username)) { | ||
Admin user = new Admin(); | ||
user.setEmail(email); | ||
user.setId(username); | ||
user.setPassword(password); | ||
user.encodePassword(); | ||
adminRepository.save(user); | ||
|
||
} | ||
} | ||
|
||
@When("^I create a shelter with a name \"([^\"]*)\", email \"([^\"]*)\" and phone \"([^\"]*)\" and location \"([^\"]*)\"$") | ||
public void iCreateAShelterWithAName(String name, String email, String mobile, String location) throws Throwable {; | ||
Shelter newshelter = new Shelter(); | ||
newshelter.setName(name); | ||
newshelter.setEmail(email); | ||
newshelter.setMobile(mobile); | ||
newshelter.setCreatedAt(ZonedDateTime.now()); | ||
newshelter.setUpdatedAt(ZonedDateTime.now()); | ||
//Following code is doing a post request to /shelters storing the response in stepDefs.result to test REST functionalty | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/shelters") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(newshelter)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
|
||
} | ||
|
||
@And("^There is (\\d+) Shelter created$") | ||
public void thereIsShelterCreated(int sheltersCreatedNum) { | ||
Assert.assertEquals("Shelters created", sheltersCreatedNum, shelterRepository.count()); | ||
} | ||
|
||
@Given("^There is no shelter registered with the name \"([^\"]*)\"$") | ||
public void thereIsNoShelterRegisteredWithTheName(String name) { | ||
Assert.assertTrue("Shelter with name \"" | ||
+ name + "\" shouldn't exist", shelterRepository.findByName(name).isEmpty()); | ||
} | ||
|
||
@Given("^There is a registered volunteer with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$") | ||
public void thereIsARegisteredVolunteerWithNameAndPasswordAndEmail(String name, String password, String email) { | ||
if(!ShelterVolunteerRepository.existsById(name)) { | ||
ShelterVolunteer volunteer = new ShelterVolunteer(); | ||
volunteer.setEmail(email); | ||
volunteer.setId(name); | ||
volunteer.setPassword(password); | ||
volunteer.encodePassword(); | ||
ShelterVolunteerRepository.save(volunteer); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/cat/udl/eps/softarch/demo/steps/DeleteShelterStepDefs.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,49 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Shelter; | ||
import cat.udl.eps.softarch.demo.repository.ShelterRepository; | ||
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.util.List; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
|
||
public class DeleteShelterStepDefs { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private ShelterRepository shelterRepository; | ||
|
||
|
||
@Given("There is a created shelter with name {string}, email {string} and phone {string}") | ||
public void thereIsACreatedShelterWithNameEmailAndPhone(String name, String email, String phone) { | ||
if (shelterRepository.findByName(name).isEmpty()) { | ||
Shelter shelter = new Shelter(); | ||
shelter.setName(name); | ||
shelter.setEmail(email); | ||
shelter.setMobile(phone); | ||
shelter.setLocatedAt(null); | ||
|
||
shelterRepository.save(shelter); | ||
} | ||
} | ||
|
||
@When("I try to delete Shelter with name {string}") | ||
public void iTryToDeleteShelterWithName(String name) throws Exception { | ||
List<Shelter> shelterList = shelterRepository.findByName(name); | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
delete("/shelters/{id}", shelterList.isEmpty() ? "0" : shelterList.get(0).getId()) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} | ||
|
||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/cat/udl/eps/softarch/demo/steps/LoginStepDefs.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,60 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.User; | ||
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 static org.hamcrest.Matchers.is; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
public class LoginStepDefs { | ||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Given("^There isn't registered user with username \"([^\"]*)\"$") | ||
public void thereIsNoRegisteredUserWithUsername(String user) { | ||
Assert.assertFalse("User \"" | ||
+ user + "\"shouldn't exist", | ||
userRepository.existsById(user)); | ||
} | ||
|
||
@Given("^There is a registered user with username \"([^\"]*)\" and password \"([^\"]*)\"$") | ||
public void thereIsARegisteredUserWithUsernameAndPassword(String username, String password) { | ||
if(!userRepository.existsById(username)){ | ||
User user = new User(); | ||
user.setId(username); | ||
user.setEmail("[email protected]"); | ||
user.setPassword(password); | ||
user.encodePassword(); | ||
userRepository.save(user); | ||
} | ||
} | ||
|
||
|
||
|
||
@When("^I login with username \"([^\"]*)\" and password \"([^\"]*)\"$") | ||
public void iLoginWithUsernameAndPassword(String username, String password) throws Exception { | ||
AuthenticationStepDefs.currentUsername = username; | ||
AuthenticationStepDefs.currentPassword = password; | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
get("/identity") | ||
.accept(MediaType.APPLICATION_JSON) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} | ||
|
||
|
||
|
||
} |
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
Oops, something went wrong.