Added Embedded Database for persistence

This commit is contained in:
jesus-dayo 2016-08-15 07:42:58 +08:00
parent e479614eaa
commit e4bc7713f5
7 changed files with 63 additions and 58 deletions

View File

@ -1,39 +0,0 @@
package org.baeldung.mock;
import java.util.ArrayList;
import java.util.List;
import org.baeldung.web.entity.Student;
public class MockStudentData {
private static List<Student> studentList = new ArrayList<>();
// static {
// studentList.add(new Student("1", "Bryan", "Male", 20));
// studentList.add(new Student("2", "Ben", "Male", 22));
// studentList.add(new Student("3", "Lisa", "Female", 24));
// studentList.add(new Student("4", "Sarah", "Female", 26));
// studentList.add(new Student("5", "Jay", "Male", 20));
// studentList.add(new Student("6", "John", "Male", 22));
// studentList.add(new Student("7", "Jordan", "Male", 24));
// studentList.add(new Student("8", "Rob", "Male", 26));
// studentList.add(new Student("9", "Will", "Male", 20));
// studentList.add(new Student("10", "Shawn", "Male", 22));
// studentList.add(new Student("11", "Taylor", "Female", 24));
// studentList.add(new Student("12", "Venus", "Female", 26));
// studentList.add(new Student("13", "Vince", "Male", 20));
// studentList.add(new Student("14", "Carol", "Female", 22));
// studentList.add(new Student("15", "Joana", "Female", 24));
// studentList.add(new Student("16", "Dion", "Male", 26));
// studentList.add(new Student("17", "Evans", "Male", 20));
// studentList.add(new Student("18", "Bart", "Male", 22));
// studentList.add(new Student("19", "Jenny", "Female", 24));
// studentList.add(new Student("20", "Kristine", "Female", 26));
// }
public static List<Student> getMockDataStudents(){
return studentList;
}
}

View File

@ -12,7 +12,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@EnableJpaRepositories("org.baeldung")
@EnableJpaRepositories("org.baeldung.web.dao")
@ComponentScan(basePackages = { "org.baeldung.web" })
@EntityScan("org.baeldung.web.entity")
@Configuration
@ -25,7 +25,6 @@ public class PersistenceConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.HSQL)

View File

@ -1,6 +1,7 @@
package org.baeldung.web.rest;
import org.baeldung.web.entity.Student;
import org.baeldung.web.exception.MyResourceNotFoundException;
import org.baeldung.web.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
@ -15,11 +16,14 @@ public class StudentDirectoryRestController {
@Autowired
private StudentService service;
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET, produces = "application/json")
public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){
@RequestMapping(value = "/student/get", params = { "page",
"size" }, method = RequestMethod.GET, produces = "application/json")
public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size) {
Page<Student> resultPage = service.findPaginated(page, size);
if (page > resultPage.getTotalPages()) {
throw new MyResourceNotFoundException();
}
return resultPage;
}

View File

@ -1,3 +1,4 @@
server.contextPath=/
spring.h2.console.enabled=true
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.SQL=trace
spring.jpa.hibernate.ddl-auto=none

View File

@ -6,4 +6,42 @@ CREATE TABLE student (
);
INSERT INTO student (id,name,gender,age)
VALUES (1,'Bryan', 'Male',20);
VALUES (1,'Bryan', 'Male',20);
INSERT INTO student (id,name,gender,age)
VALUES (2, 'Ben', 'Male', 22);
INSERT INTO student (id,name,gender,age)
VALUES (3,'Lisa', 'Female',24);
INSERT INTO student (id,name,gender,age)
VALUES (4,'Sarah', 'Female',20);
INSERT INTO student (id,name,gender,age)
VALUES (5,'Jay', 'Male',20);
INSERT INTO student (id,name,gender,age)
VALUES (6,'John', 'Male',22);
INSERT INTO student (id,name,gender,age)
VALUES (7,'Jordan', 'Male',24);
INSERT INTO student (id,name,gender,age)
VALUES (8,'Rob', 'Male',26);
INSERT INTO student (id,name,gender,age)
VALUES (9,'Will', 'Male',20);
INSERT INTO student (id,name,gender,age)
VALUES (10,'Shawn', 'Male',22);
INSERT INTO student (id,name,gender,age)
VALUES (11,'Taylor', 'Female',24);
INSERT INTO student (id,name,gender,age)
VALUES (12,'Venus', 'Female',26);
INSERT INTO student (id,name,gender,age)
VALUES (13,'Vince', 'Male',20);
INSERT INTO student (id,name,gender,age)
VALUES (14,'Carol', 'Female',22);
INSERT INTO student (id,name,gender,age)
VALUES (15,'Joana', 'Female',24);
INSERT INTO student (id,name,gender,age)
VALUES (16,'Dion', 'Male',26);
INSERT INTO student (id,name,gender,age)
VALUES (17,'Evans', 'Male',20);
INSERT INTO student (id,name,gender,age)
VALUES (18,'Bart', 'Male',22);
INSERT INTO student (id,name,gender,age)
VALUES (19,'Jenny', 'Female',24);
INSERT INTO student (id,name,gender,age)
VALUES (20,'Kristine', 'Female',26);

View File

@ -19,7 +19,7 @@ app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,Stude
enableColumnMenus:false,
useExternalPagination: true,
columnDefs: [
{ name: 'studentId' },
{ name: 'id' },
{ name: 'name' },
{ name: 'gender' },
{ name: 'age' }
@ -42,6 +42,7 @@ app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,Stude
app.service('StudentService',['$http', function ($http) {
function getStudents(pageNumber,size) {
pageNumber = pageNumber > 0?pageNumber - 1:0;
return $http({
method: 'GET',
url: 'student/get?page='+pageNumber+'&size='+size

View File

@ -9,9 +9,10 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.hamcrest.core.IsEqual.equalTo;
import static io.restassured.RestAssured.*;
import static org.hamcrest.core.IsCollectionContaining.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ -23,28 +24,28 @@ public class StudentServiceTest {
@Test
public void givenRequestForStudents_whenPageIsOne_expectContainsNames() {
given().params("page", "1", "size", "2").get(ENDPOINT)
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.assertThat().body("content.name", hasItems("Bryan", "Ben"));
}
@Test
public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() {
given().params("page", "1", "size", "2").get(ENDPOINT)
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.assertThat().body("size", equalTo(2));
}
@Test
public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() {
given().params("page", "1", "size", "2").get(ENDPOINT)
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.assertThat().body("numberOfElements", equalTo(2));
}
@Test
public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() {
given().params("page", "1", "size", "2").get(ENDPOINT)
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.statusCode(200);
}
@ -64,15 +65,15 @@ public class StudentServiceTest {
}
@Test
public void givenRequestForStudents_whenPageIsFive_expectFiveItems() {
given().params("page", "1", "size", "5").get(ENDPOINT)
public void givenRequestForStudents_whenPageSizeIsFive_expectFiveItems() {
given().params("page", "0", "size", "5").get(ENDPOINT)
.then()
.body("content.studentId.max()", equalTo("5"));
.body("content.size()", is(5));
}
@Test
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
given().params("page", "1", "size", "2").get(ENDPOINT)
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.assertThat().body("first", equalTo(true));
}