From 13571f2f5ebc022a86a2b8e997654f12dfab165e Mon Sep 17 00:00:00 2001 From: aisensiy Date: Wed, 16 Aug 2017 15:58:51 +0800 Subject: [PATCH] get profile --- src/main/java/io/spring/api/ProfileApi.java | 40 ++++++++++++++ .../api/security/WebSecurityConfig.java | 2 +- .../profile/ProfileQueryService.java | 36 +++++++++++++ .../application/user/UserReadService.java | 1 + .../MyBatisArticleFavoriteRepository.java | 2 +- src/main/resources/application.properties | 3 +- .../resources/mapper/ArticleReadService.xml | 8 +-- .../io/spring/api/ArticleFavoriteApiTest.java | 3 +- .../java/io/spring/api/ArticlesApiTest.java | 2 +- .../java/io/spring/api/CommentsApiTest.java | 4 +- .../io/spring/api/CurrentUserApiTest.java | 2 +- .../java/io/spring/api/ProfileApiTest.java | 52 +++++++++++++++++++ .../io/spring/api/TestWithCurrentUser.java | 9 +++- .../article/ArticleQueryServiceTest.java | 2 + .../profile/ProfileQueryServiceTest.java | 36 +++++++++++++ .../MyBatisArticleFavoriteRepositoryTest.java | 3 +- 16 files changed, 191 insertions(+), 14 deletions(-) create mode 100644 src/main/java/io/spring/api/ProfileApi.java create mode 100644 src/main/java/io/spring/application/profile/ProfileQueryService.java create mode 100644 src/test/java/io/spring/api/ProfileApiTest.java create mode 100644 src/test/java/io/spring/application/profile/ProfileQueryServiceTest.java diff --git a/src/main/java/io/spring/api/ProfileApi.java b/src/main/java/io/spring/api/ProfileApi.java new file mode 100644 index 0000000..ec55dab --- /dev/null +++ b/src/main/java/io/spring/api/ProfileApi.java @@ -0,0 +1,40 @@ +package io.spring.api; + +import io.spring.api.exception.ResourceNotFoundException; +import io.spring.application.profile.ProfileData; +import io.spring.application.profile.ProfileQueryService; +import io.spring.core.user.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; + +@RestController +@RequestMapping(path = "profiles/{username}") +public class ProfileApi { + private ProfileQueryService profileQueryService; + + @Autowired + public ProfileApi(ProfileQueryService profileQueryService) { + this.profileQueryService = profileQueryService; + } + + @GetMapping + public ResponseEntity getProfile(@PathVariable("username") String username, + @AuthenticationPrincipal User user) { + return profileQueryService.findByUsername(username, user) + .map(this::profileResponse) + .orElseThrow(ResourceNotFoundException::new); + } + + private ResponseEntity profileResponse(ProfileData profile) { + return ResponseEntity.ok(new HashMap() {{ + put("profile", profile); + }}); + } +} diff --git a/src/main/java/io/spring/api/security/WebSecurityConfig.java b/src/main/java/io/spring/api/security/WebSecurityConfig.java index e8c267c..62c0093 100644 --- a/src/main/java/io/spring/api/security/WebSecurityConfig.java +++ b/src/main/java/io/spring/api/security/WebSecurityConfig.java @@ -26,7 +26,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .authorizeRequests() .antMatchers(HttpMethod.POST, "/users", "/users/login").permitAll() - .antMatchers(HttpMethod.GET, "/articles/**").permitAll() + .antMatchers(HttpMethod.GET, "/articles/**", "/profiles/**").permitAll() .anyRequest().authenticated(); http.addFilterBefore(jwtTokenFilter(), UsernamePasswordAuthenticationFilter.class); diff --git a/src/main/java/io/spring/application/profile/ProfileQueryService.java b/src/main/java/io/spring/application/profile/ProfileQueryService.java new file mode 100644 index 0000000..fffdc9f --- /dev/null +++ b/src/main/java/io/spring/application/profile/ProfileQueryService.java @@ -0,0 +1,36 @@ +package io.spring.application.profile; + +import io.spring.application.user.UserData; +import io.spring.application.user.UserReadService; +import io.spring.core.user.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Optional; + +@Component +public class ProfileQueryService { + private UserReadService userReadService; + private UserRelationshipQueryService userRelationshipQueryService; + + @Autowired + public ProfileQueryService(UserReadService userReadService, UserRelationshipQueryService userRelationshipQueryService) { + this.userReadService = userReadService; + this.userRelationshipQueryService = userRelationshipQueryService; + } + + public Optional findByUsername(String username, User currentUser) { + UserData userData = userReadService.findByUsername(username); + if (userData == null) { + return Optional.empty(); + } else { + ProfileData profileData = new ProfileData( + userData.getId(), + userData.getUsername(), + userData.getBio(), + userData.getImage(), + userRelationshipQueryService.isUserFollowing(currentUser.getId(), userData.getId())); + return Optional.of(profileData); + } + } +} diff --git a/src/main/java/io/spring/application/user/UserReadService.java b/src/main/java/io/spring/application/user/UserReadService.java index 08998b9..a16400e 100644 --- a/src/main/java/io/spring/application/user/UserReadService.java +++ b/src/main/java/io/spring/application/user/UserReadService.java @@ -1,5 +1,6 @@ package io.spring.application.user; +import io.spring.application.profile.ProfileData; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Component; diff --git a/src/main/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepository.java b/src/main/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepository.java index 4f0eccd..7670fcf 100644 --- a/src/main/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepository.java +++ b/src/main/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepository.java @@ -18,7 +18,7 @@ public class MyBatisArticleFavoriteRepository implements ArticleFavoriteReposito @Override public void save(ArticleFavorite articleFavorite) { - if (mapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()) != null) { + if (mapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()) == null) { mapper.insert(articleFavorite); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index af5094c..062526c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,4 +3,5 @@ image.default=https://static.productionready.io/images/smiley-cyrus.jpg jwt.secret=nRvyYC4soFxBdZ-F-5Nnzz5USXstR1YylsTd-mA0aKtI9HUlriGrtkf-TiuDapkLiUCogO3JOK7kwZisrHp6wA jwt.sessionTime=86400 mybatis.config-location=classpath:mybatis-config.xml -mybatis.mapper-locations=mapper/*.xml \ No newline at end of file +mybatis.mapper-locations=mapper/*.xml +logging.level.io.spring.application.article.ArticleReadService=DEBUG \ No newline at end of file diff --git a/src/main/resources/mapper/ArticleReadService.xml b/src/main/resources/mapper/ArticleReadService.xml index 98ed172..883e1df 100644 --- a/src/main/resources/mapper/ArticleReadService.xml +++ b/src/main/resources/mapper/ArticleReadService.xml @@ -16,12 +16,12 @@ A.body articleBody, A.created_at articleCreatedAt, A.updated_at articleUpdatedAt, - T.name as tagName, + T.name tagName, from articles A left join article_tags AT on A.id = AT.article_id - left join tags T on T.id = AT.article_id + left join tags T on T.id = AT.tag_id left join users U on U.id = A.user_id @@ -43,7 +43,9 @@ - + + + diff --git a/src/test/java/io/spring/api/ArticleFavoriteApiTest.java b/src/test/java/io/spring/api/ArticleFavoriteApiTest.java index d969c9a..bd48099 100644 --- a/src/test/java/io/spring/api/ArticleFavoriteApiTest.java +++ b/src/test/java/io/spring/api/ArticleFavoriteApiTest.java @@ -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)); diff --git a/src/test/java/io/spring/api/ArticlesApiTest.java b/src/test/java/io/spring/api/ArticlesApiTest.java index 9e42e69..3bf7472 100644 --- a/src/test/java/io/spring/api/ArticlesApiTest.java +++ b/src/test/java/io/spring/api/ArticlesApiTest.java @@ -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 diff --git a/src/test/java/io/spring/api/CommentsApiTest.java b/src/test/java/io/spring/api/CommentsApiTest.java index 29fb06e..3303e64 100644 --- a/src/test/java/io/spring/api/CommentsApiTest.java +++ b/src/test/java/io/spring/api/CommentsApiTest.java @@ -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) diff --git a/src/test/java/io/spring/api/CurrentUserApiTest.java b/src/test/java/io/spring/api/CurrentUserApiTest.java index 6d94399..b617881 100644 --- a/src/test/java/io/spring/api/CurrentUserApiTest.java +++ b/src/test/java/io/spring/api/CurrentUserApiTest.java @@ -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 diff --git a/src/test/java/io/spring/api/ProfileApiTest.java b/src/test/java/io/spring/api/ProfileApiTest.java new file mode 100644 index 0000000..688ecea --- /dev/null +++ b/src/test/java/io/spring/api/ProfileApiTest.java @@ -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())); + } +} \ No newline at end of file diff --git a/src/test/java/io/spring/api/TestWithCurrentUser.java b/src/test/java/io/spring/api/TestWithCurrentUser.java index cd4bc83..2173e2f 100644 --- a/src/test/java/io/spring/api/TestWithCurrentUser.java +++ b/src/test/java/io/spring/api/TestWithCurrentUser.java @@ -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)); diff --git a/src/test/java/io/spring/application/article/ArticleQueryServiceTest.java b/src/test/java/io/spring/application/article/ArticleQueryServiceTest.java index 66f15e4..15b1814 100644 --- a/src/test/java/io/spring/application/article/ArticleQueryServiceTest.java +++ b/src/test/java/io/spring/application/article/ArticleQueryServiceTest.java @@ -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 diff --git a/src/test/java/io/spring/application/profile/ProfileQueryServiceTest.java b/src/test/java/io/spring/application/profile/ProfileQueryServiceTest.java new file mode 100644 index 0000000..2c0be3b --- /dev/null +++ b/src/test/java/io/spring/application/profile/ProfileQueryServiceTest.java @@ -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 optional = profileQueryService.findByUsername(profileUser.getUsername(), currentUser); + assertThat(optional.isPresent(), is(true)); + } +} \ No newline at end of file diff --git a/src/test/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepositoryTest.java b/src/test/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepositoryTest.java index f0131e9..2eeb3aa 100644 --- a/src/test/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepositoryTest.java +++ b/src/test/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepositoryTest.java @@ -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