Skip to content

Commit

Permalink
Merge branch 'main' into crud-Shelter
Browse files Browse the repository at this point in the history
  • Loading branch information
vGerJ02 committed May 16, 2024
2 parents 2bd3b16 + 466e3b2 commit 7a41e8f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion EntitiesModel.puml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Pet {
age: String
description: String
breed: String

img: String
}

class Shelter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cat.udl.eps.softarch.demo.config;

import cat.udl.eps.softarch.demo.domain.ShelterVolunteer;
import cat.udl.eps.softarch.demo.domain.User;
import cat.udl.eps.softarch.demo.repository.UserRepository;
import cat.udl.eps.softarch.demo.repository.ShelterVolunteerRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import jakarta.annotation.PostConstruct;
import java.util.Arrays;

Expand All @@ -13,9 +17,11 @@ public class DBInitialization {
@Value("${spring.profiles.active:}")
private String activeProfiles;
private final UserRepository userRepository;
private final ShelterVolunteerRepository shelterVolunteerRepository;

public DBInitialization(UserRepository userRepository) {
public DBInitialization(UserRepository userRepository, ShelterVolunteerRepository shelterVolunteerRepository) {
this.userRepository = userRepository;
this.shelterVolunteerRepository = shelterVolunteerRepository;
}

@PostConstruct
Expand All @@ -29,6 +35,17 @@ public void initializeDatabase() {
user.encodePassword();
userRepository.save(user);
}

// Default ShelterVolunteer
if (!shelterVolunteerRepository.existsById("volunteer")) {
ShelterVolunteer volunteer = new ShelterVolunteer();
volunteer.setEmail("[email protected]");
volunteer.setId("volunteer");
volunteer.setPassword(defaultPassword);
volunteer.encodePassword();
shelterVolunteerRepository.save(volunteer);
}

if (Arrays.asList(activeProfiles.split(",")).contains("test")) {
// Testing instances
if (!userRepository.existsById("test")) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/cat/udl/eps/softarch/demo/domain/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
@Data
public class Pet extends UriEntity<Long> {
@Id
@NotNull
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;


Expand All @@ -23,7 +22,7 @@ public class Pet extends UriEntity<Long> {
String age;
String description;
String breed;

String img;

@ManyToOne
public Shelter isIn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public void thereIsAShelterVolunteerWithUsernameInTheShelter(String username, St
volunteer.setPassword(password);
volunteer.encodePassword();
shelterVolunteerRepository.save(volunteer);
} else {
shelterVolunteerRepository.findById(username).ifPresent(shelterVolunteer -> {
shelterVolunteer.setUserShelter(shelterRepository.findByName(shelterName).get(0));
shelterVolunteerRepository.save(shelterVolunteer);
});

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import org.json.JSONObject;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
Expand Down Expand Up @@ -89,10 +86,9 @@ public void thenAdminShouldVerifyTheCertificateValidityAssociatedWithAShelterWit
JSONObject jsonObject = new JSONObject(stepDefs.result.andReturn().getResponse().getContentAsString());

String shelterCertificateUri = jsonObject.getString("uri");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
LocalDate shelterCertificateExpirationDate = LocalDate.parse(jsonObject.getString("expirationDate"), formatter);
ZonedDateTime shelterCertificateExpirationDate = ZonedDateTime.parse(jsonObject.getString("expirationDate"));

boolean isShelterCertificateValid = shelterCertificateExpirationDate.isAfter(LocalDate.now());
boolean isShelterCertificateValid = shelterCertificateExpirationDate.isAfter(ZonedDateTime.now());
if (!isShelterCertificateValid) {
System.out.println("The Shelter Certificate IS not valid!" + stepDefs.result);
Assert.fail("The Shelter Certificate IS not valid!");
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/features/AddShelterCertificate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Feature: Add Shelter Certificate

Background: Exists a shelter with name "test" and a shelter volunteer
Given A shelter with name "test"
And There is a registered user with username "volunteer" and password "123456789" and email "[email protected]"
And There is a registered user with username "volunteer" and password "password" and email "[email protected]"

Scenario: Adding a Shelter Certificate as a Shelter Volunteer
Given I can login with username "volunteer" and password "123456789"
Given I can login with username "volunteer" and password "password"
When I add a Shelter Certificate with valid expiration date to the shelter with name "test"
Then The response code is 201

Scenario: Adding a Shelter Certificate as a non Shelter Volunteer
Given I cannot login with username "test" and password "wrongPassword"
When I add a Shelter Certificate with valid expiration date to the shelter with name "test"
Then The response code is 401
Then The response code is 401
4 changes: 2 additions & 2 deletions src/test/resources/features/EditShelter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Feature: Edit Shelter
Background:
Given There is a registered user with username "user" and password "existing" and email "[email protected]"
Given There is a registered already admin with username "admin" and password "admin" and email "[email protected]"
Given There is a registered volunteer with username "volunteer" and password "volunteer"
Given There is a registered volunteer with username "volunteer" and password "password"
And There is already a shelter with name "Shelter 1" email "[email protected]" and mobile "999999999"

Scenario: Edit shelter without being logged in
Expand All @@ -28,7 +28,7 @@ Feature: Edit Shelter
Then The response code is 200

Scenario: Edit shelter with volunteer
Given I login as "volunteer" with password "volunteer"
Given I login as "volunteer" with password "password"
When I update the shelter with name "Shelter 1" to name "Another Shelter" email "[email protected]" and mobile "123123123"
And I get the shelter with name "Another Shelter"
Then The response code is 200
Expand Down
20 changes: 10 additions & 10 deletions src/test/resources/features/KickUserFromShelter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ Feature: Kick ShelterVolunteer from Shelter
I want to be able to kick a volunteer from my shelter but not able to kick volunteers from other shelters

Background:
Given There is a registered user with username "user" and password "pass" and email "[email protected]"
Given There is a registered user with username "user" and password "password" and email "[email protected]"
And There is already a shelter with name "shelter1" email "[email protected]" and mobile "12345678"
And There is a shelter volunteer with username "volunteer" and password "pass" in the shelter "shelter1"
And There is a shelter volunteer with username "volunteer1" and password "pass" in the shelter "shelter1"
And There is a shelter volunteer with username "volunteer" and password "password" in the shelter "shelter1"
And There is a shelter volunteer with username "volunteer1" and password "password" in the shelter "shelter1"
And There is already a shelter with name "shelter2" email "[email protected]" and mobile "12345679"
And There is a shelter volunteer with username "volunteer2" and password "pass" in the shelter "shelter2"
And There is a shelter volunteer with username "volunteer2" and password "password" in the shelter "shelter2"

Scenario: Kick volunteer from shelter
Given I can login with username "volunteer" and password "pass"
Given I can login with username "volunteer" and password "password"
When I kick user "volunteer1" from shelter "shelter"
Then The response code is 200
And I cannot login with username "volunteer1" and password "pass"
And I cannot login with username "volunteer1" and password "password"

Scenario: Kick volunteer from shelter as user
And I can login with username "user" and password "pass"
And I can login with username "user" and password "password"
When I kick user "volunteer1" from shelter "shelter"
Then The response code is 403
And I can login with username "volunteer1" and password "pass"
And I can login with username "volunteer1" and password "password"

Scenario: Kick volunteer from shelter as volunteer in another shelter
Given I can login with username "volunteer" and password "pass"
Given I can login with username "volunteer" and password "password"
When I kick user "volunteer2" from shelter "shelter"
Then The response code is 412
And I can login with username "volunteer2" and password "pass"
And I can login with username "volunteer2" and password "password"

0 comments on commit 7a41e8f

Please sign in to comment.