Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 45 additions & 50 deletions src/test/java/io/spring/api/ArticleApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest({ArticleApi.class})
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
@WebMvcTest({ ArticleApi.class })
@Import({ WebSecurityConfig.class, JacksonCustomizations.class })
public class ArticleApiTest extends TestWithCurrentUser {
@Autowired private MockMvc mvc;
@Autowired
private MockMvc mvc;

@MockBean private ArticleQueryService articleQueryService;
@MockBean
private ArticleQueryService articleQueryService;

@MockBean private ArticleRepository articleRepository;
@MockBean
private ArticleRepository articleRepository;

@MockBean ArticleCommandService articleCommandService;
@MockBean
ArticleCommandService articleCommandService;

@Override
@BeforeEach
Expand All @@ -56,14 +60,13 @@ public void setUp() throws Exception {
public void should_read_article_success() throws Exception {
String slug = "test-new-article";
DateTime time = new DateTime();
Article article =
new Article(
"Test New Article",
"Desc",
"Body",
Arrays.asList("java", "spring", "jpg"),
user.getId(),
time);
Article article = new Article(
"Test New Article",
"Desc",
"Body",
Arrays.asList("java", "spring", "jpg"),
user.getId(),
time);
ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user);

when(articleQueryService.findBySlug(eq(slug), eq(null))).thenReturn(Optional.of(articleData));
Expand All @@ -80,25 +83,21 @@ public void should_read_article_success() throws Exception {
@Test
public void should_404_if_article_not_found() throws Exception {
when(articleQueryService.findBySlug(anyString(), any())).thenReturn(Optional.empty());
RestAssuredMockMvc.when().get("/articles/not-exists").then().statusCode(404);
RestAssuredMockMvc.when().get("/articles/not-exists").then().statusCode(404).body("errors.body[0]", equalTo("article not found"));
}

@Test
public void should_update_article_content_success() throws Exception {
List<String> tagList = Arrays.asList("java", "spring", "jpg");

Article originalArticle =
new Article("old title", "old description", "old body", tagList, user.getId());
Article originalArticle = new Article("old title", "old description", "old body", tagList, user.getId());

Article updatedArticle =
new Article("new title", "new description", "new body", tagList, user.getId());
Article updatedArticle = new Article("new title", "new description", "new body", tagList, user.getId());

Map<String, Object> updateParam =
prepareUpdateParam(
updatedArticle.getTitle(), updatedArticle.getBody(), updatedArticle.getDescription());
Map<String, Object> updateParam = prepareUpdateParam(
updatedArticle.getTitle(), updatedArticle.getBody(), updatedArticle.getDescription());

ArticleData updatedArticleData =
TestHelper.getArticleDataFromArticleAndUser(updatedArticle, user);
ArticleData updatedArticleData = TestHelper.getArticleDataFromArticleAndUser(updatedArticle, user);

when(articleRepository.findBySlug(eq(originalArticle.getSlug())))
.thenReturn(Optional.of(originalArticle));
Expand Down Expand Up @@ -127,29 +126,27 @@ public void should_get_403_if_not_author_to_update_article() throws Exception {

User anotherUser = new User("test@test.com", "test", "123123", "", "");

Article article =
new Article(
title, description, body, Arrays.asList("java", "spring", "jpg"), anotherUser.getId());
Article article = new Article(
title, description, body, Arrays.asList("java", "spring", "jpg"), anotherUser.getId());

DateTime time = new DateTime();
ArticleData articleData =
new ArticleData(
article.getId(),
article.getSlug(),
article.getTitle(),
article.getDescription(),
article.getBody(),
false,
0,
time,
time,
Arrays.asList("joda"),
new ProfileData(
anotherUser.getId(),
anotherUser.getUsername(),
anotherUser.getBio(),
anotherUser.getImage(),
false));
ArticleData articleData = new ArticleData(
article.getId(),
article.getSlug(),
article.getTitle(),
article.getDescription(),
article.getBody(),
false,
0,
time,
time,
Arrays.asList("joda"),
new ProfileData(
anotherUser.getId(),
anotherUser.getUsername(),
anotherUser.getBio(),
anotherUser.getImage(),
false));

when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user)))
Expand All @@ -171,8 +168,7 @@ public void should_delete_article_success() throws Exception {
String body = "body";
String description = "description";

Article article =
new Article(title, description, body, Arrays.asList("java", "spring", "jpg"), user.getId());
Article article = new Article(title, description, body, Arrays.asList("java", "spring", "jpg"), user.getId());
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));

given()
Expand All @@ -193,9 +189,8 @@ public void should_403_if_not_author_delete_article() throws Exception {

User anotherUser = new User("test@test.com", "test", "123123", "", "");

Article article =
new Article(
title, description, body, Arrays.asList("java", "spring", "jpg"), anotherUser.getId());
Article article = new Article(
title, description, body, Arrays.asList("java", "spring", "jpg"), anotherUser.getId());

when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
given()
Expand Down