Skip to content

Commit 3064a22

Browse files
alejandrogervasioKevinGilmore
authored andcommitted
BAEL-2413 - Integrating Spring Boot with HSQLDB (eugenp#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 <[email protected]> * Update CustomerController.java * Update CustomerControllerUnitTest.java * Update CustomerController.java * Update CustomerControllerUnitTest.java * Update CustomerControllerUnitTest.java
1 parent 53abf67 commit 3064a22

File tree

7 files changed

+175
-17
lines changed

7 files changed

+175
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.springboothsqldb.application;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.springboothsqldb.application.controllers;
2+
3+
import com.baeldung.springboothsqldb.application.entities.Customer;
4+
import com.baeldung.springboothsqldb.application.repositories.CustomerRepository;
5+
import java.util.List;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.ResponseBody;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
@RestController
14+
public class CustomerController {
15+
16+
private final CustomerRepository customerRepository;
17+
18+
@Autowired
19+
public CustomerController(CustomerRepository customerRepository) {
20+
this.customerRepository = customerRepository;
21+
}
22+
23+
@PostMapping("/customers")
24+
public Customer addCustomer(@RequestBody Customer customer) {
25+
customerRepository.save(customer);
26+
return customer;
27+
}
28+
29+
@GetMapping("/customers")
30+
public List<Customer> getCustomers() {
31+
return (List<Customer>) customerRepository.findAll();
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.springboothsqldb.application.entities;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.Table;
8+
9+
@Entity
10+
@Table(name = "customers")
11+
public class Customer {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.AUTO)
15+
private long id;
16+
17+
private String name;
18+
private String email;
19+
20+
public Customer() {}
21+
22+
public Customer(String name, String email) {
23+
this.name = name;
24+
this.email = email;
25+
}
26+
27+
public void setId(long id) {
28+
this.id = id;
29+
}
30+
31+
public long getId() {
32+
return id;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setEmail(String email) {
44+
this.email = email;
45+
}
46+
47+
public String getEmail() {
48+
return email;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "Customer{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.springboothsqldb.application.repositories;
2+
3+
import com.baeldung.springboothsqldb.application.entities.Customer;
4+
import org.springframework.data.repository.CrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface CustomerRepository extends CrudRepository<Customer, Long> {}
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
spring.datasource.tomcat.initial-size=15
2-
spring.datasource.tomcat.max-wait=20000
3-
spring.datasource.tomcat.max-active=50
4-
spring.datasource.tomcat.max-idle=15
5-
spring.datasource.tomcat.min-idle=8
6-
spring.datasource.tomcat.default-auto-commit=true
7-
spring.datasource.url=jdbc:h2:mem:test
8-
spring.datasource.username=root
9-
spring.datasource.password=
10-
spring.datasource.driver-class-name=org.h2.Driver
11-
12-
spring.jpa.show-sql=false
13-
spring.jpa.hibernate.ddl-auto=update
14-
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
15-
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
16-
spring.jpa.properties.hibernate.id.new_generator_mappings=false
1+
spring.datasource.driver-class-name = org.hsqldb.jdbc.JDBCDriver
2+
spring.datasource.url = jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
3+
spring.datasource.username = sa
4+
spring.datasource.password =
5+
spring.jpa.hibernate.ddl-auto = create

persistence-modules/spring-boot-persistence/src/main/resources/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ <h2 class="my-5">Users</h2>
4040
</div>
4141
</div>
4242
</body>
43-
</html>
43+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.springboothsqldb.application.tests;
2+
3+
import com.baeldung.springboothsqldb.application.entities.Customer;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.ObjectWriter;
6+
import com.fasterxml.jackson.databind.SerializationFeature;
7+
import java.nio.charset.Charset;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.test.context.junit4.SpringRunner;
16+
import org.springframework.test.web.servlet.MockMvc;
17+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
18+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
19+
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest
22+
@AutoConfigureMockMvc
23+
public class CustomerControllerUnitTest {
24+
25+
private static MediaType MEDIA_TYPE_JSON;
26+
27+
@Autowired
28+
private MockMvc mockMvc;
29+
30+
@Before
31+
public void setUpJsonMediaType() {
32+
MEDIA_TYPE_JSON = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
33+
34+
}
35+
36+
@Test
37+
public void whenPostHttpRequesttoCustomers_thenStatusOK() throws Exception {
38+
Customer customer = new Customer("John", "[email protected]");
39+
ObjectMapper mapper = new ObjectMapper();
40+
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
41+
ObjectWriter objectWriter = mapper.writer().withDefaultPrettyPrinter();
42+
String requestJson = objectWriter.writeValueAsString(customer);
43+
44+
this.mockMvc
45+
.perform(MockMvcRequestBuilders.post("/customers")
46+
.contentType(MEDIA_TYPE_JSON)
47+
.content(requestJson)
48+
)
49+
50+
.andExpect(MockMvcResultMatchers.status().isOk());
51+
}
52+
53+
@Test
54+
public void whenGetHttpRequesttoCustomers_thenStatusOK() throws Exception {
55+
this.mockMvc
56+
.perform(MockMvcRequestBuilders.get("/customers"))
57+
58+
.andExpect(MockMvcResultMatchers.content().contentType(MEDIA_TYPE_JSON))
59+
.andExpect(MockMvcResultMatchers.status().isOk());
60+
}
61+
}

0 commit comments

Comments
 (0)