-
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 #115 from gfa-cc-after/SCRUM-96
Scrum 96
- Loading branch information
Showing
18 changed files
with
460 additions
and
40 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
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/greenfoxacademy/backend/dtos/AddPetResponseDto.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; | ||
|
||
/** | ||
* A Data Transfer Object (DTO) for the response after adding a new pet. | ||
* Contains the ID of the newly added pet. | ||
* | ||
* @param id the ID of the newly added pet | ||
*/ | ||
public record AddPetResponseDto( | ||
Integer id | ||
) { | ||
} |
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/com/greenfoxacademy/backend/dtos/CreatePetDto.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,33 @@ | ||
package com.greenfoxacademy.backend.dtos; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.PastOrPresent; | ||
import java.util.Date; | ||
|
||
/** | ||
* A Data Transfer Object (DTO) for creating a new pet. | ||
* Contains the necessary information to create a pet, including name, breed, sex, and birth date. | ||
* | ||
* <p>Validation constraints are applied to ensure the data integrity: | ||
* <ul> | ||
* <li>{@link NotBlank} ensures that the name, breed, and sex fields are not blank.</li> | ||
* <li>{@link PastOrPresent} ensures that the birth date is not in the future.</li> | ||
* </ul> | ||
* </p> | ||
* | ||
* @param name the name of the pet, must not be blank | ||
* @param breed the breed of the pet, must not be blank | ||
* @param sex the sex of the pet, must not be blank | ||
* @param birthDate the birth date of the pet, must be in the past or present | ||
*/ | ||
public record CreatePetDto( | ||
@NotBlank | ||
String name, | ||
@NotBlank | ||
String breed, | ||
@NotBlank | ||
String sex, | ||
@PastOrPresent(message = "The birth date must be in the past or present") | ||
Date birthDate | ||
) { | ||
} |
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
38 changes: 38 additions & 0 deletions
38
backend/src/test/java/com/greenfoxacademy/backend/services/pet/AddPetServiceImplTest.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,38 @@ | ||
package com.greenfoxacademy.backend.services.pet; | ||
|
||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.greenfoxacademy.backend.controller.PetController; | ||
import com.greenfoxacademy.backend.dtos.CreatePetDto; | ||
import com.greenfoxacademy.backend.repositories.PetRepository; | ||
import java.util.Date; | ||
import lombok.RequiredArgsConstructor; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@Nested | ||
@RequiredArgsConstructor | ||
@ExtendWith(MockitoExtension.class) | ||
class AddPetServiceImplTest { | ||
@Mock | ||
private PetService petService; | ||
|
||
@Mock | ||
private PetRepository petRepository; | ||
|
||
@InjectMocks | ||
private PetController petController; | ||
|
||
@Test | ||
void petSuccessfullyAddedToPetRepository() { | ||
CreatePetDto pet = new CreatePetDto("Morzsi", "Dog", "Male", new Date(2024, 9, 13)); | ||
petService.addPet("owner", pet); | ||
|
||
verify(petService, times(1)).addPet("owner", pet); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ChakraProvider } from "@chakra-ui/react"; | ||
import { fireEvent, render, screen } from "@testing-library/react"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import { describe, expect, test, vi } from "vitest"; | ||
import { addPet } from "../httpClient"; | ||
import AddPet from "./AddPet"; | ||
|
||
// Mock the addPet function | ||
vi.mock("../httpClient", () => ({ | ||
addPet: vi.fn(), | ||
})); | ||
|
||
describe("AddPet Component", () => { | ||
test("renders AddPet form and submits data", async () => { | ||
// Arrange | ||
render( | ||
<ChakraProvider> | ||
<BrowserRouter> | ||
<AddPet /> | ||
</BrowserRouter> | ||
</ChakraProvider>, | ||
); | ||
|
||
// Act | ||
fireEvent.change(screen.getByLabelText(/name/i), { | ||
target: { value: "Buddy" }, | ||
}); | ||
fireEvent.change(screen.getByLabelText(/breed/i), { | ||
target: { value: "Golden Retriever" }, | ||
}); | ||
fireEvent.change(screen.getByLabelText(/sex/i), { | ||
target: { value: "Male" }, | ||
}); | ||
fireEvent.change(screen.getByLabelText(/birthDate/i), { | ||
target: { value: "2020-01-01" }, | ||
}); | ||
|
||
fireEvent.click(screen.getByRole("button", { name: /add pet/i })); | ||
|
||
// Assert | ||
expect(addPet).toHaveBeenCalledWith({ | ||
name: "Buddy", | ||
breed: "Golden Retriever", | ||
sex: "Male", | ||
birthDate: new Date("2020-01-01"), | ||
}); | ||
expect( | ||
await screen.findByText(/pet added successfully/i), | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.