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:
jadmalek 2018-02-25 09:44:21 -05:00 committed by GitHub
commit e1a35884b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 278 additions and 19 deletions

6
.gitignore vendored
View file

@ -5,4 +5,10 @@ target/*
.idea
*.iml
/target
.sts4-cache/
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
_site/

26
.vscode/launch.json vendored Normal file
View 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
View file

@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}

19
.vscode/tasks.json vendored Normal file
View 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"
}
]
}

View file

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>spring-petclinic</artifactId>
<version>2.0.0</version>
<version>2.0.0.BUILD-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
@ -84,7 +84,7 @@
<!-- webjars -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>

View file

@ -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-graphql]: https://github.com/spring-petclinic/spring-petclinic-graphql
[spring-petclinic-kotlin]: https://github.com/spring-petclinic/spring-petclinic-kotlin
[spring-petclinic-rest]: https://github.com/spring-petclinic/spring-petclinic-rest

View file

@ -3,5 +3,5 @@ database=mysql
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=root
# Uncomment this the first time the app runs
# spring.datasource.initialize=true
# Uncomment this the first time the app runs
# spring.datasource.initialization-mode=always

View file

@ -14,8 +14,7 @@ spring.messages.basename=messages/messages
# Actuator / Management
management.endpoints.web.base-path=/manage
# Spring Boot 1.5 makes actuator secure by default
management.endpoints.web.enabled=true
management.endpoints.web.exposure.include=*
# Logging
logging.level.org.springframework=INFO
@ -24,3 +23,6 @@ logging.level.org.springframework=INFO
# Active Spring profiles
spring.profiles.active=production
# Adjusting local host to 8090 for application launch
server.port = 8090

View file

@ -1,14 +1,18 @@
<html>
<body>
<form>
<th:block th:fragment="input (label, name)">
<th:block th:fragment="input (label, name, type)">
<div th:with="valid=${!#fields.hasErrors(name)}"
th:class="${'form-group' + (valid ? '' : ' has-error')}"
class="form-group">
<label class="col-sm-2 control-label" th:text="${label}">Label</label>
<div class="col-sm-10">
<input class="form-control" type="text"
th:field="*{__${name}__}" />
<div th:switch="${type}">
<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}"
class="glyphicon glyphicon-ok form-control-feedback"
aria-hidden="true"></span>

View file

@ -7,15 +7,15 @@
<form th:object="${owner}" class="form-horizontal" id="add-owner-form" method="post">
<div class="form-group has-feedback">
<input
th:replace="~{fragments/inputField :: input ('First Name', 'firstName')}" />
th:replace="~{fragments/inputField :: input ('First Name', 'firstName', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('Last Name', 'lastName')}" />
th:replace="~{fragments/inputField :: input ('Last Name', 'lastName', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('Address', 'address')}" />
th:replace="~{fragments/inputField :: input ('Address', 'address', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('City', 'city')}" />
th:replace="~{fragments/inputField :: input ('City', 'city', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('Telephone', 'telephone')}" />
th:replace="~{fragments/inputField :: input ('Telephone', 'telephone', 'text')}" />
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">

View file

@ -17,9 +17,9 @@
</div>
</div>
<input
th:replace="~{fragments/inputField :: input ('Name', 'name')}" />
th:replace="~{fragments/inputField :: input ('Name', 'name', 'text')}" />
<input
th:replace="~{fragments/inputField :: input ('Birth Date', 'birthDate')}" />
th:replace="~{fragments/inputField :: input ('Birth Date', 'birthDate', 'date')}" />
<input
th:replace="~{fragments/selectField :: select ('Type', 'type', ${types})}" />
</div>

View file

@ -31,9 +31,9 @@
<form th:object="${visit}" class="form-horizontal" method="post">
<div class="form-group has-feedback">
<input
th:replace="~{fragments/inputField :: input ('Date', 'date')}" />
th:replace="~{fragments/inputField :: input ('Date', 'date', 'date')}" />
<input
th:replace="~{fragments/inputField :: input ('Description', 'description')}" />
th:replace="~{fragments/inputField :: input ('Description', 'description', 'text')}" />
</div>
<div class="form-group">

View file

@ -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());
}
}

View file

@ -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());
}
}

View file

@ -14,7 +14,7 @@ public class ProductionConfigurationTests {
@Autowired
private VetRepository vets;
//Test error introduced by addition of 10 commits since forking of our repo from original repo
@Test
public void testFindAll() throws Exception {
vets.findAll();