should favorite article
This commit is contained in:
92
src/test/java/io/spring/api/ArticleFavoriteApiTest.java
Normal file
92
src/test/java/io/spring/api/ArticleFavoriteApiTest.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package io.spring.api;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.spring.application.article.ArticleData;
|
||||
import io.spring.application.article.ArticleQueryService;
|
||||
import io.spring.application.article.ArticleReadService;
|
||||
import io.spring.application.profile.ProfileData;
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.article.Tag;
|
||||
import io.spring.core.favorite.ArticleFavoriteRepository;
|
||||
import io.spring.core.user.User;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@RunWith(SpringRunner.class)
|
||||
public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
||||
@MockBean
|
||||
private ArticleFavoriteRepository articleFavoriteRepository;
|
||||
|
||||
@MockBean
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@MockBean
|
||||
private ArticleQueryService articleQueryService;
|
||||
|
||||
protected String email;
|
||||
protected String username;
|
||||
protected String defaultAvatar;
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
private Article article;
|
||||
private User anotherUser;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
RestAssured.port = port;
|
||||
email = "john@jacob.com";
|
||||
username = "johnjacob";
|
||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||
userFixture(email, username, defaultAvatar);
|
||||
anotherUser = new User("other@test.com", "other", "123", "", "");
|
||||
article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId());
|
||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||
ArticleData articleData = new ArticleData(
|
||||
article.getId(),
|
||||
article.getSlug(),
|
||||
article.getTitle(),
|
||||
article.getDescription(),
|
||||
article.getBody(),
|
||||
true,
|
||||
1,
|
||||
article.getCreatedAt(),
|
||||
article.getUpdatedAt(),
|
||||
article.getTags().stream().map(Tag::getName).collect(Collectors.toList()),
|
||||
new ProfileData(
|
||||
anotherUser.getId(),
|
||||
anotherUser.getUsername(),
|
||||
anotherUser.getBio(),
|
||||
anotherUser.getImage(),
|
||||
false
|
||||
));
|
||||
when(articleQueryService.findBySlug(eq(articleData.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_favorite_an_article_success() throws Exception {
|
||||
given()
|
||||
.header("Authorization", "Token " + token)
|
||||
.when()
|
||||
.post("/articles/{slug}/favorite", article.getSlug())
|
||||
.prettyPeek()
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("article.id", equalTo(article.getId()));
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,14 @@ package io.spring.application.article;
|
||||
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.favorite.ArticleFavorite;
|
||||
import io.spring.core.favorite.ArticleFavoriteRepository;
|
||||
import io.spring.core.user.User;
|
||||
import io.spring.core.user.UserRepository;
|
||||
import io.spring.infrastructure.article.MyBatisArticleRepository;
|
||||
import io.spring.infrastructure.favorite.MyBatisArticleFavoriteRepository;
|
||||
import io.spring.infrastructure.user.MyBatisUserRepository;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
|
||||
@@ -15,13 +19,14 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.anyOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@MybatisTest
|
||||
@Import({ArticleQueryService.class, MyBatisUserRepository.class, MyBatisArticleRepository.class})
|
||||
@Import({ArticleQueryService.class, MyBatisUserRepository.class, MyBatisArticleRepository.class, MyBatisArticleFavoriteRepository.class})
|
||||
public class ArticleQueryServiceTest {
|
||||
@Autowired
|
||||
private ArticleQueryService queryService;
|
||||
@@ -32,13 +37,22 @@ public class ArticleQueryServiceTest {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private ArticleFavoriteRepository articleFavoriteRepository;
|
||||
|
||||
private User user;
|
||||
private Article article;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
||||
userRepository.save(user);
|
||||
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||
articleRepository.save(article);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_fetch_article_success() throws Exception {
|
||||
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
||||
userRepository.save(user);
|
||||
|
||||
Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||
articleRepository.save(article);
|
||||
|
||||
Optional<ArticleData> optional = queryService.findById(article.getId(), user);
|
||||
assertThat(optional.isPresent(), is(true));
|
||||
@@ -48,4 +62,15 @@ public class ArticleQueryServiceTest {
|
||||
assertThat(fetched.getCreatedAt(), notNullValue());
|
||||
assertThat(fetched.getUpdatedAt(), notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_article_with_right_favorite_and_favorite_count() throws Exception {
|
||||
User anotherUser = new User("other@test.com", "other", "123", "", "");
|
||||
userRepository.save(anotherUser);
|
||||
articleFavoriteRepository.save(new ArticleFavorite(article.getId(), anotherUser.getId()));
|
||||
|
||||
ArticleData articleData = queryService.findById(article.getId(), anotherUser).get();
|
||||
assertThat(articleData.getFavoritesCount(), is(1));
|
||||
assertThat(articleData.isFavorited(), is(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.spring.infrastructure.favorite;
|
||||
|
||||
import io.spring.core.favorite.ArticleFavorite;
|
||||
import io.spring.core.favorite.ArticleFavoriteRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@MybatisTest
|
||||
@Import({MyBatisArticleFavoriteRepository.class})
|
||||
public class MyBatisArticleFavoriteRepositoryTest {
|
||||
@Autowired
|
||||
private ArticleFavoriteRepository articleFavoriteRepository;
|
||||
|
||||
@Autowired
|
||||
private ArticleFavoriteMapper articleFavoriteMapper;
|
||||
|
||||
@Test
|
||||
public void should_save_and_fetch_articleFavorite_success() throws Exception {
|
||||
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
|
||||
articleFavoriteRepository.save(articleFavorite);
|
||||
assertThat(articleFavoriteMapper.find(articleFavorite), is(true));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user