diff --git a/boot/src/main/java/com/example/demo/domain/model/Post.java b/boot/src/main/java/com/example/demo/domain/model/Post.java index c903632eb..94f3d091f 100644 --- a/boot/src/main/java/com/example/demo/domain/model/Post.java +++ b/boot/src/main/java/com/example/demo/domain/model/Post.java @@ -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")) diff --git a/boot/src/main/java/com/example/demo/web/PaginatedResult.java b/boot/src/main/java/com/example/demo/web/PaginatedResult.java new file mode 100644 index 000000000..b1796cacd --- /dev/null +++ b/boot/src/main/java/com/example/demo/web/PaginatedResult.java @@ -0,0 +1,10 @@ +package com.example.demo.web; + +import java.util.List; + +public record PaginatedResult(List data, Long count ) { + + public static PaginatedResult of(List content, Long count) { + return new PaginatedResult<>(content, count); + } +} diff --git a/boot/src/main/java/com/example/demo/web/PostController.java b/boot/src/main/java/com/example/demo/web/PostController.java index 99071205b..a7062a1dc 100644 --- a/boot/src/main/java/com/example/demo/web/PostController.java +++ b/boot/src/main/java/com/example/demo/web/PostController.java @@ -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; @@ -28,14 +27,14 @@ public class PostController { private final PostRepository posts; @GetMapping(value = "", produces = APPLICATION_JSON_VALUE) - public ResponseEntity> getAll(@RequestParam(defaultValue = "") String q, + public ResponseEntity> 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) diff --git a/boot/src/main/java/com/example/demo/web/PostSummary.java b/boot/src/main/java/com/example/demo/web/PostSummary.java index 971266de2..c08f75cc0 100644 --- a/boot/src/main/java/com/example/demo/web/PostSummary.java +++ b/boot/src/main/java/com/example/demo/web/PostSummary.java @@ -1,5 +1,6 @@ package com.example.demo.web; +import java.io.Serializable; import java.time.LocalDateTime; public record PostSummary(String title, LocalDateTime createdAt) { diff --git a/boot/src/main/resources/application.properties b/boot/src/main/resources/application.properties index cf490c5fa..75dc179cc 100644 --- a/boot/src/main/resources/application.properties +++ b/boot/src/main/resources/application.properties @@ -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 diff --git a/boot/src/test/java/com/example/demo/domain/PostRepositoryTest.java b/boot/src/test/java/com/example/demo/domain/PostRepositoryTest.java index 8b18ed1b9..934b77c18 100644 --- a/boot/src/test/java/com/example/demo/domain/PostRepositoryTest.java +++ b/boot/src/test/java/com/example/demo/domain/PostRepositoryTest.java @@ -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); @@ -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); @@ -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 -> { @@ -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) @@ -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( @@ -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); diff --git a/boot/src/test/java/com/example/demo/domain/PostRepositoryTestWithTestcontainers.java b/boot/src/test/java/com/example/demo/domain/PostRepositoryTestWithTestcontainers.java index 15d35882a..f8958d84b 100644 --- a/boot/src/test/java/com/example/demo/domain/PostRepositoryTestWithTestcontainers.java +++ b/boot/src/test/java/com/example/demo/domain/PostRepositoryTestWithTestcontainers.java @@ -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); @@ -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) @@ -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); diff --git a/boot/src/test/java/com/example/demo/web/PostControllerTest.java b/boot/src/test/java/com/example/demo/web/PostControllerTest.java index 274afe51f..83e0d5524 100644 --- a/boot/src/test/java/com/example/demo/web/PostControllerTest.java +++ b/boot/src/test/java/com/example/demo/web/PostControllerTest.java @@ -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; @@ -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; @@ -70,14 +68,14 @@ public void tetGetAllPosts() throws Exception { when(this.posts.findAll(isA(Specification.class), isA(Pageable.class))) .thenReturn(new PageImpl( 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); @@ -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); @@ -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")) @@ -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()); @@ -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()); diff --git a/boot/src/test/java/com/example/demo/web/PostControllerTestWithMockMvcWebTestClient.java b/boot/src/test/java/com/example/demo/web/PostControllerTestWithMockMvcWebTestClient.java index 8cae51b0f..d9c36ef7a 100644 --- a/boot/src/test/java/com/example/demo/web/PostControllerTestWithMockMvcWebTestClient.java +++ b/boot/src/test/java/com/example/demo/web/PostControllerTestWithMockMvcWebTestClient.java @@ -45,8 +45,8 @@ public void getAllPostsWillBeOk() throws Exception { when(this.posts.findAll(isA(Specification.class), isA(Pageable.class))) .thenReturn(new PageImpl( 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() ) ) ); @@ -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); diff --git a/boot/src/test/java/com/example/demo/web/PostControllerTestWithRestAssuredMockMvc.java b/boot/src/test/java/com/example/demo/web/PostControllerTestWithRestAssuredMockMvc.java index ac22ee8ae..b079e42d9 100644 --- a/boot/src/test/java/com/example/demo/web/PostControllerTestWithRestAssuredMockMvc.java +++ b/boot/src/test/java/com/example/demo/web/PostControllerTestWithRestAssuredMockMvc.java @@ -75,8 +75,8 @@ public void tetGetAllPosts() throws Exception { when(this.posts.findAll(isA(Specification.class), isA(Pageable.class))) .thenReturn(new PageImpl( 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() ) ) ); @@ -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)); @@ -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(); @@ -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); @@ -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() @@ -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() @@ -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() diff --git a/data-jpa/src/main/java/com/example/demo/web/PaginatedResult.java b/data-jpa/src/main/java/com/example/demo/web/PaginatedResult.java new file mode 100644 index 000000000..10d83a770 --- /dev/null +++ b/data-jpa/src/main/java/com/example/demo/web/PaginatedResult.java @@ -0,0 +1,6 @@ +package com.example.demo.web; + +import java.util.List; + +public record PaginatedResult(List data, Long count) { +} diff --git a/data-jpa/src/main/java/com/example/demo/web/PostController.java b/data-jpa/src/main/java/com/example/demo/web/PostController.java index 99071205b..8b88cbffb 100644 --- a/data-jpa/src/main/java/com/example/demo/web/PostController.java +++ b/data-jpa/src/main/java/com/example/demo/web/PostController.java @@ -28,14 +28,14 @@ public class PostController { private final PostRepository posts; @GetMapping(value = "", produces = APPLICATION_JSON_VALUE) - public ResponseEntity> getAll(@RequestParam(defaultValue = "") String q, + public ResponseEntity> 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(new PaginatedResult<>(data.getContent(), data.getTotalElements())); } @PostMapping(value = "", consumes = APPLICATION_JSON_VALUE) diff --git a/data-jpa/src/test/java/com/example/demo/web/PostControllerTest.java b/data-jpa/src/test/java/com/example/demo/web/PostControllerTest.java index db3c6d392..67f871104 100644 --- a/data-jpa/src/test/java/com/example/demo/web/PostControllerTest.java +++ b/data-jpa/src/test/java/com/example/demo/web/PostControllerTest.java @@ -87,7 +87,7 @@ public void tetGetAllPosts() throws Exception { ); 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); diff --git a/data-jpa/src/test/java/com/example/demo/web/PostControllerTestWithMockMvcWebTestClient.java b/data-jpa/src/test/java/com/example/demo/web/PostControllerTestWithMockMvcWebTestClient.java index 4e239747a..b1ad557f3 100644 --- a/data-jpa/src/test/java/com/example/demo/web/PostControllerTestWithMockMvcWebTestClient.java +++ b/data-jpa/src/test/java/com/example/demo/web/PostControllerTestWithMockMvcWebTestClient.java @@ -60,8 +60,8 @@ public void getAllPostsWillBeOk() throws Exception { .get() .uri("/posts") .exchange() - .expectStatus().isOk(); - // .expectBody().jsonPath("$.totalElements").isEqualTo(2); + .expectStatus().isOk() + .expectBody().jsonPath("$.count").isEqualTo(2); verify(this.posts, times(1)).findAll(isA(Specification.class), isA(Pageable.class)); verifyNoMoreInteractions(this.posts); diff --git a/declarative-http-client/pom.xml b/declarative-http-client/pom.xml index 9d7045774..99fffc9ed 100644 --- a/declarative-http-client/pom.xml +++ b/declarative-http-client/pom.xml @@ -225,7 +225,7 @@ com.github.tomakehurst wiremock-jre8 - 2.35.0 + 3.0.1 test diff --git a/rest-client/pom.xml b/rest-client/pom.xml index ad1f4500a..975a7cff8 100644 --- a/rest-client/pom.xml +++ b/rest-client/pom.xml @@ -225,7 +225,7 @@ com.github.tomakehurst wiremock-jre8 - 2.35.0 + 3.0.1 test