From 6d4682fa367e36d76048147e192d296335ed912b Mon Sep 17 00:00:00 2001 From: lor6 Date: Thu, 18 May 2017 10:16:13 +0300 Subject: [PATCH] boot example app (#1808) * boot example app * move project and rename packages * update constructor injection --- guest/spring-boot-app/.gitignore | 4 ++ .../WebContent/META-INF/MANIFEST.MF | 3 + guest/spring-boot-app/pom.xml | 64 +++++++++++++++++++ .../main/java/com/stackify/Application.java | 11 ++++ .../stackify/config/PersistenceConfig.java | 24 +++++++ .../controller/EmployeeController.java | 34 ++++++++++ .../java/com/stackify/model/Employee.java | 41 ++++++++++++ .../repository/EmployeeRepository.java | 9 +++ .../src/main/resources/application.properties | 5 ++ .../src/main/resources/myData.sql | 1 + .../src/main/resources/mySchema.sql | 1 + .../stackify/test/EmployeeControllerTest.java | 54 ++++++++++++++++ 12 files changed, 251 insertions(+) create mode 100644 guest/spring-boot-app/.gitignore create mode 100644 guest/spring-boot-app/WebContent/META-INF/MANIFEST.MF create mode 100644 guest/spring-boot-app/pom.xml create mode 100644 guest/spring-boot-app/src/main/java/com/stackify/Application.java create mode 100644 guest/spring-boot-app/src/main/java/com/stackify/config/PersistenceConfig.java create mode 100644 guest/spring-boot-app/src/main/java/com/stackify/controller/EmployeeController.java create mode 100644 guest/spring-boot-app/src/main/java/com/stackify/model/Employee.java create mode 100644 guest/spring-boot-app/src/main/java/com/stackify/repository/EmployeeRepository.java create mode 100644 guest/spring-boot-app/src/main/resources/application.properties create mode 100644 guest/spring-boot-app/src/main/resources/myData.sql create mode 100644 guest/spring-boot-app/src/main/resources/mySchema.sql create mode 100644 guest/spring-boot-app/src/test/java/com/stackify/test/EmployeeControllerTest.java diff --git a/guest/spring-boot-app/.gitignore b/guest/spring-boot-app/.gitignore new file mode 100644 index 0000000000..60be5b80aa --- /dev/null +++ b/guest/spring-boot-app/.gitignore @@ -0,0 +1,4 @@ +/target/ +.settings/ +.classpath +.project diff --git a/guest/spring-boot-app/WebContent/META-INF/MANIFEST.MF b/guest/spring-boot-app/WebContent/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..254272e1c0 --- /dev/null +++ b/guest/spring-boot-app/WebContent/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/guest/spring-boot-app/pom.xml b/guest/spring-boot-app/pom.xml new file mode 100644 index 0000000000..e92e5dbbc9 --- /dev/null +++ b/guest/spring-boot-app/pom.xml @@ -0,0 +1,64 @@ + + 4.0.0 + spring-boot-app + spring-boot-app + 0.0.1-SNAPSHOT + war + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-test + + + + + src + + + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + maven-war-plugin + 3.0.0 + + WebContent + + + + + + + 8.0.43 + + + \ No newline at end of file diff --git a/guest/spring-boot-app/src/main/java/com/stackify/Application.java b/guest/spring-boot-app/src/main/java/com/stackify/Application.java new file mode 100644 index 0000000000..81251e9765 --- /dev/null +++ b/guest/spring-boot-app/src/main/java/com/stackify/Application.java @@ -0,0 +1,11 @@ +package com.stackify; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args){ + SpringApplication.run(Application.class, args); + } +} diff --git a/guest/spring-boot-app/src/main/java/com/stackify/config/PersistenceConfig.java b/guest/spring-boot-app/src/main/java/com/stackify/config/PersistenceConfig.java new file mode 100644 index 0000000000..9cc003e167 --- /dev/null +++ b/guest/spring-boot-app/src/main/java/com/stackify/config/PersistenceConfig.java @@ -0,0 +1,24 @@ +package com.stackify.config; + +import javax.sql.DataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +@Configuration +public class PersistenceConfig { + + @Bean + public DataSource dataSource() { + EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); + EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2) + .addScript("mySchema.sql") + .addScript("myData.sql") + .build(); + return db; + } + +} \ No newline at end of file diff --git a/guest/spring-boot-app/src/main/java/com/stackify/controller/EmployeeController.java b/guest/spring-boot-app/src/main/java/com/stackify/controller/EmployeeController.java new file mode 100644 index 0000000000..17501921bf --- /dev/null +++ b/guest/spring-boot-app/src/main/java/com/stackify/controller/EmployeeController.java @@ -0,0 +1,34 @@ +package com.stackify.controller; + +import java.util.List; + +import org.springframework.web.bind.annotation.GetMapping; +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; +import org.springframework.http.HttpStatus; + +import com.stackify.model.Employee; +import com.stackify.repository.EmployeeRepository; + +@RestController +public class EmployeeController { + + private EmployeeRepository employeeRepository; + + public EmployeeController(EmployeeRepository employeeRepository){ + this.employeeRepository = employeeRepository; + } + + @PostMapping("/employees") + @ResponseStatus(HttpStatus.CREATED) + public void addEmployee(@RequestBody Employee employee) { + employeeRepository.save(employee); + } + + @GetMapping("/employees") + public List getEmployees() { + return employeeRepository.findAll(); + } +} diff --git a/guest/spring-boot-app/src/main/java/com/stackify/model/Employee.java b/guest/spring-boot-app/src/main/java/com/stackify/model/Employee.java new file mode 100644 index 0000000000..708a990166 --- /dev/null +++ b/guest/spring-boot-app/src/main/java/com/stackify/model/Employee.java @@ -0,0 +1,41 @@ +package com.stackify.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Employee { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String name; + + public Employee() { + } + + public Employee(long id, String name) { + super(); + this.id = id; + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/guest/spring-boot-app/src/main/java/com/stackify/repository/EmployeeRepository.java b/guest/spring-boot-app/src/main/java/com/stackify/repository/EmployeeRepository.java new file mode 100644 index 0000000000..583fa21ab0 --- /dev/null +++ b/guest/spring-boot-app/src/main/java/com/stackify/repository/EmployeeRepository.java @@ -0,0 +1,9 @@ +package com.stackify.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.stackify.model.Employee; + +public interface EmployeeRepository extends JpaRepository { + +} diff --git a/guest/spring-boot-app/src/main/resources/application.properties b/guest/spring-boot-app/src/main/resources/application.properties new file mode 100644 index 0000000000..71ec7156b4 --- /dev/null +++ b/guest/spring-boot-app/src/main/resources/application.properties @@ -0,0 +1,5 @@ +server.port=8081 +server.contextPath=/springbootapp +logging.level.org.springframework.web: DEBUG + +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file diff --git a/guest/spring-boot-app/src/main/resources/myData.sql b/guest/spring-boot-app/src/main/resources/myData.sql new file mode 100644 index 0000000000..abf63b6d5b --- /dev/null +++ b/guest/spring-boot-app/src/main/resources/myData.sql @@ -0,0 +1 @@ +insert into employee(name) values ('ana'); \ No newline at end of file diff --git a/guest/spring-boot-app/src/main/resources/mySchema.sql b/guest/spring-boot-app/src/main/resources/mySchema.sql new file mode 100644 index 0000000000..48ba63d69f --- /dev/null +++ b/guest/spring-boot-app/src/main/resources/mySchema.sql @@ -0,0 +1 @@ +create table employee(id int identity primary key, name varchar(30)); \ No newline at end of file diff --git a/guest/spring-boot-app/src/test/java/com/stackify/test/EmployeeControllerTest.java b/guest/spring-boot-app/src/test/java/com/stackify/test/EmployeeControllerTest.java new file mode 100644 index 0000000000..8ca34b6224 --- /dev/null +++ b/guest/spring-boot-app/src/test/java/com/stackify/test/EmployeeControllerTest.java @@ -0,0 +1,54 @@ +package com.stackify.test; + +import org.junit.Before; +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.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.hamcrest.Matchers.*; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import com.stackify.Application; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +@WebAppConfiguration +public class EmployeeControllerTest { + + private static final String CONTENT_TYPE = "application/json;charset=UTF-8"; + + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext webApplicationContext; + + @Before + public void setup() throws Exception { + this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .build(); + } + + @Test + public void whenCreateGetEmployee_thenOk() throws Exception { + String employeeJson = "{\"name\":\"john\"}"; + + this.mockMvc.perform(post("/employees").contentType(CONTENT_TYPE) + .content(employeeJson)) + .andExpect(status().isCreated()); + + this.mockMvc.perform(get("/employees")) + .andExpect(status().isOk()) + .andExpect(content().contentType(CONTENT_TYPE)) + .andExpect(jsonPath("$", hasSize(2))) + .andExpect(jsonPath("$[0].name", is("ana"))) + .andExpect(jsonPath("$[1].name", is("john"))); + } +}