-
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 #107 from gfa-cc-after/SCRUM-95
Scrum 95
- Loading branch information
Showing
23 changed files
with
519 additions
and
29 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
26 changes: 26 additions & 0 deletions
26
backend/src/main/java/com/greenfoxacademy/backend/controller/PetController.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,26 @@ | ||
package com.greenfoxacademy.backend.controller; | ||
|
||
import com.greenfoxacademy.backend.dtos.PetListResponseDto; | ||
import com.greenfoxacademy.backend.services.pet.PetService; | ||
import java.security.Principal; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* A REST controller that handles operations related to users' pets. | ||
* | ||
* @author Your Name | ||
*/ | ||
@RequiredArgsConstructor | ||
@RestController | ||
public class PetController { | ||
private final PetService petService; | ||
|
||
@GetMapping("/pets") | ||
public ResponseEntity<PetListResponseDto> getPets(Principal owner) { | ||
return ResponseEntity.status(HttpStatus.OK).body(petService.getOwnerPets(owner.getName())); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/greenfoxacademy/backend/dtos/PetDetailsDto.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,28 @@ | ||
package com.greenfoxacademy.backend.dtos; | ||
|
||
import jakarta.validation.constraints.FutureOrPresent; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.PastOrPresent; | ||
import java.util.Date; | ||
import lombok.Data; | ||
|
||
/** | ||
* A data transfer object for pet details. | ||
* | ||
* @author Your Name | ||
*/ | ||
@Data | ||
public class PetDetailsDto { | ||
@NotBlank | ||
String name; | ||
@NotBlank | ||
String breed; | ||
@NotBlank | ||
String sex; | ||
@PastOrPresent(message = "The birth date must be in the past or present") | ||
Date birthDate; | ||
@PastOrPresent(message = "The last check-up date must be in the past or present") | ||
Date lastCheckUp; | ||
@FutureOrPresent(message = "The next check-up date must be in the future or present") | ||
Date nextCheckUp; | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/greenfoxacademy/backend/dtos/PetListResponseDto.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,12 @@ | ||
package com.greenfoxacademy.backend.dtos; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A data transfer object for a list of pets. | ||
* | ||
*/ | ||
public record PetListResponseDto( | ||
List<PetDetailsDto> pets | ||
){ | ||
} |
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
2 changes: 2 additions & 0 deletions
2
backend/src/main/java/com/greenfoxacademy/backend/repositories/PetRepository.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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package com.greenfoxacademy.backend.repositories; | ||
|
||
import com.greenfoxacademy.backend.models.Pet; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
/** | ||
* Repository to manage Pet entities. | ||
*/ | ||
public interface PetRepository extends JpaRepository<Pet, Integer> { | ||
List<Pet> findAllByOwnerId(Integer ownerId); | ||
} |
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
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/com/greenfoxacademy/backend/services/pet/PetService.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,16 @@ | ||
package com.greenfoxacademy.backend.services.pet; | ||
|
||
import com.greenfoxacademy.backend.dtos.PetListResponseDto; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Retrieves the pets of the specified owner. | ||
* | ||
* @param name The name of the owner. | ||
* @return A response containing the owner's pets. | ||
*/ | ||
|
||
public interface PetService { | ||
PetListResponseDto getOwnerPets(String name); | ||
} |
43 changes: 43 additions & 0 deletions
43
backend/src/main/java/com/greenfoxacademy/backend/services/pet/PetServiceImpl.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,43 @@ | ||
package com.greenfoxacademy.backend.services.pet; | ||
|
||
import com.greenfoxacademy.backend.dtos.PetDetailsDto; | ||
import com.greenfoxacademy.backend.dtos.PetListResponseDto; | ||
import com.greenfoxacademy.backend.models.Pet; | ||
import com.greenfoxacademy.backend.repositories.PetRepository; | ||
import com.greenfoxacademy.backend.services.user.owner.OwnerService; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Retrieves a list of pets owned by the user with the specified email. | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class PetServiceImpl implements PetService { | ||
private final PetRepository petRepository; | ||
private final OwnerService ownerService; | ||
private final ModelMapper modelMapper = new ModelMapper(); | ||
|
||
/** | ||
* Retrieves a list of pets owned by the user with the specified email. | ||
* | ||
* @param email the email of the pet owner | ||
* @return a {@link PetListResponseDto} containing the list of pets | ||
* @throws UsernameNotFoundException if the user with the specified email is not found | ||
*/ | ||
@Override | ||
public PetListResponseDto getOwnerPets(String email) { | ||
List<Pet> petList = petRepository | ||
.findAllByOwnerId(ownerService.findByEmail(email).getId()); | ||
|
||
List<PetDetailsDto> petDtoList = petList.stream() | ||
.map(pet -> modelMapper.map(pet, PetDetailsDto.class)) | ||
.collect(Collectors.toList()); | ||
|
||
return new PetListResponseDto(petDtoList); | ||
} | ||
} |
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
98 changes: 98 additions & 0 deletions
98
backend/src/test/java/com/greenfoxacademy/backend/services/pet/PetServiceImplTest.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,98 @@ | ||
package com.greenfoxacademy.backend.services.pet; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.greenfoxacademy.backend.models.Owner; | ||
import com.greenfoxacademy.backend.models.Pet; | ||
import com.greenfoxacademy.backend.repositories.OwnerRepository; | ||
import com.greenfoxacademy.backend.repositories.PetRepository; | ||
import jakarta.transaction.Transactional; | ||
import java.util.Arrays; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
/** | ||
* Integration test class for PetServiceImpl. | ||
* This class uses the following annotations: | ||
* - {@link SpringBootTest}: Indicates that the class is a Spring Boot test that | ||
* will start the full application context. | ||
* - {@link AutoConfigureMockMvc}: Automatically configures MockMvc for testing web layer. | ||
* - {@link Transactional}: Ensures that each test method runs within a transaction | ||
* that is rolled back after the test completes. | ||
*/ | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@Transactional | ||
public class PetServiceImplTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private PetRepository petRepository; | ||
|
||
@Autowired | ||
private OwnerRepository ownerRepository; | ||
|
||
/** | ||
* Sets up mock data before each test. | ||
* This method is executed before each test method in the current test class. | ||
* It initializes mock data for owners and pets and saves them to the repository. | ||
*/ | ||
@BeforeEach | ||
public void setUp() { | ||
// Set up mock data | ||
Owner userWithPets = new Owner(); | ||
userWithPets.setEmail("userWithPets@example.com"); | ||
userWithPets.setPassword("Password"); | ||
Owner userWithNoPets = new Owner(); | ||
userWithNoPets.setEmail("userWithNoPets@example.com"); | ||
userWithNoPets.setPassword("Password"); | ||
|
||
ownerRepository.saveAll(Arrays.asList(userWithPets, userWithNoPets)); | ||
|
||
Pet pet1 = new Pet(); | ||
pet1.setName("Morzsi"); | ||
pet1.setOwner(userWithPets); | ||
Pet pet2 = new Pet(); | ||
pet2.setName("Rusty"); | ||
pet2.setOwner(userWithPets); | ||
|
||
petRepository.saveAll(Arrays.asList(pet1, pet2)); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "userWithPets@example.com") | ||
public void testCorrectEmailWithExistingPets() throws Exception { | ||
mockMvc.perform(get("/pets") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.pets[0].name").value("Morzsi")) | ||
.andExpect(jsonPath("$.pets[1].name").value("Rusty")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "userWithNoPets@example.com") | ||
public void testCorrectEmailWithNoExistingPets() throws Exception { | ||
mockMvc.perform(get("/pets") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.pets").isEmpty()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "nonExistingUser@example.com") | ||
public void testIncorrectEmail() throws Exception { | ||
mockMvc.perform(get("/pets") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().is4xxClientError()); | ||
} | ||
} |
Oops, something went wrong.