From ba9c778b53ba0c0d1c62a7db3d31adb455f01702 Mon Sep 17 00:00:00 2001 From: ctate Date: Mon, 16 Nov 2020 22:09:36 -0700 Subject: [PATCH 1/2] Getting the persistence working with PostgreSQL. --- .gitignore | 5 +++ README.md | 30 +++++++++++++++-- pom.xml | 28 +++++++++++++--- src/main/java/META-INF/MANIFEST.MF | 3 ++ .../redhat/poc/SpringRestPocApplication.java | 2 ++ src/main/java/com/redhat/poc/dao/HatDAO.java | 27 ++++----------- src/main/java/com/redhat/poc/model/Hat.java | 22 ++++++++++--- .../com/redhat/poc/rest/RestController.java | 15 +++++---- .../com/redhat/poc/service/HatService.java | 31 ++++++++++------- .../com/redhat/poc/SpringRestPocJpaTests.java | 33 +++++++++++++++++++ 10 files changed, 145 insertions(+), 51 deletions(-) create mode 100644 .gitignore create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/test/java/com/redhat/poc/SpringRestPocJpaTests.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c050a79 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/target/ +.classpath +.project +.settings + diff --git a/README.md b/README.md index 85ef082..9c2f011 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,34 @@ # SpringRestPOC -to run locally: +## Setup a database locally -mvn clean install +```bash +sudo yum install -y postgresql-server +sudo /usr/bin/postgresql-setup initdb +sudo systemctl enable postgresql +sudo systemctl start postgresql +sudo systemctl status postgresql +psql +create user myuser password '...'; +create database mce owner myuser; +\q +psql mce +create table hat(id serial primary key, type text, color text, description text); +alter table hat owner to myuser; +\q +``` +# Run SpringRestPOC locally -mvn spring-boot:run +```bash +mvn clean install +SPRING_DATASOURCE_USERNAME=myuser SPRING_DATASOURCE_PASSWORD=... SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/mce mvn spring-boot:run +``` This requires java 8 or higher + +# Test the SpringRESTPOC API + +curl http://localhost:8080/hats; echo +curl -X POST -H "Content-Type: application/json" -d '{ "color":"Red", "type":"Fedora" }' http://localhost:8080/hats; echo +curl -X POST -H "Content-Type: application/json" -d '{ "color":"White", "type":"White Sox Hat" }' http://localhost:8080/hats; echo diff --git a/pom.xml b/pom.xml index f1d0fc9..6b46888 100644 --- a/pom.xml +++ b/pom.xml @@ -28,10 +28,18 @@ org.springframework.boot spring-boot-starter-web - - - - + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.data + spring-data-commons + + + org.postgresql + postgresql + mysql @@ -49,6 +57,18 @@ + + + + com.h2database + h2 + test + + + junit + junit + test + diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5e94951 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/src/main/java/com/redhat/poc/SpringRestPocApplication.java b/src/main/java/com/redhat/poc/SpringRestPocApplication.java index 5c40696..9f9a54d 100644 --- a/src/main/java/com/redhat/poc/SpringRestPocApplication.java +++ b/src/main/java/com/redhat/poc/SpringRestPocApplication.java @@ -1,9 +1,11 @@ package com.redhat.poc; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@EnableAutoConfiguration public class SpringRestPocApplication { public static void main(String[] args) { diff --git a/src/main/java/com/redhat/poc/dao/HatDAO.java b/src/main/java/com/redhat/poc/dao/HatDAO.java index 2f16242..2b3ac03 100644 --- a/src/main/java/com/redhat/poc/dao/HatDAO.java +++ b/src/main/java/com/redhat/poc/dao/HatDAO.java @@ -2,27 +2,12 @@ import com.redhat.poc.model.Hat; -// put logic get connection and query db in here -public class HatDAO { - - public Hat createHat(Hat hat){ - return new Hat(); - } - - - public Hat getHat(Integer id){ - +import java.util.Optional; - return new Hat(); +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; - } - - public void deleteHat(Integer id){ - } - - public Hat updateHat(Hat hat){ - - - return hat; - } +// put logic get connection and query db in here +@Repository +public interface HatDAO extends CrudRepository { } diff --git a/src/main/java/com/redhat/poc/model/Hat.java b/src/main/java/com/redhat/poc/model/Hat.java index 304a003..2342773 100644 --- a/src/main/java/com/redhat/poc/model/Hat.java +++ b/src/main/java/com/redhat/poc/model/Hat.java @@ -1,10 +1,22 @@ package com.redhat.poc.model; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; + +@Entity +@Table(name = "hat") public class Hat { String color; String type; - String descrption; + String description; + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) Integer id; public String getColor() { @@ -23,12 +35,12 @@ public void setType(String type) { this.type = type; } - public String getDescrption() { - return descrption; + public String getDescription() { + return description; } - public void setDescrption(String descrption) { - this.descrption = descrption; + public void setDescription(String descrption) { + this.description = descrption; } public Integer getId() { diff --git a/src/main/java/com/redhat/poc/rest/RestController.java b/src/main/java/com/redhat/poc/rest/RestController.java index 461f99d..9c139fe 100644 --- a/src/main/java/com/redhat/poc/rest/RestController.java +++ b/src/main/java/com/redhat/poc/rest/RestController.java @@ -2,6 +2,8 @@ import com.redhat.poc.model.Hat; import com.redhat.poc.service.HatService; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -17,7 +19,7 @@ @org.springframework.web.bind.annotation.RestController public class RestController { - // put logic to retrieve, update, create, delete the hats in hat service. Currently below is just a place holder that need to be replaced + @Autowired private HatService hatService; @GetMapping("/test") @@ -31,8 +33,8 @@ public Hat getHat(@PathVariable("hatId") Integer hatId){ } @PostMapping("/hats") - public ResponseEntity createHat(@RequestBody Hat hat){ - return new ResponseEntity( hat, HttpStatus.CREATED ); + public ResponseEntity createHat(@RequestBody Hat hat){ + return new ResponseEntity<>( hatService.createHat(hat), HttpStatus.CREATED ); } @PutMapping("/hats/{hatId}") @@ -42,14 +44,13 @@ public Hat updateHat(@RequestBody Hat hat, @PathVariable("hatId") Integer hatId } @DeleteMapping("/hats/{hatId}") - public ResponseEntity updateHat(@PathVariable("hatId") Integer hatId) { + public ResponseEntity updateHat(@PathVariable("hatId") Integer hatId) { return new ResponseEntity( HttpStatus.NO_CONTENT ); } @GetMapping("/hats") - public List getAllHats(){ - List hats = new ArrayList<>(); - return hats; + public Iterable getAllHats(){ + return hatService.getHats(); } } \ No newline at end of file diff --git a/src/main/java/com/redhat/poc/service/HatService.java b/src/main/java/com/redhat/poc/service/HatService.java index 3545a01..b6424c6 100644 --- a/src/main/java/com/redhat/poc/service/HatService.java +++ b/src/main/java/com/redhat/poc/service/HatService.java @@ -1,33 +1,42 @@ package com.redhat.poc.service; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + import com.redhat.poc.dao.HatDAO; import com.redhat.poc.model.Hat; +@Component public class HatService { - // put logic to query the db in hatDAO + + @Autowired private HatDAO dao; public Hat createHat(Hat hat){ - return dao.createHat(hat); +// return dao.save(hat); + Iterable a = dao.findAll(); + System.out.println(a); + Hat o = dao.save(hat); + a = dao.findAll(); + System.out.println(a); + return o; } + public Iterable getHats(){ + return dao.findAll(); + } + public Hat getHat(Integer id){ - Hat hat = new Hat(); - hat.setId(id); - hat.setColor("blue"); - hat.setType("fedora"); - hat.setDescrption("blue hat"); -// return dao.getHat(id); - return hat; + return dao.findById(id).orElse(null); } public void deleteHat(Integer id){ - dao.deleteHat(id); + dao.deleteById(id); } public Hat updateHat(Hat hat){ - return dao.updateHat(hat); + return dao.save(hat); } diff --git a/src/test/java/com/redhat/poc/SpringRestPocJpaTests.java b/src/test/java/com/redhat/poc/SpringRestPocJpaTests.java new file mode 100644 index 0000000..9798188 --- /dev/null +++ b/src/test/java/com/redhat/poc/SpringRestPocJpaTests.java @@ -0,0 +1,33 @@ +package com.redhat.poc; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; + +import com.redhat.poc.dao.HatDAO; +import com.redhat.poc.model.Hat; + +@DataJpaTest +class SpringRestPocJpaTests { + + @Autowired + private TestEntityManager entityManager; + + @Autowired + private HatDAO hatDAO; + + @Test + public void testFindByLastName() { + Hat hat = new Hat(); + hat.setColor("Red"); + hat.setType("Fedora"); + entityManager.persist(hat); + + hatDAO.findAll().forEach(h -> { + Assert.assertEquals(h.getColor(), "Red"); + Assert.assertEquals(h.getType(), "Fedora"); + }); + } +} From 13aedc9ed76da574dcea45cd0afaf79e912266ca Mon Sep 17 00:00:00 2001 From: ctate Date: Tue, 17 Nov 2020 06:54:50 -0700 Subject: [PATCH 2/2] Adding the OpenAPI 3 Swagger spec. --- .gitignore | 2 +- .../com/redhat/poc/rest/RestController.java | 2 +- .../openapi/SpringRestPOC-openapi-1.0.0.yml | 195 ++++++++++++++++++ 3 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/openapi/SpringRestPOC-openapi-1.0.0.yml diff --git a/.gitignore b/.gitignore index c050a79..a2092f3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ .classpath .project .settings - +ansible/vaults/* diff --git a/src/main/java/com/redhat/poc/rest/RestController.java b/src/main/java/com/redhat/poc/rest/RestController.java index 9c139fe..20f3204 100644 --- a/src/main/java/com/redhat/poc/rest/RestController.java +++ b/src/main/java/com/redhat/poc/rest/RestController.java @@ -44,7 +44,7 @@ public Hat updateHat(@RequestBody Hat hat, @PathVariable("hatId") Integer hatId } @DeleteMapping("/hats/{hatId}") - public ResponseEntity updateHat(@PathVariable("hatId") Integer hatId) { + public ResponseEntity deleteHat(@PathVariable("hatId") Integer hatId) { return new ResponseEntity( HttpStatus.NO_CONTENT ); } diff --git a/src/main/resources/openapi/SpringRestPOC-openapi-1.0.0.yml b/src/main/resources/openapi/SpringRestPOC-openapi-1.0.0.yml new file mode 100644 index 0000000..5f930c3 --- /dev/null +++ b/src/main/resources/openapi/SpringRestPOC-openapi-1.0.0.yml @@ -0,0 +1,195 @@ +openapi: 3.0.2 +info: + title: SpringRestPOC + version: 1.0.0 + description: |- + A sample API that uses a petstore as an example to demonstrate features + in the OpenAPI 3.0 specification + termsOfService: 'http://swagger.io/terms/' + contact: + name: Swagger API Team + url: 'http://swagger.io' + email: apiteam@swagger.io + license: + name: Apache 2.0 + url: 'https://www.apache.org/licenses/LICENSE-2.0.html' +servers: + - url: 'http://petstore.swagger.io/api' +paths: + /hats: + get: + responses: + '200': + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Hat' + examples: + laurent_cats: + value: + - id: 1 + color: Red + type: Fedora + description: hat response + default: + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + description: unexpected error + operationId: getAllHats + description: | + Returns all hats from the system that the user has access to + post: + requestBody: + description: Hat to add to the store + content: + application/json: + schema: + $ref: '#/components/schemas/NewHat' + examples: + tigresse: + value: + color: Red + type: Fedora + required: true + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/Hat' + examples: + tigresse: + value: + id: 1 + color: Red + type: Fedora + description: hat response + default: + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + description: unexpected error + operationId: createHat + description: Creates a new hat in the store. Duplicates are allowed + '/hats/{id}': + get: + parameters: + - examples: + id: + value: 1 + name: id + description: ID of hat to fetch + schema: + format: int32 + in: path + required: true + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/Hat' + examples: + zaza: + value: + id: 1 + color: Red + type: Fedora + description: hat response + default: + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + description: unexpected error + operationId: getHat + description: Returns a hat based on a single ID + put: + parameters: + - name: id + description: ID of pet to update + schema: + format: int32 + type: integer + in: path + required: true + responses: + '204': + description: hat deleted + default: + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + description: unexpected error + operationId: delete + description: updates a single hat based on the ID supplied + delete: + parameters: + - name: id + description: ID of hat to delete + schema: + format: int32 + type: integer + in: path + required: true + responses: + '204': + description: hat deleted + default: + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + description: unexpected error + operationId: deleteHat + description: deletes a single hat based on the ID supplied + parameters: + - name: id + description: Hat identifier + schema: + type: integer + in: path + required: true +components: + schemas: + Error: + required: + - code + - message + properties: + code: + format: int32 + type: integer + message: + type: string + NewHat: + required: + - name + properties: + color: + type: string + type: + type: string + description: + type: string + Hat: + allOf: + - $ref: '#/components/schemas/NewHat' + - required: + - id + properties: + id: + format: int32 + type: integer + color: + type: string + type: + type: string + description: + type: string