From 0c5a5395c72a1cdf2c2493c5eea01df98e8147fc Mon Sep 17 00:00:00 2001 From: jeanvalentin51 Date: Fri, 6 Dec 2019 17:27:21 -0500 Subject: [PATCH 1/4] created stubs --- .../crudapp/controllers/PersonController.java | 46 +++++++++++++++++++ .../io/zipcoder/crudapp/models/Person.java | 36 +++++++++++++++ .../repositories/PersonRepository.java | 9 ++++ .../crudapp/services/PersonService.java | 40 ++++++++++++++++ .../controllers/PersonControllerTests.java | 36 +++++++++++++++ 5 files changed, 167 insertions(+) create mode 100644 src/main/java/io/zipcoder/crudapp/controllers/PersonController.java create mode 100644 src/main/java/io/zipcoder/crudapp/models/Person.java create mode 100644 src/main/java/io/zipcoder/crudapp/repositories/PersonRepository.java create mode 100644 src/main/java/io/zipcoder/crudapp/services/PersonService.java create mode 100644 src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java diff --git a/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java b/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java new file mode 100644 index 0000000..638b732 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java @@ -0,0 +1,46 @@ +package io.zipcoder.crudapp.controllers; + +import io.zipcoder.crudapp.models.Person; +import io.zipcoder.crudapp.services.PersonService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RequestMapping("/people/") +@RestController +public class PersonController { + + private PersonService personService; + + public PersonController(PersonService personService){ + this.personService = personService; + } + + @GetMapping("{id}") + public ResponseEntity getPersonById(@PathVariable Integer id){ + return new ResponseEntity<>(this.personService.getPerson(id), HttpStatus.OK); + } + + @GetMapping + public ResponseEntity> getPersonList(){ + return new ResponseEntity<>(this.personService.getAll(),HttpStatus.OK); + } + + @PostMapping + public ResponseEntity createPerson(@RequestBody Person p){ + return new ResponseEntity<>(this.personService.createPerson(p),HttpStatus.CREATED); + } + + @PutMapping("{id}") + public ResponseEntity updatePerson (Person p, @PathVariable Integer id){ + return new ResponseEntity<>(this.personService.updatePerson(p,id),HttpStatus.OK) ; + } + + @DeleteMapping("{id}") + public void deletePersonById(@PathVariable Integer id){ + this.personService.deletePersonById(id); + } +} diff --git a/src/main/java/io/zipcoder/crudapp/models/Person.java b/src/main/java/io/zipcoder/crudapp/models/Person.java new file mode 100644 index 0000000..060b425 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/models/Person.java @@ -0,0 +1,36 @@ +package io.zipcoder.crudapp.models; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Person { + private String firstName; + private String lastName; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int Id; + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public int getId() { + return Id; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/src/main/java/io/zipcoder/crudapp/repositories/PersonRepository.java b/src/main/java/io/zipcoder/crudapp/repositories/PersonRepository.java new file mode 100644 index 0000000..3d486aa --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/repositories/PersonRepository.java @@ -0,0 +1,9 @@ +package io.zipcoder.crudapp.repositories; + +import io.zipcoder.crudapp.models.Person; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PersonRepository extends CrudRepository { +} diff --git a/src/main/java/io/zipcoder/crudapp/services/PersonService.java b/src/main/java/io/zipcoder/crudapp/services/PersonService.java new file mode 100644 index 0000000..768dc72 --- /dev/null +++ b/src/main/java/io/zipcoder/crudapp/services/PersonService.java @@ -0,0 +1,40 @@ +package io.zipcoder.crudapp.services; + +import io.zipcoder.crudapp.models.Person; +import io.zipcoder.crudapp.repositories.PersonRepository; +import org.springframework.stereotype.Service; + +@Service +public class PersonService { + private PersonRepository personRepository; + + public PersonService(PersonRepository personRepository) { + this.personRepository = personRepository; + } + + public void deletePersonById (Integer id){ + this.personRepository.delete(id); + } + + public Person updatePerson(Person newPersonData, Integer id){ + Person personToUpdate = this.personRepository.findOne(id); + personToUpdate.setFirstName(newPersonData.getFirstName()); + personToUpdate.setLastName(newPersonData.getLastName()); + this.personRepository.save(personToUpdate); + + return personToUpdate; + } + + public Person getPerson(Integer id){ + return this.personRepository.findOne(id); + } + + public Person createPerson(Person p){ + return this.personRepository.save(p); + } + + public Iterable getAll (){ + return this.personRepository.findAll(); + } + +} diff --git a/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java b/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java new file mode 100644 index 0000000..fa0a4e3 --- /dev/null +++ b/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java @@ -0,0 +1,36 @@ +package io.zipcoder.crudapp.controllers; + +import io.zipcoder.crudapp.controllers.PersonController; +import io.zipcoder.crudapp.models.Person; +import io.zipcoder.crudapp.repositories.PersonRepository; +import io.zipcoder.crudapp.services.PersonService; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.util.Optional; + +@SpringBootTest +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +public class PersonControllerTests { + + @Autowired + private MockMvc mvc; + + + @MockBean + private PersonRepository repository; + + +} From 4ba789d1a18a2d058b458ec0ef31e1050afea706 Mon Sep 17 00:00:00 2001 From: jeanvalentin51 Date: Fri, 6 Dec 2019 18:31:16 -0500 Subject: [PATCH 2/4] Added test --- .../io/zipcoder/crudapp/models/Person.java | 4 ++++ .../controllers/PersonControllerTests.java | 21 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/zipcoder/crudapp/models/Person.java b/src/main/java/io/zipcoder/crudapp/models/Person.java index 060b425..7423ad6 100644 --- a/src/main/java/io/zipcoder/crudapp/models/Person.java +++ b/src/main/java/io/zipcoder/crudapp/models/Person.java @@ -14,6 +14,10 @@ public class Person { @GeneratedValue(strategy = GenerationType.AUTO) private int Id; + public Person(){ + + } + public String getFirstName() { return firstName; } diff --git a/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java b/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java index fa0a4e3..b028d8c 100644 --- a/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java +++ b/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java @@ -32,5 +32,24 @@ public class PersonControllerTests { @MockBean private PersonRepository repository; - + @Test + public void testCreatePerson() throws Exception { + Person newPerson = new Person(); + newPerson.setFirstName("Valentin"); + newPerson.setLastName("G"); + + BDDMockito + .given(repository.save(newPerson)) + .willReturn(newPerson); + + String expectedContent = "{\"ID\":null,\"FIRST_NAME\":\"Valentin\",\"LAST_NAME\":\"G\"}"; + this.mvc.perform(MockMvcRequestBuilders + .post("/people/") + .content(expectedContent) + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + ) + .andExpect(MockMvcResultMatchers.status().isCreated()) + .andExpect(MockMvcResultMatchers.content().string(expectedContent)); + } } From 72349b256186e974d9edd53f58363a03cf68ef73 Mon Sep 17 00:00:00 2001 From: jeanvalentin51 Date: Fri, 6 Dec 2019 19:53:25 -0500 Subject: [PATCH 3/4] changed case --- .../crudapp/controllers/PersonController.java | 8 +++---- .../io/zipcoder/crudapp/models/Person.java | 24 +++++++++---------- .../crudapp/services/PersonService.java | 4 ++-- src/main/resources/schema-h2.sql | 4 ++-- .../controllers/PersonControllerTests.java | 10 +++----- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java b/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java index 638b732..8fc8f23 100644 --- a/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java +++ b/src/main/java/io/zipcoder/crudapp/controllers/PersonController.java @@ -9,7 +9,7 @@ import java.util.List; -@RequestMapping("/people/") +@RequestMapping("/people") @RestController public class PersonController { @@ -19,7 +19,7 @@ public PersonController(PersonService personService){ this.personService = personService; } - @GetMapping("{id}") + @GetMapping("/{id}") public ResponseEntity getPersonById(@PathVariable Integer id){ return new ResponseEntity<>(this.personService.getPerson(id), HttpStatus.OK); } @@ -34,12 +34,12 @@ public ResponseEntity createPerson(@RequestBody Person p){ return new ResponseEntity<>(this.personService.createPerson(p),HttpStatus.CREATED); } - @PutMapping("{id}") + @PutMapping("/{id}") public ResponseEntity updatePerson (Person p, @PathVariable Integer id){ return new ResponseEntity<>(this.personService.updatePerson(p,id),HttpStatus.OK) ; } - @DeleteMapping("{id}") + @DeleteMapping("/{id}") public void deletePersonById(@PathVariable Integer id){ this.personService.deletePersonById(id); } diff --git a/src/main/java/io/zipcoder/crudapp/models/Person.java b/src/main/java/io/zipcoder/crudapp/models/Person.java index 7423ad6..42f1f66 100644 --- a/src/main/java/io/zipcoder/crudapp/models/Person.java +++ b/src/main/java/io/zipcoder/crudapp/models/Person.java @@ -7,34 +7,34 @@ @Entity public class Person { - private String firstName; - private String lastName; + private String first_NAME; + private String last_NAME; @Id @GeneratedValue(strategy = GenerationType.AUTO) - private int Id; + private int id; public Person(){ } - public String getFirstName() { - return firstName; + public String getFirst_NAME() { + return first_NAME; } - public String getLastName() { - return lastName; + public String getLast_NAME() { + return last_NAME; } public int getId() { - return Id; + return id; } - public void setFirstName(String firstName) { - this.firstName = firstName; + public void setFirst_NAME(String firstName) { + this.first_NAME = firstName; } - public void setLastName(String lastName) { - this.lastName = lastName; + public void setLast_NAME(String lastName) { + this.last_NAME = lastName; } } diff --git a/src/main/java/io/zipcoder/crudapp/services/PersonService.java b/src/main/java/io/zipcoder/crudapp/services/PersonService.java index 768dc72..5584243 100644 --- a/src/main/java/io/zipcoder/crudapp/services/PersonService.java +++ b/src/main/java/io/zipcoder/crudapp/services/PersonService.java @@ -18,8 +18,8 @@ public void deletePersonById (Integer id){ public Person updatePerson(Person newPersonData, Integer id){ Person personToUpdate = this.personRepository.findOne(id); - personToUpdate.setFirstName(newPersonData.getFirstName()); - personToUpdate.setLastName(newPersonData.getLastName()); + personToUpdate.setFirst_NAME(newPersonData.getFirst_NAME()); + personToUpdate.setLast_NAME(newPersonData.getLast_NAME()); this.personRepository.save(personToUpdate); return personToUpdate; diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql index 94f7b0f..402e2fc 100644 --- a/src/main/resources/schema-h2.sql +++ b/src/main/resources/schema-h2.sql @@ -1,4 +1,4 @@ -DROP TABLE PERSON; +-- DROP TABLE PERSON; CREATE TABLE PERSON ( ID NUMBER(10,0) NOT NULL AUTO_INCREMENT, @@ -6,6 +6,6 @@ FIRST_NAME VARCHAR2(255) DEFAULT NULL, LAST_NAME VARCHAR2(255) DEFAULT NULL, PRIMARY KEY (ID)); -DROP SEQUENCE hibernate_sequence; +-- DROP SEQUENCE hibernate_sequence; CREATE SEQUENCE hibernate_sequence; diff --git a/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java b/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java index b028d8c..4499799 100644 --- a/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java +++ b/src/test/java/io/zipcoder/crudapp/controllers/PersonControllerTests.java @@ -1,9 +1,7 @@ package io.zipcoder.crudapp.controllers; -import io.zipcoder.crudapp.controllers.PersonController; import io.zipcoder.crudapp.models.Person; import io.zipcoder.crudapp.repositories.PersonRepository; -import io.zipcoder.crudapp.services.PersonService; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,8 +16,6 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; -import java.util.Optional; - @SpringBootTest @AutoConfigureMockMvc @RunWith(SpringRunner.class) @@ -35,8 +31,8 @@ public class PersonControllerTests { @Test public void testCreatePerson() throws Exception { Person newPerson = new Person(); - newPerson.setFirstName("Valentin"); - newPerson.setLastName("G"); + newPerson.setFirst_NAME("Valentin"); + newPerson.setLast_NAME("G"); BDDMockito .given(repository.save(newPerson)) @@ -44,7 +40,7 @@ public void testCreatePerson() throws Exception { String expectedContent = "{\"ID\":null,\"FIRST_NAME\":\"Valentin\",\"LAST_NAME\":\"G\"}"; this.mvc.perform(MockMvcRequestBuilders - .post("/people/") + .post("/people") .content(expectedContent) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) From 3ee33797b609cbfd56d1c6bc231e2efad7566c47 Mon Sep 17 00:00:00 2001 From: jeanvalentin51 Date: Fri, 6 Dec 2019 19:57:53 -0500 Subject: [PATCH 4/4] changed schema --- src/main/resources/application-h2.properties | 2 +- src/main/resources/schema-h2.sql | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/application-h2.properties b/src/main/resources/application-h2.properties index 74765cc..0ed91d5 100644 --- a/src/main/resources/application-h2.properties +++ b/src/main/resources/application-h2.properties @@ -1,4 +1,4 @@ spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle spring.datasource.platform=h2 spring.jpa.hibernate.ddl-auto=none -spring.datasource.continue-on-error=true \ No newline at end of file +spring.datasource.continue-on-error=false \ No newline at end of file diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql index 402e2fc..c36308a 100644 --- a/src/main/resources/schema-h2.sql +++ b/src/main/resources/schema-h2.sql @@ -1,4 +1,4 @@ --- DROP TABLE PERSON; +DROP TABLE IF EXISTS PERSON; CREATE TABLE PERSON ( ID NUMBER(10,0) NOT NULL AUTO_INCREMENT, @@ -6,6 +6,6 @@ FIRST_NAME VARCHAR2(255) DEFAULT NULL, LAST_NAME VARCHAR2(255) DEFAULT NULL, PRIMARY KEY (ID)); --- DROP SEQUENCE hibernate_sequence; +DROP SEQUENCE IF EXISTS hibernate_sequence; CREATE SEQUENCE hibernate_sequence;