diff --git a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java index 984884e11091e73dc8864a347c8b92e9aaf3e52a..6f157854fe2661abd2b0feab960ae690e819069d 100644 --- a/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java +++ b/spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java @@ -8,6 +8,9 @@ import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; +/** + * @author Maciej Szarlinski + */ @EnableZuulProxy @EnableDiscoveryClient @SpringBootApplication diff --git a/spring-petclinic-config-server/src/main/java/org/springframework/samples/petclinic/config/ConfigServerApplication.java b/spring-petclinic-config-server/src/main/java/org/springframework/samples/petclinic/config/ConfigServerApplication.java index dea83320be555a72265a78662ab9296d4821abad..99e4e8fe592413dbe06bd4dab8f78abdd31b9ff7 100644 --- a/spring-petclinic-config-server/src/main/java/org/springframework/samples/petclinic/config/ConfigServerApplication.java +++ b/spring-petclinic-config-server/src/main/java/org/springframework/samples/petclinic/config/ConfigServerApplication.java @@ -4,6 +4,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; +/** + * @author Maciej Szarlinski + */ @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/CustomersServiceApplication.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/CustomersServiceApplication.java index 681e655b3eb7567b841ef06968e45d31522bc047..014149ee97fa65bdc34613348a5adf076c2d6597 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/CustomersServiceApplication.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/CustomersServiceApplication.java @@ -6,6 +6,9 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Import; import org.springframework.samples.petclinic.monitoring.MonitoringConfig; +/** + * @author Maciej Szarlinski + */ @EnableDiscoveryClient @SpringBootApplication @Import(MonitoringConfig.class) diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/application/OwnerService.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/application/OwnerService.java deleted file mode 100644 index ac363749e5e00a43fb52080ac7f62dc3e9901d56..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/application/OwnerService.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.springframework.samples.petclinic.customers.application; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.customers.domain.model.owner.Owner; -import org.springframework.samples.petclinic.customers.domain.model.owner.OwnerRepository; -import org.springframework.samples.petclinic.monitoring.Monitored; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Collection; - -/** - * @author Maciej Szarlinski - */ -@Service -public class OwnerService { - - private static final Logger LOG = LoggerFactory.getLogger(OwnerService.class); - - private final OwnerRepository ownerRepository; - - @Autowired - public OwnerService(OwnerRepository ownerRepository) { - this.ownerRepository = ownerRepository; - } - - @Transactional(readOnly = true) - public Owner findOwnerById(int id) throws DataAccessException { - return ownerRepository.findOne(id); - } - - @Monitored - @Transactional(readOnly = true) - public Collection<Owner> findAll() throws DataAccessException { - return ownerRepository.findAll(); - } - - @Monitored - @Transactional - public void saveOwner(Owner owner) throws DataAccessException { - LOG.info("Saving owner {}", owner); - ownerRepository.save(owner); - } - -} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/application/PetService.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/application/PetService.java deleted file mode 100644 index 54d97ca9230df20763cbe19a0398e92756748512..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/application/PetService.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.springframework.samples.petclinic.customers.application; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.customers.domain.model.pet.Pet; -import org.springframework.samples.petclinic.customers.domain.model.pet.PetRepository; -import org.springframework.samples.petclinic.customers.domain.model.pet.PetType; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Collection; -import java.util.Optional; - -/** - * @author Maciej Szarlinski - */ -@Service -public class PetService { - - private static final Logger LOG = LoggerFactory.getLogger(PetService.class); - - private final PetRepository petRepository; - - @Autowired - public PetService(PetRepository petRepository) { - this.petRepository = petRepository; - } - - @Transactional(readOnly = true) - public Pet findPetById(int id) throws DataAccessException { - return petRepository.findById(id); - } - - @Transactional - public void savePet(Pet pet) throws DataAccessException { - LOG.info("Saving pet {}", pet); - petRepository.save(pet); - } - - @Transactional(readOnly = true) - public Collection<PetType> findPetTypes() throws DataAccessException { - return petRepository.findPetTypes(); - } - - @Transactional(readOnly = true) - public Optional<PetType> findPetTypeById(int typeId) { - return petRepository.findPetTypeById(typeId); - } -} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetResource.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetResource.java deleted file mode 100644 index 5276deef4641f7495355c236b09dda884afca70d..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetResource.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.customers.boundary.web.pet; - -import com.fasterxml.jackson.annotation.JsonFormat; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.http.HttpStatus; -import org.springframework.samples.petclinic.customers.application.OwnerService; -import org.springframework.samples.petclinic.customers.application.PetService; -import org.springframework.samples.petclinic.customers.domain.model.owner.Owner; -import org.springframework.samples.petclinic.customers.domain.model.pet.Pet; -import org.springframework.samples.petclinic.customers.domain.model.pet.PetType; -import org.springframework.web.bind.annotation.*; - -import javax.validation.constraints.Size; -import java.util.Collection; -import java.util.Date; - -/** - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - */ -@RestController -public class PetResource { - - private final PetService petService; - - private final OwnerService ownerService; - - @Autowired - public PetResource(PetService petService, OwnerService ownerService) { - this.petService = petService; - this.ownerService = ownerService; - } - - @GetMapping("/petTypes") - public Collection<PetType> getPetTypes() { - return petService.findPetTypes(); - } - - @PostMapping("/owners/{ownerId}/pets") - @ResponseStatus(HttpStatus.NO_CONTENT) - public void processCreationForm( - @RequestBody PetRequest petRequest, - @PathVariable("ownerId") int ownerId) { - - Pet pet = new Pet(); - Owner owner = ownerService.findOwnerById(ownerId); - owner.addPet(pet); - - save(pet, petRequest); - } - - @PutMapping("/owners/*/pets/{petId}") - @ResponseStatus(HttpStatus.NO_CONTENT) - public void processUpdateForm(@RequestBody PetRequest petRequest) { - save(petService.findPetById(petRequest.getId()), petRequest); - } - - private void save(Pet pet, PetRequest petRequest) { - - pet.setName(petRequest.getName()); - pet.setBirthDate(petRequest.getBirthDate()); - - petService.findPetTypeById(petRequest.getTypeId()) - .ifPresent(pet::setType); - - petService.savePet(pet); - } - - @GetMapping("owners/*/pets/{petId}") - public PetDetails findPet(@PathVariable("petId") int petId) { - return new PetDetails(petService.findPetById(petId)); - } - - static class PetRequest { - int id; - @JsonFormat(pattern = "yyyy-MM-dd") - Date birthDate; - @Size(min = 1) - String name; - int typeId; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public Date getBirthDate() { - return birthDate; - } - - public void setBirthDate(Date birthDate) { - this.birthDate = birthDate; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getTypeId() { - return typeId; - } - - public void setTypeId(int typeId) { - this.typeId = typeId; - } - } - - static class PetDetails { - - long id; - String name; - String owner; - @DateTimeFormat(pattern = "yyyy-MM-dd") - Date birthDate; - PetType type; - - PetDetails(Pet pet) { - this.id = pet.getId(); - this.name = pet.getName(); - this.owner = pet.getOwner().getFirstName() + " " + pet.getOwner().getLastName(); - this.birthDate = pet.getBirthDate(); - this.type = pet.getType(); - } - - public long getId() { - return id; - } - - public String getName() { - return name; - } - - public String getOwner() { - return owner; - } - - public Date getBirthDate() { - return birthDate; - } - - public PetType getType() { - return type; - } - } - -} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetValidator.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetValidator.java deleted file mode 100644 index fb8cdc24bca5b6547f7cd9cbcd9d87f8b9fbaaa2..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetValidator.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.customers.boundary.web.pet; - -import org.springframework.samples.petclinic.customers.domain.model.pet.Pet; -import org.springframework.util.StringUtils; -import org.springframework.validation.Errors; - -/** - * <code>Validator</code> for <code>Pet</code> forms. - * - * @author Ken Krebs - * @author Juergen Hoeller - */ -//TODO: unused? -public class PetValidator { - - public void validate(Pet pet, Errors errors) { - String name = pet.getName(); - // name validation - if (!StringUtils.hasLength(name)) { - errors.rejectValue("name", "required", "required"); - } else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) { - errors.rejectValue("name", "duplicate", "already exists"); - } - - // type validation - if (pet.isNew() && pet.getType() == null) { - errors.rejectValue("type", "required", "required"); - } - - // birth date validation - if (pet.getBirthDate() == null) { - errors.rejectValue("birthDate", "required", "required"); - } - } - -} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/Person.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/Person.java deleted file mode 100644 index faea7f4ece48141ec8063fd92f9b896d5c0db894..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/Person.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.customers.domain.model; - -import org.hibernate.validator.constraints.NotEmpty; -import org.springframework.samples.petclinic.customers.support.jpa.BaseEntity; - -import javax.persistence.Column; -import javax.persistence.MappedSuperclass; - -/** - * Simple JavaBean domain object representing an person. - * - * @author Ken Krebs - */ -@MappedSuperclass -public class Person extends BaseEntity { - - @Column(name = "first_name") - @NotEmpty - protected String firstName; - - @Column(name = "last_name") - @NotEmpty - protected String lastName; - - public String getFirstName() { - return this.firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return this.lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - -} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/PetType.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/PetType.java deleted file mode 100644 index 59065f5eb6e907eb02b3a03b5f5db288218f592a..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/PetType.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.customers.domain.model.pet; - -import org.springframework.samples.petclinic.customers.support.jpa.NamedEntity; - -import javax.persistence.Entity; -import javax.persistence.Table; - -/** - * @author Juergen Hoeller - * Can be Cat, Dog, Hamster... - */ -@Entity -@Table(name = "types") -public class PetType extends NamedEntity { - -} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/owner/Owner.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Owner.java similarity index 66% rename from spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/owner/Owner.java rename to spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Owner.java index 4b873b20efb1bbf5d809877c562e1ea8e8338c72..49fc96ea599f55eb78cefb59606b359e4b1ee2d7 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/owner/Owner.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Owner.java @@ -13,18 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.customers.domain.model.owner; +package org.springframework.samples.petclinic.customers.model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.validation.constraints.Digits; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.core.style.ToStringCreator; -import org.springframework.samples.petclinic.customers.domain.model.Person; -import org.springframework.samples.petclinic.customers.domain.model.pet.Pet; - -import javax.persistence.*; -import javax.validation.constraints.Digits; -import java.util.*; /** * Simple JavaBean domain object representing an owner. @@ -33,10 +44,24 @@ import java.util.*; * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Maciej Szarlinski */ @Entity @Table(name = "owners") -public class Owner extends Person { +public class Owner { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "first_name") + @NotEmpty + private String firstName; + + @Column(name = "last_name") + @NotEmpty + private String lastName; + @Column(name = "address") @NotEmpty private String address; @@ -53,6 +78,26 @@ public class Owner extends Person { @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "owner") private Set<Pet> pets; + public Integer getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(final String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(final String lastName) { + this.lastName = lastName; + } + public String getAddress() { return this.address; } @@ -77,19 +122,15 @@ public class Owner extends Person { this.telephone = telephone; } - protected void setPetsInternal(Set<Pet> pets) { - this.pets = pets; - } - protected Set<Pet> getPetsInternal() { if (this.pets == null) { - this.pets = new HashSet<Pet>(); + this.pets = new HashSet<>(); } return this.pets; } public List<Pet> getPets() { - List<Pet> sortedPets = new ArrayList<Pet>(getPetsInternal()); + final List<Pet> sortedPets = new ArrayList<>(getPetsInternal()); PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); return Collections.unmodifiableList(sortedPets); } @@ -99,42 +140,11 @@ public class Owner extends Person { pet.setOwner(this); } - /** - * Return the Pet with the given name, or null if none found for this Owner. - * - * @param name to test - * @return true if pet name is already in use - */ - public Pet getPet(String name) { - return getPet(name, false); - } - - /** - * Return the Pet with the given name, or null if none found for this Owner. - * - * @param name to test - * @return true if pet name is already in use - */ - public Pet getPet(String name, boolean ignoreNew) { - name = name.toLowerCase(); - for (Pet pet : getPetsInternal()) { - if (!ignoreNew || !pet.isNew()) { - String compName = pet.getName(); - compName = compName.toLowerCase(); - if (compName.equals(name)) { - return pet; - } - } - } - return null; - } - @Override public String toString() { return new ToStringCreator(this) .append("id", this.getId()) - .append("new", this.isNew()) .append("lastName", this.getLastName()) .append("firstName", this.getFirstName()) .append("address", this.address) diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/owner/OwnerRepository.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/OwnerRepository.java similarity index 92% rename from spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/owner/OwnerRepository.java rename to spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/OwnerRepository.java index 56980bbb623a72105c980302a5fb1f53dade2504..73aad51101572678f52407112d71e6aa3a9a143d 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/owner/OwnerRepository.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/OwnerRepository.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.customers.domain.model.owner; +package org.springframework.samples.petclinic.customers.model; import org.springframework.data.jpa.repository.JpaRepository; @@ -25,5 +25,6 @@ import org.springframework.data.jpa.repository.JpaRepository; * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Maciej Szarlinski */ public interface OwnerRepository extends JpaRepository<Owner, Integer> { } diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/Pet.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Pet.java similarity index 58% rename from spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/Pet.java rename to spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Pet.java index be6decb4d70dfc931b5ba4e6c9daa40726a2539b..625ff7371da9423f2f5304020624ed468cb9e8a1 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/Pet.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/Pet.java @@ -13,25 +13,40 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.customers.domain.model.pet; +package org.springframework.samples.petclinic.customers.model; -import com.fasterxml.jackson.annotation.JsonIgnore; -import org.springframework.samples.petclinic.customers.domain.model.owner.Owner; -import org.springframework.samples.petclinic.customers.support.jpa.NamedEntity; - -import javax.persistence.*; import java.util.Date; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +import com.fasterxml.jackson.annotation.JsonIgnore; + /** * Simple business object representing a pet. * * @author Ken Krebs * @author Juergen Hoeller * @author Sam Brannen + * @author Maciej Szarlinski */ @Entity @Table(name = "pets") -public class Pet extends NamedEntity { +public class Pet { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "name") + private String name; @Column(name = "birth_date") @Temporal(TemporalType.DATE) @@ -46,27 +61,43 @@ public class Pet extends NamedEntity { @JsonIgnore private Owner owner; - public void setBirthDate(Date birthDate) { - this.birthDate = birthDate; + public Integer getId() { + return id; + } + + public void setId(final Integer id) { + this.id = id; + } + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; } public Date getBirthDate() { return birthDate; } - public void setType(PetType type) { - this.type = type; + public void setBirthDate(final Date birthDate) { + this.birthDate = birthDate; } public PetType getType() { return type; } - public void setOwner(Owner owner) { - this.owner = owner; + public void setType(final PetType type) { + this.type = type; } public Owner getOwner() { return owner; } + + public void setOwner(final Owner owner) { + this.owner = owner; + } } diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/PetRepository.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetRepository.java similarity index 74% rename from spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/PetRepository.java rename to spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetRepository.java index 1a386d62cce67f170f4550b3be1562ca859907aa..4473853e543825da2a8559775005c7ec401a9331 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/domain/model/pet/PetRepository.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetRepository.java @@ -13,15 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.customers.domain.model.pet; - -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; -import org.springframework.data.repository.query.Param; +package org.springframework.samples.petclinic.customers.model; 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.query.Param; + /** * Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming * conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation @@ -30,8 +30,9 @@ import java.util.Optional; * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Maciej Szarlinski */ -public interface PetRepository extends Repository<Pet, Integer> { +public interface PetRepository extends JpaRepository<Pet, Integer> { /** * Retrieve all {@link PetType}s from the data store. @@ -43,18 +44,6 @@ public interface PetRepository extends Repository<Pet, Integer> { @Query("FROM PetType ptype WHERE ptype.id = :typeId") Optional<PetType> findPetTypeById(@Param("typeId") int typeId); - /** - * Retrieve a {@link Pet} from the data store by id. - * @param id the id to search for - * @return the {@link Pet} if found - */ - Pet findById(int 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); } diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/support/jpa/BaseEntity.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetType.java similarity index 68% rename from spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/support/jpa/BaseEntity.java rename to spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetType.java index c59a4384d79d9cd8fdf5d0c70601c5b5cf35e1b5..2074390160cd0b380576ea479a73196bc1ae12a6 100644 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/support/jpa/BaseEntity.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetType.java @@ -13,36 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.visits.support.jpa; +package org.springframework.samples.petclinic.customers.model; +import javax.persistence.Column; +import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.MappedSuperclass; +import javax.persistence.Table; /** - * Simple JavaBean domain object with an id property. Used as a base class for objects needing this property. - * - * @author Ken Krebs * @author Juergen Hoeller + * Can be Cat, Dog, Hamster... */ -@MappedSuperclass -public class BaseEntity { +@Entity +@Table(name = "types") +public class PetType { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - protected Integer id; - + private Integer id; - public void setId(Integer id) { - this.id = id; - } + @Column(name = "name") + private String name; public Integer getId() { return id; } - public boolean isNew() { - return (this.id == null); + public void setId(final Integer id) { + this.id = id; } + public String getName() { + return this.name; + } } diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/support/jpa/BaseEntity.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/support/jpa/BaseEntity.java deleted file mode 100644 index 09dc6e985052d47d66bbb8789d19b2b93690e317..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/support/jpa/BaseEntity.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.customers.support.jpa; - -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.MappedSuperclass; - -/** - * Simple JavaBean domain object with an id property. Used as a base class for objects needing this property. - * - * @author Ken Krebs - * @author Juergen Hoeller - */ -@MappedSuperclass -public class BaseEntity { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - protected Integer id; - - - public void setId(Integer id) { - this.id = id; - } - - public Integer getId() { - return id; - } - - public boolean isNew() { - return (this.id == null); - } - -} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/support/jpa/NamedEntity.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/support/jpa/NamedEntity.java deleted file mode 100644 index cf5b8218d74cf471a3b889f53bbcfb1454c4d9b3..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/support/jpa/NamedEntity.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.customers.support.jpa; - -import javax.persistence.Column; -import javax.persistence.MappedSuperclass; - - -/** - * Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. Used as a base class for objects - * needing these properties. - * - * @author Ken Krebs - * @author Juergen Hoeller - */ -@MappedSuperclass -public class NamedEntity extends BaseEntity { - - @Column(name = "name") - private String name; - - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return this.name; - } - - @Override - public String toString() { - return this.getName(); - } - -} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/owner/OwnerResource.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/OwnerResource.java similarity index 61% rename from spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/owner/OwnerResource.java rename to spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/OwnerResource.java index 652f348dd3c10a8e69b17849298efbe862631345..1da650a4a90692a5dcb1b47cf6650ba259ceb455 100644 --- a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/boundary/web/owner/OwnerResource.java +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/OwnerResource.java @@ -13,43 +13,40 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.customers.boundary.web.owner; +package org.springframework.samples.petclinic.customers.web; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.samples.petclinic.customers.application.OwnerService; -import org.springframework.samples.petclinic.customers.domain.model.owner.Owner; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.*; +import lombok.RequiredArgsConstructor; + +import java.util.List; import javax.validation.Valid; -import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.samples.petclinic.customers.model.Owner; +import org.springframework.samples.petclinic.customers.model.OwnerRepository; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; /** * @author Juergen Hoeller * @author Ken Krebs * @author Arjen Poutsma * @author Michael Isvy + * @author Maciej Szarlinski */ @RequestMapping("/owners") @RestController -public class OwnerResource { - - private final OwnerService ownerService; +@RequiredArgsConstructor(onConstructor = @__(@Autowired)) +class OwnerResource { - @Autowired - public OwnerResource(OwnerService ownerService) { - this.ownerService = ownerService; - } - - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - private Owner retrieveOwner(int ownerId) { - return this.ownerService.findOwnerById(ownerId); - } + private final OwnerRepository ownerRepository; /** * Create Owner @@ -57,7 +54,7 @@ public class OwnerResource { @PostMapping @ResponseStatus(HttpStatus.CREATED) public void createOwner(@Valid @RequestBody Owner owner) { - this.ownerService.saveOwner(owner); + ownerRepository.save(owner); } /** @@ -65,15 +62,15 @@ public class OwnerResource { */ @GetMapping(value = "/{ownerId}") public Owner findOwner(@PathVariable("ownerId") int ownerId) { - return retrieveOwner(ownerId); + return ownerRepository.findOne(ownerId); } /** * Read List of Owners */ @GetMapping - public Collection<Owner> findAll() { - return ownerService.findAll(); + public List<Owner> findAll() { + return ownerRepository.findAll(); } /** @@ -81,16 +78,13 @@ public class OwnerResource { */ @PutMapping(value = "/{ownerId}") public Owner updateOwner(@PathVariable("ownerId") int ownerId, @Valid @RequestBody Owner ownerRequest) { - Owner ownerModel = retrieveOwner(ownerId); + final Owner ownerModel = ownerRepository.findOne(ownerId); // This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. ownerModel.setFirstName(ownerRequest.getFirstName()); ownerModel.setLastName(ownerRequest.getLastName()); ownerModel.setCity(ownerRequest.getCity()); ownerModel.setAddress(ownerRequest.getAddress()); ownerModel.setTelephone(ownerRequest.getTelephone()); - this.ownerService.saveOwner(ownerModel); - return ownerModel; + return ownerRepository.save(ownerModel); } - - } diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetDetails.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetDetails.java new file mode 100644 index 0000000000000000000000000000000000000000..f04d1eb124a0cfbd597d723d2fdb74167c22a45c --- /dev/null +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetDetails.java @@ -0,0 +1,35 @@ +package org.springframework.samples.petclinic.customers.web; + +import lombok.Data; + +import java.util.Date; + +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.samples.petclinic.customers.model.Pet; +import org.springframework.samples.petclinic.customers.model.PetType; + +/** + * @author mszarlinski@bravurasolutions.com on 2016-12-05. + */ +@Data +class PetDetails { + + private long id; + + private String name; + + private String owner; + + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date birthDate; + + private PetType type; + + PetDetails(Pet pet) { + this.id = pet.getId(); + this.name = pet.getName(); + this.owner = pet.getOwner().getFirstName() + " " + pet.getOwner().getLastName(); + this.birthDate = pet.getBirthDate(); + this.type = pet.getType(); + } +} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetRequest.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..4e15ccdf35957eb3e63d961653661fe4d50f441e --- /dev/null +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetRequest.java @@ -0,0 +1,25 @@ +package org.springframework.samples.petclinic.customers.web; + +import lombok.Data; + +import java.util.Date; + +import javax.validation.constraints.Size; + +import com.fasterxml.jackson.annotation.JsonFormat; + +/** + * @author mszarlinski@bravurasolutions.com on 2016-12-05. + */ +@Data +class PetRequest { + private int id; + + @JsonFormat(pattern = "yyyy-MM-dd") + private Date birthDate; + + @Size(min = 1) + private String name; + + private int typeId; +} diff --git a/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java new file mode 100644 index 0000000000000000000000000000000000000000..aa007cbeb974a4ff41122cfb86cb3f01b8949ead --- /dev/null +++ b/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java @@ -0,0 +1,91 @@ +/* + * Copyright 2002-2013 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 + * + * http://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.customers.web; + +import lombok.RequiredArgsConstructor; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.samples.petclinic.customers.model.Owner; +import org.springframework.samples.petclinic.customers.model.OwnerRepository; +import org.springframework.samples.petclinic.customers.model.Pet; +import org.springframework.samples.petclinic.customers.model.PetRepository; +import org.springframework.samples.petclinic.customers.model.PetType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + * @author Maciej Szarlinski + */ +@RestController +@RequiredArgsConstructor(onConstructor = @__(@Autowired)) +class PetResource { + + private final PetRepository petRepository; + + private final OwnerRepository ownerRepository; + + @GetMapping("/petTypes") + public List<PetType> getPetTypes() { + return petRepository.findPetTypes(); + } + + @PostMapping("/owners/{ownerId}/pets") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void processCreationForm( + @RequestBody PetRequest petRequest, + @PathVariable("ownerId") int ownerId) { + + final Pet pet = new Pet(); + final Owner owner = ownerRepository.findOne(ownerId); + owner.addPet(pet); + + save(pet, petRequest); + } + + @PutMapping("/owners/*/pets/{petId}") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void processUpdateForm(@RequestBody PetRequest petRequest) { + save(petRepository.findOne(petRequest.getId()), petRequest); + } + + private void save(final Pet pet, final PetRequest petRequest) { + + pet.setName(petRequest.getName()); + pet.setBirthDate(petRequest.getBirthDate()); + + petRepository.findPetTypeById(petRequest.getTypeId()) + .ifPresent(pet::setType); + + petRepository.save(pet); + } + + @GetMapping("owners/*/pets/{petId}") + public PetDetails findPet(@PathVariable("petId") int petId) { + return new PetDetails(petRepository.findOne(petId)); + } + +} diff --git a/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/application/CustomersServicesTest.java b/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/application/CustomersServicesTest.java deleted file mode 100644 index 0fa93345959aba24c6aaf0790b9f2520fbb9522f..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/application/CustomersServicesTest.java +++ /dev/null @@ -1,140 +0,0 @@ -package org.springframework.samples.petclinic.customers.application; - -import com.google.common.collect.Iterables; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.samples.petclinic.customers.CustomersServiceApplication; -import org.springframework.samples.petclinic.customers.domain.model.owner.Owner; -import org.springframework.samples.petclinic.customers.domain.model.pet.Pet; -import org.springframework.samples.petclinic.customers.domain.model.pet.PetType; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Collection; -import java.util.Date; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = CustomersServiceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE) -@ActiveProfiles("test") -public class CustomersServicesTest { - - @Autowired - private PetService petService; - - @Autowired - private OwnerService ownerService; - - @Test - public void shouldFindPetWithCorrectId() { - Pet pet7 = petService.findPetById(7); - assertThat(pet7.getName()).startsWith("Samantha"); - assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean"); - } - - @Test - public void shouldFindAllPetTypes() { - Collection<PetType> petTypes = petService.findPetTypes(); - - PetType petType1 = Iterables.find(petTypes, type -> type.getId().equals(1)); - assertThat(petType1.getName()).isEqualTo("cat"); - PetType petType4 = Iterables.find(petTypes, type -> type.getId().equals(4)); - assertThat(petType4.getName()).isEqualTo("snake"); - } - - @Test - public void shouldFindPetTypeById() { - assertThat(petService.findPetTypeById(1)).hasValueSatisfying(t -> t.getName().equals("cat")); - assertThat(petService.findPetTypeById(4)).hasValueSatisfying(t -> t.getName().equals("snake")); - } - - @Test - @Transactional - public void shouldInsertPetIntoDatabaseAndGenerateId() { - Owner owner6 = ownerService.findOwnerById(6); - int found = owner6.getPets().size(); - - Pet pet = new Pet(); - pet.setName("bowser"); - Collection<PetType> types = petService.findPetTypes(); - pet.setType(Iterables.find(types, type -> type.getId().equals(2))); - pet.setBirthDate(new Date()); - owner6.addPet(pet); - assertThat(owner6.getPets().size()).isEqualTo(found + 1); - - petService.savePet(pet); - ownerService.saveOwner(owner6); - - owner6 = ownerService.findOwnerById(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 = petService.findPetById(7); - String oldName = pet7.getName(); - - String newName = oldName + "X"; - pet7.setName(newName); - petService.savePet(pet7); - - pet7 = petService.findPetById(7); - assertThat(pet7.getName()).isEqualTo(newName); - } - - - @Test - public void shouldFindSingleOwnerWithPet() { - Owner owner = ownerService.findOwnerById(1); - assertThat(owner.getLastName()).startsWith("Franklin"); - assertThat(owner.getPets().size()).isEqualTo(1); - } - - @Test - public void shouldReturnAllOwnersInCaseLastNameIsEmpty() { - Collection<Owner> owners = ownerService.findAll(); - assertThat(owners).extracting("lastName").contains("Davis", "Franklin"); - } - - @Test - @Transactional - public void shouldInsertOwner() { - Collection<Owner> owners = ownerService.findAll(); - 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"); - ownerService.saveOwner(owner); - assertThat(owner.getId().longValue()).isNotEqualTo(0); - - owners = ownerService.findAll(); - assertThat(owners.size()).isEqualTo(found + 1); - } - - @Test - @Transactional - public void shouldUpdateOwner() { - Owner owner = ownerService.findOwnerById(1); - String oldLastName = owner.getLastName(); - String newLastName = oldLastName + "X"; - - owner.setLastName(newLastName); - ownerService.saveOwner(owner); - - // retrieving new name from database - owner = ownerService.findOwnerById(1); - assertThat(owner.getLastName()).isEqualTo(newLastName); - } - -} diff --git a/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/infrastructure/ValidatorTests.java b/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/infrastructure/ValidatorTests.java deleted file mode 100644 index 36d29774bdb24f21ab9e47f444086015ec0158b6..0000000000000000000000000000000000000000 --- a/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/infrastructure/ValidatorTests.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.springframework.samples.petclinic.customers.infrastructure; - -import org.junit.Test; -import org.springframework.context.i18n.LocaleContextHolder; -import org.springframework.samples.petclinic.customers.domain.model.Person; -import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; - -import javax.validation.ConstraintViolation; -import javax.validation.Validator; -import java.util.Locale; -import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Michael Isvy - * Simple test to make sure that Bean Validation is working - * (useful when upgrading to a new version of Hibernate Validator/ Bean Validation) - */ -public class ValidatorTests { - - private Validator createValidator() { - LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); - localValidatorFactoryBean.afterPropertiesSet(); - return localValidatorFactoryBean; - } - - @Test - public void shouldNotValidateWhenFirstNameEmpty() { - - LocaleContextHolder.setLocale(Locale.ENGLISH); - Person person = new Person(); - person.setFirstName(""); - person.setLastName("smith"); - - Validator validator = createValidator(); - Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person); - - assertThat(constraintViolations.size()).isEqualTo(1); - ConstraintViolation<Person> violation = constraintViolations.iterator().next(); - assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName"); - assertThat(violation.getMessage()).isEqualTo("may not be empty"); - } - -} diff --git a/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetResourceTest.java b/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/web/PetResourceTest.java similarity index 69% rename from spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetResourceTest.java rename to spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/web/PetResourceTest.java index 0151d114bc066869bd77364b1b2dc9b3b2e20baa..7f152a673abca990cef3ab0d2bd991ee688ccd67 100644 --- a/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/boundary/web/pet/PetResourceTest.java +++ b/spring-petclinic-customers-service/src/test/java/org/springframework/samples/petclinic/customers/web/PetResourceTest.java @@ -1,4 +1,10 @@ -package org.springframework.samples.petclinic.customers.boundary.web.pet; +package org.springframework.samples.petclinic.customers.web; + +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Test; import org.junit.runner.RunWith; @@ -6,19 +12,15 @@ 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.http.MediaType; -import org.springframework.samples.petclinic.customers.application.OwnerService; -import org.springframework.samples.petclinic.customers.application.PetService; -import org.springframework.samples.petclinic.customers.domain.model.owner.Owner; -import org.springframework.samples.petclinic.customers.domain.model.pet.Pet; -import org.springframework.samples.petclinic.customers.domain.model.pet.PetType; +import org.springframework.samples.petclinic.customers.model.Owner; +import org.springframework.samples.petclinic.customers.model.OwnerRepository; +import org.springframework.samples.petclinic.customers.model.Pet; +import org.springframework.samples.petclinic.customers.model.PetRepository; +import org.springframework.samples.petclinic.customers.model.PetType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; -import static org.mockito.BDDMockito.given; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - /** * @author Maciej Szarlinski */ @@ -31,17 +33,17 @@ public class PetResourceTest { private MockMvc mvc; @MockBean - PetService petService; + PetRepository petRepository; @MockBean - OwnerService ownerService; + OwnerRepository ownerRepository; @Test public void shouldGetAPetInJSonFormat() throws Exception { Pet pet = setupPet(); - given(petService.findPetById(2)).willReturn(pet); + given(petRepository.findOne(2)).willReturn(pet); mvc.perform(get("/owners/2/pets/2.json").accept(MediaType.APPLICATION_JSON)) diff --git a/spring-petclinic-discovery-server/src/main/java/org/springframework/samples/petclinic/discovery/DiscoveryServerApplication.java b/spring-petclinic-discovery-server/src/main/java/org/springframework/samples/petclinic/discovery/DiscoveryServerApplication.java index 53772be82f7ab54a24d0933af49e1d64c1f8b036..a84d83d6367bf10ad55181bca8bd99169c8b073d 100644 --- a/spring-petclinic-discovery-server/src/main/java/org/springframework/samples/petclinic/discovery/DiscoveryServerApplication.java +++ b/spring-petclinic-discovery-server/src/main/java/org/springframework/samples/petclinic/discovery/DiscoveryServerApplication.java @@ -4,6 +4,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; +/** + * @author Maciej Szarlinski + */ @EnableEurekaServer @SpringBootApplication public class DiscoveryServerApplication { diff --git a/spring-petclinic-vets-service/pom.xml b/spring-petclinic-vets-service/pom.xml index b8796ace30b2fdc222abab6c060cb852fc9932a6..9a753f371298944b228149b34bf9806101164d60 100644 --- a/spring-petclinic-vets-service/pom.xml +++ b/spring-petclinic-vets-service/pom.xml @@ -71,6 +71,11 @@ <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + </dependency> + <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/VetsServiceApplication.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/VetsServiceApplication.java index c460cf3e7fb2c57e8303d6581a7ffb04f359583a..86cd4cc087ae166a47f11be02984dc9ae93f7705 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/VetsServiceApplication.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/VetsServiceApplication.java @@ -4,8 +4,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.samples.petclinic.vets.infrastructure.config.VetsProperties; +import org.springframework.samples.petclinic.vets.system.VetsProperties; +/** + * @author Maciej Szarlinski + */ @EnableDiscoveryClient @SpringBootApplication @EnableConfigurationProperties(VetsProperties.class) diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/application/VetService.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/application/VetService.java deleted file mode 100644 index 4dab7955541e0b32f8d8343ef064bd902c112ae9..0000000000000000000000000000000000000000 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/application/VetService.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.springframework.samples.petclinic.vets.application; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.vets.domain.model.vet.Vet; -import org.springframework.samples.petclinic.vets.domain.model.vet.VetRepository; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import javax.cache.annotation.CacheResult; -import java.util.Collection; - -/** - * @author Maciej Szarlinski - */ -@Service -public class VetService { - - private final VetRepository vetRepository; - - @Autowired - public VetService(VetRepository vetRepository) { - this.vetRepository = vetRepository; - } - - @Transactional(readOnly = true) - @CacheResult(cacheName = "vets") - public Collection<Vet> findVets() throws DataAccessException { - return vetRepository.findAll(); - } -} diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/shared/Person.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/shared/Person.java deleted file mode 100644 index f823162e6fc4fdcf4562c98d3b10dd0537ebb09d..0000000000000000000000000000000000000000 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/shared/Person.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.vets.domain.model.shared; - -import org.hibernate.validator.constraints.NotEmpty; -import org.springframework.samples.petclinic.vets.support.jpa.BaseEntity; - -import javax.persistence.Column; -import javax.persistence.MappedSuperclass; - -/** - * Simple JavaBean domain object representing an person. - * - * @author Ken Krebs - */ -@MappedSuperclass -public class Person extends BaseEntity { - - @Column(name = "first_name") - @NotEmpty - protected String firstName; - - @Column(name = "last_name") - @NotEmpty - protected String lastName; - - public String getFirstName() { - return this.firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return this.lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - -} diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Specialty.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Specialty.java deleted file mode 100644 index ea2dd5901d67d2b0e8c1c3d0b1d601268d70617f..0000000000000000000000000000000000000000 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Specialty.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.vets.domain.model.vet; - -import org.springframework.samples.petclinic.vets.support.jpa.NamedEntity; - -import javax.persistence.Entity; -import javax.persistence.Table; - -/** - * Models a {@link Vet Vet's} specialty (for example, dentistry). - * - * @author Juergen Hoeller - */ -@Entity -@Table(name = "specialties") -public class Specialty extends NamedEntity { - -} diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Vets.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Vets.java deleted file mode 100644 index 69016f00014a5776452ff8e71ec1bff6d8c33957..0000000000000000000000000000000000000000 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Vets.java +++ /dev/null @@ -1,43 +0,0 @@ -/* -/* - * Copyright 2002-2013 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 - * - * http://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.vets.domain.model.vet; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import java.util.ArrayList; -import java.util.List; - -/** - * Simple domain object representing a list of veterinarians. Mostly here to be used for the 'vets' {@link - * org.springframework.web.servlet.view.xml.MarshallingView}. - * - * @author Arjen Poutsma - */ -@XmlRootElement -public class Vets { - - private List<Vet> vets; - - @XmlElement - public List<Vet> getVetList() { - if (vets == null) { - vets = new ArrayList<>(); - } - return vets; - } - -} diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/support/jpa/BaseEntity.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Specialty.java similarity index 63% rename from spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/support/jpa/BaseEntity.java rename to spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Specialty.java index dfe71e81ec8221783afa74448303252146fe1318..f310e81a993dfc972bdbc81be092eef50b13065c 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/support/jpa/BaseEntity.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Specialty.java @@ -13,36 +13,43 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.vets.support.jpa; +package org.springframework.samples.petclinic.vets.model; +import javax.persistence.Column; +import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.MappedSuperclass; +import javax.persistence.Table; /** - * Simple JavaBean domain object with an id property. Used as a base class for objects needing this property. + * Models a {@link Vet Vet's} specialty (for example, dentistry). * - * @author Ken Krebs * @author Juergen Hoeller */ -@MappedSuperclass -public class BaseEntity { +@Entity +@Table(name = "specialties") +public class Specialty { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - protected Integer id; + private Integer id; - - public void setId(Integer id) { - this.id = id; - } + @Column(name = "name") + private String name; public Integer getId() { return id; } - public boolean isNew() { - return (this.id == null); + public void setId(final Integer id) { + this.id = id; } + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } } diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Vet.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Vet.java similarity index 57% rename from spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Vet.java rename to spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Vet.java index 9d473cf96e8fdfe0449a14ca00c94faa6c5ada87..830ab06160706b7df9d69f9cd8271fe3a6d59d2e 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/Vet.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/Vet.java @@ -13,15 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.vets.domain.model.vet; +package org.springframework.samples.petclinic.vets.model; -import org.springframework.beans.support.MutableSortDefinition; -import org.springframework.beans.support.PropertyComparator; -import org.springframework.samples.petclinic.vets.domain.model.shared.Person; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.Table; import javax.xml.bind.annotation.XmlElement; -import java.util.*; + +import org.hibernate.validator.constraints.NotEmpty; +import org.springframework.beans.support.MutableSortDefinition; +import org.springframework.beans.support.PropertyComparator; /** * Simple JavaBean domain object representing a veterinarian. @@ -30,19 +44,51 @@ import java.util.*; * @author Juergen Hoeller * @author Sam Brannen * @author Arjen Poutsma + * @author Maciej Szarlinski */ @Entity @Table(name = "vets") -public class Vet extends Person { +public class Vet { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "first_name") + @NotEmpty + private String firstName; + + @Column(name = "last_name") + @NotEmpty + private String lastName; @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), - inverseJoinColumns = @JoinColumn(name = "specialty_id")) + inverseJoinColumns = @JoinColumn(name = "specialty_id")) private Set<Specialty> specialties; + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getFirstName() { + return this.firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return this.lastName; + } - protected void setSpecialtiesInternal(Set<Specialty> specialties) { - this.specialties = specialties; + public void setLastName(String lastName) { + this.lastName = lastName; } protected Set<Specialty> getSpecialtiesInternal() { diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/VetRepository.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/VetRepository.java similarity index 68% rename from spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/VetRepository.java rename to spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/VetRepository.java index 56e8ed659caf2a44d78ebad2dd4615a024dd8466..01c35d0b6e38fe08e474d4d33b925d45f80fc523 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/domain/model/vet/VetRepository.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/model/VetRepository.java @@ -13,12 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.vets.domain.model.vet; +package org.springframework.samples.petclinic.vets.model; -import org.springframework.dao.DataAccessException; -import org.springframework.data.repository.Repository; - -import java.util.Collection; +import org.springframework.data.jpa.repository.JpaRepository; /** * Repository class for <code>Vet</code> domain objects All method names are compliant with Spring Data naming @@ -28,15 +25,7 @@ import java.util.Collection; * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Maciej Szarlinski */ -public interface VetRepository extends Repository<Vet, Integer> { - - /** - * Retrieve all <code>Vet</code>s from the data store. - * - * @return a <code>Collection</code> of <code>Vet</code>s - */ - Collection<Vet> findAll() throws DataAccessException; - - +public interface VetRepository extends JpaRepository<Vet, Integer> { } diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/support/jpa/NamedEntity.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/support/jpa/NamedEntity.java deleted file mode 100644 index 7733f61f261d327163d723c5ed366be1646a9a08..0000000000000000000000000000000000000000 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/support/jpa/NamedEntity.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.vets.support.jpa; - -import javax.persistence.Column; -import javax.persistence.MappedSuperclass; - - -/** - * Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. Used as a base class for objects - * needing these properties. - * - * @author Ken Krebs - * @author Juergen Hoeller - */ -@MappedSuperclass -public class NamedEntity extends BaseEntity { - - @Column(name = "name") - private String name; - - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return this.name; - } - - @Override - public String toString() { - return this.getName(); - } - -} diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/infrastructure/config/CacheConfig.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/CacheConfig.java similarity index 94% rename from spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/infrastructure/config/CacheConfig.java rename to spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/CacheConfig.java index e17b912dfc6fcfc5003c779e4ff279fc0a28dda7..3e88b4fbfd47bb032df2e4c3b900124e470b527c 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/infrastructure/config/CacheConfig.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/CacheConfig.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.vets.infrastructure.config; +package org.springframework.samples.petclinic.vets.system; import org.ehcache.config.CacheConfiguration; import org.ehcache.config.builders.CacheConfigurationBuilder; @@ -17,6 +17,7 @@ import java.util.concurrent.TimeUnit; /** * Cache could be disable in unit test. + * @author Maciej Szarlinski */ @Configuration @EnableCaching diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/infrastructure/config/VetsProperties.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/VetsProperties.java similarity index 83% rename from spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/infrastructure/config/VetsProperties.java rename to spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/VetsProperties.java index 33df8f8f92ee448c49ac1e84df5fabc6438d8fe9..1169114a8336e0d4ee1f6e48dfbcbda9c95eb279 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/infrastructure/config/VetsProperties.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/system/VetsProperties.java @@ -1,4 +1,4 @@ -package org.springframework.samples.petclinic.vets.infrastructure.config; +package org.springframework.samples.petclinic.vets.system; import lombok.Data; diff --git a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/boundary/VetResource.java b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/VetResource.java similarity index 63% rename from spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/boundary/VetResource.java rename to spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/VetResource.java index c05c7d0f695a3cd4f69c123507cf7169a9b45cd3..52540d4cb32f1eb8f548d48d0c1652f8080f253c 100644 --- a/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/boundary/VetResource.java +++ b/spring-petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/VetResource.java @@ -13,36 +13,34 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.vets.web.boundary; +package org.springframework.samples.petclinic.vets.web; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.vets.application.VetService; -import org.springframework.samples.petclinic.vets.domain.model.vet.Vet; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +import org.springframework.samples.petclinic.vets.model.Vet; +import org.springframework.samples.petclinic.vets.model.VetRepository; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Collection; - /** * @author Juergen Hoeller * @author Mark Fisher * @author Ken Krebs * @author Arjen Poutsma + * @author Maciej Szarlinski */ @RequestMapping("/vets") @RestController -public class VetResource { +@RequiredArgsConstructor +class VetResource { - private final VetService vetService; - - @Autowired - public VetResource(VetService vetService) { - this.vetService = vetService; - } + private final VetRepository vetRepository; @GetMapping - public Collection<Vet> showResourcesVetList() { - return vetService.findVets(); + public List<Vet> showResourcesVetList() { + return vetRepository.findAll(); } } diff --git a/spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/application/VetServiceTest.java b/spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/application/VetServiceTest.java deleted file mode 100644 index ce0330d83fa096ad0b8d8d5869409dabb53b629f..0000000000000000000000000000000000000000 --- a/spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/application/VetServiceTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.springframework.samples.petclinic.vets.application; - -import com.google.common.collect.Iterables; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.samples.petclinic.vets.VetsServiceApplication; -import org.springframework.samples.petclinic.vets.domain.model.vet.Vet; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; - -import java.util.Collection; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = VetsServiceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE) -@ActiveProfiles("test") -public class VetServiceTest { - - @Autowired - private VetService vetService; - - @Test - public void shouldFindVets() { - Collection<Vet> vets = vetService.findVets(); - - Vet vet = Iterables.find(vets, v -> v.getId().equals(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"); - } -} diff --git a/spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/web/boundary/VetResourceTest.java b/spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/web/VetResourceTest.java similarity index 81% rename from spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/web/boundary/VetResourceTest.java rename to spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/web/VetResourceTest.java index 60172bf097ff6ca483a8cb0637908a835fbb8f14..b4c555f17f57129f6426786addd7fb61b0c92d4e 100644 --- a/spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/web/boundary/VetResourceTest.java +++ b/spring-petclinic-vets-service/src/test/java/org/springframework/samples/petclinic/vets/web/VetResourceTest.java @@ -1,4 +1,10 @@ -package org.springframework.samples.petclinic.vets.web.boundary; +package org.springframework.samples.petclinic.vets.web; + +import static java.util.Arrays.asList; +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Test; import org.junit.runner.RunWith; @@ -6,18 +12,12 @@ 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.http.MediaType; -import org.springframework.samples.petclinic.vets.application.VetService; -import org.springframework.samples.petclinic.vets.domain.model.vet.Vet; +import org.springframework.samples.petclinic.vets.model.Vet; +import org.springframework.samples.petclinic.vets.model.VetRepository; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; -import static java.util.Arrays.asList; -import static org.mockito.BDDMockito.given; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - /** * @author Maciej Szarlinski */ @@ -30,7 +30,7 @@ public class VetResourceTest { private MockMvc mvc; @MockBean - private VetService vetService; + private VetRepository vetRepository; @Test public void shouldGetAListOfVetsInJSonFormat() throws Exception { @@ -38,7 +38,7 @@ public class VetResourceTest { Vet vet = new Vet(); vet.setId(1); - given(vetService.findVets()).willReturn(asList(vet)); + given(vetRepository.findAll()).willReturn(asList(vet)); mvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) diff --git a/spring-petclinic-visits-service/pom.xml b/spring-petclinic-visits-service/pom.xml index 3cb5fd72fb388082e5dd48e056cf4147aa8e005f..6e76176acbd2d42060447b913a63e0f8dee855df 100644 --- a/spring-petclinic-visits-service/pom.xml +++ b/spring-petclinic-visits-service/pom.xml @@ -54,6 +54,10 @@ </dependency> <!-- Third parties --> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/VisitsServiceApplication.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/VisitsServiceApplication.java index 5a342a8474c0f00daf25dbef6d58934539c18ec6..1af5ef436e76e043210f7e227295d73022829291 100644 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/VisitsServiceApplication.java +++ b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/VisitsServiceApplication.java @@ -4,11 +4,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +/** + * @author Maciej Szarlinski + */ @EnableDiscoveryClient @SpringBootApplication public class VisitsServiceApplication { - public static void main(String[] args) { - SpringApplication.run(VisitsServiceApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(VisitsServiceApplication.class, args); + } } diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/application/VisitService.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/application/VisitService.java deleted file mode 100644 index cdb066236d5f715df771ce44524e340cd1cb792c..0000000000000000000000000000000000000000 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/application/VisitService.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.springframework.samples.petclinic.visits.application; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.visits.domain.model.visit.Visit; -import org.springframework.samples.petclinic.visits.domain.model.visit.VisitRepository; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.util.List; - -/** - * @author Maciej Szarlinski - */ -@Service -public class VisitService { - - private static final Logger LOG = LoggerFactory.getLogger(VisitService.class); - - private final VisitRepository visitRepository; - - @Autowired - public VisitService(VisitRepository visitRepository) { - this.visitRepository = visitRepository; - } - - @Transactional - public void saveVisit(Visit visit) throws DataAccessException { - LOG.info("Saving visit {}", visit); - visitRepository.save(visit); - } - - @Transactional(readOnly = true) - public List<Visit> findVisitsByPetId(final int petId) { - return visitRepository.findByPetId(petId); - } - -} diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/shared/Person.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/shared/Person.java deleted file mode 100644 index a59ff78ae0de9fe1f8ec74cdad7216ab1648c307..0000000000000000000000000000000000000000 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/shared/Person.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.visits.domain.shared; - -import org.hibernate.validator.constraints.NotEmpty; -import org.springframework.samples.petclinic.visits.support.jpa.BaseEntity; - -import javax.persistence.Column; -import javax.persistence.MappedSuperclass; - -/** - * Simple JavaBean domain object representing an person. - * - * @author Ken Krebs - */ -@MappedSuperclass -public class Person extends BaseEntity { - - @Column(name = "first_name") - @NotEmpty - protected String firstName; - - @Column(name = "last_name") - @NotEmpty - protected String lastName; - - public String getFirstName() { - return this.firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return this.lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - -} diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/model/visit/Visit.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/Visit.java similarity index 59% rename from spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/model/visit/Visit.java rename to spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/Visit.java index d99bbac259a96be626e42dbab7aa052ba8011441..7aaa63a9f933171f0105a017251b8ef00fa41085 100644 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/model/visit/Visit.java +++ b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/Visit.java @@ -13,81 +13,60 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.visits.domain.model.visit; +package org.springframework.samples.petclinic.visits.model; -import com.fasterxml.jackson.annotation.JsonFormat; -import org.springframework.samples.petclinic.visits.support.jpa.BaseEntity; +import java.util.Date; -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; import javax.validation.constraints.Size; -import java.util.Date; + +import com.fasterxml.jackson.annotation.JsonFormat; /** * Simple JavaBean domain object representing a visit. * * @author Ken Krebs + * @author Maciej Szarlinski */ @Entity @Table(name = "visits") -public class Visit extends BaseEntity { +public class Visit { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - /** - * Holds value of property date. - */ @Column(name = "visit_date") @Temporal(TemporalType.TIMESTAMP) @JsonFormat(pattern = "yyyy-MM-dd") private Date date = new Date(); - /** - * Holds value of property description. - */ @Size(max = 8192) @Column(name = "description") private String description; - /** - * Holds id of property pet. - */ @Column(name = "pet_id") private int petId; - /** - * Getter for property date. - * - * @return Value of property date. - */ - public Date getDate() { - return date; + public Integer getId() { + return id; } - /** - * Setter for property date. - * - * @param date New value of property date. - */ - public void setDate(Date date) { - this.date = date; + public Date getDate() { + return date; } - /** - * Getter for property description. - * - * @return Value of property description. - */ public String getDescription() { return description; } - /** - * Setter for property description. - * - * @param description New value of property description. - */ - public void setDescription(String description) { - this.description = description; - } - public int getPetId() { return petId; } diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/model/visit/VisitRepository.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/VisitRepository.java similarity index 55% rename from spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/model/visit/VisitRepository.java rename to spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/VisitRepository.java index a90892a1da9aff9651af99641e36a18a2f48e6d9..f3ed33d7c7c27c843aad784558ed90fdfc7e653e 100644 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/domain/model/visit/VisitRepository.java +++ b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/model/VisitRepository.java @@ -13,32 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.visits.domain.model.visit; - -import org.springframework.dao.DataAccessException; -import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.visits.support.jpa.BaseEntity; +package org.springframework.samples.petclinic.visits.model; import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; + /** - * Repository class for <code>Visit</code> domain objects All method names are compliant with Spring Data naming - * conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation + * Repository class for <code>Visit</code> domain objects All method names are compliant with Spring Data naming conventions so this interface can easily be extended for Spring + * Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation * * @author Ken Krebs * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy + * @author Maciej Szarlinski */ -public interface VisitRepository extends Repository<Visit, Integer> { - - /** - * Save a <code>Visit</code> to the data store, either inserting or updating it. - * - * @param visit the <code>Visit</code> to save - * @see BaseEntity#isNew - */ - void save(Visit visit) throws DataAccessException; +public interface VisitRepository extends JpaRepository<Visit, Integer> { List<Visit> findByPetId(int petId); diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/support/jpa/NamedEntity.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/support/jpa/NamedEntity.java deleted file mode 100644 index 33e980c458a658004a9897de9a694707d537899c..0000000000000000000000000000000000000000 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/support/jpa/NamedEntity.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2002-2013 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 - * - * http://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.visits.support.jpa; - -import javax.persistence.Column; -import javax.persistence.MappedSuperclass; - - -/** - * Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. Used as a base class for objects - * needing these properties. - * - * @author Ken Krebs - * @author Juergen Hoeller - */ -@MappedSuperclass -public class NamedEntity extends BaseEntity { - - @Column(name = "name") - private String name; - - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return this.name; - } - - @Override - public String toString() { - return this.getName(); - } - -} diff --git a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/boundary/web/VisitResource.java b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/web/VisitResource.java similarity index 61% rename from spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/boundary/web/VisitResource.java rename to spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/web/VisitResource.java index b0b9a7d1e832c70559772a45399a002fc6051272..28b8850815be3008e18ebefb0163b78ece0245da 100644 --- a/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/boundary/web/VisitResource.java +++ b/spring-petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/web/VisitResource.java @@ -13,32 +13,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.visits.boundary.web; +package org.springframework.samples.petclinic.visits.web; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.samples.petclinic.visits.application.VisitService; -import org.springframework.samples.petclinic.visits.domain.model.visit.Visit; -import org.springframework.web.bind.annotation.*; +import lombok.RequiredArgsConstructor; -import javax.validation.Valid; import java.util.List; +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.samples.petclinic.visits.model.Visit; +import org.springframework.samples.petclinic.visits.model.VisitRepository; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + /** * @author Juergen Hoeller * @author Ken Krebs * @author Arjen Poutsma * @author Michael Isvy + * @author Maciej Szarlinski */ @RestController +@RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class VisitResource { - private final VisitService visitService; - - @Autowired - public VisitResource(VisitService visitService) { - this.visitService = visitService; - } + private final VisitRepository visitRepository; @PostMapping("owners/*/pets/{petId}/visits") @ResponseStatus(HttpStatus.NO_CONTENT) @@ -47,11 +52,11 @@ public class VisitResource { @PathVariable("petId") int petId) { visit.setPetId(petId); - visitService.saveVisit(visit); + visitRepository.save(visit); } @GetMapping("owners/*/pets/{petId}/visits") public List<Visit> visits(@PathVariable("petId") int petId) { - return visitService.findVisitsByPetId(petId); + return visitRepository.findByPetId(petId); } } diff --git a/spring-petclinic-visits-service/src/test/java/org/springframework/samples/petclinic/visits/application/VisitServiceTest.java b/spring-petclinic-visits-service/src/test/java/org/springframework/samples/petclinic/visits/application/VisitServiceTest.java deleted file mode 100644 index 76c650145d23cb31d7a8212b6a719373fe2196a4..0000000000000000000000000000000000000000 --- a/spring-petclinic-visits-service/src/test/java/org/springframework/samples/petclinic/visits/application/VisitServiceTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.springframework.samples.petclinic.visits.application; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.samples.petclinic.visits.VisitsServiceApplication; -import org.springframework.samples.petclinic.visits.domain.model.visit.Visit; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.transaction.annotation.Transactional; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = VisitsServiceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE) -@ActiveProfiles("test") -public class VisitServiceTest { - - @Autowired - protected VisitService visitService; - - @Test - @Transactional - public void shouldAddNewVisitForPet() { - // given - int petId = 7; - Visit visit = new Visit(); - visit.setPetId(petId); - visit.setDescription("test"); - //when - visitService.saveVisit(visit); - // then - assertThat(visit.getPetId()).isEqualTo(petId); - } - -}