diff --git a/pom.xml b/pom.xml index d35db9f34122b7aa268511d3a9f36983dbd927de..d5d0428e88d54beffd08d0b2629b4c0e6917ccdc 100644 --- a/pom.xml +++ b/pom.xml @@ -139,6 +139,10 @@ <artifactId>jackson-databind-nullable</artifactId> <version>${jackson-databind-nullable.version}</version> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-jsr310</artifactId> + </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> @@ -280,7 +284,7 @@ <!-- Activating JAVA8 features --> <configOptions> <performBeanValidation>true</performBeanValidation> - <dateLibrary>legacy</dateLibrary> + <dateLibrary>java8</dateLibrary> <java8>true</java8> <openApiNullable>false</openApiNullable> <serializationLibrary>jackson</serializationLibrary> diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/model/Pet.java index bee28ffbdc7222ddd41924afef6a86c5dc3581a5..d1f8da9f4a12f160083092b322ac6c877306d2bc 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java @@ -20,6 +20,7 @@ import org.springframework.beans.support.PropertyComparator; import org.springframework.format.annotation.DateTimeFormat; import javax.persistence.*; +import java.time.LocalDate; import java.util.*; @@ -34,10 +35,9 @@ import java.util.*; @Table(name = "pets") public class Pet extends NamedEntity { - @Column(name = "birth_date") - @Temporal(TemporalType.DATE) + @Column(name = "birth_date", columnDefinition = "DATE") @DateTimeFormat(pattern = "yyyy/MM/dd") - private Date birthDate; + private LocalDate birthDate; @ManyToOne @JoinColumn(name = "type_id") @@ -50,11 +50,11 @@ public class Pet extends NamedEntity { @OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER) private Set<Visit> visits; - public Date getBirthDate() { + public LocalDate getBirthDate() { return this.birthDate; } - public void setBirthDate(Date birthDate) { + public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Visit.java b/src/main/java/org/springframework/samples/petclinic/model/Visit.java index e030aae1d22e7e3dc7d9a3b0d9493a3fe9e89a5e..eb82dfbba938d30e06b4011596c89b79a6fd7b11 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Visit.java @@ -15,12 +15,11 @@ */ package org.springframework.samples.petclinic.model; -import com.fasterxml.jackson.annotation.JsonFormat; import org.springframework.format.annotation.DateTimeFormat; import javax.persistence.*; import javax.validation.constraints.NotEmpty; -import java.util.Date; +import java.time.LocalDate; /** * Simple JavaBean domain object representing a visit. @@ -34,11 +33,9 @@ public class Visit extends BaseEntity { /** * Holds value of property date. */ - @Column(name = "visit_date") - @Temporal(TemporalType.TIMESTAMP) + @Column(name = "visit_date", columnDefinition = "DATE") @DateTimeFormat(pattern = "yyyy/MM/dd") - @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy/MM/dd") - private Date date; + private LocalDate date; /** * Holds value of property description. @@ -59,7 +56,7 @@ public class Visit extends BaseEntity { * Creates a new instance of Visit for the current date */ public Visit() { - this.date = new Date(); + this.date = LocalDate.now(); } @@ -68,7 +65,7 @@ public class Visit extends BaseEntity { * * @return Value of property date. */ - public Date getDate() { + public LocalDate getDate() { return this.date; } @@ -77,7 +74,7 @@ public class Visit extends BaseEntity { * * @param date New value of property date. */ - public void setDate(Date date) { + public void setDate(LocalDate date) { this.date = date; } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java index f034e20b9163807abfc4d3b66fdaee8b5458e4f0..62c1cb6226f03d71c35a7ce484834ddcda910cf2 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java @@ -15,12 +15,12 @@ */ package org.springframework.samples.petclinic.repository.jdbc; +import org.springframework.jdbc.core.RowMapper; + import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; -import org.springframework.jdbc.core.RowMapper; - /** * {@link RowMapper} implementation mapping data from a {@link ResultSet} to the corresponding properties * of the {@link JdbcPet} class. @@ -33,7 +33,7 @@ public class JdbcPetRowMapper implements RowMapper<JdbcPet> { pet.setId(rs.getInt("pets_id")); pet.setName(rs.getString("name")); Date birthDate = rs.getDate("birth_date"); - pet.setBirthDate(new Date(birthDate.getTime())); + pet.setBirthDate(new java.sql.Date(birthDate.getTime()).toLocalDate()); pet.setTypeId(rs.getInt("type_id")); pet.setOwnerId(rs.getInt("owner_id")); return pet; diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java index 140b7395cb81a7b54525a30ed8ad63352f108e18..b8e6ea91542a04df87bc68687c4d665fec182f3f 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java @@ -32,14 +32,9 @@ import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Repository; import javax.sql.DataSource; - import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * A simple JDBC-based implementation of the {@link VisitRepository} interface. @@ -57,9 +52,8 @@ import java.util.Map; @Profile("jdbc") public class JdbcVisitRepositoryImpl implements VisitRepository { - private NamedParameterJdbcTemplate namedParameterJdbcTemplate; - protected SimpleJdbcInsert insertVisit; + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; @Autowired public JdbcVisitRepositoryImpl(DataSource dataSource) { @@ -87,97 +81,97 @@ public class JdbcVisitRepositoryImpl implements VisitRepository { Map<String, Object> params = new HashMap<>(); params.put("id", petId); JdbcPet pet = this.namedParameterJdbcTemplate.queryForObject( - "SELECT id as pets_id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id", - params, - new JdbcPetRowMapper()); + "SELECT id as pets_id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id", + params, + new JdbcPetRowMapper()); List<Visit> visits = this.namedParameterJdbcTemplate.query( "SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=:id", params, new JdbcVisitRowMapper()); - for (Visit visit: visits) { + for (Visit visit : visits) { visit.setPet(pet); } return visits; } - - @Override - public Visit findById(int id) throws DataAccessException { - Visit visit; - try { - Map<String, Object> params = new HashMap<>(); - params.put("id", id); - visit = this.namedParameterJdbcTemplate.queryForObject( - "SELECT id as visit_id, visits.pet_id as pets_id, visit_date, description FROM visits WHERE id= :id", - params, - new JdbcVisitRowMapperExt()); - } catch (EmptyResultDataAccessException ex) { - throw new ObjectRetrievalFailureException(Visit.class, id); - } - return visit; - } - - @Override - public Collection<Visit> findAll() throws DataAccessException { - Map<String, Object> params = new HashMap<>(); - return this.namedParameterJdbcTemplate.query( - "SELECT id as visit_id, pets.id as pets_id, visit_date, description FROM visits LEFT JOIN pets ON visits.pet_id = pets.id", - params, new JdbcVisitRowMapperExt()); - } - - @Override - public void save(Visit visit) throws DataAccessException { - if (visit.isNew()) { - Number newKey = this.insertVisit.executeAndReturnKey(createVisitParameterSource(visit)); - visit.setId(newKey.intValue()); - } else { - this.namedParameterJdbcTemplate.update( - "UPDATE visits SET visit_date=:visit_date, description=:description, pet_id=:pet_id WHERE id=:id ", - createVisitParameterSource(visit)); - } - } - - @Override - public void delete(Visit visit) throws DataAccessException { - Map<String, Object> params = new HashMap<>(); - params.put("id", visit.getId()); - this.namedParameterJdbcTemplate.update("DELETE FROM visits WHERE id=:id", params); - } - - protected class JdbcVisitRowMapperExt implements RowMapper<Visit> { - - @Override - public Visit mapRow(ResultSet rs, int rowNum) throws SQLException { - Visit visit = new Visit(); - JdbcPet pet = new JdbcPet(); - PetType petType = new PetType(); - Owner owner = new Owner(); - visit.setId(rs.getInt("visit_id")); - Date visitDate = rs.getDate("visit_date"); - visit.setDate(new Date(visitDate.getTime())); - visit.setDescription(rs.getString("description")); - Map<String, Object> params = new HashMap<>(); - params.put("id", rs.getInt("pets_id")); - pet = JdbcVisitRepositoryImpl.this.namedParameterJdbcTemplate.queryForObject( - "SELECT pets.id as pets_id, name, birth_date, type_id, owner_id FROM pets WHERE pets.id=:id", - params, - new JdbcPetRowMapper()); - params.put("type_id", pet.getTypeId()); - petType = JdbcVisitRepositoryImpl.this.namedParameterJdbcTemplate.queryForObject( - "SELECT id, name FROM types WHERE id= :type_id", - params, - BeanPropertyRowMapper.newInstance(PetType.class)); - pet.setType(petType); - params.put("owner_id", pet.getOwnerId()); - owner = JdbcVisitRepositoryImpl.this.namedParameterJdbcTemplate.queryForObject( - "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :owner_id", - params, - BeanPropertyRowMapper.newInstance(Owner.class)); - pet.setOwner(owner); - visit.setPet(pet); - return visit; - } - } + + @Override + public Visit findById(int id) throws DataAccessException { + Visit visit; + try { + Map<String, Object> params = new HashMap<>(); + params.put("id", id); + visit = this.namedParameterJdbcTemplate.queryForObject( + "SELECT id as visit_id, visits.pet_id as pets_id, visit_date, description FROM visits WHERE id= :id", + params, + new JdbcVisitRowMapperExt()); + } catch (EmptyResultDataAccessException ex) { + throw new ObjectRetrievalFailureException(Visit.class, id); + } + return visit; + } + + @Override + public Collection<Visit> findAll() throws DataAccessException { + Map<String, Object> params = new HashMap<>(); + return this.namedParameterJdbcTemplate.query( + "SELECT id as visit_id, pets.id as pets_id, visit_date, description FROM visits LEFT JOIN pets ON visits.pet_id = pets.id", + params, new JdbcVisitRowMapperExt()); + } + + @Override + public void save(Visit visit) throws DataAccessException { + if (visit.isNew()) { + Number newKey = this.insertVisit.executeAndReturnKey(createVisitParameterSource(visit)); + visit.setId(newKey.intValue()); + } else { + this.namedParameterJdbcTemplate.update( + "UPDATE visits SET visit_date=:visit_date, description=:description, pet_id=:pet_id WHERE id=:id ", + createVisitParameterSource(visit)); + } + } + + @Override + public void delete(Visit visit) throws DataAccessException { + Map<String, Object> params = new HashMap<>(); + params.put("id", visit.getId()); + this.namedParameterJdbcTemplate.update("DELETE FROM visits WHERE id=:id", params); + } + + protected class JdbcVisitRowMapperExt implements RowMapper<Visit> { + + @Override + public Visit mapRow(ResultSet rs, int rowNum) throws SQLException { + Visit visit = new Visit(); + JdbcPet pet = new JdbcPet(); + PetType petType = new PetType(); + Owner owner = new Owner(); + visit.setId(rs.getInt("visit_id")); + Date visitDate = rs.getDate("visit_date"); + visit.setDate(new java.sql.Date(visitDate.getTime()).toLocalDate()); + visit.setDescription(rs.getString("description")); + Map<String, Object> params = new HashMap<>(); + params.put("id", rs.getInt("pets_id")); + pet = JdbcVisitRepositoryImpl.this.namedParameterJdbcTemplate.queryForObject( + "SELECT pets.id as pets_id, name, birth_date, type_id, owner_id FROM pets WHERE pets.id=:id", + params, + new JdbcPetRowMapper()); + params.put("type_id", pet.getTypeId()); + petType = JdbcVisitRepositoryImpl.this.namedParameterJdbcTemplate.queryForObject( + "SELECT id, name FROM types WHERE id= :type_id", + params, + BeanPropertyRowMapper.newInstance(PetType.class)); + pet.setType(petType); + params.put("owner_id", pet.getOwnerId()); + owner = JdbcVisitRepositoryImpl.this.namedParameterJdbcTemplate.queryForObject( + "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :owner_id", + params, + BeanPropertyRowMapper.newInstance(Owner.class)); + pet.setOwner(owner); + visit.setPet(pet); + return visit; + } + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java index 0b172d3ec186b3460302612095b75af41f762797..b412fcd33865a8b87ee9755a5e9077f93d0ea87a 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRowMapper.java @@ -34,7 +34,7 @@ class JdbcVisitRowMapper implements RowMapper<Visit> { Visit visit = new Visit(); visit.setId(rs.getInt("visit_id")); Date visitDate = rs.getDate("visit_date"); - visit.setDate(new Date(visitDate.getTime())); + visit.setDate(new java.sql.Date(visitDate.getTime()).toLocalDate()); visit.setDescription(rs.getString("description")); return visit; } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java index cc59b20e84f78bbeb57f82937ae59b574453fdf5..6b236e5e53a1f8bda55a160ef832ab9fbf4aa2a8 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java @@ -17,6 +17,8 @@ package org.springframework.samples.petclinic.rest; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -39,8 +41,8 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import java.time.LocalDate; import java.util.ArrayList; -import java.util.Date; import java.util.List; import static org.mockito.BDDMockito.given; @@ -124,7 +126,7 @@ public class OwnerRestControllerTests { PetDto pet = new PetDto(); pet.setId(id); pet.setName(name); - pet.setBirthDate(new Date()); + pet.setBirthDate(LocalDate.now()); pet.setType(petType); pet.addVisitsItem(getTestVisitForPet(pet, 1)); return pet; @@ -133,7 +135,7 @@ public class OwnerRestControllerTests { private VisitDto getTestVisitForPet(final PetDto pet, final int id) { VisitDto visit = new VisitDto(); visit.setId(id); - visit.setDate(new Date()); + visit.setDate(LocalDate.now()); visit.setDescription("test" + id); return visit; } @@ -217,6 +219,8 @@ public class OwnerRestControllerTests { OwnerDto newOwnerDto = owners.get(0); newOwnerDto.setId(null); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto); this.mockMvc.perform(post("/api/owners/") .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) @@ -229,6 +233,8 @@ public class OwnerRestControllerTests { OwnerDto newOwnerDto = owners.get(0); newOwnerDto.setId(999); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto); this.mockMvc.perform(post("/api/owners/") .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) @@ -244,6 +250,8 @@ public class OwnerRestControllerTests { newOwnerDto.setId(null); newOwnerDto.setFirstName(null); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto); this.mockMvc.perform(post("/api/owners/") .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) @@ -264,6 +272,8 @@ public class OwnerRestControllerTests { updatedOwnerDto.setCity("Madison"); updatedOwnerDto.setTelephone("6085551023"); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newOwnerAsJSON = mapper.writeValueAsString(updatedOwnerDto); this.mockMvc.perform(put("/api/owners/" + ownerId) .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) diff --git a/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java index be3bddd13f357bcdb128e05c1f4ddb1c7cf79264..5d1de1d9d6f7984b4589b207ac134179c6c7bd3c 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java @@ -18,6 +18,7 @@ package org.springframework.samples.petclinic.rest; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,9 +42,9 @@ import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import java.text.SimpleDateFormat; +import java.time.LocalDate; import java.util.ArrayList; import java.util.Collection; -import java.util.Date; import java.util.List; import static org.mockito.BDDMockito.given; @@ -60,63 +61,60 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes=ApplicationTestConfig.class) +@ContextConfiguration(classes = ApplicationTestConfig.class) @WebAppConfiguration public class PetRestControllerTests { + @MockBean + protected ClinicService clinicService; @Autowired private PetRestController petRestController; - @Autowired private PetMapper petMapper; - - @MockBean - protected ClinicService clinicService; - private MockMvc mockMvc; private List<PetDto> pets; @Before - public void initPets(){ - this.mockMvc = MockMvcBuilders.standaloneSetup(petRestController) - .setControllerAdvice(new ExceptionControllerAdvice()) - .build(); - pets = new ArrayList<>(); - - OwnerDto owner = new OwnerDto(); - owner.setId(1); - owner.setFirstName("Eduardo"); - owner.setLastName("Rodriquez"); - owner.setAddress("2693 Commerce St."); - owner.setCity("McFarland"); - owner.setTelephone("6085558763"); - - PetTypeDto petType = new PetTypeDto(); - petType.setId(2); - petType.setName("dog"); - - PetDto pet = new PetDto(); - pet.setId(3); - pet.setName("Rosy"); - pet.setBirthDate(new Date()); - pet.setType(petType); - pets.add(pet); - - pet = new PetDto(); - pet.setId(4); - pet.setName("Jewel"); - pet.setBirthDate(new Date()); - pet.setType(petType); - pets.add(pet); + public void initPets() { + this.mockMvc = MockMvcBuilders.standaloneSetup(petRestController) + .setControllerAdvice(new ExceptionControllerAdvice()) + .build(); + pets = new ArrayList<>(); + + OwnerDto owner = new OwnerDto(); + owner.setId(1); + owner.setFirstName("Eduardo"); + owner.setLastName("Rodriquez"); + owner.setAddress("2693 Commerce St."); + owner.setCity("McFarland"); + owner.setTelephone("6085558763"); + + PetTypeDto petType = new PetTypeDto(); + petType.setId(2); + petType.setName("dog"); + + PetDto pet = new PetDto(); + pet.setId(3); + pet.setName("Rosy"); + pet.setBirthDate(LocalDate.now()); + pet.setType(petType); + pets.add(pet); + + pet = new PetDto(); + pet.setId(4); + pet.setName("Jewel"); + pet.setBirthDate(LocalDate.now()); + pet.setType(petType); + pets.add(pet); } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testGetPetSuccess() throws Exception { - given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0))); + given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0))); this.mockMvc.perform(get("/api/pets/3") - .accept(MediaType.APPLICATION_JSON_VALUE)) + .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(3)) @@ -124,23 +122,23 @@ public class PetRestControllerTests { } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testGetPetNotFound() throws Exception { - given(petMapper.toPetDto(this.clinicService.findPetById(-1))).willReturn(null); + given(petMapper.toPetDto(this.clinicService.findPetById(-1))).willReturn(null); this.mockMvc.perform(get("/api/pets/-1") - .accept(MediaType.APPLICATION_JSON)) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testGetAllPetsSuccess() throws Exception { final Collection<Pet> pets = petMapper.toPets(this.pets); System.err.println(pets); when(this.clinicService.findAllPets()).thenReturn(pets); //given(this.clinicService.findAllPets()).willReturn(petMapper.toPets(pets)); this.mockMvc.perform(get("/api/pets/") - .accept(MediaType.APPLICATION_JSON)) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(3)) @@ -150,63 +148,65 @@ public class PetRestControllerTests { } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testGetAllPetsNotFound() throws Exception { - pets.clear(); - given(this.clinicService.findAllPets()).willReturn(petMapper.toPets(pets)); + pets.clear(); + given(this.clinicService.findAllPets()).willReturn(petMapper.toPets(pets)); this.mockMvc.perform(get("/api/pets/") - .accept(MediaType.APPLICATION_JSON)) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testCreatePetSuccess() throws Exception { - PetDto newPet = pets.get(0); - newPet.setId(999); - ObjectMapper mapper = new ObjectMapper(); + PetDto newPet = pets.get(0); + newPet.setId(999); + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - String newPetAsJSON = mapper.writeValueAsString(newPet); - System.err.println("--> newPetAsJSON="+newPetAsJSON); - this.mockMvc.perform(post("/api/pets/") - .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(status().isCreated()); + String newPetAsJSON = mapper.writeValueAsString(newPet); + System.err.println("--> newPetAsJSON=" + newPetAsJSON); + this.mockMvc.perform(post("/api/pets/") + .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isCreated()); } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testCreatePetError() throws Exception { - PetDto newPet = pets.get(0); - newPet.setId(null); - newPet.setName(null); - ObjectMapper mapper = new ObjectMapper(); + PetDto newPet = pets.get(0); + newPet.setId(null); + newPet.setName(null); + ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - String newPetAsJSON = mapper.writeValueAsString(newPet); - this.mockMvc.perform(post("/api/pets/") - .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(status().isBadRequest()).andDo(MockMvcResultHandlers.print()); - } + String newPetAsJSON = mapper.writeValueAsString(newPet); + this.mockMvc.perform(post("/api/pets/") + .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isBadRequest()).andDo(MockMvcResultHandlers.print()); + } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testUpdatePetSuccess() throws Exception { - given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0))); - PetDto newPet = pets.get(0); - newPet.setName("Rosy I"); - ObjectMapper mapper = new ObjectMapper(); + given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0))); + PetDto newPet = pets.get(0); + newPet.setName("Rosy I"); + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - String newPetAsJSON = mapper.writeValueAsString(newPet); - this.mockMvc.perform(put("/api/pets/3") - .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(content().contentType("application/json")) - .andExpect(status().isNoContent()); + String newPetAsJSON = mapper.writeValueAsString(newPet); + this.mockMvc.perform(put("/api/pets/3") + .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(content().contentType("application/json")) + .andExpect(status().isNoContent()); - this.mockMvc.perform(get("/api/pets/3") - .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) + this.mockMvc.perform(get("/api/pets/3") + .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(3)) @@ -215,42 +215,42 @@ public class PetRestControllerTests { } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testUpdatePetError() throws Exception { - PetDto newPet = pets.get(0); - newPet.setName(null); - ObjectMapper mapper = new ObjectMapper(); + PetDto newPet = pets.get(0); + newPet.setName(null); + ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newPetAsJSON = mapper.writeValueAsString(newPet); - this.mockMvc.perform(put("/api/pets/3") - .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(status().isBadRequest()); - } + this.mockMvc.perform(put("/api/pets/3") + .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isBadRequest()); + } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testDeletePetSuccess() throws Exception { - PetDto newPet = pets.get(0); - ObjectMapper mapper = new ObjectMapper(); - String newPetAsJSON = mapper.writeValueAsString(newPet); - given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0))); - this.mockMvc.perform(delete("/api/pets/3") - .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(status().isNoContent()); + PetDto newPet = pets.get(0); + ObjectMapper mapper = new ObjectMapper(); + String newPetAsJSON = mapper.writeValueAsString(newPet); + given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0))); + this.mockMvc.perform(delete("/api/pets/3") + .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isNoContent()); } @Test - @WithMockUser(roles="OWNER_ADMIN") + @WithMockUser(roles = "OWNER_ADMIN") public void testDeletePetError() throws Exception { - PetDto newPet = pets.get(0); - ObjectMapper mapper = new ObjectMapper(); - String newPetAsJSON = mapper.writeValueAsString(newPet); - given(this.clinicService.findPetById(-1)).willReturn(null); - this.mockMvc.perform(delete("/api/pets/-1") - .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(status().isNotFound()); + PetDto newPet = pets.get(0); + ObjectMapper mapper = new ObjectMapper(); + String newPetAsJSON = mapper.writeValueAsString(newPet); + given(this.clinicService.findPetById(-1)).willReturn(null); + this.mockMvc.perform(delete("/api/pets/-1") + .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isNotFound()); } } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java index cd51385e9980ec2ea5d1a72a3fa750d92fb88d85..a664a0c1db65f3dbb5630d34ac7bf189e33c0903 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java @@ -17,6 +17,8 @@ package org.springframework.samples.petclinic.rest; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -38,9 +40,8 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import java.io.IOException; +import java.time.LocalDate; import java.util.ArrayList; -import java.util.Date; import java.util.List; import static org.mockito.BDDMockito.given; @@ -94,7 +95,7 @@ public class VisitRestControllerTests { Pet pet = new Pet(); pet.setId(8); pet.setName("Rosy"); - pet.setBirthDate(new Date()); + pet.setBirthDate(LocalDate.now()); pet.setOwner(owner); pet.setType(petType); @@ -102,14 +103,14 @@ public class VisitRestControllerTests { Visit visit = new Visit(); visit.setId(2); visit.setPet(pet); - visit.setDate(new Date()); + visit.setDate(LocalDate.now()); visit.setDescription("rabies shot"); visits.add(visit); visit = new Visit(); visit.setId(3); visit.setPet(pet); - visit.setDate(new Date()); + visit.setDate(LocalDate.now()); visit.setDescription("neutered"); visits.add(visit); @@ -167,6 +168,8 @@ public class VisitRestControllerTests { Visit newVisit = visits.get(0); newVisit.setId(999); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newVisitAsJSON = mapper.writeValueAsString(visitMapper.toVisitDto(newVisit)); System.out.println("newVisitAsJSON " + newVisitAsJSON); this.mockMvc.perform(post("/api/visits/") @@ -193,6 +196,8 @@ public class VisitRestControllerTests { Visit newVisit = visits.get(0); newVisit.setDescription("rabies shot test"); ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newVisitAsJSON = mapper.writeValueAsString(visitMapper.toVisitDto(newVisit)); this.mockMvc.perform(put("/api/visits/2") .content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) diff --git a/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java index a312decedceeeb6976a90bbb259528080cd8fb3e..3ea778f422a2ef1c9211563f8ef48b12bc8ba0d1 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java @@ -15,24 +15,19 @@ */ package org.springframework.samples.petclinic.service.clinicService; -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Collection; -import java.util.Date; - import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Specialty; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.model.*; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.util.EntityUtils; import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDate; +import java.util.Collection; + +import static org.assertj.core.api.Assertions.assertThat; + /** * <p> Base class for {@link ClinicService} integration tests. </p> <p> Subclasses should specify Spring context * configuration using {@link ContextConfiguration @ContextConfiguration} annotation </p> <p> @@ -138,7 +133,7 @@ public abstract class AbstractClinicServiceTests { pet.setName("bowser"); Collection<PetType> types = this.clinicService.findPetTypes(); pet.setType(EntityUtils.getById(types, PetType.class, 2)); - pet.setBirthDate(new Date()); + pet.setBirthDate(LocalDate.now()); owner6.addPet(pet); assertThat(owner6.getPets().size()).isEqualTo(found + 1); @@ -250,7 +245,7 @@ public abstract class AbstractClinicServiceTests { Visit visit = new Visit(); visit.setPet(pet); - visit.setDate(new Date()); + visit.setDate(LocalDate.now()); visit.setDescription("new visit");