mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 16:25:49 +00:00
Merge pull request #3 from jadmalek/jadDev
Adding tests for the methods in the Pet.java and PetValidator.java classes
This commit is contained in:
commit
e1a35884b3
15 changed files with 278 additions and 19 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -5,4 +5,10 @@ target/*
|
||||||
.idea
|
.idea
|
||||||
*.iml
|
*.iml
|
||||||
/target
|
/target
|
||||||
|
.sts4-cache/
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/settings.json
|
||||||
|
!.vscode/tasks.json
|
||||||
|
!.vscode/launch.json
|
||||||
|
!.vscode/extensions.json
|
||||||
_site/
|
_site/
|
||||||
|
|
26
.vscode/launch.json
vendored
Normal file
26
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Debug (Launch)-PetClinicApplication<spring-petclinic>",
|
||||||
|
"request": "launch",
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"console": "internalConsole",
|
||||||
|
"stopOnEntry": false,
|
||||||
|
"mainClass": "org.springframework.samples.petclinic.PetClinicApplication",
|
||||||
|
"projectName": "spring-petclinic",
|
||||||
|
"args": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Debug (Attach)",
|
||||||
|
"request": "attach",
|
||||||
|
"hostName": "localhost",
|
||||||
|
"port": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"java.configuration.updateBuildConfiguration": "interactive"
|
||||||
|
}
|
19
.vscode/tasks.json
vendored
Normal file
19
.vscode/tasks.json
vendored
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||||
|
// for the documentation about the tasks.json format
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "verify",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "mvn -B verify",
|
||||||
|
"group": "build"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "test",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "mvn -B test",
|
||||||
|
"group": "test"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
4
pom.xml
4
pom.xml
|
@ -5,7 +5,7 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.springframework.samples</groupId>
|
<groupId>org.springframework.samples</groupId>
|
||||||
<artifactId>spring-petclinic</artifactId>
|
<artifactId>spring-petclinic</artifactId>
|
||||||
<version>2.0.0</version>
|
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -84,7 +84,7 @@
|
||||||
<!-- webjars -->
|
<!-- webjars -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.webjars</groupId>
|
<groupId>org.webjars</groupId>
|
||||||
<artifactId>webjars-locator</artifactId>
|
<artifactId>webjars-locator-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.webjars</groupId>
|
<groupId>org.webjars</groupId>
|
||||||
|
|
|
@ -117,3 +117,4 @@ The Spring PetClinic sample application is released under version 2.0 of the [Ap
|
||||||
[spring-petclinic-reactjs]: https://github.com/spring-petclinic/spring-petclinic-reactjs
|
[spring-petclinic-reactjs]: https://github.com/spring-petclinic/spring-petclinic-reactjs
|
||||||
[spring-petclinic-graphql]: https://github.com/spring-petclinic/spring-petclinic-graphql
|
[spring-petclinic-graphql]: https://github.com/spring-petclinic/spring-petclinic-graphql
|
||||||
[spring-petclinic-kotlin]: https://github.com/spring-petclinic/spring-petclinic-kotlin
|
[spring-petclinic-kotlin]: https://github.com/spring-petclinic/spring-petclinic-kotlin
|
||||||
|
[spring-petclinic-rest]: https://github.com/spring-petclinic/spring-petclinic-rest
|
||||||
|
|
|
@ -3,5 +3,5 @@ database=mysql
|
||||||
spring.datasource.url=jdbc:mysql://localhost/test
|
spring.datasource.url=jdbc:mysql://localhost/test
|
||||||
spring.datasource.username=root
|
spring.datasource.username=root
|
||||||
spring.datasource.password=root
|
spring.datasource.password=root
|
||||||
# Uncomment this the first time the app runs
|
# Uncomment this the first time the app runs
|
||||||
# spring.datasource.initialize=true
|
# spring.datasource.initialization-mode=always
|
||||||
|
|
|
@ -14,8 +14,7 @@ spring.messages.basename=messages/messages
|
||||||
|
|
||||||
# Actuator / Management
|
# Actuator / Management
|
||||||
management.endpoints.web.base-path=/manage
|
management.endpoints.web.base-path=/manage
|
||||||
# Spring Boot 1.5 makes actuator secure by default
|
management.endpoints.web.exposure.include=*
|
||||||
management.endpoints.web.enabled=true
|
|
||||||
|
|
||||||
# Logging
|
# Logging
|
||||||
logging.level.org.springframework=INFO
|
logging.level.org.springframework=INFO
|
||||||
|
@ -24,3 +23,6 @@ logging.level.org.springframework=INFO
|
||||||
|
|
||||||
# Active Spring profiles
|
# Active Spring profiles
|
||||||
spring.profiles.active=production
|
spring.profiles.active=production
|
||||||
|
|
||||||
|
# Adjusting local host to 8090 for application launch
|
||||||
|
server.port = 8090
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<form>
|
<form>
|
||||||
<th:block th:fragment="input (label, name)">
|
<th:block th:fragment="input (label, name, type)">
|
||||||
<div th:with="valid=${!#fields.hasErrors(name)}"
|
<div th:with="valid=${!#fields.hasErrors(name)}"
|
||||||
th:class="${'form-group' + (valid ? '' : ' has-error')}"
|
th:class="${'form-group' + (valid ? '' : ' has-error')}"
|
||||||
class="form-group">
|
class="form-group">
|
||||||
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
|
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
|
||||||
<div class="col-sm-10">
|
<div class="col-sm-10">
|
||||||
<input class="form-control" type="text"
|
<div th:switch="${type}">
|
||||||
th:field="*{__${name}__}" />
|
<input th:case="'text'" class="form-control" type="text" th:field="*{__${name}__}" />
|
||||||
|
<input th:case="'date'" class="form-control" type="text" th:field="*{__${name}__}"
|
||||||
|
placeholder="YYYY-MM-DD" title="Enter a date in this format: YYYY-MM-DD"
|
||||||
|
pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))"/>
|
||||||
|
</div>
|
||||||
<span th:if="${valid}"
|
<span th:if="${valid}"
|
||||||
class="glyphicon glyphicon-ok form-control-feedback"
|
class="glyphicon glyphicon-ok form-control-feedback"
|
||||||
aria-hidden="true"></span>
|
aria-hidden="true"></span>
|
||||||
|
|
|
@ -7,15 +7,15 @@
|
||||||
<form th:object="${owner}" class="form-horizontal" id="add-owner-form" method="post">
|
<form th:object="${owner}" class="form-horizontal" id="add-owner-form" method="post">
|
||||||
<div class="form-group has-feedback">
|
<div class="form-group has-feedback">
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('First Name', 'firstName')}" />
|
th:replace="~{fragments/inputField :: input ('First Name', 'firstName', 'text')}" />
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Last Name', 'lastName')}" />
|
th:replace="~{fragments/inputField :: input ('Last Name', 'lastName', 'text')}" />
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Address', 'address')}" />
|
th:replace="~{fragments/inputField :: input ('Address', 'address', 'text')}" />
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('City', 'city')}" />
|
th:replace="~{fragments/inputField :: input ('City', 'city', 'text')}" />
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Telephone', 'telephone')}" />
|
th:replace="~{fragments/inputField :: input ('Telephone', 'telephone', 'text')}" />
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="col-sm-offset-2 col-sm-10">
|
<div class="col-sm-offset-2 col-sm-10">
|
||||||
|
|
|
@ -17,9 +17,9 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Name', 'name')}" />
|
th:replace="~{fragments/inputField :: input ('Name', 'name', 'text')}" />
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Birth Date', 'birthDate')}" />
|
th:replace="~{fragments/inputField :: input ('Birth Date', 'birthDate', 'date')}" />
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/selectField :: select ('Type', 'type', ${types})}" />
|
th:replace="~{fragments/selectField :: select ('Type', 'type', ${types})}" />
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -31,9 +31,9 @@
|
||||||
<form th:object="${visit}" class="form-horizontal" method="post">
|
<form th:object="${visit}" class="form-horizontal" method="post">
|
||||||
<div class="form-group has-feedback">
|
<div class="form-group has-feedback">
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Date', 'date')}" />
|
th:replace="~{fragments/inputField :: input ('Date', 'date', 'date')}" />
|
||||||
<input
|
<input
|
||||||
th:replace="~{fragments/inputField :: input ('Description', 'description')}" />
|
th:replace="~{fragments/inputField :: input ('Description', 'description', 'text')}" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
package org.springframework.samples.petclinic.owner;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.samples.petclinic.visit.Visit;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.time.*;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class PetTests {
|
||||||
|
|
||||||
|
private Pet pet;
|
||||||
|
private Date birthDate;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void testSetUp() {
|
||||||
|
//Initialization of pet
|
||||||
|
this.pet = new Pet();
|
||||||
|
PetType dog = new PetType();
|
||||||
|
dog.setId(9);
|
||||||
|
dog.setName("Duncan Jones");
|
||||||
|
//Initialization of birthDate
|
||||||
|
//Converting the current time to a local date and ultimately a date to be input into setBirthDate;
|
||||||
|
LocalDateTime timePoint = LocalDateTime.now();
|
||||||
|
LocalDate localDate = timePoint.toLocalDate();
|
||||||
|
birthDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetAndGetBirthDate() {
|
||||||
|
pet.setBirthDate(this.birthDate);
|
||||||
|
Date resultOfGetDate = pet.getBirthDate();
|
||||||
|
assertEquals(this.birthDate, resultOfGetDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetAndGetType() {
|
||||||
|
//Creating a new pet type to test the setters and getters for pet's type
|
||||||
|
PetType walrus = new PetType();
|
||||||
|
walrus.setId(36);
|
||||||
|
walrus.setName("Alex Garland");
|
||||||
|
pet.setType(walrus);
|
||||||
|
PetType resultOfGetType = pet.getType();
|
||||||
|
assertEquals(walrus.getName(), resultOfGetType.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetAndGetOwner() {
|
||||||
|
//Creating a new owner type to test the setters and getters for the pet's owner
|
||||||
|
Owner amandeepBhandal = new Owner();
|
||||||
|
amandeepBhandal.setAddress("Off-world Colony");
|
||||||
|
amandeepBhandal.setCity("Beirut");
|
||||||
|
amandeepBhandal.setTelephone("514-333-3333");
|
||||||
|
//Attach the newly created owner to the pet
|
||||||
|
pet.setOwner(amandeepBhandal);
|
||||||
|
Owner resultOfGetOwner = pet.getOwner();
|
||||||
|
assertEquals(resultOfGetOwner.getAddress(), "Off-world Colony");
|
||||||
|
assertEquals(resultOfGetOwner.getCity(), "Beirut");
|
||||||
|
assertEquals(resultOfGetOwner.getTelephone(), "514-333-3333");
|
||||||
|
assertEquals(resultOfGetOwner.getPetsInternal().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetAndGetVisitsInternal() {
|
||||||
|
//Creating a new set of visits, albeit an empty set, to test the setters and getters for the pet's visits
|
||||||
|
Set<Visit> visitsForTesting = new LinkedHashSet<>();
|
||||||
|
pet.setVisitsInternal(visitsForTesting);
|
||||||
|
Set<Visit> resultOfGetVisitsInternal = pet.getVisitsInternal();
|
||||||
|
assertEquals(visitsForTesting.size(), resultOfGetVisitsInternal.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddVisitAndGetVisits() {
|
||||||
|
//Creating a new set of visits, albeit an empty set, to test the setters and getters for the pet's visits
|
||||||
|
Visit visitForTesting = new Visit();
|
||||||
|
pet.addVisit(visitForTesting);
|
||||||
|
List<Visit> resultOfGetVisits = pet.getVisits();
|
||||||
|
Visit onlyVisitInCollection = resultOfGetVisits.iterator().next();
|
||||||
|
assertEquals(1, resultOfGetVisits.size());
|
||||||
|
assertEquals(visitForTesting.getId(), onlyVisitInCollection.getId());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,112 @@
|
||||||
|
package org.springframework.samples.petclinic.owner;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.validation.BeanPropertyBindingResult;
|
||||||
|
import org.springframework.validation.Errors;
|
||||||
|
|
||||||
|
public class PetValidatorTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidationWithValidName() {
|
||||||
|
//given
|
||||||
|
PetValidator petValidator = new PetValidator();
|
||||||
|
Pet validPet = new Pet();
|
||||||
|
validPet.setName("Peter");
|
||||||
|
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||||
|
|
||||||
|
//when
|
||||||
|
petValidator.validate(validPet, errors);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertNull(errors.getFieldError("name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidationWithInvalidName() {
|
||||||
|
//given
|
||||||
|
PetValidator petValidator = new PetValidator();
|
||||||
|
Pet invalidPet = new Pet();
|
||||||
|
invalidPet.setName("");
|
||||||
|
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||||
|
|
||||||
|
//when
|
||||||
|
petValidator.validate(invalidPet, errors);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertTrue(errors.hasErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidationWithValidType() {
|
||||||
|
//given
|
||||||
|
PetValidator petValidator = new PetValidator();
|
||||||
|
Pet validPet = new Pet();
|
||||||
|
PetType tiger = new PetType();
|
||||||
|
tiger.setId(24);
|
||||||
|
validPet.setType(tiger);
|
||||||
|
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||||
|
|
||||||
|
//when
|
||||||
|
petValidator.validate(validPet, errors);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertNull(errors.getFieldError("type"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidationWithInvalidType() {
|
||||||
|
//given
|
||||||
|
PetValidator petValidator = new PetValidator();
|
||||||
|
Pet invalidPet = new Pet();
|
||||||
|
PetType emptyType = null;
|
||||||
|
invalidPet.setType(emptyType);
|
||||||
|
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||||
|
|
||||||
|
//when
|
||||||
|
petValidator.validate(invalidPet, errors);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertTrue(errors.hasErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidationWithValidBirthDate() {
|
||||||
|
//given
|
||||||
|
PetValidator petValidator = new PetValidator();
|
||||||
|
Pet validPet = new Pet();
|
||||||
|
LocalDateTime timePoint = LocalDateTime.now();
|
||||||
|
LocalDate localDate = timePoint.toLocalDate();
|
||||||
|
Date birthDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
||||||
|
validPet.setBirthDate(birthDate);
|
||||||
|
Errors errors = new BeanPropertyBindingResult(validPet, "");
|
||||||
|
|
||||||
|
//when
|
||||||
|
petValidator.validate(validPet, errors);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertNull(errors.getFieldError("birthDate"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidationWithInvalidBirthDate() {
|
||||||
|
//given
|
||||||
|
PetValidator petValidator = new PetValidator();
|
||||||
|
Pet invalidPet = new Pet();
|
||||||
|
Date birthDate = null;
|
||||||
|
invalidPet.setBirthDate(birthDate);
|
||||||
|
Errors errors = new BeanPropertyBindingResult(invalidPet, "");
|
||||||
|
|
||||||
|
//when
|
||||||
|
petValidator.validate(invalidPet, errors);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertTrue(errors.hasErrors());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class ProductionConfigurationTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private VetRepository vets;
|
private VetRepository vets;
|
||||||
|
//Test error introduced by addition of 10 commits since forking of our repo from original repo
|
||||||
@Test
|
@Test
|
||||||
public void testFindAll() throws Exception {
|
public void testFindAll() throws Exception {
|
||||||
vets.findAll();
|
vets.findAll();
|
||||||
|
|
Loading…
Reference in a new issue