Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Aug 30, 2023
1 parent 6958aa9 commit 61b1052
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 43 deletions.
2 changes: 1 addition & 1 deletion boot/src/main/java/com/example/demo/domain/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Post implements Serializable {
@Column(name = "title")
private String title;

@Column(name = "content")
@Column(name = "data")
private String content;

@CollectionTable(name = "post_labels", joinColumns = @JoinColumn(name = "post_id"))
Expand Down
10 changes: 10 additions & 0 deletions boot/src/main/java/com/example/demo/web/PaginatedResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.demo.web;

import java.util.List;

public record PaginatedResult<T>(List<T> data, Long count ) {

public static <E> PaginatedResult<E> of(List<E> content, Long count) {
return new PaginatedResult<>(content, count);
}
}
5 changes: 2 additions & 3 deletions boot/src/main/java/com/example/demo/web/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
Expand All @@ -28,14 +27,14 @@ public class PostController {
private final PostRepository posts;

@GetMapping(value = "", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Page<PostSummary>> getAll(@RequestParam(defaultValue = "") String q,
public ResponseEntity<PaginatedResult<PostSummary>> getAll(@RequestParam(defaultValue = "") String q,
@RequestParam(defaultValue = "") String status,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
var postStatus = StringUtils.hasText(status) ? Status.valueOf(status) : null;
var data = this.posts.findAll(Specifications.findByKeyword(q, postStatus), PageRequest.of(page, size))
.map(p -> new PostSummary(p.getTitle(), p.getCreatedAt()));
return ok(data);
return ok(PaginatedResult.of(data.getContent(), data.getTotalElements()));
}

@PostMapping(value = "", consumes = APPLICATION_JSON_VALUE)
Expand Down
1 change: 1 addition & 0 deletions boot/src/main/java/com/example/demo/web/PostSummary.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.demo.web;

import java.io.Serializable;
import java.time.LocalDateTime;

public record PostSummary(String title, LocalDateTime createdAt) {
Expand Down
2 changes: 1 addition & 1 deletion boot/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spring.mvc.problemdetails.enabled=true

# logging
logging.level.root=INFO
logging.level.web=DEBUG
logging.level.web=TRACE
logging.level.sql=TRACE
logging.level.com.example.demo=DEBUG
# SQL statements and parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void setup() {
@Test
public void testSaveAll() {
var data = List.of(
Post.builder().title("test").content("content").status(Status.PENDING_MODERATION).build(),
Post.builder().title("test").content("data").status(Status.PENDING_MODERATION).build(),
Post.builder().title("test1").content("content1").build());
data.forEach(this.posts::save);

Expand All @@ -65,7 +65,7 @@ public void testSaveAll() {
public void testSaveAllAndFindAll_QueryDSL() {

var data = List.of(
Post.builder().title("test").content("content").status(Status.PENDING_MODERATION).build(),
Post.builder().title("test").content("data").status(Status.PENDING_MODERATION).build(),
Post.builder().title("test1").content("content1").build());
data.forEach(this.posts::save);

Expand All @@ -81,7 +81,7 @@ public void testSaveAllAndFindAll_QueryDSL() {

@Test
public void testInsertAndQuery() {
var data = Post.builder().title("test").content("test content").status(Status.DRAFT).build();
var data = Post.builder().title("test").content("test data").status(Status.DRAFT).build();
var saved = this.posts.save(data);
this.posts.findById(saved.getId()).ifPresent(
p -> {
Expand All @@ -96,7 +96,7 @@ public void testInsertAndQuery() {

@Test
public void testInsertAndQuery_QueryDSL() {
var data = Post.builder().title("test").content("test content").status(Status.DRAFT).build();
var data = Post.builder().title("test").content("test data").status(Status.DRAFT).build();
var saved = this.posts.save(data);
this.posts.findOne(QPost.post.id.eq(saved.getId())).ifPresent(
p -> assertThat(p.getStatus()).isEqualTo(Status.DRAFT)
Expand All @@ -105,7 +105,7 @@ public void testInsertAndQuery_QueryDSL() {

@Test
public void testInsertAndQuery_QueryByExample() {
var data = Post.builder().title("test").content("test content").status(Status.DRAFT).build();
var data = Post.builder().title("test").content("test data").status(Status.DRAFT).build();
var saved = this.posts.save(data);
var probe = Post.builder().id(saved.getId()).build();
this.posts.findOne(Example.of(probe, ExampleMatcher.matching().withIgnorePaths("status"))).ifPresent(
Expand All @@ -116,7 +116,7 @@ public void testInsertAndQuery_QueryByExample() {
@Test
public void testLabels() {
var data = List.of(
Post.builder().title("test").content("content").labels(Set.of("java17", "spring6")).build(),
Post.builder().title("test").content("data").labels(Set.of("java17", "spring6")).build(),
Post.builder().title("test1").content("content1").labels(Set.of("spring6")).build());
data.forEach(this.posts::save);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void setup() {
@Test
public void testSaveAll() {
var data = List.of(
Post.builder().title("test").content("content").status(Status.PENDING_MODERATION).build(),
Post.builder().title("test").content("data").status(Status.PENDING_MODERATION).build(),
Post.builder().title("test1").content("content1").build());
this.posts.saveAllAndFlush(data);

Expand All @@ -74,7 +74,7 @@ public void testSaveAll() {

@Test
public void testInsertAndQuery() {
var data = Post.builder().title("test").content("test content").status(Status.DRAFT).build();
var data = Post.builder().title("test").content("test data").status(Status.DRAFT).build();
var saved = this.posts.save(data);
this.posts.findById(saved.getId()).ifPresent(
p -> assertThat(p.getStatus()).isEqualTo(Status.DRAFT)
Expand All @@ -85,7 +85,7 @@ public void testInsertAndQuery() {
@Test
@Disabled
public void testUpdateStatus() {
var data = Post.builder().title("test").content("test content").status(Status.DRAFT).build();
var data = Post.builder().title("test").content("test data").status(Status.DRAFT).build();
var saved = this.posts.save(data);
log.debug("saved post : {}", saved);

Expand Down
26 changes: 12 additions & 14 deletions boot/src/test/java/com/example/demo/web/PostControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -17,7 +16,6 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;
Expand Down Expand Up @@ -70,14 +68,14 @@ public void tetGetAllPosts() throws Exception {
when(this.posts.findAll(isA(Specification.class), isA(Pageable.class)))
.thenReturn(new PageImpl<Post>(
List.of(
Post.builder().title("test").content("content of test1").build(),
Post.builder().title("test2").content("content of test2").build()
Post.builder().title("test").content("data of test1").build(),
Post.builder().title("test2").content("data of test2").build()
)
)
);

this.rest.perform(get("/posts").accept(MediaType.APPLICATION_JSON))
.andExpectAll(status().isOk(), jsonPath("$.totalElements", equalTo(2)));
.andExpectAll(status().isOk(), jsonPath("$.count", equalTo(2)));

verify(this.posts, times(1)).findAll(isA(Specification.class), isA(Pageable.class));
verifyNoMoreInteractions(this.posts);
Expand All @@ -86,14 +84,14 @@ public void tetGetAllPosts() throws Exception {
@Test
public void testGetPostById() throws Exception {
when(this.posts.findById(any(UUID.class)))
.thenReturn(Optional.of(Post.builder().title("test").content("content of test").build()));
.thenReturn(Optional.of(Post.builder().title("test").content("data of test").build()));

var id = UUID.randomUUID();
this.rest.perform(get("/posts/{id}", id).accept(MediaType.APPLICATION_JSON))
.andExpectAll(
status().isOk(),
jsonPath("$.title", is("test")),
jsonPath("$.content", is("content of test"))
jsonPath("$.content", is("data of test"))
);

verify(this.posts, times(1)).findById(id);
Expand All @@ -117,9 +115,9 @@ public void testGetPostById_nonExisting() throws Exception {
public void testCreatePost() throws Exception {
var id = UUID.randomUUID();
when(this.posts.save(any(Post.class)))
.thenReturn(Post.builder().id(id).title("test").content("content of test").build());
.thenReturn(Post.builder().id(id).title("test").content("data of test").build());

var data = new CreatePostCommand("test post", "content of test");
var data = new CreatePostCommand("test post", "data of test");
this.rest.perform(post("/posts").content(objectMapper.writeValueAsBytes(data)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(header().exists("Location"))
Expand Down Expand Up @@ -148,11 +146,11 @@ public void testCreatePost_validationFailed() throws Exception {
public void testUpdatePost() throws Exception {
var id = UUID.randomUUID();
when(this.posts.findById(any(UUID.class)))
.thenReturn(Optional.of(Post.builder().id(id).title("test").content("content of test").build()));
.thenReturn(Optional.of(Post.builder().id(id).title("test").content("data of test").build()));
when(this.posts.save(any(Post.class)))
.thenReturn(Post.builder().id(id).title("updated test").content("updated content of test").build());
.thenReturn(Post.builder().id(id).title("updated test").content("updated data of test").build());

var data = new UpdatePostCommand("updated test", "updated content of test", Status.PUBLISHED);
var data = new UpdatePostCommand("updated test", "updated data of test", Status.PUBLISHED);
this.rest.perform(put("/posts/{id}", id).content(objectMapper.writeValueAsBytes(data)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());

Expand All @@ -167,9 +165,9 @@ public void testUpdatePost_nonExisting() throws Exception {
when(this.posts.findById(any(UUID.class)))
.thenReturn(Optional.ofNullable(null));
when(this.posts.save(any(Post.class)))
.thenReturn(Post.builder().id(id).title("updated test").content("updated content of test").build());
.thenReturn(Post.builder().id(id).title("updated test").content("updated data of test").build());

var data = new UpdatePostCommand("updated test", "updated content of test", Status.PUBLISHED);
var data = new UpdatePostCommand("updated test", "updated data of test", Status.PUBLISHED);
this.rest.perform(put("/posts/{id}", id).content(objectMapper.writeValueAsBytes(data)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void getAllPostsWillBeOk() throws Exception {
when(this.posts.findAll(isA(Specification.class), isA(Pageable.class)))
.thenReturn(new PageImpl<Post>(
List.of(
Post.builder().title("test").content("content of test1").build(),
Post.builder().title("test2").content("content of test2").build()
Post.builder().title("test").content("data of test1").build(),
Post.builder().title("test2").content("data of test2").build()
)
)
);
Expand All @@ -56,7 +56,7 @@ public void getAllPostsWillBeOk() throws Exception {
.uri("/posts")
.exchange()
.expectStatus().isOk()
.expectBody().jsonPath("$.totalElements").isEqualTo(2);
.expectBody().jsonPath("$.count").isEqualTo(2);

verify(this.posts, times(1)).findAll(isA(Specification.class), isA(Pageable.class));
verifyNoMoreInteractions(this.posts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public void tetGetAllPosts() throws Exception {
when(this.posts.findAll(isA(Specification.class), isA(Pageable.class)))
.thenReturn(new PageImpl<Post>(
List.of(
Post.builder().title("test").content("content of test1").build(),
Post.builder().title("test2").content("content of test2").build()
Post.builder().title("test").content("data of test1").build(),
Post.builder().title("test2").content("data of test2").build()
)
)
);
Expand All @@ -88,7 +88,7 @@ public void tetGetAllPosts() throws Exception {
.get("/posts")
.then()
.status(HttpStatus.OK)
.expect(jsonPath("$.totalElements", equalTo(2)));
.expect(jsonPath("$.count", equalTo(2)));
//@formatter:on

verify(this.posts, times(1)).findAll(isA(Specification.class), isA(Pageable.class));
Expand All @@ -98,7 +98,7 @@ public void tetGetAllPosts() throws Exception {
@Test
public void testGetPostById() throws Exception {
when(this.posts.findById(any(UUID.class)))
.thenReturn(Optional.of(Post.builder().title("test").content("content of test").build()));
.thenReturn(Optional.of(Post.builder().title("test").content("data of test").build()));

var id = UUID.randomUUID();

Expand All @@ -110,7 +110,7 @@ public void testGetPostById() throws Exception {
.then()
.status(HttpStatus.OK)
.expect(jsonPath("$.title", is("test")))
.expect(jsonPath("$.content", is("content of test")));
.expect(jsonPath("$.content", is("data of test")));
//@formatter:on

verify(this.posts, times(1)).findById(id);
Expand Down Expand Up @@ -141,9 +141,9 @@ public void testGetPostById_nonExisting() throws Exception {
public void testCreatePost() throws Exception {
var id = UUID.randomUUID();
when(this.posts.save(any(Post.class)))
.thenReturn(Post.builder().id(id).title("test").content("content of test").build());
.thenReturn(Post.builder().id(id).title("test").content("data of test").build());

var data = new CreatePostCommand("test post", "content of test");
var data = new CreatePostCommand("test post", "data of test");

//@formatter:off
given()
Expand Down Expand Up @@ -189,11 +189,11 @@ public void testCreatePost_validationFailed() throws Exception {
public void testUpdatePost() throws Exception {
var id = UUID.randomUUID();
when(this.posts.findById(any(UUID.class)))
.thenReturn(Optional.of(Post.builder().id(id).title("test").content("content of test").build()));
.thenReturn(Optional.of(Post.builder().id(id).title("test").content("data of test").build()));
when(this.posts.save(any(Post.class)))
.thenReturn(Post.builder().id(id).title("updated test").content("updated content of test").build());
.thenReturn(Post.builder().id(id).title("updated test").content("updated data of test").build());

var data = new UpdatePostCommand("updated test", "updated content of test", Status.PUBLISHED);
var data = new UpdatePostCommand("updated test", "updated data of test", Status.PUBLISHED);

//@formatter:off
given()
Expand All @@ -216,9 +216,9 @@ public void testUpdatePost_nonExisting() throws Exception {
when(this.posts.findById(any(UUID.class)))
.thenReturn(Optional.ofNullable(null));
when(this.posts.save(any(Post.class)))
.thenReturn(Post.builder().id(id).title("updated test").content("updated content of test").build());
.thenReturn(Post.builder().id(id).title("updated test").content("updated data of test").build());

var data = new UpdatePostCommand("updated test", "updated content of test", Status.PUBLISHED);
var data = new UpdatePostCommand("updated test", "updated data of test", Status.PUBLISHED);

//@formatter:off
given()
Expand Down

0 comments on commit 61b1052

Please sign in to comment.