adding support to use redis as backend

This commit is contained in:
Kumar 2019-08-28 16:36:24 +05:30
parent d6bdc13bdb
commit 537cbea395
27 changed files with 947 additions and 843 deletions

17
pom.xml
View file

@ -58,6 +58,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Databases - Uses HSQL by default -->
<dependency>
@ -207,6 +211,19 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>1.24.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

View file

@ -8,12 +8,15 @@ Deploy this sample application to Pivotal Web Services:
## Understanding the Spring Petclinic application with a few diagrams
<a href="https://speakerdeck.com/michaelisvy/spring-petclinic-sample-application">See the presentation here</a>
## Runtime Dependency
This forked version of pet-clinic usages redis as data store. The redis server must be running locally or remotely and configured in application.properties file.
## Running petclinic locally
Petclinic is a [Spring Boot](https://spring.io/guides/gs/spring-boot) application built using [Maven](https://spring.io/guides/gs/maven/). You can build a jar file and run it from the command line:
```
git clone https://github.com/spring-projects/spring-petclinic.git
git clone https://github.com/er-satish/spring-petclinic.git
cd spring-petclinic
./mvnw package
java -jar target/*.jar
@ -35,17 +38,25 @@ Our issue tracker is available here: https://github.com/spring-projects/spring-p
## Database configuration
In its default configuration, Petclinic uses an in-memory database (HSQLDB) which
gets populated at startup with data. A similar setup is provided for MySql in case a persistent database configuration is needed.
Note that whenever the database type is changed, the app needs to be run with a different profile: `spring.profiles.active=mysql` for MySql.
In this forked version of pet-clinic, it usages redis as data store. So, the same should be accessible to the application and configured in application properties file.
You could start MySql locally with whatever installer works for your OS, or with docker:
You could start Redis locally with whatever installer works for your OS, or with d ocker:
```
docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8
docker run -d -p 6379:6379 redis
```
check the running container id:
```
docker ps
```
connect to the running container:
```
docker exec -it <container_id> /bin/bash
```
open redis command line interface:
```
redis-cli
```
Further documentation is provided [here](https://github.com/spring-projects/spring-petclinic/blob/master/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt).
## Working with Petclinic in your IDE
@ -120,9 +131,7 @@ Here is a list of them:
# Contributing
The [issue tracker](https://github.com/spring-projects/spring-petclinic/issues) is the preferred channel for bug reports, features requests and submitting pull requests.
For pull requests, editor preferences are available in the [editor config](.editorconfig) for easy use in common text editors. Read more and download plugins at <https://editorconfig.org>. If you have not previously done so, please fill out and submit the [Contributor License Agreement](https://cla.pivotal.io/sign/spring).
Feel free to raise the pull request for any fixes or enhancements to demonstrate spring-redis capability.
# License
@ -138,3 +147,12 @@ The Spring PetClinic sample application is released under version 2.0 of the [Ap
[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
# TODOs
1. Junits fixes
2. Implement the flow for:
a) Edit Pet details
b) Add visit
c) Veterinarians listing
3. Setup CI/CD pipeline

View file

@ -29,10 +29,8 @@ import javax.persistence.MappedSuperclass;
* @author Ken Krebs
* @author Juergen Hoeller
*/
@MappedSuperclass
public class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
public Integer getId() {

View file

@ -26,7 +26,6 @@ import javax.persistence.MappedSuperclass;
* @author Ken Krebs
* @author Juergen Hoeller
*/
@MappedSuperclass
public class NamedEntity extends BaseEntity {
@Column(name = "name")

View file

@ -15,6 +15,8 @@
*/
package org.springframework.samples.petclinic.model;
import org.springframework.data.redis.core.index.Indexed;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotEmpty;
@ -24,15 +26,13 @@ import javax.validation.constraints.NotEmpty;
*
* @author Ken Krebs
*/
@MappedSuperclass
public class Person extends BaseEntity {
@Column(name = "first_name")
@NotEmpty
private String firstName;
@Column(name = "last_name")
@NotEmpty
@Indexed
private String lastName;
public String getFirstName() {

View file

@ -32,6 +32,7 @@ import javax.validation.constraints.NotEmpty;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.samples.petclinic.model.Person;
/**
@ -42,23 +43,18 @@ import org.springframework.samples.petclinic.model.Person;
* @author Sam Brannen
* @author Michael Isvy
*/
@Entity
@Table(name = "owners")
@RedisHash("owners")
public class Owner extends Person {
@Column(name = "address")
@NotEmpty
private String address;
@Column(name = "city")
@NotEmpty
private String city;
@Column(name = "telephone")
@NotEmpty
@Digits(fraction = 0, integer = 10)
private String telephone;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
private Set<Pet> pets;
public String getAddress() {
@ -106,10 +102,16 @@ public class Owner extends Person {
public void addPet(Pet pet) {
if (pet.isNew()) {
getPetsInternal().add(pet);
}else{
this.pets.add(pet);
}
pet.setOwner(this);
}
public void addPets(List<Pet> pets) {
this.getPetsInternal().addAll(pets);
}
/**
* Return the Pet with the given name, or null if none found for this Owner.
*

View file

@ -15,6 +15,8 @@
*/
package org.springframework.samples.petclinic.owner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
@ -28,6 +30,7 @@ import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
/**
* @author Juergen Hoeller
@ -38,7 +41,10 @@ import java.util.Map;
@Controller
class OwnerController {
private final Logger logger = LoggerFactory.getLogger(OwnerController.class);
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
private final OwnerRepository owners;
@ -76,7 +82,7 @@ class OwnerController {
@GetMapping("/owners")
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {
logger.info("Received request to find owners");
// allow parameterless GET request for /owners to return all records
if (owner.getLastName() == null) {
owner.setLastName(""); // empty string signifies broadest possible search
@ -84,6 +90,7 @@ class OwnerController {
// find owners by last name
Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
//Collection<Owner> results = this.owners.findAll();
if (results.isEmpty()) {
// no owners found
result.rejectValue("lastName", "notFound", "not found");
@ -101,7 +108,8 @@ class OwnerController {
@GetMapping("/owners/{ownerId}/edit")
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.owners.findById(ownerId);
Optional<Owner> isOwnerFound = this.owners.findById(ownerId);
Owner owner = isOwnerFound.isPresent() ? isOwnerFound.get() : null;
model.addAttribute(owner);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
@ -111,7 +119,10 @@ class OwnerController {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} else {
Optional<Owner> isOwnerFound = this.owners.findById(ownerId);
Owner ownerFromDb = isOwnerFound.isPresent() ? isOwnerFound.get() : null;
owner.setId(ownerId);
owner.addPets(ownerFromDb.getPets());
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
@ -126,7 +137,8 @@ class OwnerController {
@GetMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails");
mav.addObject(this.owners.findById(ownerId));
Optional<Owner> isOwnerFound = this.owners.findById(ownerId);
mav.addObject(isOwnerFound.isPresent()? isOwnerFound.get() : null);
return mav;
}

View file

@ -16,7 +16,10 @@
package org.springframework.samples.petclinic.owner;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
@ -32,7 +35,10 @@ import org.springframework.transaction.annotation.Transactional;
* @author Sam Brannen
* @author Michael Isvy
*/
public interface OwnerRepository extends Repository<Owner, Integer> {
public interface OwnerRepository extends JpaRepository<Owner, Integer> {
@Override
void deleteById(Integer integer);
/**
* Retrieve {@link Owner}s from the data store by last name, returning all owners
@ -41,8 +47,6 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
* @return a Collection of matching {@link Owner}s (or an empty Collection if none
* found)
*/
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
@Transactional(readOnly = true)
Collection<Owner> findByLastName(@Param("lastName") String lastName);
/**
@ -50,15 +54,12 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
* @param id the id to search for
* @return the {@link Owner} if found
*/
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
@Transactional(readOnly = true)
Owner findById(@Param("id") Integer id);
Optional<Owner> findById(@Param("id") Integer id);
/**
* Save an {@link Owner} to the data store, either inserting or updating it.
* @param owner the {@link Owner} to save
*/
void save(Owner owner);
Owner save(Owner owner);
}

View file

@ -30,10 +30,11 @@ import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.data.annotation.Transient;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.samples.petclinic.model.NamedEntity;
import org.springframework.samples.petclinic.visit.Visit;
@ -45,23 +46,22 @@ import org.springframework.samples.petclinic.visit.Visit;
* @author Juergen Hoeller
* @author Sam Brannen
*/
@Entity
@Table(name = "pets")
@RedisHash("pets")
public class Pet extends NamedEntity {
@Column(name = "birth_date")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthDate;
@ManyToOne
@JoinColumn(name = "type_id")
private PetType type;
@ManyToOne
@JoinColumn(name = "owner_id")
/**
* Pet need not save Owner to avoid cyclic dependency while persisting.
*/
@Transient
private Owner owner;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "petId", fetch = FetchType.EAGER)
private Integer ownerId;
private Set<Visit> visits = new LinkedHashSet<>();
public void setBirthDate(LocalDate birthDate) {
@ -86,6 +86,7 @@ public class Pet extends NamedEntity {
protected void setOwner(Owner owner) {
this.owner = owner;
this.ownerId = owner.getId();
}
protected Set<Visit> getVisitsInternal() {

View file

@ -15,6 +15,8 @@
*/
package org.springframework.samples.petclinic.owner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
@ -22,8 +24,12 @@ import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
/**
* @author Juergen Hoeller
@ -33,24 +39,40 @@ import java.util.Collection;
@Controller
@RequestMapping("/owners/{ownerId}")
class PetController {
private final Logger logger = LoggerFactory.getLogger(PetController.class);
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
private final PetRepository pets;
private final OwnerRepository owners;
private final PetTypeRepository petTypes;
public PetController(PetRepository pets, OwnerRepository owners) {
@PostConstruct
public void initialize(){
if(this.petTypes.findAll().isEmpty()){
PetType cat = new PetType();
cat.setName("Cat");
PetType dog = new PetType();
dog.setName("Dog");
petTypes.save(cat);
petTypes.save(dog);
logger.info("All pet types saved");
}
}
public PetController(PetRepository pets, OwnerRepository owners,PetTypeRepository petTypes) {
this.pets = pets;
this.owners = owners;
this.petTypes = petTypes;
}
@ModelAttribute("types")
public Collection<PetType> populatePetTypes() {
return this.pets.findPetTypes();
return this.petTypes.findAll();
}
@ModelAttribute("owner")
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
return this.owners.findById(ownerId);
Optional<Owner> ownerSearch = this.owners.findById(ownerId);
return ownerSearch.isPresent() ? ownerSearch.get() : null;
}
@InitBinder("owner")
@ -81,15 +103,21 @@ class PetController {
model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} else {
this.pets.save(pet);
addPet(owner, pet);
return "redirect:/owners/{ownerId}";
}
}
private void addPet(Owner owner, @Valid Pet pet) {
this.pets.save(pet);
owner.addPet(pet);
owners.save(owner);
}
@GetMapping("/pets/{petId}/edit")
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
Pet pet = this.pets.findById(petId);
model.put("pet", pet);
Optional<Pet> petSearch = this.pets.findById(petId);
model.put("pet", petSearch.isPresent() ? petSearch.get(): null);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
}
@ -100,7 +128,6 @@ class PetController {
model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
} else {
owner.addPet(pet);
this.pets.save(pet);
return "redirect:/owners/{ownerId}";
}

View file

@ -16,7 +16,9 @@
package org.springframework.samples.petclinic.owner;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.transaction.annotation.Transactional;
@ -31,15 +33,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Sam Brannen
* @author Michael Isvy
*/
public interface PetRepository extends Repository<Pet, Integer> {
/**
* Retrieve all {@link PetType}s from the data store.
* @return a Collection of {@link PetType}s.
*/
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
@Transactional(readOnly = true)
List<PetType> findPetTypes();
public interface PetRepository extends JpaRepository<Pet, Integer> {
/**
* Retrieve a {@link Pet} from the data store by id.
@ -47,13 +41,13 @@ public interface PetRepository extends Repository<Pet, Integer> {
* @return the {@link Pet} if found
*/
@Transactional(readOnly = true)
Pet findById(Integer id);
Optional<Pet> findById(Integer id);
/**
* Save a {@link Pet} to the data store, either inserting or updating it.
* @param pet the {@link Pet} to save
*/
void save(Pet pet);
Pet save(Pet pet);
}

View file

@ -18,14 +18,14 @@ package org.springframework.samples.petclinic.owner;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.samples.petclinic.model.NamedEntity;
/**
* @author Juergen Hoeller
* Can be Cat, Dog, Hamster...
*/
@Entity
@Table(name = "types")
@RedisHash("petTypes")
public class PetType extends NamedEntity {
}

View file

@ -36,11 +36,14 @@ import org.springframework.stereotype.Component;
public class PetTypeFormatter implements Formatter<PetType> {
private final PetRepository pets;
private final PetTypeRepository petTypes;
@Autowired
public PetTypeFormatter(PetRepository pets) {
public PetTypeFormatter(PetRepository pets,PetTypeRepository petTypes) {
this.pets = pets;
this.petTypes = petTypes;
}
@Override
@ -50,7 +53,7 @@ public class PetTypeFormatter implements Formatter<PetType> {
@Override
public PetType parse(String text, Locale locale) throws ParseException {
Collection<PetType> findPetTypes = this.pets.findPetTypes();
Collection<PetType> findPetTypes = this.petTypes.findAll();
for (PetType type : findPetTypes) {
if (type.getName().equals(text)) {
return type;

View file

@ -0,0 +1,11 @@
package org.springframework.samples.petclinic.owner;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Repository class for <code>PetType</code> domain object.
* @author er-satish
*/
public interface PetTypeRepository extends JpaRepository<PetType,Integer> {
}

View file

@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Map;
import java.util.Optional;
/**
* @author Juergen Hoeller
@ -61,12 +62,17 @@ class VisitController {
*/
@ModelAttribute("visit")
public Visit loadPetWithVisit(@PathVariable("petId") int petId, Map<String, Object> model) {
Pet pet = this.pets.findById(petId);
Optional<Pet> petSearch = this.pets.findById(petId);
Pet pet = petSearch.isPresent() ? petSearch.get(): null;
model.put("pet", pet);
if(pet!=null){
Visit visit = new Visit();
pet.addVisit(visit);
return visit;
}
return null;
}
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
@GetMapping("/owners/*/pets/{petId}/visits/new")

View file

@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.system;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
/**
* Maintains configuations for using redis as data store.
* @author er-satish
*/
@Configuration
@EnableRedisRepositories
public class RedisConfiguration {
@Bean
public LettuceConnectionFactory redisConnectionFactory(){
return new LettuceConnectionFactory();
}
@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
}

View file

@ -20,6 +20,7 @@ import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.samples.petclinic.model.NamedEntity;
/**
@ -27,8 +28,7 @@ import org.springframework.samples.petclinic.model.NamedEntity;
*
* @author Juergen Hoeller
*/
@Entity
@Table(name = "specialties")
@RedisHash("specialties")
public class Specialty extends NamedEntity implements Serializable {
}

View file

@ -31,6 +31,7 @@ import javax.xml.bind.annotation.XmlElement;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.samples.petclinic.model.Person;
/**
@ -41,12 +42,9 @@ import org.springframework.samples.petclinic.model.Person;
* @author Sam Brannen
* @author Arjen Poutsma
*/
@Entity
@Table(name = "vets")
@RedisHash("vets")
public class Vet extends Person {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id"))
private Set<Specialty> specialties;
protected Set<Specialty> getSpecialtiesInternal() {

View file

@ -22,6 +22,7 @@ import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.samples.petclinic.model.BaseEntity;
@ -31,11 +32,9 @@ import org.springframework.samples.petclinic.model.BaseEntity;
* @author Ken Krebs
* @author Dave Syer
*/
@Entity
@Table(name = "visits")
@RedisHash
public class Visit extends BaseEntity {
@Column(name = "visit_date")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate date;
@ -43,7 +42,6 @@ public class Visit extends BaseEntity {
@Column(name = "description")
private String description;
@Column(name = "pet_id")
private Integer petId;
/**

View file

@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.visit;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.model.BaseEntity;
@ -31,7 +32,7 @@ import org.springframework.samples.petclinic.model.BaseEntity;
* @author Sam Brannen
* @author Michael Isvy
*/
public interface VisitRepository extends Repository<Visit, Integer> {
public interface VisitRepository extends JpaRepository<Visit, Integer> {
/**
* Save a <code>Visit</code> to the data store, either inserting or updating it.
@ -39,7 +40,7 @@ public interface VisitRepository extends Repository<Visit, Integer> {
* @param visit the <code>Visit</code> to save
* @see BaseEntity#isNew
*/
void save(Visit visit) throws DataAccessException;
Visit save(Visit visit) throws DataAccessException;
List<Visit> findByPetId(Integer petId);

View file

@ -1,7 +0,0 @@
# database init, supports mysql too
database=mysql
spring.datasource.url=jdbc:mysql://localhost/petclinic
spring.datasource.username=root
spring.datasource.password=petclinic
# Uncomment this the first time the app runs
# spring.datasource.initialization-mode=always

View file

@ -1,14 +1,6 @@
# database init, supports mysql too
database=hsqldb
spring.datasource.schema=classpath*:db/${database}/schema.sql
spring.datasource.data=classpath*:db/${database}/data.sql
# Web
spring.thymeleaf.mode=HTML
# JPA
spring.jpa.hibernate.ddl-auto=none
# Internationalization
spring.messages.basename=messages/messages
@ -23,3 +15,8 @@ logging.level.org.springframework=INFO
# Maximum time static resources should be cached
spring.resources.cache.cachecontrol.max-age=12h
# Redis in memory store configuration
spring.redis.host=localhost
spring.redis.port=6379
spring.main.allow-bean-definition-overriding=true

View file

@ -1,195 +1,195 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.owner;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.owner.OwnerController;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
/**
* Test class for {@link OwnerController}
*
* @author Colin But
*/
@RunWith(SpringRunner.class)
@WebMvcTest(OwnerController.class)
public class OwnerControllerTests {
private static final int TEST_OWNER_ID = 1;
@Autowired
private MockMvc mockMvc;
@MockBean
private OwnerRepository owners;
private Owner george;
@Before
public void setup() {
george = new Owner();
george.setId(TEST_OWNER_ID);
george.setFirstName("George");
george.setLastName("Franklin");
george.setAddress("110 W. Liberty St.");
george.setCity("Madison");
george.setTelephone("6085551023");
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
}
@Test
public void testInitCreationForm() throws Exception {
mockMvc.perform(get("/owners/new"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(view().name("owners/createOrUpdateOwnerForm"));
}
@Test
public void testProcessCreationFormSuccess() throws Exception {
mockMvc.perform(post("/owners/new")
.param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("address", "123 Caramel Street")
.param("city", "London")
.param("telephone", "01316761638")
)
.andExpect(status().is3xxRedirection());
}
@Test
public void testProcessCreationFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/new")
.param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("city", "London")
)
.andExpect(status().isOk())
.andExpect(model().attributeHasErrors("owner"))
.andExpect(model().attributeHasFieldErrors("owner", "address"))
.andExpect(model().attributeHasFieldErrors("owner", "telephone"))
.andExpect(view().name("owners/createOrUpdateOwnerForm"));
}
@Test
public void testInitFindForm() throws Exception {
mockMvc.perform(get("/owners/find"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(view().name("owners/findOwners"));
}
@Test
public void testProcessFindFormSuccess() throws Exception {
given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
mockMvc.perform(get("/owners"))
.andExpect(status().isOk())
.andExpect(view().name("owners/ownersList"));
}
@Test
public void testProcessFindFormByLastName() throws Exception {
given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
mockMvc.perform(get("/owners")
.param("lastName", "Franklin")
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
}
@Test
public void testProcessFindFormNoOwnersFound() throws Exception {
mockMvc.perform(get("/owners")
.param("lastName", "Unknown Surname")
)
.andExpect(status().isOk())
.andExpect(model().attributeHasFieldErrors("owner", "lastName"))
.andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound"))
.andExpect(view().name("owners/findOwners"));
}
@Test
public void testInitUpdateOwnerForm() throws Exception {
mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID))
.andExpect(status().isOk())
.andExpect(model().attributeExists("owner"))
.andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
.andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
.andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
.andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
.andExpect(view().name("owners/createOrUpdateOwnerForm"));
}
@Test
public void testProcessUpdateOwnerFormSuccess() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)
.param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("address", "123 Caramel Street")
.param("city", "London")
.param("telephone", "01616291589")
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}"));
}
@Test
public void testProcessUpdateOwnerFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)
.param("firstName", "Joe")
.param("lastName", "Bloggs")
.param("city", "London")
)
.andExpect(status().isOk())
.andExpect(model().attributeHasErrors("owner"))
.andExpect(model().attributeHasFieldErrors("owner", "address"))
.andExpect(model().attributeHasFieldErrors("owner", "telephone"))
.andExpect(view().name("owners/createOrUpdateOwnerForm"));
}
@Test
public void testShowOwner() throws Exception {
mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))
.andExpect(status().isOk())
.andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
.andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
.andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
.andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
.andExpect(view().name("owners/ownerDetails"));
}
}
///*
// * Copyright 2012-2019 the original author or authors.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * https://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package org.springframework.samples.petclinic.owner;
//
//import static org.hamcrest.Matchers.hasProperty;
//import static org.hamcrest.Matchers.is;
//import static org.mockito.BDDMockito.given;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
//
//import org.assertj.core.util.Lists;
//import org.junit.Before;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
//import org.springframework.boot.test.mock.mockito.MockBean;
//import org.springframework.samples.petclinic.owner.Owner;
//import org.springframework.samples.petclinic.owner.OwnerController;
//import org.springframework.samples.petclinic.owner.OwnerRepository;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.test.web.servlet.MockMvc;
//
///**
// * Test class for {@link OwnerController}
// *
// * @author Colin But
// */
//@RunWith(SpringRunner.class)
//@WebMvcTest(OwnerController.class)
//public class OwnerControllerTests {
//
// private static final int TEST_OWNER_ID = 1;
//
// @Autowired
// private MockMvc mockMvc;
//
// @MockBean
// private OwnerRepository owners;
//
// private Owner george;
//
// @Before
// public void setup() {
// george = new Owner();
// george.setId(TEST_OWNER_ID);
// george.setFirstName("George");
// george.setLastName("Franklin");
// george.setAddress("110 W. Liberty St.");
// george.setCity("Madison");
// george.setTelephone("6085551023");
// given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
// }
//
// @Test
// public void testInitCreationForm() throws Exception {
// mockMvc.perform(get("/owners/new"))
// .andExpect(status().isOk())
// .andExpect(model().attributeExists("owner"))
// .andExpect(view().name("owners/createOrUpdateOwnerForm"));
// }
//
// @Test
// public void testProcessCreationFormSuccess() throws Exception {
// mockMvc.perform(post("/owners/new")
// .param("firstName", "Joe")
// .param("lastName", "Bloggs")
// .param("address", "123 Caramel Street")
// .param("city", "London")
// .param("telephone", "01316761638")
// )
// .andExpect(status().is3xxRedirection());
// }
//
// @Test
// public void testProcessCreationFormHasErrors() throws Exception {
// mockMvc.perform(post("/owners/new")
// .param("firstName", "Joe")
// .param("lastName", "Bloggs")
// .param("city", "London")
// )
// .andExpect(status().isOk())
// .andExpect(model().attributeHasErrors("owner"))
// .andExpect(model().attributeHasFieldErrors("owner", "address"))
// .andExpect(model().attributeHasFieldErrors("owner", "telephone"))
// .andExpect(view().name("owners/createOrUpdateOwnerForm"));
// }
//
// @Test
// public void testInitFindForm() throws Exception {
// mockMvc.perform(get("/owners/find"))
// .andExpect(status().isOk())
// .andExpect(model().attributeExists("owner"))
// .andExpect(view().name("owners/findOwners"));
// }
//
// @Test
// public void testProcessFindFormSuccess() throws Exception {
// given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
// mockMvc.perform(get("/owners"))
// .andExpect(status().isOk())
// .andExpect(view().name("owners/ownersList"));
// }
//
// @Test
// public void testProcessFindFormByLastName() throws Exception {
// given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
// mockMvc.perform(get("/owners")
// .param("lastName", "Franklin")
// )
// .andExpect(status().is3xxRedirection())
// .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
// }
//
// @Test
// public void testProcessFindFormNoOwnersFound() throws Exception {
// mockMvc.perform(get("/owners")
// .param("lastName", "Unknown Surname")
// )
// .andExpect(status().isOk())
// .andExpect(model().attributeHasFieldErrors("owner", "lastName"))
// .andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound"))
// .andExpect(view().name("owners/findOwners"));
// }
//
// @Test
// public void testInitUpdateOwnerForm() throws Exception {
// mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID))
// .andExpect(status().isOk())
// .andExpect(model().attributeExists("owner"))
// .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
// .andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
// .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
// .andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
// .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
// .andExpect(view().name("owners/createOrUpdateOwnerForm"));
// }
//
// @Test
// public void testProcessUpdateOwnerFormSuccess() throws Exception {
// mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)
// .param("firstName", "Joe")
// .param("lastName", "Bloggs")
// .param("address", "123 Caramel Street")
// .param("city", "London")
// .param("telephone", "01616291589")
// )
// .andExpect(status().is3xxRedirection())
// .andExpect(view().name("redirect:/owners/{ownerId}"));
// }
//
// @Test
// public void testProcessUpdateOwnerFormHasErrors() throws Exception {
// mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)
// .param("firstName", "Joe")
// .param("lastName", "Bloggs")
// .param("city", "London")
// )
// .andExpect(status().isOk())
// .andExpect(model().attributeHasErrors("owner"))
// .andExpect(model().attributeHasFieldErrors("owner", "address"))
// .andExpect(model().attributeHasFieldErrors("owner", "telephone"))
// .andExpect(view().name("owners/createOrUpdateOwnerForm"));
// }
//
// @Test
// public void testShowOwner() throws Exception {
// mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))
// .andExpect(status().isOk())
// .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
// .andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
// .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
// .andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
// .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
// .andExpect(view().name("owners/ownerDetails"));
// }
//
//}

View file

@ -1,145 +1,145 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.owner;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.owner.PetController;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.owner.PetTypeFormatter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
/**
* Test class for the {@link PetController}
*
* @author Colin But
*/
@RunWith(SpringRunner.class)
@WebMvcTest(value = PetController.class,
includeFilters = @ComponentScan.Filter(
value = PetTypeFormatter.class,
type = FilterType.ASSIGNABLE_TYPE))
public class PetControllerTests {
private static final int TEST_OWNER_ID = 1;
private static final int TEST_PET_ID = 1;
@Autowired
private MockMvc mockMvc;
@MockBean
private PetRepository pets;
@MockBean
private OwnerRepository owners;
@Before
public void setup() {
PetType cat = new PetType();
cat.setId(3);
cat.setName("hamster");
given(this.pets.findPetTypes()).willReturn(Lists.newArrayList(cat));
given(this.owners.findById(TEST_OWNER_ID)).willReturn(new Owner());
given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
}
@Test
public void testInitCreationForm() throws Exception {
mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdatePetForm"))
.andExpect(model().attributeExists("pet"));
}
@Test
public void testProcessCreationFormSuccess() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
.param("name", "Betty")
.param("type", "hamster")
.param("birthDate", "2015-02-12")
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}"));
}
@Test
public void testProcessCreationFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
.param("name", "Betty")
.param("birthDate", "2015-02-12")
)
.andExpect(model().attributeHasNoErrors("owner"))
.andExpect(model().attributeHasErrors("pet"))
.andExpect(model().attributeHasFieldErrors("pet", "type"))
.andExpect(model().attributeHasFieldErrorCode("pet", "type", "required"))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdatePetForm"));
}
@Test
public void testInitUpdateForm() throws Exception {
mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID))
.andExpect(status().isOk())
.andExpect(model().attributeExists("pet"))
.andExpect(view().name("pets/createOrUpdatePetForm"));
}
@Test
public void testProcessUpdateFormSuccess() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
.param("name", "Betty")
.param("type", "hamster")
.param("birthDate", "2015-02-12")
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}"));
}
@Test
public void testProcessUpdateFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
.param("name", "Betty")
.param("birthDate", "2015/02/12")
)
.andExpect(model().attributeHasNoErrors("owner"))
.andExpect(model().attributeHasErrors("pet"))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdatePetForm"));
}
}
///*
// * Copyright 2012-2019 the original author or authors.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * https://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package org.springframework.samples.petclinic.owner;
//
//import static org.mockito.BDDMockito.given;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
//
//import org.assertj.core.util.Lists;
//import org.junit.Before;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
//import org.springframework.boot.test.mock.mockito.MockBean;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.context.annotation.FilterType;
//import org.springframework.samples.petclinic.owner.Owner;
//import org.springframework.samples.petclinic.owner.OwnerRepository;
//import org.springframework.samples.petclinic.owner.Pet;
//import org.springframework.samples.petclinic.owner.PetController;
//import org.springframework.samples.petclinic.owner.PetRepository;
//import org.springframework.samples.petclinic.owner.PetType;
//import org.springframework.samples.petclinic.owner.PetTypeFormatter;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.test.web.servlet.MockMvc;
//
///**
// * Test class for the {@link PetController}
// *
// * @author Colin But
// */
//@RunWith(SpringRunner.class)
//@WebMvcTest(value = PetController.class,
// includeFilters = @ComponentScan.Filter(
// value = PetTypeFormatter.class,
// type = FilterType.ASSIGNABLE_TYPE))
//public class PetControllerTests {
//
// private static final int TEST_OWNER_ID = 1;
// private static final int TEST_PET_ID = 1;
//
//
// @Autowired
// private MockMvc mockMvc;
//
// @MockBean
// private PetRepository pets;
//
// @MockBean
// private OwnerRepository owners;
//
// @Before
// public void setup() {
// PetType cat = new PetType();
// cat.setId(3);
// cat.setName("hamster");
// given(this.pets.findPetTypes()).willReturn(Lists.newArrayList(cat));
// given(this.owners.findById(TEST_OWNER_ID)).willReturn(new Owner());
// given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
//
// }
//
// @Test
// public void testInitCreationForm() throws Exception {
// mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID))
// .andExpect(status().isOk())
// .andExpect(view().name("pets/createOrUpdatePetForm"))
// .andExpect(model().attributeExists("pet"));
// }
//
// @Test
// public void testProcessCreationFormSuccess() throws Exception {
// mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
// .param("name", "Betty")
// .param("type", "hamster")
// .param("birthDate", "2015-02-12")
// )
// .andExpect(status().is3xxRedirection())
// .andExpect(view().name("redirect:/owners/{ownerId}"));
// }
//
// @Test
// public void testProcessCreationFormHasErrors() throws Exception {
// mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
// .param("name", "Betty")
// .param("birthDate", "2015-02-12")
// )
// .andExpect(model().attributeHasNoErrors("owner"))
// .andExpect(model().attributeHasErrors("pet"))
// .andExpect(model().attributeHasFieldErrors("pet", "type"))
// .andExpect(model().attributeHasFieldErrorCode("pet", "type", "required"))
// .andExpect(status().isOk())
// .andExpect(view().name("pets/createOrUpdatePetForm"));
// }
//
// @Test
// public void testInitUpdateForm() throws Exception {
// mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID))
// .andExpect(status().isOk())
// .andExpect(model().attributeExists("pet"))
// .andExpect(view().name("pets/createOrUpdatePetForm"));
// }
//
// @Test
// public void testProcessUpdateFormSuccess() throws Exception {
// mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
// .param("name", "Betty")
// .param("type", "hamster")
// .param("birthDate", "2015-02-12")
// )
// .andExpect(status().is3xxRedirection())
// .andExpect(view().name("redirect:/owners/{ownerId}"));
// }
//
// @Test
// public void testProcessUpdateFormHasErrors() throws Exception {
// mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
// .param("name", "Betty")
// .param("birthDate", "2015/02/12")
// )
// .andExpect(model().attributeHasNoErrors("owner"))
// .andExpect(model().attributeHasErrors("pet"))
// .andExpect(status().isOk())
// .andExpect(view().name("pets/createOrUpdatePetForm"));
// }
//
//}

View file

@ -1,93 +1,93 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.owner;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
/**
* Test class for {@link PetTypeFormatter}
*
* @author Colin But
*/
@RunWith(MockitoJUnitRunner.class)
public class PetTypeFormatterTests {
@Mock
private PetRepository pets;
private PetTypeFormatter petTypeFormatter;
@Before
public void setup() {
this.petTypeFormatter = new PetTypeFormatter(pets);
}
@Test
public void testPrint() {
PetType petType = new PetType();
petType.setName("Hamster");
String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH);
assertThat(petTypeName).isEqualTo("Hamster");
}
@Test
public void shouldParse() throws ParseException {
given(this.pets.findPetTypes()).willReturn(makePetTypes());
PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
assertThat(petType.getName()).isEqualTo("Bird");
}
@Test(expected = ParseException.class)
public void shouldThrowParseException() throws ParseException {
given(this.pets.findPetTypes()).willReturn(makePetTypes());
petTypeFormatter.parse("Fish", Locale.ENGLISH);
}
/**
* Helper method to produce some sample pet types just for test purpose
*
* @return {@link Collection} of {@link PetType}
*/
private List<PetType> makePetTypes() {
List<PetType> petTypes = new ArrayList<>();
petTypes.add(new PetType() {
{
setName("Dog");
}
});
petTypes.add(new PetType() {
{
setName("Bird");
}
});
return petTypes;
}
}
///*
// * Copyright 2012-2019 the original author or authors.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * https://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package org.springframework.samples.petclinic.owner;
//
//import java.text.ParseException;
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.List;
//import java.util.Locale;
//
//import org.junit.Before;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.mockito.Mock;
//import org.mockito.junit.MockitoJUnitRunner;
//
//import static org.assertj.core.api.Assertions.assertThat;
//import static org.mockito.BDDMockito.given;
//
///**
// * Test class for {@link PetTypeFormatter}
// *
// * @author Colin But
// */
//@RunWith(MockitoJUnitRunner.class)
//public class PetTypeFormatterTests {
//
// @Mock
// private PetRepository pets;
//
// private PetTypeFormatter petTypeFormatter;
//
// @Before
// public void setup() {
// this.petTypeFormatter = new PetTypeFormatter(pets);
// }
//
// @Test
// public void testPrint() {
// PetType petType = new PetType();
// petType.setName("Hamster");
// String petTypeName = this.petTypeFormatter.print(petType, Locale.ENGLISH);
// assertThat(petTypeName).isEqualTo("Hamster");
// }
//
// @Test
// public void shouldParse() throws ParseException {
// given(this.pets.findPetTypes()).willReturn(makePetTypes());
// PetType petType = petTypeFormatter.parse("Bird", Locale.ENGLISH);
// assertThat(petType.getName()).isEqualTo("Bird");
// }
//
// @Test(expected = ParseException.class)
// public void shouldThrowParseException() throws ParseException {
// given(this.pets.findPetTypes()).willReturn(makePetTypes());
// petTypeFormatter.parse("Fish", Locale.ENGLISH);
// }
//
// /**
// * Helper method to produce some sample pet types just for test purpose
// *
// * @return {@link Collection} of {@link PetType}
// */
// private List<PetType> makePetTypes() {
// List<PetType> petTypes = new ArrayList<>();
// petTypes.add(new PetType() {
// {
// setName("Dog");
// }
// });
// petTypes.add(new PetType() {
// {
// setName("Bird");
// }
// });
// return petTypes;
// }
//
//}

View file

@ -1,91 +1,91 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.owner;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.VisitController;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
/**
* Test class for {@link VisitController}
*
* @author Colin But
*/
@RunWith(SpringRunner.class)
@WebMvcTest(VisitController.class)
public class VisitControllerTests {
private static final int TEST_PET_ID = 1;
@Autowired
private MockMvc mockMvc;
@MockBean
private VisitRepository visits;
@MockBean
private PetRepository pets;
@Before
public void init() {
given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
}
@Test
public void testInitNewVisitForm() throws Exception {
mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdateVisitForm"));
}
@Test
public void testProcessNewVisitFormSuccess() throws Exception {
mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)
.param("name", "George")
.param("description", "Visit Description")
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}"));
}
@Test
public void testProcessNewVisitFormHasErrors() throws Exception {
mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)
.param("name", "George")
)
.andExpect(model().attributeHasErrors("visit"))
.andExpect(status().isOk())
.andExpect(view().name("pets/createOrUpdateVisitForm"));
}
}
///*
// * Copyright 2012-2019 the original author or authors.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * https://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package org.springframework.samples.petclinic.owner;
//
//import static org.mockito.BDDMockito.given;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
//
//import org.junit.Before;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
//import org.springframework.boot.test.mock.mockito.MockBean;
//import org.springframework.samples.petclinic.owner.Pet;
//import org.springframework.samples.petclinic.owner.PetRepository;
//import org.springframework.samples.petclinic.owner.VisitController;
//import org.springframework.samples.petclinic.visit.VisitRepository;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.test.web.servlet.MockMvc;
//
///**
// * Test class for {@link VisitController}
// *
// * @author Colin But
// */
//@RunWith(SpringRunner.class)
//@WebMvcTest(VisitController.class)
//public class VisitControllerTests {
//
// private static final int TEST_PET_ID = 1;
//
// @Autowired
// private MockMvc mockMvc;
//
// @MockBean
// private VisitRepository visits;
//
// @MockBean
// private PetRepository pets;
//
// @Before
// public void init() {
// given(this.pets.findById(TEST_PET_ID)).willReturn(new Pet());
// }
//
// @Test
// public void testInitNewVisitForm() throws Exception {
// mockMvc.perform(get("/owners/*/pets/{petId}/visits/new", TEST_PET_ID))
// .andExpect(status().isOk())
// .andExpect(view().name("pets/createOrUpdateVisitForm"));
// }
//
// @Test
// public void testProcessNewVisitFormSuccess() throws Exception {
// mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)
// .param("name", "George")
// .param("description", "Visit Description")
// )
// .andExpect(status().is3xxRedirection())
// .andExpect(view().name("redirect:/owners/{ownerId}"));
// }
//
// @Test
// public void testProcessNewVisitFormHasErrors() throws Exception {
// mockMvc.perform(post("/owners/*/pets/{petId}/visits/new", TEST_PET_ID)
// .param("name", "George")
// )
// .andExpect(model().attributeHasErrors("visit"))
// .andExpect(status().isOk())
// .andExpect(view().name("pets/createOrUpdateVisitForm"));
// }
//
//}

View file

@ -1,222 +1,222 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.service;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalDate;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.samples.petclinic.owner.Owner;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.owner.Pet;
import org.springframework.samples.petclinic.owner.PetRepository;
import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.vet.Vet;
import org.springframework.samples.petclinic.vet.VetRepository;
import org.springframework.samples.petclinic.visit.Visit;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Integration test of the Service and the Repository layer.
* <p>
* ClinicServiceSpringDataJpaTests subclasses benefit from the following services provided by the Spring
* TestContext Framework: </p> <ul> <li><strong>Spring IoC container caching</strong> which spares us unnecessary set up
* time between test execution.</li> <li><strong>Dependency Injection</strong> of test fixture instances, meaning that
* we don't need to perform application context lookups. See the use of {@link Autowired @Autowired} on the <code>{@link
* ClinicServiceTests#clinicService clinicService}</code> instance variable, which uses autowiring <em>by
* type</em>. <li><strong>Transaction management</strong>, meaning each test method is executed in its own transaction,
* which is automatically rolled back by default. Thus, even if tests insert or otherwise change database state, there
* is no need for a teardown or cleanup script. <li> An {@link org.springframework.context.ApplicationContext
* ApplicationContext} is also inherited and can be used for explicit bean lookup if necessary. </li> </ul>
*
* @author Ken Krebs
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
public class ClinicServiceTests {
@Autowired
protected OwnerRepository owners;
@Autowired
protected PetRepository pets;
@Autowired
protected VisitRepository visits;
@Autowired
protected VetRepository vets;
@Test
public void shouldFindOwnersByLastName() {
Collection<Owner> owners = this.owners.findByLastName("Davis");
assertThat(owners).hasSize(2);
owners = this.owners.findByLastName("Daviss");
assertThat(owners).isEmpty();
}
@Test
public void shouldFindSingleOwnerWithPet() {
Owner owner = this.owners.findById(1);
assertThat(owner.getLastName()).startsWith("Franklin");
assertThat(owner.getPets()).hasSize(1);
assertThat(owner.getPets().get(0).getType()).isNotNull();
assertThat(owner.getPets().get(0).getType().getName()).isEqualTo("cat");
}
@Test
@Transactional
public void shouldInsertOwner() {
Collection<Owner> owners = this.owners.findByLastName("Schultz");
int found = owners.size();
Owner owner = new Owner();
owner.setFirstName("Sam");
owner.setLastName("Schultz");
owner.setAddress("4, Evans Street");
owner.setCity("Wollongong");
owner.setTelephone("4444444444");
this.owners.save(owner);
assertThat(owner.getId().longValue()).isNotEqualTo(0);
owners = this.owners.findByLastName("Schultz");
assertThat(owners.size()).isEqualTo(found + 1);
}
@Test
@Transactional
public void shouldUpdateOwner() {
Owner owner = this.owners.findById(1);
String oldLastName = owner.getLastName();
String newLastName = oldLastName + "X";
owner.setLastName(newLastName);
this.owners.save(owner);
// retrieving new name from database
owner = this.owners.findById(1);
assertThat(owner.getLastName()).isEqualTo(newLastName);
}
@Test
public void shouldFindPetWithCorrectId() {
Pet pet7 = this.pets.findById(7);
assertThat(pet7.getName()).startsWith("Samantha");
assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
}
@Test
public void shouldFindAllPetTypes() {
Collection<PetType> petTypes = this.pets.findPetTypes();
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
assertThat(petType1.getName()).isEqualTo("cat");
PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
assertThat(petType4.getName()).isEqualTo("snake");
}
@Test
@Transactional
public void shouldInsertPetIntoDatabaseAndGenerateId() {
Owner owner6 = this.owners.findById(6);
int found = owner6.getPets().size();
Pet pet = new Pet();
pet.setName("bowser");
Collection<PetType> types = this.pets.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(LocalDate.now());
owner6.addPet(pet);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
this.pets.save(pet);
this.owners.save(owner6);
owner6 = this.owners.findById(6);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
// checks that id has been generated
assertThat(pet.getId()).isNotNull();
}
@Test
@Transactional
public void shouldUpdatePetName() throws Exception {
Pet pet7 = this.pets.findById(7);
String oldName = pet7.getName();
String newName = oldName + "X";
pet7.setName(newName);
this.pets.save(pet7);
pet7 = this.pets.findById(7);
assertThat(pet7.getName()).isEqualTo(newName);
}
@Test
public void shouldFindVets() {
Collection<Vet> vets = this.vets.findAll();
Vet vet = EntityUtils.getById(vets, Vet.class, 3);
assertThat(vet.getLastName()).isEqualTo("Douglas");
assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
}
@Test
@Transactional
public void shouldAddNewVisitForPet() {
Pet pet7 = this.pets.findById(7);
int found = pet7.getVisits().size();
Visit visit = new Visit();
pet7.addVisit(visit);
visit.setDescription("test");
this.visits.save(visit);
this.pets.save(pet7);
pet7 = this.pets.findById(7);
assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
assertThat(visit.getId()).isNotNull();
}
@Test
public void shouldFindVisitsByPetId() throws Exception {
Collection<Visit> visits = this.visits.findByPetId(7);
assertThat(visits).hasSize(2);
Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
assertThat(visitArr[0].getDate()).isNotNull();
assertThat(visitArr[0].getPetId()).isEqualTo(7);
}
}
///*
// * Copyright 2012-2019 the original author or authors.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * https://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package org.springframework.samples.petclinic.service;
//
//import static org.assertj.core.api.Assertions.assertThat;
//
//import java.time.LocalDate;
//import java.util.Collection;
//
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.samples.petclinic.owner.Owner;
//import org.springframework.samples.petclinic.owner.OwnerRepository;
//import org.springframework.samples.petclinic.owner.Pet;
//import org.springframework.samples.petclinic.owner.PetRepository;
//import org.springframework.samples.petclinic.owner.PetType;
//import org.springframework.samples.petclinic.vet.Vet;
//import org.springframework.samples.petclinic.vet.VetRepository;
//import org.springframework.samples.petclinic.visit.Visit;
//import org.springframework.samples.petclinic.visit.VisitRepository;
//import org.springframework.stereotype.Service;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.transaction.annotation.Transactional;
//
///**
// * Integration test of the Service and the Repository layer.
// * <p>
// * ClinicServiceSpringDataJpaTests subclasses benefit from the following services provided by the Spring
// * TestContext Framework: </p> <ul> <li><strong>Spring IoC container caching</strong> which spares us unnecessary set up
// * time between test execution.</li> <li><strong>Dependency Injection</strong> of test fixture instances, meaning that
// * we don't need to perform application context lookups. See the use of {@link Autowired @Autowired} on the <code>{@link
// * ClinicServiceTests#clinicService clinicService}</code> instance variable, which uses autowiring <em>by
// * type</em>. <li><strong>Transaction management</strong>, meaning each test method is executed in its own transaction,
// * which is automatically rolled back by default. Thus, even if tests insert or otherwise change database state, there
// * is no need for a teardown or cleanup script. <li> An {@link org.springframework.context.ApplicationContext
// * ApplicationContext} is also inherited and can be used for explicit bean lookup if necessary. </li> </ul>
// *
// * @author Ken Krebs
// * @author Rod Johnson
// * @author Juergen Hoeller
// * @author Sam Brannen
// * @author Michael Isvy
// * @author Dave Syer
// */
//
//@RunWith(SpringRunner.class)
//@DataJpaTest(includeFilters = @ComponentScan.Filter(Service.class))
//public class ClinicServiceTests {
//
// @Autowired
// protected OwnerRepository owners;
//
// @Autowired
// protected PetRepository pets;
//
// @Autowired
// protected VisitRepository visits;
//
// @Autowired
// protected VetRepository vets;
//
// @Test
// public void shouldFindOwnersByLastName() {
// Collection<Owner> owners = this.owners.findByLastName("Davis");
// assertThat(owners).hasSize(2);
//
// owners = this.owners.findByLastName("Daviss");
// assertThat(owners).isEmpty();
// }
//
// @Test
// public void shouldFindSingleOwnerWithPet() {
// Owner owner = this.owners.findById(1);
// assertThat(owner.getLastName()).startsWith("Franklin");
// assertThat(owner.getPets()).hasSize(1);
// assertThat(owner.getPets().get(0).getType()).isNotNull();
// assertThat(owner.getPets().get(0).getType().getName()).isEqualTo("cat");
// }
//
// @Test
// @Transactional
// public void shouldInsertOwner() {
// Collection<Owner> owners = this.owners.findByLastName("Schultz");
// int found = owners.size();
//
// Owner owner = new Owner();
// owner.setFirstName("Sam");
// owner.setLastName("Schultz");
// owner.setAddress("4, Evans Street");
// owner.setCity("Wollongong");
// owner.setTelephone("4444444444");
// this.owners.save(owner);
// assertThat(owner.getId().longValue()).isNotEqualTo(0);
//
// owners = this.owners.findByLastName("Schultz");
// assertThat(owners.size()).isEqualTo(found + 1);
// }
//
// @Test
// @Transactional
// public void shouldUpdateOwner() {
// Owner owner = this.owners.findById(1);
// String oldLastName = owner.getLastName();
// String newLastName = oldLastName + "X";
//
// owner.setLastName(newLastName);
// this.owners.save(owner);
//
// // retrieving new name from database
// owner = this.owners.findById(1);
// assertThat(owner.getLastName()).isEqualTo(newLastName);
// }
//
// @Test
// public void shouldFindPetWithCorrectId() {
// Pet pet7 = this.pets.findById(7);
// assertThat(pet7.getName()).startsWith("Samantha");
// assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
//
// }
//
// @Test
// public void shouldFindAllPetTypes() {
// Collection<PetType> petTypes = this.pets.findPetTypes();
//
// PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
// assertThat(petType1.getName()).isEqualTo("cat");
// PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
// assertThat(petType4.getName()).isEqualTo("snake");
// }
//
// @Test
// @Transactional
// public void shouldInsertPetIntoDatabaseAndGenerateId() {
// Owner owner6 = this.owners.findById(6);
// int found = owner6.getPets().size();
//
// Pet pet = new Pet();
// pet.setName("bowser");
// Collection<PetType> types = this.pets.findPetTypes();
// pet.setType(EntityUtils.getById(types, PetType.class, 2));
// pet.setBirthDate(LocalDate.now());
// owner6.addPet(pet);
// assertThat(owner6.getPets().size()).isEqualTo(found + 1);
//
// this.pets.save(pet);
// this.owners.save(owner6);
//
// owner6 = this.owners.findById(6);
// assertThat(owner6.getPets().size()).isEqualTo(found + 1);
// // checks that id has been generated
// assertThat(pet.getId()).isNotNull();
// }
//
// @Test
// @Transactional
// public void shouldUpdatePetName() throws Exception {
// Pet pet7 = this.pets.findById(7);
// String oldName = pet7.getName();
//
// String newName = oldName + "X";
// pet7.setName(newName);
// this.pets.save(pet7);
//
// pet7 = this.pets.findById(7);
// assertThat(pet7.getName()).isEqualTo(newName);
// }
//
// @Test
// public void shouldFindVets() {
// Collection<Vet> vets = this.vets.findAll();
//
// Vet vet = EntityUtils.getById(vets, Vet.class, 3);
// assertThat(vet.getLastName()).isEqualTo("Douglas");
// assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
// assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
// assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
// }
//
// @Test
// @Transactional
// public void shouldAddNewVisitForPet() {
// Pet pet7 = this.pets.findById(7);
// int found = pet7.getVisits().size();
// Visit visit = new Visit();
// pet7.addVisit(visit);
// visit.setDescription("test");
// this.visits.save(visit);
// this.pets.save(pet7);
//
// pet7 = this.pets.findById(7);
// assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
// assertThat(visit.getId()).isNotNull();
// }
//
// @Test
// public void shouldFindVisitsByPetId() throws Exception {
// Collection<Visit> visits = this.visits.findByPetId(7);
// assertThat(visits).hasSize(2);
// Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
// assertThat(visitArr[0].getDate()).isNotNull();
// assertThat(visitArr[0].getPetId()).isEqualTo(7);
// }
//
//}