get profile
This commit is contained in:
@@ -3,7 +3,6 @@ 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;
|
||||
@@ -56,7 +55,7 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
||||
email = "john@jacob.com";
|
||||
username = "johnjacob";
|
||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||
userFixture(email, username, defaultAvatar);
|
||||
userFixture();
|
||||
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));
|
||||
|
||||
@@ -52,7 +52,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
email = "john@jacob.com";
|
||||
username = "johnjacob";
|
||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||
userFixture(email, username, defaultAvatar);
|
||||
userFixture();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -58,7 +58,7 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
||||
email = "john@jacob.com";
|
||||
username = "johnjacob";
|
||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||
userFixture(email, username, defaultAvatar);
|
||||
userFixture();
|
||||
|
||||
article = new Article("title", "desc", "body", new String[]{"test", "java"}, user.getId());
|
||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||
@@ -126,7 +126,7 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
||||
|
||||
@Test
|
||||
public void should_delete_comment_success() throws Exception {
|
||||
when(commentRepository.findById(article.getId(), eq(comment.getId()))).thenReturn(Optional.of(comment));
|
||||
when(commentRepository.findById(eq(article.getId()), eq(comment.getId()))).thenReturn(Optional.of(comment));
|
||||
|
||||
given()
|
||||
.header("Authorization", "Token " + token)
|
||||
|
||||
@@ -34,7 +34,7 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
|
||||
email = "john@jacob.com";
|
||||
username = "johnjacob";
|
||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||
userFixture(email, username, defaultAvatar);
|
||||
userFixture();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
52
src/test/java/io/spring/api/ProfileApiTest.java
Normal file
52
src/test/java/io/spring/api/ProfileApiTest.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package io.spring.api;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.spring.application.profile.ProfileData;
|
||||
import io.spring.application.profile.ProfileQueryService;
|
||||
import io.spring.core.article.Article;
|
||||
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 static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class ProfileApiTest extends TestWithCurrentUser {
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
private Article article;
|
||||
private User anotherUser;
|
||||
|
||||
@MockBean
|
||||
private ProfileQueryService profileQueryService;
|
||||
private ProfileData profileData;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
RestAssured.port = port;
|
||||
userFixture();
|
||||
profileData = new ProfileData("id", "username", "bio", "img", false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_user_profile_success() throws Exception {
|
||||
when(profileQueryService.findByUsername(eq(profileData.getUsername()), eq(null)))
|
||||
.thenReturn(Optional.of(profileData));
|
||||
RestAssured.when()
|
||||
.get("/profiles/{username}", profileData.getUsername())
|
||||
.prettyPeek()
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("profile.username", equalTo(profileData.getUsername()));
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,18 @@ class TestWithCurrentUser {
|
||||
protected User user;
|
||||
protected UserData userData;
|
||||
protected String token;
|
||||
protected String email;
|
||||
protected String username;
|
||||
protected String defaultAvatar;
|
||||
|
||||
@Autowired
|
||||
protected JwtService jwtService;
|
||||
|
||||
protected void userFixture(String email, String username, String defaultAvatar) {
|
||||
protected void userFixture() {
|
||||
email = "john@jacob.com";
|
||||
username = "johnjacob";
|
||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||
|
||||
user = new User(email, username, "123", "", defaultAvatar);
|
||||
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.anyOf;
|
||||
@@ -61,6 +62,7 @@ public class ArticleQueryServiceTest {
|
||||
assertThat(fetched.isFavorited(), is(false));
|
||||
assertThat(fetched.getCreatedAt(), notNullValue());
|
||||
assertThat(fetched.getUpdatedAt(), notNullValue());
|
||||
assertThat(fetched.getTagList().contains("java"), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package io.spring.application.profile;
|
||||
|
||||
import io.spring.core.user.User;
|
||||
import io.spring.core.user.UserRepository;
|
||||
import io.spring.infrastructure.user.MyBatisUserRepository;
|
||||
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 java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@MybatisTest
|
||||
@Import({ProfileQueryService.class, MyBatisUserRepository.class})
|
||||
public class ProfileQueryServiceTest {
|
||||
@Autowired
|
||||
private ProfileQueryService profileQueryService;
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
public void should_fetch_profile_success() throws Exception {
|
||||
User currentUser = new User("a@test.com", "a", "123", "", "");
|
||||
User profileUser = new User("p@test.com", "p", "123", "", "");
|
||||
userRepository.save(profileUser);
|
||||
|
||||
Optional<ProfileData> optional = profileQueryService.findByUsername(profileUser.getUsername(), currentUser);
|
||||
assertThat(optional.isPresent(), is(true));
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@@ -26,7 +27,7 @@ public class MyBatisArticleFavoriteRepositoryTest {
|
||||
public void should_save_and_fetch_articleFavorite_success() throws Exception {
|
||||
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
|
||||
articleFavoriteRepository.save(articleFavorite);
|
||||
assertThat(articleFavoriteMapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()), is(true));
|
||||
assertThat(articleFavoriteMapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()), notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user