BAEL-2413 - Integrating Spring Boot with HSQLDB (#5891)

* Initial Commit

* Update source file location

* Update CustomerControllerUnitTest.java

* Update CustomerControllerUnitTest.java

* Update CustomerControllerUnitTest.java

* Update source files

* Update CustomerController.java

* Update CustomerController.java

* Update CustomerControllerUnitTest.java

* Update CustomerControllerUnitTest.java

* Update CustomerControllerUnitTest.java

* Update index.html

* Update persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/controllers/CustomerController.java

Co-Authored-By: alejandrogervasio <alejandro.gervasio@gmail.com>

* Update CustomerController.java

* Update CustomerControllerUnitTest.java

* Update CustomerController.java

* Update CustomerControllerUnitTest.java

* Update CustomerControllerUnitTest.java
This commit is contained in:
Alejandro Gervasio 2018-12-19 10:32:39 -03:00 committed by KevinGilmore
parent 53abf6733d
commit 3064a22a7a
7 changed files with 175 additions and 17 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.springboothsqldb.application;
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);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.springboothsqldb.application.controllers;
import com.baeldung.springboothsqldb.application.entities.Customer;
import com.baeldung.springboothsqldb.application.repositories.CustomerRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
private final CustomerRepository customerRepository;
@Autowired
public CustomerController(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
@PostMapping("/customers")
public Customer addCustomer(@RequestBody Customer customer) {
customerRepository.save(customer);
return customer;
}
@GetMapping("/customers")
public List<Customer> getCustomers() {
return (List<Customer>) customerRepository.findAll();
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.springboothsqldb.application.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "customers")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String email;
public Customer() {}
public Customer(String name, String email) {
this.name = name;
this.email = email;
}
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "Customer{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.springboothsqldb.application.repositories;
import com.baeldung.springboothsqldb.application.entities.Customer;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long> {}

View File

@ -1,16 +1,5 @@
spring.datasource.tomcat.initial-size=15
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=15
spring.datasource.tomcat.min-idle=8
spring.datasource.tomcat.default-auto-commit=true
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.datasource.driver-class-name = org.hsqldb.jdbc.JDBCDriver
spring.datasource.url = jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.username = sa
spring.datasource.password =
spring.jpa.hibernate.ddl-auto = create

View File

@ -40,4 +40,4 @@
</div>
</div>
</body>
</html>
</html>

View File

@ -0,0 +1,61 @@
package com.baeldung.springboothsqldb.application.tests;
import com.baeldung.springboothsqldb.application.entities.Customer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.nio.charset.Charset;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CustomerControllerUnitTest {
private static MediaType MEDIA_TYPE_JSON;
@Autowired
private MockMvc mockMvc;
@Before
public void setUpJsonMediaType() {
MEDIA_TYPE_JSON = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
}
@Test
public void whenPostHttpRequesttoCustomers_thenStatusOK() throws Exception {
Customer customer = new Customer("John", "john@domain.com");
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter objectWriter = mapper.writer().withDefaultPrettyPrinter();
String requestJson = objectWriter.writeValueAsString(customer);
this.mockMvc
.perform(MockMvcRequestBuilders.post("/customers")
.contentType(MEDIA_TYPE_JSON)
.content(requestJson)
)
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void whenGetHttpRequesttoCustomers_thenStatusOK() throws Exception {
this.mockMvc
.perform(MockMvcRequestBuilders.get("/customers"))
.andExpect(MockMvcResultMatchers.content().contentType(MEDIA_TYPE_JSON))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}