code format

This commit is contained in:
jesus-dayo 2016-08-15 13:44:56 +08:00
parent 4fff6716e5
commit a9d714c389
10 changed files with 91 additions and 113 deletions

View File

@ -9,61 +9,61 @@ import javax.persistence.Id;
@Entity
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
private static final long serialVersionUID = 1L;
public Student() {
}
public Student() {
}
public Student(long id, String name, String gender, Integer age) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public Student(long id, String name, String gender, Integer age) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
@Id
private long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String gender;
@Column(nullable = false)
private Integer age;
@Id
private long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String gender;
@Column(nullable = false)
private Integer age;
public long getId() {
return id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void setAge(Integer age) {
this.age = age;
}
}

View File

@ -3,11 +3,11 @@ package org.baeldung.web.exception;
public class MyResourceNotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 4088649120307193208L;
*
*/
private static final long serialVersionUID = 4088649120307193208L;
public MyResourceNotFoundException() {
public MyResourceNotFoundException() {
super();
}
@ -23,5 +23,4 @@ public class MyResourceNotFoundException extends RuntimeException {
super(cause);
}
}

View File

@ -13,7 +13,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
@Import(PersistenceConfig.class)
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@ -22,5 +21,5 @@ public class Application extends WebMvcConfigurerAdapter {
public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
return new ShallowEtagHeaderFilter();
}
}

View File

@ -14,23 +14,20 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@EnableJpaRepositories("org.baeldung.web.dao")
@ComponentScan(basePackages = { "org.baeldung.web" })
@EntityScan("org.baeldung.web.entity")
@EntityScan("org.baeldung.web.entity")
@Configuration
public class PersistenceConfig {
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.HSQL)
.addScript("db/sql/data.sql")
.build();
return db;
}
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.sql").build();
return db;
}
}

View File

@ -13,18 +13,17 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentDirectoryRestController {
@Autowired
private StudentService service;
@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;
}
Page<Student> resultPage = service.findPaginated(page, size);
if (page > resultPage.getTotalPages()) {
throw new MyResourceNotFoundException();
}
return resultPage;
}
}

View File

@ -4,6 +4,6 @@ import org.springframework.data.domain.Page;
public interface IOperations<T> {
public Page<T> findPaginated(final int page, final int size);
public Page<T> findPaginated(final int page, final int size);
}

View File

@ -2,6 +2,6 @@ package org.baeldung.web.service;
import org.baeldung.web.entity.Student;
public interface StudentService extends IOperations<Student>{
public interface StudentService extends IOperations<Student> {
}

View File

@ -10,12 +10,12 @@ import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository dao;
@Override
public Page<Student> findPaginated(int page, int size) {
return dao.findAll(new PageRequest(page,size));
}
@Autowired
private StudentRepository dao;
@Override
public Page<Student> findPaginated(int page, int size) {
return dao.findAll(new PageRequest(page, size));
}
}

View File

@ -3,7 +3,7 @@
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

View File

@ -24,58 +24,42 @@ public class StudentServiceTest {
@Test
public void givenRequestForStudents_whenPageIsOne_expectContainsNames() {
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.assertThat().body("content.name", hasItems("Bryan", "Ben"));
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", "0", "size", "2").get(ENDPOINT)
.then()
.assertThat().body("size", equalTo(2));
given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("size", equalTo(2));
}
@Test
public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() {
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.assertThat().body("numberOfElements", equalTo(2));
given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("numberOfElements", equalTo(2));
}
@Test
public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() {
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.statusCode(200);
given().params("page", "0", "size", "2").get(ENDPOINT).then().statusCode(200);
}
@Test
public void givenRequestForStudents_whenPageOfResourcesAreRetrievedOutOfBounds_thenExpect500() {
given().params("page", "1000", "size", "2").get(ENDPOINT)
.then()
.statusCode(500);
given().params("page", "1000", "size", "2").get(ENDPOINT).then().statusCode(500);
}
@Test
public void givenRequestForStudents_whenPageNotValid_thenExpect500() {
given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT)
.then()
.statusCode(500);
given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT).then().statusCode(500);
}
@Test
public void givenRequestForStudents_whenPageSizeIsFive_expectFiveItems() {
given().params("page", "0", "size", "5").get(ENDPOINT)
.then()
.body("content.size()", is(5));
given().params("page", "0", "size", "5").get(ENDPOINT).then().body("content.size()", is(5));
}
@Test
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
given().params("page", "0", "size", "2").get(ENDPOINT)
.then()
.assertThat().body("first", equalTo(true));
given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("first", equalTo(true));
}
}