get profile

This commit is contained in:
aisensiy 2017-08-16 15:58:51 +08:00
parent 7c685942ea
commit 13571f2f5e
16 changed files with 191 additions and 14 deletions

View File

@ -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<String, Object>() {{
put("profile", profile);
}});
}
}

View File

@ -26,7 +26,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests() .authorizeRequests()
.antMatchers(HttpMethod.POST, "/users", "/users/login").permitAll() .antMatchers(HttpMethod.POST, "/users", "/users/login").permitAll()
.antMatchers(HttpMethod.GET, "/articles/**").permitAll() .antMatchers(HttpMethod.GET, "/articles/**", "/profiles/**").permitAll()
.anyRequest().authenticated(); .anyRequest().authenticated();
http.addFilterBefore(jwtTokenFilter(), UsernamePasswordAuthenticationFilter.class); http.addFilterBefore(jwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

View File

@ -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<ProfileData> 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);
}
}
}

View File

@ -1,5 +1,6 @@
package io.spring.application.user; package io.spring.application.user;
import io.spring.application.profile.ProfileData;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;

View File

@ -18,7 +18,7 @@ public class MyBatisArticleFavoriteRepository implements ArticleFavoriteReposito
@Override @Override
public void save(ArticleFavorite articleFavorite) { public void save(ArticleFavorite articleFavorite) {
if (mapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()) != null) { if (mapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()) == null) {
mapper.insert(articleFavorite); mapper.insert(articleFavorite);
} }
} }

View File

@ -4,3 +4,4 @@ jwt.secret=nRvyYC4soFxBdZ-F-5Nnzz5USXstR1YylsTd-mA0aKtI9HUlriGrtkf-TiuDapkLiUCog
jwt.sessionTime=86400 jwt.sessionTime=86400
mybatis.config-location=classpath:mybatis-config.xml mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=mapper/*.xml mybatis.mapper-locations=mapper/*.xml
logging.level.io.spring.application.article.ArticleReadService=DEBUG

View File

@ -16,12 +16,12 @@
A.body articleBody, A.body articleBody,
A.created_at articleCreatedAt, A.created_at articleCreatedAt,
A.updated_at articleUpdatedAt, A.updated_at articleUpdatedAt,
T.name as tagName, T.name tagName,
<include refid="profileColumns"/> <include refid="profileColumns"/>
from from
articles A articles A
left join article_tags AT on A.id = AT.article_id 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 left join users U on U.id = A.user_id
</sql> </sql>
@ -43,7 +43,9 @@
<result column="articleCreatedAt" property="createdAt"/> <result column="articleCreatedAt" property="createdAt"/>
<result column="articleUpdatedAt" property="updatedAt"/> <result column="articleUpdatedAt" property="updatedAt"/>
<association property="profileData" resultMap="profileData"/> <association property="profileData" resultMap="profileData"/>
<collection property="tagList" ofType="arraylist" column="tagName" javaType="string"/> <collection property="tagList" javaType="list" ofType="string">
<result column="tagName"/>
</collection>
</resultMap> </resultMap>
<resultMap id="profileData" type="io.spring.application.profile.ProfileData"> <resultMap id="profileData" type="io.spring.application.profile.ProfileData">

View File

@ -3,7 +3,6 @@ package io.spring.api;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.spring.application.article.ArticleData; import io.spring.application.article.ArticleData;
import io.spring.application.article.ArticleQueryService; import io.spring.application.article.ArticleQueryService;
import io.spring.application.article.ArticleReadService;
import io.spring.application.profile.ProfileData; import io.spring.application.profile.ProfileData;
import io.spring.core.article.Article; import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository; import io.spring.core.article.ArticleRepository;
@ -56,7 +55,7 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
email = "john@jacob.com"; email = "john@jacob.com";
username = "johnjacob"; username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg"; defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
userFixture(email, username, defaultAvatar); userFixture();
anotherUser = new User("other@test.com", "other", "123", "", ""); anotherUser = new User("other@test.com", "other", "123", "", "");
article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId()); article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId());
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article)); when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));

View File

@ -52,7 +52,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
email = "john@jacob.com"; email = "john@jacob.com";
username = "johnjacob"; username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg"; defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
userFixture(email, username, defaultAvatar); userFixture();
} }
@Test @Test

View File

@ -58,7 +58,7 @@ public class CommentsApiTest extends TestWithCurrentUser {
email = "john@jacob.com"; email = "john@jacob.com";
username = "johnjacob"; username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg"; 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()); article = new Article("title", "desc", "body", new String[]{"test", "java"}, user.getId());
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article)); when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
@ -126,7 +126,7 @@ public class CommentsApiTest extends TestWithCurrentUser {
@Test @Test
public void should_delete_comment_success() throws Exception { 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() given()
.header("Authorization", "Token " + token) .header("Authorization", "Token " + token)

View File

@ -34,7 +34,7 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
email = "john@jacob.com"; email = "john@jacob.com";
username = "johnjacob"; username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg"; defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
userFixture(email, username, defaultAvatar); userFixture();
} }
@Test @Test

View 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()));
}
}

View File

@ -23,11 +23,18 @@ class TestWithCurrentUser {
protected User user; protected User user;
protected UserData userData; protected UserData userData;
protected String token; protected String token;
protected String email;
protected String username;
protected String defaultAvatar;
@Autowired @Autowired
protected JwtService jwtService; 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); user = new User(email, username, "123", "", defaultAvatar);
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user)); when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));

View File

@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.anyOf;
@ -61,6 +62,7 @@ public class ArticleQueryServiceTest {
assertThat(fetched.isFavorited(), is(false)); assertThat(fetched.isFavorited(), is(false));
assertThat(fetched.getCreatedAt(), notNullValue()); assertThat(fetched.getCreatedAt(), notNullValue());
assertThat(fetched.getUpdatedAt(), notNullValue()); assertThat(fetched.getUpdatedAt(), notNullValue());
assertThat(fetched.getTagList().contains("java"), is(true));
} }
@Test @Test

View File

@ -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));
}
}

View File

@ -10,6 +10,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -26,7 +27,7 @@ public class MyBatisArticleFavoriteRepositoryTest {
public void should_save_and_fetch_articleFavorite_success() throws Exception { public void should_save_and_fetch_articleFavorite_success() throws Exception {
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456"); ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
articleFavoriteRepository.save(articleFavorite); articleFavoriteRepository.save(articleFavorite);
assertThat(articleFavoriteMapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()), is(true)); assertThat(articleFavoriteMapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()), notNullValue());
} }
@Test @Test