-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* polish * feat : adds new testcases * fix input type * fix wildcard import
- Loading branch information
1 parent
186b678
commit c4b3134
Showing
8 changed files
with
169 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 17 additions & 1 deletion
18
...read-replica-postgresql/src/main/java/com/example/demo/readreplica/domain/ArticleDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
package com.example.demo.readreplica.domain; | ||
|
||
import static com.example.demo.readreplica.domain.CommentDTO.convertToComment; | ||
|
||
import com.example.demo.readreplica.entities.Article; | ||
import com.example.demo.readreplica.entities.Comment; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public record ArticleDTO( | ||
String title, | ||
LocalDateTime authored, | ||
LocalDateTime published, | ||
List<CommentDTO> commentDTOs) {} | ||
List<CommentDTO> commentDTOs) { | ||
|
||
public Article convertToArticle() { | ||
Article article = | ||
new Article().setAuthored(authored).setTitle(title).setPublished(published); | ||
commentDTOs.forEach( | ||
commentDTO -> { | ||
Comment comment = convertToComment(commentDTO); | ||
article.addComment(comment); | ||
}); | ||
return article; | ||
} | ||
} |
9 changes: 8 additions & 1 deletion
9
...read-replica-postgresql/src/main/java/com/example/demo/readreplica/domain/CommentDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
package com.example.demo.readreplica.domain; | ||
|
||
public record CommentDTO(String comment) {} | ||
import com.example.demo.readreplica.entities.Comment; | ||
|
||
public record CommentDTO(String comment) { | ||
|
||
static Comment convertToComment(CommentDTO commentDTO) { | ||
return new Comment().setComment(commentDTO.comment()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...resql/src/test/java/com/example/demo/readreplica/controller/ArticleControllerIntTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.example.demo.readreplica.controller; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.example.demo.readreplica.domain.ArticleDTO; | ||
import com.example.demo.readreplica.domain.CommentDTO; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.assertj.MockMvcTester; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
class ArticleControllerIntTest { | ||
|
||
@Autowired private MockMvcTester mvcTester; | ||
|
||
@Autowired private ObjectMapper objectMapper; | ||
|
||
@Test | ||
void findArticleById() { | ||
|
||
mvcTester | ||
.get() | ||
.uri("/articles/1") | ||
.assertThat() | ||
.hasStatusOk() | ||
.hasContentType(MediaType.APPLICATION_JSON) | ||
.bodyJson() | ||
.convertTo(ArticleDTO.class) | ||
.satisfies( | ||
articleDTO -> { | ||
assertThat(articleDTO.title()) | ||
.isNotNull() | ||
.isEqualTo("Waiter! There is a bug in my JSoup!"); | ||
assertThat(articleDTO.authored()) | ||
.isNotNull() | ||
.isInstanceOf(LocalDateTime.class); | ||
assertThat(articleDTO.published()) | ||
.isNotNull() | ||
.isInstanceOf(LocalDateTime.class); | ||
assertThat(articleDTO.commentDTOs()) | ||
.isNotNull() | ||
.hasSize(2) | ||
.hasOnlyElementsOfType(CommentDTO.class); | ||
}); | ||
} | ||
|
||
@Test | ||
void saveArticleRetriveAndDelete() throws JsonProcessingException { | ||
ArticleDTO articleDTO = | ||
new ArticleDTO( | ||
"junitTitle", | ||
LocalDateTime.now().minusDays(1), | ||
LocalDateTime.now(), | ||
List.of(new CommentDTO("junitComment"))); | ||
AtomicReference<String> location = new AtomicReference<>(); | ||
mvcTester | ||
.post() | ||
.uri("/articles/") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(articleDTO)) | ||
.assertThat() | ||
.hasStatus(HttpStatus.CREATED) | ||
.matches( | ||
result -> { | ||
location.set(result.getResponse().getHeader("Location")); | ||
assertThat(location.get()).isNotBlank().contains("/articles/"); | ||
}); | ||
|
||
mvcTester | ||
.get() | ||
.uri(location.get()) | ||
.assertThat() | ||
.hasStatusOk() | ||
.hasContentType(MediaType.APPLICATION_JSON) | ||
.bodyJson() | ||
.convertTo(ArticleDTO.class) | ||
.satisfies( | ||
response -> { | ||
assertThat(response.title()).isNotNull().isEqualTo("junitTitle"); | ||
assertThat(response.authored()) | ||
.isNotNull() | ||
.isInstanceOf(LocalDateTime.class); | ||
assertThat(response.published()) | ||
.isNotNull() | ||
.isInstanceOf(LocalDateTime.class); | ||
assertThat(response.commentDTOs()) | ||
.isNotNull() | ||
.hasSize(1) | ||
.hasOnlyElementsOfType(CommentDTO.class); | ||
}); | ||
|
||
mvcTester.delete().uri(location.get()).assertThat().hasStatus(HttpStatus.ACCEPTED); | ||
} | ||
} |