diff --git a/.gitignore b/.gitignore index b00af803d..8cdbd1ff4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,10 @@ target/* .idea *.iml /target +.sts4-cache/ +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json _site/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..559c53805 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Debug (Launch)-PetClinicApplication", + "request": "launch", + "cwd": "${workspaceFolder}", + "console": "internalConsole", + "stopOnEntry": false, + "mainClass": "org.springframework.samples.petclinic.PetClinicApplication", + "projectName": "spring-petclinic", + "args": "" + }, + { + "type": "java", + "name": "Debug (Attach)", + "request": "attach", + "hostName": "localhost", + "port": 0 + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..c5f3f6b9c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 000000000..fabd5c416 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,19 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "verify", + "type": "shell", + "command": "mvn -B verify", + "group": "build" + }, + { + "label": "test", + "type": "shell", + "command": "mvn -B test", + "group": "test" + } + ] +} diff --git a/pom.xml b/pom.xml index bdb939f31..fe07d59b6 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.springframework.samples spring-petclinic - 2.0.0 + 2.0.0.BUILD-SNAPSHOT org.springframework.boot @@ -84,7 +84,7 @@ org.webjars - webjars-locator + webjars-locator-core org.webjars diff --git a/readme.md b/readme.md index 3f4c376ec..78e4ac828 100644 --- a/readme.md +++ b/readme.md @@ -117,3 +117,4 @@ The Spring PetClinic sample application is released under version 2.0 of the [Ap [spring-petclinic-reactjs]: https://github.com/spring-petclinic/spring-petclinic-reactjs [spring-petclinic-graphql]: https://github.com/spring-petclinic/spring-petclinic-graphql [spring-petclinic-kotlin]: https://github.com/spring-petclinic/spring-petclinic-kotlin +[spring-petclinic-rest]: https://github.com/spring-petclinic/spring-petclinic-rest diff --git a/src/main/resources/application-mysql.properties b/src/main/resources/application-mysql.properties index fd561fa90..c2c654fe4 100644 --- a/src/main/resources/application-mysql.properties +++ b/src/main/resources/application-mysql.properties @@ -3,5 +3,5 @@ database=mysql spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=root spring.datasource.password=root - # Uncomment this the first time the app runs -# spring.datasource.initialize=true \ No newline at end of file +# Uncomment this the first time the app runs +# spring.datasource.initialization-mode=always diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ed5392afe..300538118 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -14,8 +14,7 @@ spring.messages.basename=messages/messages # Actuator / Management management.endpoints.web.base-path=/manage -# Spring Boot 1.5 makes actuator secure by default -management.endpoints.web.enabled=true +management.endpoints.web.exposure.include=* # Logging logging.level.org.springframework=INFO @@ -24,3 +23,6 @@ logging.level.org.springframework=INFO # Active Spring profiles spring.profiles.active=production + +# Adjusting local host to 8090 for application launch +server.port = 8090 diff --git a/src/main/resources/templates/fragments/inputField.html b/src/main/resources/templates/fragments/inputField.html index 7cef1ee9e..c3373bea0 100644 --- a/src/main/resources/templates/fragments/inputField.html +++ b/src/main/resources/templates/fragments/inputField.html @@ -1,14 +1,18 @@
- +
- +
+ + +
diff --git a/src/main/resources/templates/owners/createOrUpdateOwnerForm.html b/src/main/resources/templates/owners/createOrUpdateOwnerForm.html index a34a39c7f..ca22d9de1 100644 --- a/src/main/resources/templates/owners/createOrUpdateOwnerForm.html +++ b/src/main/resources/templates/owners/createOrUpdateOwnerForm.html @@ -7,15 +7,15 @@
+ th:replace="~{fragments/inputField :: input ('First Name', 'firstName', 'text')}" /> + th:replace="~{fragments/inputField :: input ('Last Name', 'lastName', 'text')}" /> + th:replace="~{fragments/inputField :: input ('Address', 'address', 'text')}" /> + th:replace="~{fragments/inputField :: input ('City', 'city', 'text')}" /> + th:replace="~{fragments/inputField :: input ('Telephone', 'telephone', 'text')}" />
diff --git a/src/main/resources/templates/pets/createOrUpdatePetForm.html b/src/main/resources/templates/pets/createOrUpdatePetForm.html index e4726e155..a0c182aa5 100644 --- a/src/main/resources/templates/pets/createOrUpdatePetForm.html +++ b/src/main/resources/templates/pets/createOrUpdatePetForm.html @@ -17,9 +17,9 @@
+ th:replace="~{fragments/inputField :: input ('Name', 'name', 'text')}" /> + th:replace="~{fragments/inputField :: input ('Birth Date', 'birthDate', 'date')}" />
diff --git a/src/main/resources/templates/pets/createOrUpdateVisitForm.html b/src/main/resources/templates/pets/createOrUpdateVisitForm.html index 26a71378f..4401d36ce 100644 --- a/src/main/resources/templates/pets/createOrUpdateVisitForm.html +++ b/src/main/resources/templates/pets/createOrUpdateVisitForm.html @@ -31,9 +31,9 @@
+ th:replace="~{fragments/inputField :: input ('Date', 'date', 'date')}" /> + th:replace="~{fragments/inputField :: input ('Description', 'description', 'text')}" />
diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java new file mode 100644 index 000000000..18d0c3b18 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetTests.java @@ -0,0 +1,86 @@ +package org.springframework.samples.petclinic.owner; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import org.springframework.samples.petclinic.visit.Visit; + +import java.util.List; +import java.util.Set; +import java.util.LinkedHashSet; +import java.time.*; +import java.util.Date; + +public class PetTests { + + private Pet pet; + private Date birthDate; + + @Before + public void testSetUp() { + //Initialization of pet + this.pet = new Pet(); + PetType dog = new PetType(); + dog.setId(9); + dog.setName("Duncan Jones"); + //Initialization of birthDate + //Converting the current time to a local date and ultimately a date to be input into setBirthDate; + LocalDateTime timePoint = LocalDateTime.now(); + LocalDate localDate = timePoint.toLocalDate(); + birthDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); + } + + @Test + public void testSetAndGetBirthDate() { + pet.setBirthDate(this.birthDate); + Date resultOfGetDate = pet.getBirthDate(); + assertEquals(this.birthDate, resultOfGetDate); + } + + @Test + public void testSetAndGetType() { + //Creating a new pet type to test the setters and getters for pet's type + PetType walrus = new PetType(); + walrus.setId(36); + walrus.setName("Alex Garland"); + pet.setType(walrus); + PetType resultOfGetType = pet.getType(); + assertEquals(walrus.getName(), resultOfGetType.getName()); + } + + @Test + public void testSetAndGetOwner() { + //Creating a new owner type to test the setters and getters for the pet's owner + Owner amandeepBhandal = new Owner(); + amandeepBhandal.setAddress("Off-world Colony"); + amandeepBhandal.setCity("Beirut"); + amandeepBhandal.setTelephone("514-333-3333"); + //Attach the newly created owner to the pet + pet.setOwner(amandeepBhandal); + Owner resultOfGetOwner = pet.getOwner(); + assertEquals(resultOfGetOwner.getAddress(), "Off-world Colony"); + assertEquals(resultOfGetOwner.getCity(), "Beirut"); + assertEquals(resultOfGetOwner.getTelephone(), "514-333-3333"); + assertEquals(resultOfGetOwner.getPetsInternal().size(), 0); + } + + @Test + public void testSetAndGetVisitsInternal() { + //Creating a new set of visits, albeit an empty set, to test the setters and getters for the pet's visits + Set visitsForTesting = new LinkedHashSet<>(); + pet.setVisitsInternal(visitsForTesting); + Set resultOfGetVisitsInternal = pet.getVisitsInternal(); + assertEquals(visitsForTesting.size(), resultOfGetVisitsInternal.size()); + } + + @Test + public void testAddVisitAndGetVisits() { + //Creating a new set of visits, albeit an empty set, to test the setters and getters for the pet's visits + Visit visitForTesting = new Visit(); + pet.addVisit(visitForTesting); + List resultOfGetVisits = pet.getVisits(); + Visit onlyVisitInCollection = resultOfGetVisits.iterator().next(); + assertEquals(1, resultOfGetVisits.size()); + assertEquals(visitForTesting.getId(), onlyVisitInCollection.getId()); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java new file mode 100644 index 000000000..220429ed8 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/PetValidatorTests.java @@ -0,0 +1,112 @@ +package org.springframework.samples.petclinic.owner; +import static org.junit.Assert.*; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Date; + +import org.junit.Test; +import org.springframework.validation.BeanPropertyBindingResult; +import org.springframework.validation.Errors; + +public class PetValidatorTests { + + @Test + public void testValidationWithValidName() { + //given + PetValidator petValidator = new PetValidator(); + Pet validPet = new Pet(); + validPet.setName("Peter"); + Errors errors = new BeanPropertyBindingResult(validPet, ""); + + //when + petValidator.validate(validPet, errors); + + //then + assertNull(errors.getFieldError("name")); + } + + @Test + public void testValidationWithInvalidName() { + //given + PetValidator petValidator = new PetValidator(); + Pet invalidPet = new Pet(); + invalidPet.setName(""); + Errors errors = new BeanPropertyBindingResult(invalidPet, ""); + + //when + petValidator.validate(invalidPet, errors); + + //then + assertTrue(errors.hasErrors()); + } + + @Test + public void testValidationWithValidType() { + //given + PetValidator petValidator = new PetValidator(); + Pet validPet = new Pet(); + PetType tiger = new PetType(); + tiger.setId(24); + validPet.setType(tiger); + Errors errors = new BeanPropertyBindingResult(validPet, ""); + + //when + petValidator.validate(validPet, errors); + + //then + assertNull(errors.getFieldError("type")); + } + + @Test + public void testValidationWithInvalidType() { + //given + PetValidator petValidator = new PetValidator(); + Pet invalidPet = new Pet(); + PetType emptyType = null; + invalidPet.setType(emptyType); + Errors errors = new BeanPropertyBindingResult(invalidPet, ""); + + //when + petValidator.validate(invalidPet, errors); + + //then + assertTrue(errors.hasErrors()); + } + + @Test + public void testValidationWithValidBirthDate() { + //given + PetValidator petValidator = new PetValidator(); + Pet validPet = new Pet(); + LocalDateTime timePoint = LocalDateTime.now(); + LocalDate localDate = timePoint.toLocalDate(); + Date birthDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); + validPet.setBirthDate(birthDate); + Errors errors = new BeanPropertyBindingResult(validPet, ""); + + //when + petValidator.validate(validPet, errors); + + //then + assertNull(errors.getFieldError("birthDate")); + } + + @Test + public void testValidationWithInvalidBirthDate() { + //given + PetValidator petValidator = new PetValidator(); + Pet invalidPet = new Pet(); + Date birthDate = null; + invalidPet.setBirthDate(birthDate); + Errors errors = new BeanPropertyBindingResult(invalidPet, ""); + + //when + petValidator.validate(invalidPet, errors); + + //then + assertTrue(errors.hasErrors()); + } +} + diff --git a/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java b/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java index 9636e3623..d247d178e 100644 --- a/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java +++ b/src/test/java/org/springframework/samples/petclinic/system/ProductionConfigurationTests.java @@ -14,7 +14,7 @@ public class ProductionConfigurationTests { @Autowired private VetRepository vets; - + //Test error introduced by addition of 10 commits since forking of our repo from original repo @Test public void testFindAll() throws Exception { vets.findAll();