Added Embedded Database for persistence
This commit is contained in:
parent
e479614eaa
commit
e4bc7713f5
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -12,7 +12,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
|||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
|
||||||
@EnableJpaRepositories("org.baeldung")
|
@EnableJpaRepositories("org.baeldung.web.dao")
|
||||||
@ComponentScan(basePackages = { "org.baeldung.web" })
|
@ComponentScan(basePackages = { "org.baeldung.web" })
|
||||||
@EntityScan("org.baeldung.web.entity")
|
@EntityScan("org.baeldung.web.entity")
|
||||||
@Configuration
|
@Configuration
|
||||||
@ -25,7 +25,6 @@ public class PersistenceConfig {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
|
|
||||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
||||||
EmbeddedDatabase db = builder
|
EmbeddedDatabase db = builder
|
||||||
.setType(EmbeddedDatabaseType.HSQL)
|
.setType(EmbeddedDatabaseType.HSQL)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package org.baeldung.web.rest;
|
package org.baeldung.web.rest;
|
||||||
|
|
||||||
import org.baeldung.web.entity.Student;
|
import org.baeldung.web.entity.Student;
|
||||||
|
import org.baeldung.web.exception.MyResourceNotFoundException;
|
||||||
import org.baeldung.web.service.StudentService;
|
import org.baeldung.web.service.StudentService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
@ -15,11 +16,14 @@ public class StudentDirectoryRestController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private StudentService service;
|
private StudentService service;
|
||||||
|
|
||||||
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET, produces = "application/json")
|
@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) {
|
public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size) {
|
||||||
|
|
||||||
Page<Student> resultPage = service.findPaginated(page, size);
|
Page<Student> resultPage = service.findPaginated(page, size);
|
||||||
|
if (page > resultPage.getTotalPages()) {
|
||||||
|
throw new MyResourceNotFoundException();
|
||||||
|
}
|
||||||
return resultPage;
|
return resultPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
server.contextPath=/
|
server.contextPath=/
|
||||||
spring.h2.console.enabled=true
|
spring.h2.console.enabled=true
|
||||||
logging.level.org.hibernate.SQL=debug
|
logging.level.org.hibernate.SQL=trace
|
||||||
|
spring.jpa.hibernate.ddl-auto=none
|
@ -7,3 +7,41 @@ CREATE TABLE student (
|
|||||||
|
|
||||||
INSERT INTO student (id,name,gender,age)
|
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);
|
@ -19,7 +19,7 @@ app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,Stude
|
|||||||
enableColumnMenus:false,
|
enableColumnMenus:false,
|
||||||
useExternalPagination: true,
|
useExternalPagination: true,
|
||||||
columnDefs: [
|
columnDefs: [
|
||||||
{ name: 'studentId' },
|
{ name: 'id' },
|
||||||
{ name: 'name' },
|
{ name: 'name' },
|
||||||
{ name: 'gender' },
|
{ name: 'gender' },
|
||||||
{ name: 'age' }
|
{ name: 'age' }
|
||||||
@ -42,6 +42,7 @@ app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,Stude
|
|||||||
app.service('StudentService',['$http', function ($http) {
|
app.service('StudentService',['$http', function ($http) {
|
||||||
|
|
||||||
function getStudents(pageNumber,size) {
|
function getStudents(pageNumber,size) {
|
||||||
|
pageNumber = pageNumber > 0?pageNumber - 1:0;
|
||||||
return $http({
|
return $http({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: 'student/get?page='+pageNumber+'&size='+size
|
url: 'student/get?page='+pageNumber+'&size='+size
|
||||||
|
@ -9,9 +9,10 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.RestAssured.*;
|
||||||
import static org.hamcrest.core.IsCollectionContaining.hasItems;
|
import static org.hamcrest.core.IsCollectionContaining.*;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.Is.*;
|
||||||
|
import static org.hamcrest.core.IsEqual.*;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = Application.class)
|
@SpringApplicationConfiguration(classes = Application.class)
|
||||||
@ -23,28 +24,28 @@ public class StudentServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRequestForStudents_whenPageIsOne_expectContainsNames() {
|
public void givenRequestForStudents_whenPageIsOne_expectContainsNames() {
|
||||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
given().params("page", "0", "size", "2").get(ENDPOINT)
|
||||||
.then()
|
.then()
|
||||||
.assertThat().body("content.name", hasItems("Bryan", "Ben"));
|
.assertThat().body("content.name", hasItems("Bryan", "Ben"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() {
|
public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() {
|
||||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
given().params("page", "0", "size", "2").get(ENDPOINT)
|
||||||
.then()
|
.then()
|
||||||
.assertThat().body("size", equalTo(2));
|
.assertThat().body("size", equalTo(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() {
|
public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() {
|
||||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
given().params("page", "0", "size", "2").get(ENDPOINT)
|
||||||
.then()
|
.then()
|
||||||
.assertThat().body("numberOfElements", equalTo(2));
|
.assertThat().body("numberOfElements", equalTo(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() {
|
public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() {
|
||||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
given().params("page", "0", "size", "2").get(ENDPOINT)
|
||||||
.then()
|
.then()
|
||||||
.statusCode(200);
|
.statusCode(200);
|
||||||
}
|
}
|
||||||
@ -64,15 +65,15 @@ public class StudentServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRequestForStudents_whenPageIsFive_expectFiveItems() {
|
public void givenRequestForStudents_whenPageSizeIsFive_expectFiveItems() {
|
||||||
given().params("page", "1", "size", "5").get(ENDPOINT)
|
given().params("page", "0", "size", "5").get(ENDPOINT)
|
||||||
.then()
|
.then()
|
||||||
.body("content.studentId.max()", equalTo("5"));
|
.body("content.size()", is(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
|
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
|
||||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
given().params("page", "0", "size", "2").get(ENDPOINT)
|
||||||
.then()
|
.then()
|
||||||
.assertThat().body("first", equalTo(true));
|
.assertThat().body("first", equalTo(true));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user