refactor: move logic to application level
This commit is contained in:
parent
49cac729f1
commit
36e33e7730
7
src/main/java/io/spring/Util.java
Normal file
7
src/main/java/io/spring/Util.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package io.spring;
|
||||||
|
|
||||||
|
public class Util {
|
||||||
|
public static boolean isEmpty(String value) {
|
||||||
|
return value == null || value.isEmpty();
|
||||||
|
}
|
||||||
|
}
|
@ -1,15 +1,17 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
|
||||||
import io.spring.api.exception.NoAuthorizationException;
|
import io.spring.api.exception.NoAuthorizationException;
|
||||||
import io.spring.api.exception.ResourceNotFoundException;
|
import io.spring.api.exception.ResourceNotFoundException;
|
||||||
import io.spring.core.service.AuthorizationService;
|
|
||||||
import io.spring.application.data.ArticleData;
|
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
|
import io.spring.application.article.ArticleCommandService;
|
||||||
|
import io.spring.application.article.UpdateArticleParam;
|
||||||
|
import io.spring.application.data.ArticleData;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
|
import io.spring.core.service.AuthorizationService;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import lombok.Getter;
|
import java.util.HashMap;
|
||||||
import lombok.NoArgsConstructor;
|
import java.util.Map;
|
||||||
|
import javax.validation.Valid;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
@ -21,71 +23,72 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(path = "/articles/{slug}")
|
@RequestMapping(path = "/articles/{slug}")
|
||||||
public class ArticleApi {
|
public class ArticleApi {
|
||||||
private ArticleQueryService articleQueryService;
|
private ArticleQueryService articleQueryService;
|
||||||
private ArticleRepository articleRepository;
|
private ArticleRepository articleRepository;
|
||||||
|
private ArticleCommandService articleCommandService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ArticleApi(ArticleQueryService articleQueryService, ArticleRepository articleRepository) {
|
public ArticleApi(
|
||||||
|
ArticleQueryService articleQueryService,
|
||||||
|
ArticleRepository articleRepository,
|
||||||
|
ArticleCommandService articleCommandService) {
|
||||||
this.articleQueryService = articleQueryService;
|
this.articleQueryService = articleQueryService;
|
||||||
this.articleRepository = articleRepository;
|
this.articleRepository = articleRepository;
|
||||||
|
this.articleCommandService = articleCommandService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public ResponseEntity<?> article(@PathVariable("slug") String slug,
|
public ResponseEntity<?> article(
|
||||||
@AuthenticationPrincipal User user) {
|
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
|
||||||
return articleQueryService.findBySlug(slug, user)
|
return articleQueryService
|
||||||
|
.findBySlug(slug, user)
|
||||||
.map(articleData -> ResponseEntity.ok(articleResponse(articleData)))
|
.map(articleData -> ResponseEntity.ok(articleResponse(articleData)))
|
||||||
.orElseThrow(ResourceNotFoundException::new);
|
.orElseThrow(ResourceNotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public ResponseEntity<?> updateArticle(@PathVariable("slug") String slug,
|
public ResponseEntity<?> updateArticle(
|
||||||
|
@PathVariable("slug") String slug,
|
||||||
@AuthenticationPrincipal User user,
|
@AuthenticationPrincipal User user,
|
||||||
@Valid @RequestBody UpdateArticleParam updateArticleParam) {
|
@Valid @RequestBody UpdateArticleParam updateArticleParam) {
|
||||||
return articleRepository.findBySlug(slug).map(article -> {
|
return articleRepository
|
||||||
|
.findBySlug(slug)
|
||||||
|
.map(
|
||||||
|
article -> {
|
||||||
if (!AuthorizationService.canWriteArticle(user, article)) {
|
if (!AuthorizationService.canWriteArticle(user, article)) {
|
||||||
throw new NoAuthorizationException();
|
throw new NoAuthorizationException();
|
||||||
}
|
}
|
||||||
article.update(
|
articleCommandService.updateArticle(article, updateArticleParam);
|
||||||
updateArticleParam.getTitle(),
|
return ResponseEntity.ok(
|
||||||
updateArticleParam.getDescription(),
|
articleResponse(articleQueryService.findBySlug(slug, user).get()));
|
||||||
updateArticleParam.getBody());
|
})
|
||||||
articleRepository.save(article);
|
.orElseThrow(ResourceNotFoundException::new);
|
||||||
return ResponseEntity.ok(articleResponse(articleQueryService.findBySlug(slug, user).get()));
|
|
||||||
}).orElseThrow(ResourceNotFoundException::new);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping
|
@DeleteMapping
|
||||||
public ResponseEntity deleteArticle(@PathVariable("slug") String slug,
|
public ResponseEntity deleteArticle(
|
||||||
@AuthenticationPrincipal User user) {
|
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
|
||||||
return articleRepository.findBySlug(slug).map(article -> {
|
return articleRepository
|
||||||
|
.findBySlug(slug)
|
||||||
|
.map(
|
||||||
|
article -> {
|
||||||
if (!AuthorizationService.canWriteArticle(user, article)) {
|
if (!AuthorizationService.canWriteArticle(user, article)) {
|
||||||
throw new NoAuthorizationException();
|
throw new NoAuthorizationException();
|
||||||
}
|
}
|
||||||
articleRepository.remove(article);
|
articleRepository.remove(article);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}).orElseThrow(ResourceNotFoundException::new);
|
})
|
||||||
|
.orElseThrow(ResourceNotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> articleResponse(ArticleData articleData) {
|
private Map<String, Object> articleResponse(ArticleData articleData) {
|
||||||
return new HashMap<String, Object>() {{
|
return new HashMap<String, Object>() {
|
||||||
|
{
|
||||||
put("article", articleData);
|
put("article", articleData);
|
||||||
}};
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
|
||||||
@NoArgsConstructor
|
|
||||||
@JsonRootName("article")
|
|
||||||
class UpdateArticleParam {
|
|
||||||
private String title = "";
|
|
||||||
private String body = "";
|
|
||||||
private String description = "";
|
|
||||||
}
|
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.spring.api.exception.ResourceNotFoundException;
|
import io.spring.api.exception.ResourceNotFoundException;
|
||||||
import io.spring.application.data.ArticleData;
|
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
|
import io.spring.application.data.ArticleData;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
import io.spring.core.favorite.ArticleFavorite;
|
import io.spring.core.favorite.ArticleFavorite;
|
||||||
import io.spring.core.favorite.ArticleFavoriteRepository;
|
import io.spring.core.favorite.ArticleFavoriteRepository;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
import java.util.HashMap;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
@ -17,8 +18,6 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(path = "articles/{slug}/favorite")
|
@RequestMapping(path = "articles/{slug}/favorite")
|
||||||
public class ArticleFavoriteApi {
|
public class ArticleFavoriteApi {
|
||||||
@ -27,7 +26,8 @@ public class ArticleFavoriteApi {
|
|||||||
private ArticleQueryService articleQueryService;
|
private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ArticleFavoriteApi(ArticleFavoriteRepository articleFavoriteRepository,
|
public ArticleFavoriteApi(
|
||||||
|
ArticleFavoriteRepository articleFavoriteRepository,
|
||||||
ArticleRepository articleRepository,
|
ArticleRepository articleRepository,
|
||||||
ArticleQueryService articleQueryService) {
|
ArticleQueryService articleQueryService) {
|
||||||
this.articleFavoriteRepository = articleFavoriteRepository;
|
this.articleFavoriteRepository = articleFavoriteRepository;
|
||||||
@ -36,32 +36,36 @@ public class ArticleFavoriteApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity favoriteArticle(@PathVariable("slug") String slug,
|
public ResponseEntity favoriteArticle(
|
||||||
@AuthenticationPrincipal User user) {
|
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
|
||||||
Article article = getArticle(slug);
|
Article article =
|
||||||
|
articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
|
||||||
ArticleFavorite articleFavorite = new ArticleFavorite(article.getId(), user.getId());
|
ArticleFavorite articleFavorite = new ArticleFavorite(article.getId(), user.getId());
|
||||||
articleFavoriteRepository.save(articleFavorite);
|
articleFavoriteRepository.save(articleFavorite);
|
||||||
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping
|
@DeleteMapping
|
||||||
public ResponseEntity unfavoriteArticle(@PathVariable("slug") String slug,
|
public ResponseEntity unfavoriteArticle(
|
||||||
@AuthenticationPrincipal User user) {
|
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
|
||||||
Article article = getArticle(slug);
|
Article article =
|
||||||
articleFavoriteRepository.find(article.getId(), user.getId()).ifPresent(favorite -> {
|
articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
|
||||||
|
articleFavoriteRepository
|
||||||
|
.find(article.getId(), user.getId())
|
||||||
|
.ifPresent(
|
||||||
|
favorite -> {
|
||||||
articleFavoriteRepository.remove(favorite);
|
articleFavoriteRepository.remove(favorite);
|
||||||
});
|
});
|
||||||
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseEntity<HashMap<String, Object>> responseArticleData(final ArticleData articleData) {
|
private ResponseEntity<HashMap<String, Object>> responseArticleData(
|
||||||
return ResponseEntity.ok(new HashMap<String, Object>() {{
|
final ArticleData articleData) {
|
||||||
|
return ResponseEntity.ok(
|
||||||
|
new HashMap<String, Object>() {
|
||||||
|
{
|
||||||
put("article", articleData);
|
put("article", articleData);
|
||||||
}});
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
private Article getArticle(String slug) {
|
|
||||||
return articleRepository.findBySlug(slug).map(article -> article)
|
|
||||||
.orElseThrow(ResourceNotFoundException::new);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,13 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
import io.spring.application.Page;
|
import io.spring.application.Page;
|
||||||
|
import io.spring.application.article.ArticleCommandService;
|
||||||
|
import io.spring.application.article.NewArticleParam;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import java.lang.annotation.Documented;
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import javax.validation.Constraint;
|
|
||||||
import javax.validation.ConstraintValidator;
|
|
||||||
import javax.validation.ConstraintValidatorContext;
|
|
||||||
import javax.validation.Payload;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotBlank;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
@ -33,26 +21,20 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(path = "/articles")
|
@RequestMapping(path = "/articles")
|
||||||
public class ArticlesApi {
|
public class ArticlesApi {
|
||||||
private ArticleRepository articleRepository;
|
private ArticleCommandService articleCommandService;
|
||||||
private ArticleQueryService articleQueryService;
|
private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ArticlesApi(ArticleRepository articleRepository, ArticleQueryService articleQueryService) {
|
public ArticlesApi(
|
||||||
this.articleRepository = articleRepository;
|
ArticleCommandService articleCommandService, ArticleQueryService articleQueryService) {
|
||||||
|
this.articleCommandService = articleCommandService;
|
||||||
this.articleQueryService = articleQueryService;
|
this.articleQueryService = articleQueryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity createArticle(
|
public ResponseEntity createArticle(
|
||||||
@Valid @RequestBody NewArticleParam newArticleParam, @AuthenticationPrincipal User user) {
|
@Valid @RequestBody NewArticleParam newArticleParam, @AuthenticationPrincipal User user) {
|
||||||
Article article =
|
Article article = articleCommandService.createArticle(newArticleParam, user);
|
||||||
new Article(
|
|
||||||
newArticleParam.getTitle(),
|
|
||||||
newArticleParam.getDescription(),
|
|
||||||
newArticleParam.getBody(),
|
|
||||||
newArticleParam.getTagList(),
|
|
||||||
user.getId());
|
|
||||||
articleRepository.save(article);
|
|
||||||
return ResponseEntity.ok(
|
return ResponseEntity.ok(
|
||||||
new HashMap<String, Object>() {
|
new HashMap<String, Object>() {
|
||||||
{
|
{
|
||||||
@ -82,43 +64,3 @@ public class ArticlesApi {
|
|||||||
tag, author, favoritedBy, new Page(offset, limit), user));
|
tag, author, favoritedBy, new Page(offset, limit), user));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
|
||||||
@JsonRootName("article")
|
|
||||||
@NoArgsConstructor
|
|
||||||
class NewArticleParam {
|
|
||||||
@NotBlank(message = "can't be empty")
|
|
||||||
@DuplicatedArticleConstraint
|
|
||||||
private String title;
|
|
||||||
|
|
||||||
@NotBlank(message = "can't be empty")
|
|
||||||
private String description;
|
|
||||||
|
|
||||||
@NotBlank(message = "can't be empty")
|
|
||||||
private String body;
|
|
||||||
|
|
||||||
private String[] tagList;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Documented
|
|
||||||
@Constraint(validatedBy = DuplicatedArticleValidator.class)
|
|
||||||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE_USE})
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@interface DuplicatedArticleConstraint {
|
|
||||||
String message() default "article name exists";
|
|
||||||
|
|
||||||
Class<?>[] groups() default {};
|
|
||||||
|
|
||||||
Class<? extends Payload>[] payload() default {};
|
|
||||||
}
|
|
||||||
|
|
||||||
class DuplicatedArticleValidator
|
|
||||||
implements ConstraintValidator<DuplicatedArticleConstraint, String> {
|
|
||||||
|
|
||||||
@Autowired private ArticleQueryService articleQueryService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
|
||||||
return !articleQueryService.findBySlug(Article.toSlug(value), null).isPresent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -51,7 +51,8 @@ public class CommentsApi {
|
|||||||
@PathVariable("slug") String slug,
|
@PathVariable("slug") String slug,
|
||||||
@AuthenticationPrincipal User user,
|
@AuthenticationPrincipal User user,
|
||||||
@Valid @RequestBody NewCommentParam newCommentParam) {
|
@Valid @RequestBody NewCommentParam newCommentParam) {
|
||||||
Article article = findArticle(slug);
|
Article article =
|
||||||
|
articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
|
||||||
Comment comment = new Comment(newCommentParam.getBody(), user.getId(), article.getId());
|
Comment comment = new Comment(newCommentParam.getBody(), user.getId(), article.getId());
|
||||||
commentRepository.save(comment);
|
commentRepository.save(comment);
|
||||||
return ResponseEntity.status(201)
|
return ResponseEntity.status(201)
|
||||||
@ -61,7 +62,8 @@ public class CommentsApi {
|
|||||||
@GetMapping
|
@GetMapping
|
||||||
public ResponseEntity getComments(
|
public ResponseEntity getComments(
|
||||||
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
|
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
|
||||||
Article article = findArticle(slug);
|
Article article =
|
||||||
|
articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
|
||||||
List<CommentData> comments = commentQueryService.findByArticleId(article.getId(), user);
|
List<CommentData> comments = commentQueryService.findByArticleId(article.getId(), user);
|
||||||
return ResponseEntity.ok(
|
return ResponseEntity.ok(
|
||||||
new HashMap<String, Object>() {
|
new HashMap<String, Object>() {
|
||||||
@ -76,7 +78,8 @@ public class CommentsApi {
|
|||||||
@PathVariable("slug") String slug,
|
@PathVariable("slug") String slug,
|
||||||
@PathVariable("id") String commentId,
|
@PathVariable("id") String commentId,
|
||||||
@AuthenticationPrincipal User user) {
|
@AuthenticationPrincipal User user) {
|
||||||
Article article = findArticle(slug);
|
Article article =
|
||||||
|
articleRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
|
||||||
return commentRepository
|
return commentRepository
|
||||||
.findById(article.getId(), commentId)
|
.findById(article.getId(), commentId)
|
||||||
.map(
|
.map(
|
||||||
@ -90,13 +93,6 @@ public class CommentsApi {
|
|||||||
.orElseThrow(ResourceNotFoundException::new);
|
.orElseThrow(ResourceNotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Article findArticle(String slug) {
|
|
||||||
return articleRepository
|
|
||||||
.findBySlug(slug)
|
|
||||||
.map(article -> article)
|
|
||||||
.orElseThrow(ResourceNotFoundException::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> commentResponse(CommentData commentData) {
|
private Map<String, Object> commentResponse(CommentData commentData) {
|
||||||
return new HashMap<String, Object>() {
|
return new HashMap<String, Object>() {
|
||||||
{
|
{
|
||||||
|
@ -1,28 +1,18 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
|
||||||
import io.spring.application.UserQueryService;
|
import io.spring.application.UserQueryService;
|
||||||
import io.spring.application.data.UserData;
|
import io.spring.application.data.UserData;
|
||||||
import io.spring.application.data.UserWithToken;
|
import io.spring.application.data.UserWithToken;
|
||||||
|
import io.spring.application.user.UpdateUserCommand;
|
||||||
|
import io.spring.application.user.UpdateUserParam;
|
||||||
|
import io.spring.application.user.UserService;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import io.spring.core.user.UserRepository;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.validation.Constraint;
|
|
||||||
import javax.validation.ConstraintValidator;
|
|
||||||
import javax.validation.ConstraintValidatorContext;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.Email;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
@ -71,99 +61,3 @@ public class CurrentUserApi {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Validated
|
|
||||||
@Service
|
|
||||||
class UserService {
|
|
||||||
|
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public UserService(UserRepository userRepository) {
|
|
||||||
this.userRepository = userRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateUser(@Valid UpdateUserCommand command) {
|
|
||||||
User user = command.getTargetUser();
|
|
||||||
UpdateUserParam updateUserParam = command.getParam();
|
|
||||||
user.update(
|
|
||||||
updateUserParam.getEmail(),
|
|
||||||
updateUserParam.getUsername(),
|
|
||||||
updateUserParam.getPassword(),
|
|
||||||
updateUserParam.getBio(),
|
|
||||||
updateUserParam.getImage());
|
|
||||||
userRepository.save(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor
|
|
||||||
@UpdateUserConstraint
|
|
||||||
class UpdateUserCommand {
|
|
||||||
|
|
||||||
private User targetUser;
|
|
||||||
private UpdateUserParam param;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Constraint(validatedBy = UpdateUserValidator.class)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@interface UpdateUserConstraint {
|
|
||||||
|
|
||||||
String message() default "invalid update param";
|
|
||||||
|
|
||||||
Class[] groups() default {};
|
|
||||||
|
|
||||||
Class[] payload() default {};
|
|
||||||
}
|
|
||||||
|
|
||||||
class UpdateUserValidator implements ConstraintValidator<UpdateUserConstraint, UpdateUserCommand> {
|
|
||||||
|
|
||||||
@Autowired private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isValid(UpdateUserCommand value, ConstraintValidatorContext context) {
|
|
||||||
String inputEmail = value.getParam().getEmail();
|
|
||||||
String inputUsername = value.getParam().getUsername();
|
|
||||||
final User targetUser = value.getTargetUser();
|
|
||||||
|
|
||||||
boolean isEmailValid =
|
|
||||||
userRepository.findByEmail(inputEmail).map(user -> user.equals(targetUser)).orElse(true);
|
|
||||||
boolean isUsernameValid =
|
|
||||||
userRepository
|
|
||||||
.findByUsername(inputUsername)
|
|
||||||
.map(user -> user.equals(targetUser))
|
|
||||||
.orElse(true);
|
|
||||||
if (isEmailValid && isUsernameValid) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
context.disableDefaultConstraintViolation();
|
|
||||||
if (!isEmailValid) {
|
|
||||||
context
|
|
||||||
.buildConstraintViolationWithTemplate("email already exist")
|
|
||||||
.addPropertyNode("email")
|
|
||||||
.addConstraintViolation();
|
|
||||||
}
|
|
||||||
if (!isUsernameValid) {
|
|
||||||
context
|
|
||||||
.buildConstraintViolationWithTemplate("username already exist")
|
|
||||||
.addPropertyNode("username")
|
|
||||||
.addConstraintViolation();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@JsonRootName("user")
|
|
||||||
@NoArgsConstructor
|
|
||||||
class UpdateUserParam {
|
|
||||||
|
|
||||||
@Email(message = "should be an email")
|
|
||||||
private String email = "";
|
|
||||||
|
|
||||||
private String password = "";
|
|
||||||
private String username = "";
|
|
||||||
private String bio = "";
|
|
||||||
private String image = "";
|
|
||||||
}
|
|
||||||
|
@ -7,26 +7,21 @@ import io.spring.api.exception.InvalidAuthenticationException;
|
|||||||
import io.spring.application.UserQueryService;
|
import io.spring.application.UserQueryService;
|
||||||
import io.spring.application.data.UserData;
|
import io.spring.application.data.UserData;
|
||||||
import io.spring.application.data.UserWithToken;
|
import io.spring.application.data.UserWithToken;
|
||||||
|
import io.spring.application.user.RegisterParam;
|
||||||
|
import io.spring.application.user.UserService;
|
||||||
import io.spring.core.service.JwtService;
|
import io.spring.core.service.JwtService;
|
||||||
import io.spring.core.user.EncryptService;
|
import io.spring.core.user.EncryptService;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import io.spring.core.user.UserRepository;
|
import io.spring.core.user.UserRepository;
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import javax.validation.Constraint;
|
|
||||||
import javax.validation.ConstraintValidator;
|
|
||||||
import javax.validation.ConstraintValidatorContext;
|
|
||||||
import javax.validation.Payload;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.Email;
|
import javax.validation.constraints.Email;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -36,34 +31,27 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class UsersApi {
|
public class UsersApi {
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
private UserQueryService userQueryService;
|
private UserQueryService userQueryService;
|
||||||
private String defaultImage;
|
|
||||||
private EncryptService encryptService;
|
private EncryptService encryptService;
|
||||||
private JwtService jwtService;
|
private JwtService jwtService;
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public UsersApi(
|
public UsersApi(
|
||||||
UserRepository userRepository,
|
UserRepository userRepository,
|
||||||
UserQueryService userQueryService,
|
UserQueryService userQueryService,
|
||||||
EncryptService encryptService,
|
EncryptService encryptService,
|
||||||
@Value("${image.default}") String defaultImage,
|
JwtService jwtService,
|
||||||
JwtService jwtService) {
|
UserService userService) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.userQueryService = userQueryService;
|
this.userQueryService = userQueryService;
|
||||||
this.encryptService = encryptService;
|
this.encryptService = encryptService;
|
||||||
this.defaultImage = defaultImage;
|
|
||||||
this.jwtService = jwtService;
|
this.jwtService = jwtService;
|
||||||
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = "/users", method = POST)
|
@RequestMapping(path = "/users", method = POST)
|
||||||
public ResponseEntity createUser(@Valid @RequestBody RegisterParam registerParam) {
|
public ResponseEntity createUser(@Valid @RequestBody RegisterParam registerParam) {
|
||||||
User user =
|
User user = userService.createUser(registerParam);
|
||||||
new User(
|
|
||||||
registerParam.getEmail(),
|
|
||||||
registerParam.getUsername(),
|
|
||||||
encryptService.encrypt(registerParam.getPassword()),
|
|
||||||
"",
|
|
||||||
defaultImage);
|
|
||||||
userRepository.save(user);
|
|
||||||
UserData userData = userQueryService.findById(user.getId()).get();
|
UserData userData = userQueryService.findById(user.getId()).get();
|
||||||
return ResponseEntity.status(201)
|
return ResponseEntity.status(201)
|
||||||
.body(userResponse(new UserWithToken(userData, jwtService.toToken(user))));
|
.body(userResponse(new UserWithToken(userData, jwtService.toToken(user))));
|
||||||
@ -91,47 +79,6 @@ public class UsersApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Constraint(validatedBy = DuplicatedEmailValidator.class)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@interface DuplicatedEmailConstraint {
|
|
||||||
String message() default "duplicated email";
|
|
||||||
|
|
||||||
Class<?>[] groups() default {};
|
|
||||||
|
|
||||||
Class<? extends Payload>[] payload() default {};
|
|
||||||
}
|
|
||||||
|
|
||||||
class DuplicatedEmailValidator implements ConstraintValidator<DuplicatedEmailConstraint, String> {
|
|
||||||
|
|
||||||
@Autowired private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
|
||||||
return (value == null || value.isEmpty()) || !userRepository.findByEmail(value).isPresent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Constraint(validatedBy = DuplicatedUsernameValidator.class)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@interface DuplicatedUsernameConstraint {
|
|
||||||
String message() default "duplicated username";
|
|
||||||
|
|
||||||
Class<?>[] groups() default {};
|
|
||||||
|
|
||||||
Class<? extends Payload>[] payload() default {};
|
|
||||||
}
|
|
||||||
|
|
||||||
class DuplicatedUsernameValidator
|
|
||||||
implements ConstraintValidator<DuplicatedUsernameConstraint, String> {
|
|
||||||
|
|
||||||
@Autowired private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
|
||||||
return (value == null || value.isEmpty()) || !userRepository.findByUsername(value).isPresent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@JsonRootName("user")
|
@JsonRootName("user")
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@ -143,20 +90,3 @@ class LoginParam {
|
|||||||
@NotBlank(message = "can't be empty")
|
@NotBlank(message = "can't be empty")
|
||||||
private String password;
|
private String password;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
|
||||||
@JsonRootName("user")
|
|
||||||
@NoArgsConstructor
|
|
||||||
class RegisterParam {
|
|
||||||
@NotBlank(message = "can't be empty")
|
|
||||||
@Email(message = "should be an email")
|
|
||||||
@DuplicatedEmailConstraint
|
|
||||||
private String email;
|
|
||||||
|
|
||||||
@NotBlank(message = "can't be empty")
|
|
||||||
@DuplicatedUsernameConstraint
|
|
||||||
private String username;
|
|
||||||
|
|
||||||
@NotBlank(message = "can't be empty")
|
|
||||||
private String password;
|
|
||||||
}
|
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
package io.spring.api.security;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class CORSConfig implements WebMvcConfigurer {
|
|
||||||
@Override
|
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
|
||||||
registry.addMapping("/**")
|
|
||||||
.allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,7 @@
|
|||||||
package io.spring.api.security;
|
package io.spring.api.security;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@ -15,8 +17,6 @@ import org.springframework.web.cors.CorsConfiguration;
|
|||||||
import org.springframework.web.cors.CorsConfigurationSource;
|
import org.springframework.web.cors.CorsConfigurationSource;
|
||||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
@ -32,11 +32,12 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
|
||||||
if (h2ConsoleEnabled)
|
if (h2ConsoleEnabled) {
|
||||||
http.authorizeRequests()
|
http.authorizeRequests()
|
||||||
.antMatchers("/h2-console", "/h2-console/**").permitAll()
|
.antMatchers("/h2-console", "/h2-console/**").permitAll()
|
||||||
.and()
|
.and()
|
||||||
.headers().frameOptions().sameOrigin();
|
.headers().frameOptions().sameOrigin();
|
||||||
|
}
|
||||||
|
|
||||||
http.csrf().disable()
|
http.csrf().disable()
|
||||||
.cors()
|
.cors()
|
||||||
@ -46,6 +47,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
|
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
|
||||||
.authorizeRequests()
|
.authorizeRequests()
|
||||||
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
||||||
|
.antMatchers("/graphiql").permitAll()
|
||||||
|
.antMatchers("/graphql").permitAll()
|
||||||
.antMatchers(HttpMethod.GET, "/articles/feed").authenticated()
|
.antMatchers(HttpMethod.GET, "/articles/feed").authenticated()
|
||||||
.antMatchers(HttpMethod.POST, "/users", "/users/login").permitAll()
|
.antMatchers(HttpMethod.POST, "/users", "/users/login").permitAll()
|
||||||
.antMatchers(HttpMethod.GET, "/articles/**", "/profiles/**", "/tags").permitAll()
|
.antMatchers(HttpMethod.GET, "/articles/**", "/profiles/**", "/tags").permitAll()
|
||||||
@ -62,7 +65,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
"GET", "POST", "PUT", "DELETE", "PATCH"));
|
"GET", "POST", "PUT", "DELETE", "PATCH"));
|
||||||
// setAllowCredentials(true) is important, otherwise:
|
// setAllowCredentials(true) is important, otherwise:
|
||||||
// The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
|
// The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
|
||||||
configuration.setAllowCredentials(true);
|
configuration.setAllowCredentials(false);
|
||||||
// setAllowedHeaders is important! Without it, OPTIONS preflight request
|
// setAllowedHeaders is important! Without it, OPTIONS preflight request
|
||||||
// will fail with 403 Invalid CORS request
|
// will fail with 403 Invalid CORS request
|
||||||
configuration.setAllowedHeaders(asList("Authorization", "Cache-Control", "Content-Type"));
|
configuration.setAllowedHeaders(asList("Authorization", "Cache-Control", "Content-Type"));
|
||||||
|
@ -2,21 +2,21 @@ package io.spring.application;
|
|||||||
|
|
||||||
import io.spring.application.data.ProfileData;
|
import io.spring.application.data.ProfileData;
|
||||||
import io.spring.application.data.UserData;
|
import io.spring.application.data.UserData;
|
||||||
import io.spring.infrastructure.mybatis.readservice.UserReadService;
|
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
import io.spring.infrastructure.mybatis.readservice.UserReadService;
|
||||||
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
|
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
|
||||||
|
import java.util.Optional;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class ProfileQueryService {
|
public class ProfileQueryService {
|
||||||
private UserReadService userReadService;
|
private UserReadService userReadService;
|
||||||
private UserRelationshipQueryService userRelationshipQueryService;
|
private UserRelationshipQueryService userRelationshipQueryService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ProfileQueryService(UserReadService userReadService, UserRelationshipQueryService userRelationshipQueryService) {
|
public ProfileQueryService(
|
||||||
|
UserReadService userReadService, UserRelationshipQueryService userRelationshipQueryService) {
|
||||||
this.userReadService = userReadService;
|
this.userReadService = userReadService;
|
||||||
this.userRelationshipQueryService = userRelationshipQueryService;
|
this.userRelationshipQueryService = userRelationshipQueryService;
|
||||||
}
|
}
|
||||||
@ -26,12 +26,15 @@ public class ProfileQueryService {
|
|||||||
if (userData == null) {
|
if (userData == null) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
} else {
|
} else {
|
||||||
ProfileData profileData = new ProfileData(
|
ProfileData profileData =
|
||||||
|
new ProfileData(
|
||||||
userData.getId(),
|
userData.getId(),
|
||||||
userData.getUsername(),
|
userData.getUsername(),
|
||||||
userData.getBio(),
|
userData.getBio(),
|
||||||
userData.getImage(),
|
userData.getImage(),
|
||||||
userRelationshipQueryService.isUserFollowing(currentUser.getId(), userData.getId()));
|
currentUser != null
|
||||||
|
&& userRelationshipQueryService.isUserFollowing(
|
||||||
|
currentUser.getId(), userData.getId()));
|
||||||
return Optional.of(profileData);
|
return Optional.of(profileData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import io.spring.core.article.ArticleRepository;
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Validated
|
||||||
|
public class ArticleCommandService {
|
||||||
|
|
||||||
|
private ArticleRepository articleRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public ArticleCommandService(ArticleRepository articleRepository) {
|
||||||
|
this.articleRepository = articleRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Article createArticle(@Valid NewArticleParam newArticleParam, User creator) {
|
||||||
|
Article article =
|
||||||
|
new Article(
|
||||||
|
newArticleParam.getTitle(),
|
||||||
|
newArticleParam.getDescription(),
|
||||||
|
newArticleParam.getBody(),
|
||||||
|
newArticleParam.getTagList(),
|
||||||
|
creator.getId());
|
||||||
|
articleRepository.save(article);
|
||||||
|
return article;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Article updateArticle(Article article, @Valid UpdateArticleParam updateArticleParam) {
|
||||||
|
article.update(
|
||||||
|
updateArticleParam.getTitle(),
|
||||||
|
updateArticleParam.getDescription(),
|
||||||
|
updateArticleParam.getBody());
|
||||||
|
articleRepository.save(article);
|
||||||
|
return article;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
|
||||||
|
@Documented
|
||||||
|
@Constraint(validatedBy = DuplicatedArticleValidator.class)
|
||||||
|
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE_USE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface DuplicatedArticleConstraint {
|
||||||
|
String message() default "article name exists";
|
||||||
|
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import io.spring.application.ArticleQueryService;
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
class DuplicatedArticleValidator
|
||||||
|
implements ConstraintValidator<DuplicatedArticleConstraint, String> {
|
||||||
|
|
||||||
|
@Autowired private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
|
return !articleQueryService.findBySlug(Article.toSlug(value), null).isPresent();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@JsonRootName("article")
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class NewArticleParam {
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
@DuplicatedArticleConstraint
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
private String body;
|
||||||
|
|
||||||
|
private List<String> tagList;
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonRootName("article")
|
||||||
|
public class UpdateArticleParam {
|
||||||
|
private String title = "";
|
||||||
|
private String body = "";
|
||||||
|
private String description = "";
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package io.spring.application.user;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
|
||||||
|
@Constraint(validatedBy = DuplicatedEmailValidator.class)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface DuplicatedEmailConstraint {
|
||||||
|
String message() default "duplicated email";
|
||||||
|
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package io.spring.application.user;
|
||||||
|
|
||||||
|
import io.spring.core.user.UserRepository;
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
public class DuplicatedEmailValidator
|
||||||
|
implements ConstraintValidator<DuplicatedEmailConstraint, String> {
|
||||||
|
|
||||||
|
@Autowired private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
|
return (value == null || value.isEmpty()) || !userRepository.findByEmail(value).isPresent();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package io.spring.application.user;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
|
||||||
|
@Constraint(validatedBy = DuplicatedUsernameValidator.class)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@interface DuplicatedUsernameConstraint {
|
||||||
|
String message() default "duplicated username";
|
||||||
|
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package io.spring.application.user;
|
||||||
|
|
||||||
|
import io.spring.core.user.UserRepository;
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
class DuplicatedUsernameValidator
|
||||||
|
implements ConstraintValidator<DuplicatedUsernameConstraint, String> {
|
||||||
|
|
||||||
|
@Autowired private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
|
return (value == null || value.isEmpty()) || !userRepository.findByUsername(value).isPresent();
|
||||||
|
}
|
||||||
|
}
|
26
src/main/java/io/spring/application/user/RegisterParam.java
Normal file
26
src/main/java/io/spring/application/user/RegisterParam.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package io.spring.application.user;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@JsonRootName("user")
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class RegisterParam {
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
@Email(message = "should be an email")
|
||||||
|
@DuplicatedEmailConstraint
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
@DuplicatedUsernameConstraint
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
private String password;
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package io.spring.application.user;
|
||||||
|
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@UpdateUserConstraint
|
||||||
|
public class UpdateUserCommand {
|
||||||
|
|
||||||
|
private User targetUser;
|
||||||
|
private UpdateUserParam param;
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package io.spring.application.user;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@JsonRootName("user")
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class UpdateUserParam {
|
||||||
|
|
||||||
|
@Builder.Default
|
||||||
|
@Email(message = "should be an email")
|
||||||
|
private String email = "";
|
||||||
|
|
||||||
|
@Builder.Default private String password = "";
|
||||||
|
@Builder.Default private String username = "";
|
||||||
|
@Builder.Default private String bio = "";
|
||||||
|
@Builder.Default private String image = "";
|
||||||
|
}
|
106
src/main/java/io/spring/application/user/UserService.java
Normal file
106
src/main/java/io/spring/application/user/UserService.java
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package io.spring.application.user;
|
||||||
|
|
||||||
|
import io.spring.core.user.EncryptService;
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
import io.spring.core.user.UserRepository;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Validated
|
||||||
|
public class UserService {
|
||||||
|
private UserRepository userRepository;
|
||||||
|
private String defaultImage;
|
||||||
|
private EncryptService encryptService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public UserService(
|
||||||
|
UserRepository userRepository,
|
||||||
|
@Value("${image.default}") String defaultImage,
|
||||||
|
EncryptService encryptService) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
this.defaultImage = defaultImage;
|
||||||
|
this.encryptService = encryptService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User createUser(@Valid RegisterParam registerParam) {
|
||||||
|
User user =
|
||||||
|
new User(
|
||||||
|
registerParam.getEmail(),
|
||||||
|
registerParam.getUsername(),
|
||||||
|
encryptService.encrypt(registerParam.getPassword()),
|
||||||
|
"",
|
||||||
|
defaultImage);
|
||||||
|
userRepository.save(user);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateUser(@Valid UpdateUserCommand command) {
|
||||||
|
User user = command.getTargetUser();
|
||||||
|
UpdateUserParam updateUserParam = command.getParam();
|
||||||
|
user.update(
|
||||||
|
updateUserParam.getEmail(),
|
||||||
|
updateUserParam.getUsername(),
|
||||||
|
updateUserParam.getPassword(),
|
||||||
|
updateUserParam.getBio(),
|
||||||
|
updateUserParam.getImage());
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Constraint(validatedBy = UpdateUserValidator.class)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@interface UpdateUserConstraint {
|
||||||
|
|
||||||
|
String message() default "invalid update param";
|
||||||
|
|
||||||
|
Class[] groups() default {};
|
||||||
|
|
||||||
|
Class[] payload() default {};
|
||||||
|
}
|
||||||
|
|
||||||
|
class UpdateUserValidator implements ConstraintValidator<UpdateUserConstraint, UpdateUserCommand> {
|
||||||
|
|
||||||
|
@Autowired private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(UpdateUserCommand value, ConstraintValidatorContext context) {
|
||||||
|
String inputEmail = value.getParam().getEmail();
|
||||||
|
String inputUsername = value.getParam().getUsername();
|
||||||
|
final User targetUser = value.getTargetUser();
|
||||||
|
|
||||||
|
boolean isEmailValid =
|
||||||
|
userRepository.findByEmail(inputEmail).map(user -> user.equals(targetUser)).orElse(true);
|
||||||
|
boolean isUsernameValid =
|
||||||
|
userRepository
|
||||||
|
.findByUsername(inputUsername)
|
||||||
|
.map(user -> user.equals(targetUser))
|
||||||
|
.orElse(true);
|
||||||
|
if (isEmailValid && isUsernameValid) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
context.disableDefaultConstraintViolation();
|
||||||
|
if (!isEmailValid) {
|
||||||
|
context
|
||||||
|
.buildConstraintViolationWithTemplate("email already exist")
|
||||||
|
.addPropertyNode("email")
|
||||||
|
.addConstraintViolation();
|
||||||
|
}
|
||||||
|
if (!isUsernameValid) {
|
||||||
|
context
|
||||||
|
.buildConstraintViolationWithTemplate("username already exist")
|
||||||
|
.addPropertyNode("username")
|
||||||
|
.addConstraintViolation();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,16 @@
|
|||||||
package io.spring.core.article;
|
package io.spring.core.article;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
|
import io.spring.Util;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import static java.util.stream.Collectors.toList;
|
|
||||||
import static java.util.stream.Collectors.toSet;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@EqualsAndHashCode(of = {"id"})
|
@EqualsAndHashCode(of = {"id"})
|
||||||
@ -26,35 +25,44 @@ public class Article {
|
|||||||
private DateTime createdAt;
|
private DateTime createdAt;
|
||||||
private DateTime updatedAt;
|
private DateTime updatedAt;
|
||||||
|
|
||||||
public Article(String title, String description, String body, String[] tagList, String userId) {
|
public Article(
|
||||||
|
String title, String description, String body, List<String> tagList, String userId) {
|
||||||
this(title, description, body, tagList, userId, new DateTime());
|
this(title, description, body, tagList, userId, new DateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Article(String title, String description, String body, String[] tagList, String userId, DateTime createdAt) {
|
public Article(
|
||||||
|
String title,
|
||||||
|
String description,
|
||||||
|
String body,
|
||||||
|
List<String> tagList,
|
||||||
|
String userId,
|
||||||
|
DateTime createdAt) {
|
||||||
this.id = UUID.randomUUID().toString();
|
this.id = UUID.randomUUID().toString();
|
||||||
this.slug = toSlug(title);
|
this.slug = toSlug(title);
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.body = body;
|
this.body = body;
|
||||||
this.tags = Arrays.stream(tagList).collect(toSet()).stream().map(Tag::new).collect(toList());
|
this.tags = new HashSet<>(tagList).stream().map(Tag::new).collect(toList());
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
this.createdAt = createdAt;
|
this.createdAt = createdAt;
|
||||||
this.updatedAt = createdAt;
|
this.updatedAt = createdAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(String title, String description, String body) {
|
public void update(String title, String description, String body) {
|
||||||
if (!"".equals(title)) {
|
if (!Util.isEmpty(title)) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.slug = toSlug(title);
|
this.slug = toSlug(title);
|
||||||
}
|
|
||||||
if (!"".equals(description)) {
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
if (!"".equals(body)) {
|
|
||||||
this.body = body;
|
|
||||||
}
|
|
||||||
this.updatedAt = new DateTime();
|
this.updatedAt = new DateTime();
|
||||||
}
|
}
|
||||||
|
if (!Util.isEmpty(description)) {
|
||||||
|
this.description = description;
|
||||||
|
this.updatedAt = new DateTime();
|
||||||
|
}
|
||||||
|
if (!Util.isEmpty(body)) {
|
||||||
|
this.body = body;
|
||||||
|
this.updatedAt = new DateTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static String toSlug(String title) {
|
public static String toSlug(String title) {
|
||||||
return title.toLowerCase().replaceAll("[\\&|[\\uFE30-\\uFFA0]|\\’|\\”|\\s\\?\\,\\.]+", "-");
|
return title.toLowerCase().replaceAll("[\\&|[\\uFE30-\\uFFA0]|\\’|\\”|\\s\\?\\,\\.]+", "-");
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package io.spring.core.user;
|
package io.spring.core.user;
|
||||||
|
|
||||||
|
import io.spring.Util;
|
||||||
|
import java.util.UUID;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@EqualsAndHashCode(of = {"id"})
|
@EqualsAndHashCode(of = {"id"})
|
||||||
@ -27,23 +27,23 @@ public class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void update(String email, String username, String password, String bio, String image) {
|
public void update(String email, String username, String password, String bio, String image) {
|
||||||
if (!"".equals(email)) {
|
if (!Util.isEmpty(email)) {
|
||||||
this.email = email;
|
this.email = email;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!"".equals(username)) {
|
if (!Util.isEmpty(username)) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!"".equals(password)) {
|
if (!Util.isEmpty(password)) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!"".equals(bio)) {
|
if (!Util.isEmpty(bio)) {
|
||||||
this.bio = bio;
|
this.bio = bio;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!"".equals(image)) {
|
if (!Util.isEmpty(image)) {
|
||||||
this.image = image;
|
this.image = image;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
C.id commentId,
|
C.id commentId,
|
||||||
C.body commentBody,
|
C.body commentBody,
|
||||||
C.created_at commentCreatedAt,
|
C.created_at commentCreatedAt,
|
||||||
|
C.article_id commentArticleId,
|
||||||
<include refid="io.spring.infrastructure.mybatis.readservice.ArticleReadService.profileColumns"/>
|
<include refid="io.spring.infrastructure.mybatis.readservice.ArticleReadService.profileColumns"/>
|
||||||
from comments C
|
from comments C
|
||||||
left join users U
|
left join users U
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
<result column="commentBody" property="body"/>
|
<result column="commentBody" property="body"/>
|
||||||
<result column="commentCreatedAt" property="createdAt"/>
|
<result column="commentCreatedAt" property="createdAt"/>
|
||||||
<result column="commentCreatedAt" property="updatedAt"/>
|
<result column="commentCreatedAt" property="updatedAt"/>
|
||||||
|
<result column="commentArticleId" property="articleId"/>
|
||||||
<association property="profileData" resultMap="profileData"/>
|
<association property="profileData" resultMap="profileData"/>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
</mapper>
|
</mapper>
|
@ -1,15 +1,28 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
import io.spring.JacksonCustomizations;
|
import io.spring.JacksonCustomizations;
|
||||||
import io.spring.TestHelper;
|
import io.spring.TestHelper;
|
||||||
import io.spring.api.security.WebSecurityConfig;
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
|
import io.spring.application.article.ArticleCommandService;
|
||||||
import io.spring.application.data.ArticleData;
|
import io.spring.application.data.ArticleData;
|
||||||
import io.spring.application.data.ProfileData;
|
import io.spring.application.data.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;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.joda.time.format.ISODateTimeFormat;
|
import org.joda.time.format.ISODateTimeFormat;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -20,30 +33,16 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@WebMvcTest({ArticleApi.class})
|
@WebMvcTest({ArticleApi.class})
|
||||||
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class ArticleApiTest extends TestWithCurrentUser {
|
public class ArticleApiTest extends TestWithCurrentUser {
|
||||||
@Autowired
|
@Autowired private MockMvc mvc;
|
||||||
private MockMvc mvc;
|
|
||||||
|
|
||||||
@MockBean
|
@MockBean private ArticleQueryService articleQueryService;
|
||||||
private ArticleQueryService articleQueryService;
|
|
||||||
|
|
||||||
@MockBean
|
@MockBean private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
@MockBean ArticleCommandService articleCommandService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Before
|
@Before
|
||||||
@ -56,7 +55,14 @@ public class ArticleApiTest extends TestWithCurrentUser {
|
|||||||
public void should_read_article_success() throws Exception {
|
public void should_read_article_success() throws Exception {
|
||||||
String slug = "test-new-article";
|
String slug = "test-new-article";
|
||||||
DateTime time = new DateTime();
|
DateTime time = new DateTime();
|
||||||
Article article = new Article("Test New Article", "Desc", "Body", new String[]{"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);
|
ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user);
|
||||||
|
|
||||||
when(articleQueryService.findBySlug(eq(slug), eq(null))).thenReturn(Optional.of(articleData));
|
when(articleQueryService.findBySlug(eq(slug), eq(null))).thenReturn(Optional.of(articleData));
|
||||||
@ -68,16 +74,12 @@ public class ArticleApiTest extends TestWithCurrentUser {
|
|||||||
.body("article.slug", equalTo(slug))
|
.body("article.slug", equalTo(slug))
|
||||||
.body("article.body", equalTo(articleData.getBody()))
|
.body("article.body", equalTo(articleData.getBody()))
|
||||||
.body("article.createdAt", equalTo(ISODateTimeFormat.dateTime().withZoneUTC().print(time)));
|
.body("article.createdAt", equalTo(ISODateTimeFormat.dateTime().withZoneUTC().print(time)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_404_if_article_not_found() throws Exception {
|
public void should_404_if_article_not_found() throws Exception {
|
||||||
when(articleQueryService.findBySlug(anyString(), any())).thenReturn(Optional.empty());
|
when(articleQueryService.findBySlug(anyString(), any())).thenReturn(Optional.empty());
|
||||||
RestAssuredMockMvc.when()
|
RestAssuredMockMvc.when().get("/articles/not-exists").then().statusCode(404);
|
||||||
.get("/articles/not-exists")
|
|
||||||
.then()
|
|
||||||
.statusCode(404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -87,12 +89,14 @@ public class ArticleApiTest extends TestWithCurrentUser {
|
|||||||
String description = "new description";
|
String description = "new description";
|
||||||
Map<String, Object> updateParam = prepareUpdateParam(title, body, description);
|
Map<String, Object> updateParam = prepareUpdateParam(title, body, description);
|
||||||
|
|
||||||
Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, user.getId());
|
Article article =
|
||||||
|
new Article(title, description, body, Arrays.asList("java", "spring", "jpg"), user.getId());
|
||||||
|
|
||||||
ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user);
|
ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user);
|
||||||
|
|
||||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
|
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user)))
|
||||||
|
.thenReturn(Optional.of(articleData));
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
@ -114,10 +118,13 @@ public class ArticleApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
User anotherUser = new User("test@test.com", "test", "123123", "", "");
|
User anotherUser = new User("test@test.com", "test", "123123", "", "");
|
||||||
|
|
||||||
Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, anotherUser.getId());
|
Article article =
|
||||||
|
new Article(
|
||||||
|
title, description, body, Arrays.asList("java", "spring", "jpg"), anotherUser.getId());
|
||||||
|
|
||||||
DateTime time = new DateTime();
|
DateTime time = new DateTime();
|
||||||
ArticleData articleData = new ArticleData(
|
ArticleData articleData =
|
||||||
|
new ArticleData(
|
||||||
article.getId(),
|
article.getId(),
|
||||||
article.getSlug(),
|
article.getSlug(),
|
||||||
article.getTitle(),
|
article.getTitle(),
|
||||||
@ -128,10 +135,16 @@ public class ArticleApiTest extends TestWithCurrentUser {
|
|||||||
time,
|
time,
|
||||||
time,
|
time,
|
||||||
Arrays.asList("joda"),
|
Arrays.asList("joda"),
|
||||||
new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false));
|
new ProfileData(
|
||||||
|
anotherUser.getId(),
|
||||||
|
anotherUser.getUsername(),
|
||||||
|
anotherUser.getBio(),
|
||||||
|
anotherUser.getImage(),
|
||||||
|
false));
|
||||||
|
|
||||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
|
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user)))
|
||||||
|
.thenReturn(Optional.of(articleData));
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
@ -149,7 +162,8 @@ public class ArticleApiTest extends TestWithCurrentUser {
|
|||||||
String body = "body";
|
String body = "body";
|
||||||
String description = "description";
|
String description = "description";
|
||||||
|
|
||||||
Article article = new Article(title, description, body, new String[]{"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));
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@ -170,7 +184,9 @@ public class ArticleApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
User anotherUser = new User("test@test.com", "test", "123123", "", "");
|
User anotherUser = new User("test@test.com", "test", "123123", "", "");
|
||||||
|
|
||||||
Article article = new Article(title, description, body, new String[]{"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));
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
given()
|
given()
|
||||||
@ -181,13 +197,20 @@ public class ArticleApiTest extends TestWithCurrentUser {
|
|||||||
.statusCode(403);
|
.statusCode(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<String, Object> prepareUpdateParam(final String title, final String body, final String description) {
|
private HashMap<String, Object> prepareUpdateParam(
|
||||||
return new HashMap<String, Object>() {{
|
final String title, final String body, final String description) {
|
||||||
put("article", new HashMap<String, Object>() {{
|
return new HashMap<String, Object>() {
|
||||||
|
{
|
||||||
|
put(
|
||||||
|
"article",
|
||||||
|
new HashMap<String, Object>() {
|
||||||
|
{
|
||||||
put("title", title);
|
put("title", title);
|
||||||
put("body", body);
|
put("body", body);
|
||||||
put("description", description);
|
put("description", description);
|
||||||
}});
|
}
|
||||||
}};
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
import io.spring.JacksonCustomizations;
|
import io.spring.JacksonCustomizations;
|
||||||
import io.spring.api.security.WebSecurityConfig;
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
@ -12,6 +19,9 @@ import io.spring.core.article.Tag;
|
|||||||
import io.spring.core.favorite.ArticleFavorite;
|
import io.spring.core.favorite.ArticleFavorite;
|
||||||
import io.spring.core.favorite.ArticleFavoriteRepository;
|
import io.spring.core.favorite.ArticleFavoriteRepository;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -20,30 +30,16 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@WebMvcTest(ArticleFavoriteApi.class)
|
@WebMvcTest(ArticleFavoriteApi.class)
|
||||||
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
||||||
@Autowired
|
@Autowired private MockMvc mvc;
|
||||||
private MockMvc mvc;
|
|
||||||
|
|
||||||
@MockBean
|
@MockBean private ArticleFavoriteRepository articleFavoriteRepository;
|
||||||
private ArticleFavoriteRepository articleFavoriteRepository;
|
|
||||||
|
|
||||||
@MockBean
|
@MockBean private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
@MockBean
|
@MockBean private ArticleQueryService articleQueryService;
|
||||||
private ArticleQueryService articleQueryService;
|
|
||||||
|
|
||||||
private Article article;
|
private Article article;
|
||||||
|
|
||||||
@ -52,9 +48,10 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
|||||||
super.setUp();
|
super.setUp();
|
||||||
RestAssuredMockMvc.mockMvc(mvc);
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
User anotherUser = new User("other@test.com", "other", "123", "", "");
|
User 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", Arrays.asList("java"), anotherUser.getId());
|
||||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
ArticleData articleData = new ArticleData(
|
ArticleData articleData =
|
||||||
|
new ArticleData(
|
||||||
article.getId(),
|
article.getId(),
|
||||||
article.getSlug(),
|
article.getSlug(),
|
||||||
article.getTitle(),
|
article.getTitle(),
|
||||||
@ -70,9 +67,9 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
|||||||
anotherUser.getUsername(),
|
anotherUser.getUsername(),
|
||||||
anotherUser.getBio(),
|
anotherUser.getBio(),
|
||||||
anotherUser.getImage(),
|
anotherUser.getImage(),
|
||||||
false
|
false));
|
||||||
));
|
when(articleQueryService.findBySlug(eq(articleData.getSlug()), eq(user)))
|
||||||
when(articleQueryService.findBySlug(eq(articleData.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
|
.thenReturn(Optional.of(articleData));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -91,7 +88,8 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_unfavorite_an_article_success() throws Exception {
|
public void should_unfavorite_an_article_success() throws Exception {
|
||||||
when(articleFavoriteRepository.find(eq(article.getId()), eq(user.getId()))).thenReturn(Optional.of(new ArticleFavorite(article.getId(), user.getId())));
|
when(articleFavoriteRepository.find(eq(article.getId()), eq(user.getId())))
|
||||||
|
.thenReturn(Optional.of(new ArticleFavorite(article.getId(), user.getId())));
|
||||||
given()
|
given()
|
||||||
.header("Authorization", "Token " + token)
|
.header("Authorization", "Token " + token)
|
||||||
.when()
|
.when()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
@ -11,12 +12,12 @@ import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
|||||||
import io.spring.JacksonCustomizations;
|
import io.spring.JacksonCustomizations;
|
||||||
import io.spring.api.security.WebSecurityConfig;
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
|
import io.spring.application.article.ArticleCommandService;
|
||||||
import io.spring.application.data.ArticleData;
|
import io.spring.application.data.ArticleData;
|
||||||
import io.spring.application.data.ProfileData;
|
import io.spring.application.data.ProfileData;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
@ -33,10 +34,10 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||||||
public class ArticlesApiTest extends TestWithCurrentUser {
|
public class ArticlesApiTest extends TestWithCurrentUser {
|
||||||
@Autowired private MockMvc mvc;
|
@Autowired private MockMvc mvc;
|
||||||
|
|
||||||
@MockBean private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
@MockBean private ArticleQueryService articleQueryService;
|
@MockBean private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
|
@MockBean private ArticleCommandService articleCommandService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
@ -50,7 +51,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
String slug = "how-to-train-your-dragon";
|
String slug = "how-to-train-your-dragon";
|
||||||
String description = "Ever wonder how?";
|
String description = "Ever wonder how?";
|
||||||
String body = "You have to believe";
|
String body = "You have to believe";
|
||||||
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
List<String> tagList = asList("reactjs", "angularjs", "dragons");
|
||||||
Map<String, Object> param = prepareParam(title, description, body, tagList);
|
Map<String, Object> param = prepareParam(title, description, body, tagList);
|
||||||
|
|
||||||
ArticleData articleData =
|
ArticleData articleData =
|
||||||
@ -64,9 +65,12 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
0,
|
0,
|
||||||
new DateTime(),
|
new DateTime(),
|
||||||
new DateTime(),
|
new DateTime(),
|
||||||
Arrays.asList(tagList),
|
tagList,
|
||||||
new ProfileData("userid", user.getUsername(), user.getBio(), user.getImage(), false));
|
new ProfileData("userid", user.getUsername(), user.getBio(), user.getImage(), false));
|
||||||
|
|
||||||
|
when(articleCommandService.createArticle(any(), any()))
|
||||||
|
.thenReturn(new Article(title, description, body, tagList, user.getId()));
|
||||||
|
|
||||||
when(articleQueryService.findBySlug(eq(Article.toSlug(title)), any()))
|
when(articleQueryService.findBySlug(eq(Article.toSlug(title)), any()))
|
||||||
.thenReturn(Optional.empty());
|
.thenReturn(Optional.empty());
|
||||||
|
|
||||||
@ -87,7 +91,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
.body("article.author.username", equalTo(user.getUsername()))
|
.body("article.author.username", equalTo(user.getUsername()))
|
||||||
.body("article.author.id", equalTo(null));
|
.body("article.author.id", equalTo(null));
|
||||||
|
|
||||||
verify(articleRepository).save(any());
|
verify(articleCommandService).createArticle(any(), any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -96,7 +100,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
String description = "Ever wonder how?";
|
String description = "Ever wonder how?";
|
||||||
String body = "";
|
String body = "";
|
||||||
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
||||||
Map<String, Object> param = prepareParam(title, description, body, tagList);
|
Map<String, Object> param = prepareParam(title, description, body, asList(tagList));
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
@ -117,7 +121,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
String description = "Ever wonder how?";
|
String description = "Ever wonder how?";
|
||||||
String body = "You have to believe";
|
String body = "You have to believe";
|
||||||
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
||||||
Map<String, Object> param = prepareParam(title, description, body, tagList);
|
Map<String, Object> param = prepareParam(title, description, body, asList(tagList));
|
||||||
|
|
||||||
ArticleData articleData =
|
ArticleData articleData =
|
||||||
new ArticleData(
|
new ArticleData(
|
||||||
@ -130,7 +134,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
0,
|
0,
|
||||||
new DateTime(),
|
new DateTime(),
|
||||||
new DateTime(),
|
new DateTime(),
|
||||||
Arrays.asList(tagList),
|
asList(tagList),
|
||||||
new ProfileData("userid", user.getUsername(), user.getBio(), user.getImage(), false));
|
new ProfileData("userid", user.getUsername(), user.getBio(), user.getImage(), false));
|
||||||
|
|
||||||
when(articleQueryService.findBySlug(eq(Article.toSlug(title)), any()))
|
when(articleQueryService.findBySlug(eq(Article.toSlug(title)), any()))
|
||||||
@ -150,7 +154,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<String, Object> prepareParam(
|
private HashMap<String, Object> prepareParam(
|
||||||
final String title, final String description, final String body, final String[] tagList) {
|
final String title, final String description, final String body, final List<String> tagList) {
|
||||||
return new HashMap<String, Object>() {
|
return new HashMap<String, Object>() {
|
||||||
{
|
{
|
||||||
put(
|
put(
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
import io.spring.JacksonCustomizations;
|
import io.spring.JacksonCustomizations;
|
||||||
import io.spring.api.security.WebSecurityConfig;
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
@ -11,6 +18,10 @@ import io.spring.core.article.ArticleRepository;
|
|||||||
import io.spring.core.comment.Comment;
|
import io.spring.core.comment.Comment;
|
||||||
import io.spring.core.comment.CommentRepository;
|
import io.spring.core.comment.CommentRepository;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -19,59 +30,52 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@WebMvcTest(CommentsApi.class)
|
@WebMvcTest(CommentsApi.class)
|
||||||
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class CommentsApiTest extends TestWithCurrentUser {
|
public class CommentsApiTest extends TestWithCurrentUser {
|
||||||
|
|
||||||
@MockBean
|
@MockBean private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
@MockBean
|
@MockBean private CommentRepository commentRepository;
|
||||||
private CommentRepository commentRepository;
|
@MockBean private CommentQueryService commentQueryService;
|
||||||
@MockBean
|
|
||||||
private CommentQueryService commentQueryService;
|
|
||||||
|
|
||||||
private Article article;
|
private Article article;
|
||||||
private CommentData commentData;
|
private CommentData commentData;
|
||||||
private Comment comment;
|
private Comment comment;
|
||||||
@Autowired
|
@Autowired private MockMvc mvc;
|
||||||
private MockMvc mvc;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssuredMockMvc.mockMvc(mvc);
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
super.setUp();
|
super.setUp();
|
||||||
article = new Article("title", "desc", "body", new String[]{"test", "java"}, user.getId());
|
article = new Article("title", "desc", "body", Arrays.asList("test", "java"), user.getId());
|
||||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
comment = new Comment("comment", user.getId(), article.getId());
|
comment = new Comment("comment", user.getId(), article.getId());
|
||||||
commentData = new CommentData(
|
commentData =
|
||||||
|
new CommentData(
|
||||||
comment.getId(),
|
comment.getId(),
|
||||||
comment.getBody(),
|
comment.getBody(),
|
||||||
comment.getArticleId(),
|
comment.getArticleId(),
|
||||||
comment.getCreatedAt(),
|
comment.getCreatedAt(),
|
||||||
comment.getCreatedAt(),
|
comment.getCreatedAt(),
|
||||||
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
|
new ProfileData(
|
||||||
|
user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_create_comment_success() throws Exception {
|
public void should_create_comment_success() throws Exception {
|
||||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
Map<String, Object> param =
|
||||||
put("comment", new HashMap<String, Object>() {{
|
new HashMap<String, Object>() {
|
||||||
|
{
|
||||||
|
put(
|
||||||
|
"comment",
|
||||||
|
new HashMap<String, Object>() {
|
||||||
|
{
|
||||||
put("body", "comment content");
|
put("body", "comment content");
|
||||||
}});
|
}
|
||||||
}};
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
when(commentQueryService.findById(anyString(), eq(user))).thenReturn(Optional.of(commentData));
|
when(commentQueryService.findById(anyString(), eq(user))).thenReturn(Optional.of(commentData));
|
||||||
|
|
||||||
@ -88,11 +92,18 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_422_with_empty_body() throws Exception {
|
public void should_get_422_with_empty_body() throws Exception {
|
||||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
Map<String, Object> param =
|
||||||
put("comment", new HashMap<String, Object>() {{
|
new HashMap<String, Object>() {
|
||||||
|
{
|
||||||
|
put(
|
||||||
|
"comment",
|
||||||
|
new HashMap<String, Object>() {
|
||||||
|
{
|
||||||
put("body", "");
|
put("body", "");
|
||||||
}});
|
}
|
||||||
}};
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
@ -103,12 +114,12 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
|||||||
.then()
|
.then()
|
||||||
.statusCode(422)
|
.statusCode(422)
|
||||||
.body("errors.body[0]", equalTo("can't be empty"));
|
.body("errors.body[0]", equalTo("can't be empty"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_comments_of_article_success() throws Exception {
|
public void should_get_comments_of_article_success() throws Exception {
|
||||||
when(commentQueryService.findByArticleId(anyString(), eq(null))).thenReturn(Arrays.asList(commentData));
|
when(commentQueryService.findByArticleId(anyString(), eq(null)))
|
||||||
|
.thenReturn(Arrays.asList(commentData));
|
||||||
RestAssuredMockMvc.when()
|
RestAssuredMockMvc.when()
|
||||||
.get("/articles/{slug}/comments", article.getSlug())
|
.get("/articles/{slug}/comments", article.getSlug())
|
||||||
.prettyPeek()
|
.prettyPeek()
|
||||||
@ -119,7 +130,8 @@ 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(eq(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)
|
||||||
@ -130,13 +142,17 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_403_if_not_author_of_article_or_author_of_comment_when_delete_comment() throws Exception {
|
public void should_get_403_if_not_author_of_article_or_author_of_comment_when_delete_comment()
|
||||||
|
throws Exception {
|
||||||
User anotherUser = new User("other@example.com", "other", "123", "", "");
|
User anotherUser = new User("other@example.com", "other", "123", "", "");
|
||||||
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
|
when(userRepository.findByUsername(eq(anotherUser.getUsername())))
|
||||||
|
.thenReturn(Optional.of(anotherUser));
|
||||||
when(jwtService.getSubFromToken(any())).thenReturn(Optional.of(anotherUser.getId()));
|
when(jwtService.getSubFromToken(any())).thenReturn(Optional.of(anotherUser.getId()));
|
||||||
when(userRepository.findById(eq(anotherUser.getId()))).thenReturn(Optional.ofNullable(anotherUser));
|
when(userRepository.findById(eq(anotherUser.getId())))
|
||||||
|
.thenReturn(Optional.ofNullable(anotherUser));
|
||||||
|
|
||||||
when(commentRepository.findById(eq(article.getId()), eq(comment.getId()))).thenReturn(Optional.of(comment));
|
when(commentRepository.findById(eq(article.getId()), eq(comment.getId())))
|
||||||
|
.thenReturn(Optional.of(comment));
|
||||||
String token = jwtService.toToken(anotherUser);
|
String token = jwtService.toToken(anotherUser);
|
||||||
when(userRepository.findById(eq(anotherUser.getId()))).thenReturn(Optional.of(anotherUser));
|
when(userRepository.findById(eq(anotherUser.getId()))).thenReturn(Optional.of(anotherUser));
|
||||||
given()
|
given()
|
||||||
@ -145,6 +161,5 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
|||||||
.delete("/articles/{slug}/comments/{id}", article.getSlug(), comment.getId())
|
.delete("/articles/{slug}/comments/{id}", article.getSlug(), comment.getId())
|
||||||
.then()
|
.then()
|
||||||
.statusCode(403);
|
.statusCode(403);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,9 @@ import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
|||||||
import io.spring.JacksonCustomizations;
|
import io.spring.JacksonCustomizations;
|
||||||
import io.spring.api.security.WebSecurityConfig;
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.UserQueryService;
|
import io.spring.application.UserQueryService;
|
||||||
|
import io.spring.application.user.UserService;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
import io.spring.infrastructure.service.NaiveEncryptService;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -28,7 +30,8 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||||||
WebSecurityConfig.class,
|
WebSecurityConfig.class,
|
||||||
JacksonCustomizations.class,
|
JacksonCustomizations.class,
|
||||||
UserService.class,
|
UserService.class,
|
||||||
ValidationAutoConfiguration.class
|
ValidationAutoConfiguration.class,
|
||||||
|
NaiveEncryptService.class
|
||||||
})
|
})
|
||||||
public class CurrentUserApiTest extends TestWithCurrentUser {
|
public class CurrentUserApiTest extends TestWithCurrentUser {
|
||||||
|
|
||||||
|
@ -1,10 +1,17 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
|
import static io.spring.TestHelper.articleDataFixture;
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
import io.spring.JacksonCustomizations;
|
import io.spring.JacksonCustomizations;
|
||||||
import io.spring.api.security.WebSecurityConfig;
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
import io.spring.application.Page;
|
import io.spring.application.Page;
|
||||||
|
import io.spring.application.article.ArticleCommandService;
|
||||||
import io.spring.application.data.ArticleDataList;
|
import io.spring.application.data.ArticleDataList;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -15,23 +22,16 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
|
||||||
import static io.spring.TestHelper.articleDataFixture;
|
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@WebMvcTest(ArticlesApi.class)
|
@WebMvcTest(ArticlesApi.class)
|
||||||
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class ListArticleApiTest extends TestWithCurrentUser {
|
public class ListArticleApiTest extends TestWithCurrentUser {
|
||||||
@MockBean
|
@MockBean private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
@MockBean
|
@MockBean private ArticleQueryService articleQueryService;
|
||||||
private ArticleQueryService articleQueryService;
|
|
||||||
|
|
||||||
@Autowired
|
@MockBean private ArticleCommandService articleCommandService;
|
||||||
private MockMvc mvc;
|
|
||||||
|
@Autowired private MockMvc mvc;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Before
|
@Before
|
||||||
@ -42,30 +42,27 @@ public class ListArticleApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_default_article_list() throws Exception {
|
public void should_get_default_article_list() throws Exception {
|
||||||
ArticleDataList articleDataList = new ArticleDataList(
|
ArticleDataList articleDataList =
|
||||||
|
new ArticleDataList(
|
||||||
asList(articleDataFixture("1", user), articleDataFixture("2", user)), 2);
|
asList(articleDataFixture("1", user), articleDataFixture("2", user)), 2);
|
||||||
when(articleQueryService.findRecentArticles(eq(null), eq(null), eq(null), eq(new Page(0, 20)), eq(null))).thenReturn(articleDataList);
|
when(articleQueryService.findRecentArticles(
|
||||||
RestAssuredMockMvc.when()
|
eq(null), eq(null), eq(null), eq(new Page(0, 20)), eq(null)))
|
||||||
.get("/articles")
|
.thenReturn(articleDataList);
|
||||||
.prettyPeek()
|
RestAssuredMockMvc.when().get("/articles").prettyPeek().then().statusCode(200);
|
||||||
.then()
|
|
||||||
.statusCode(200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_feeds_401_without_login() throws Exception {
|
public void should_get_feeds_401_without_login() throws Exception {
|
||||||
RestAssuredMockMvc.when()
|
RestAssuredMockMvc.when().get("/articles/feed").prettyPeek().then().statusCode(401);
|
||||||
.get("/articles/feed")
|
|
||||||
.prettyPeek()
|
|
||||||
.then()
|
|
||||||
.statusCode(401);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_feeds_success() throws Exception {
|
public void should_get_feeds_success() throws Exception {
|
||||||
ArticleDataList articleDataList = new ArticleDataList(
|
ArticleDataList articleDataList =
|
||||||
|
new ArticleDataList(
|
||||||
asList(articleDataFixture("1", user), articleDataFixture("2", user)), 2);
|
asList(articleDataFixture("1", user), articleDataFixture("2", user)), 2);
|
||||||
when(articleQueryService.findUserFeed(eq(user), eq(new Page(0, 20)))).thenReturn(articleDataList);
|
when(articleQueryService.findUserFeed(eq(user), eq(new Page(0, 20))))
|
||||||
|
.thenReturn(articleDataList);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.header("Authorization", "Token " + token)
|
.header("Authorization", "Token " + token)
|
||||||
|
@ -12,6 +12,7 @@ import io.spring.JacksonCustomizations;
|
|||||||
import io.spring.api.security.WebSecurityConfig;
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.UserQueryService;
|
import io.spring.application.UserQueryService;
|
||||||
import io.spring.application.data.UserData;
|
import io.spring.application.data.UserData;
|
||||||
|
import io.spring.application.user.UserService;
|
||||||
import io.spring.core.service.JwtService;
|
import io.spring.core.service.JwtService;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import io.spring.core.user.UserRepository;
|
import io.spring.core.user.UserRepository;
|
||||||
@ -46,6 +47,9 @@ public class UsersApiTest {
|
|||||||
@MockBean private JwtService jwtService;
|
@MockBean private JwtService jwtService;
|
||||||
|
|
||||||
@MockBean private UserReadService userReadService;
|
@MockBean private UserReadService userReadService;
|
||||||
|
|
||||||
|
@MockBean private UserService userService;
|
||||||
|
|
||||||
private String defaultAvatar;
|
private String defaultAvatar;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@ -64,6 +68,8 @@ public class UsersApiTest {
|
|||||||
UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
||||||
when(userReadService.findById(any())).thenReturn(userData);
|
when(userReadService.findById(any())).thenReturn(userData);
|
||||||
|
|
||||||
|
when(userService.createUser(any())).thenReturn(user);
|
||||||
|
|
||||||
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.empty());
|
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.empty());
|
||||||
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.empty());
|
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.empty());
|
||||||
|
|
||||||
@ -82,7 +88,7 @@ public class UsersApiTest {
|
|||||||
.body("user.image", equalTo(defaultAvatar))
|
.body("user.image", equalTo(defaultAvatar))
|
||||||
.body("user.token", equalTo("123"));
|
.body("user.token", equalTo("123"));
|
||||||
|
|
||||||
verify(userRepository).save(any());
|
verify(userService).createUser(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package io.spring.application.article;
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
import io.spring.application.Page;
|
import io.spring.application.Page;
|
||||||
import io.spring.application.data.ArticleData;
|
import io.spring.application.data.ArticleData;
|
||||||
@ -14,6 +19,8 @@ import io.spring.core.user.UserRepository;
|
|||||||
import io.spring.infrastructure.repository.MyBatisArticleFavoriteRepository;
|
import io.spring.infrastructure.repository.MyBatisArticleFavoriteRepository;
|
||||||
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
||||||
import io.spring.infrastructure.repository.MyBatisUserRepository;
|
import io.spring.infrastructure.repository.MyBatisUserRepository;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -23,32 +30,22 @@ 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.Optional;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@MybatisTest
|
@MybatisTest
|
||||||
@Import({
|
@Import({
|
||||||
ArticleQueryService.class,
|
ArticleQueryService.class,
|
||||||
MyBatisUserRepository.class,
|
MyBatisUserRepository.class,
|
||||||
MyBatisArticleRepository.class,
|
MyBatisArticleRepository.class,
|
||||||
MyBatisArticleFavoriteRepository.class})
|
MyBatisArticleFavoriteRepository.class
|
||||||
|
})
|
||||||
public class ArticleQueryServiceTest {
|
public class ArticleQueryServiceTest {
|
||||||
@Autowired
|
@Autowired private ArticleQueryService queryService;
|
||||||
private ArticleQueryService queryService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private UserRepository userRepository;
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private ArticleFavoriteRepository articleFavoriteRepository;
|
||||||
private ArticleFavoriteRepository articleFavoriteRepository;
|
|
||||||
|
|
||||||
private User user;
|
private User user;
|
||||||
private Article article;
|
private Article article;
|
||||||
@ -57,7 +54,9 @@ public class ArticleQueryServiceTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId(), new DateTime());
|
article =
|
||||||
|
new Article(
|
||||||
|
"test", "desc", "body", Arrays.asList("java", "spring"), user.getId(), new DateTime());
|
||||||
articleRepository.save(article);
|
articleRepository.save(article);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,15 +89,24 @@ public class ArticleQueryServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_default_article_list() {
|
public void should_get_default_article_list() {
|
||||||
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, user.getId(), new DateTime().minusHours(1));
|
Article anotherArticle =
|
||||||
|
new Article(
|
||||||
|
"new article",
|
||||||
|
"desc",
|
||||||
|
"body",
|
||||||
|
Arrays.asList("test"),
|
||||||
|
user.getId(),
|
||||||
|
new DateTime().minusHours(1));
|
||||||
articleRepository.save(anotherArticle);
|
articleRepository.save(anotherArticle);
|
||||||
|
|
||||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), user);
|
ArticleDataList recentArticles =
|
||||||
|
queryService.findRecentArticles(null, null, null, new Page(), user);
|
||||||
assertEquals(recentArticles.getCount(), 2);
|
assertEquals(recentArticles.getCount(), 2);
|
||||||
assertEquals(recentArticles.getArticleDatas().size(), 2);
|
assertEquals(recentArticles.getArticleDatas().size(), 2);
|
||||||
assertEquals(recentArticles.getArticleDatas().get(0).getId(), article.getId());
|
assertEquals(recentArticles.getArticleDatas().get(0).getId(), article.getId());
|
||||||
|
|
||||||
ArticleDataList nodata = queryService.findRecentArticles(null, null, null, new Page(2, 10), user);
|
ArticleDataList nodata =
|
||||||
|
queryService.findRecentArticles(null, null, null, new Page(2, 10), user);
|
||||||
assertEquals(nodata.getCount(), 2);
|
assertEquals(nodata.getCount(), 2);
|
||||||
assertEquals(nodata.getArticleDatas().size(), 0);
|
assertEquals(nodata.getArticleDatas().size(), 0);
|
||||||
}
|
}
|
||||||
@ -108,10 +116,12 @@ public class ArticleQueryServiceTest {
|
|||||||
User anotherUser = new User("other@email.com", "other", "123", "", "");
|
User anotherUser = new User("other@email.com", "other", "123", "", "");
|
||||||
userRepository.save(anotherUser);
|
userRepository.save(anotherUser);
|
||||||
|
|
||||||
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, anotherUser.getId());
|
Article anotherArticle =
|
||||||
|
new Article("new article", "desc", "body", Arrays.asList("test"), anotherUser.getId());
|
||||||
articleRepository.save(anotherArticle);
|
articleRepository.save(anotherArticle);
|
||||||
|
|
||||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, user.getUsername(), null, new Page(), user);
|
ArticleDataList recentArticles =
|
||||||
|
queryService.findRecentArticles(null, user.getUsername(), null, new Page(), user);
|
||||||
assertEquals(recentArticles.getArticleDatas().size(), 1);
|
assertEquals(recentArticles.getArticleDatas().size(), 1);
|
||||||
assertEquals(recentArticles.getCount(), 1);
|
assertEquals(recentArticles.getCount(), 1);
|
||||||
}
|
}
|
||||||
@ -121,13 +131,16 @@ public class ArticleQueryServiceTest {
|
|||||||
User anotherUser = new User("other@email.com", "other", "123", "", "");
|
User anotherUser = new User("other@email.com", "other", "123", "", "");
|
||||||
userRepository.save(anotherUser);
|
userRepository.save(anotherUser);
|
||||||
|
|
||||||
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, anotherUser.getId());
|
Article anotherArticle =
|
||||||
|
new Article("new article", "desc", "body", Arrays.asList("test"), anotherUser.getId());
|
||||||
articleRepository.save(anotherArticle);
|
articleRepository.save(anotherArticle);
|
||||||
|
|
||||||
ArticleFavorite articleFavorite = new ArticleFavorite(article.getId(), anotherUser.getId());
|
ArticleFavorite articleFavorite = new ArticleFavorite(article.getId(), anotherUser.getId());
|
||||||
articleFavoriteRepository.save(articleFavorite);
|
articleFavoriteRepository.save(articleFavorite);
|
||||||
|
|
||||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, anotherUser.getUsername(), new Page(), anotherUser);
|
ArticleDataList recentArticles =
|
||||||
|
queryService.findRecentArticles(
|
||||||
|
null, null, anotherUser.getUsername(), new Page(), anotherUser);
|
||||||
assertEquals(recentArticles.getArticleDatas().size(), 1);
|
assertEquals(recentArticles.getArticleDatas().size(), 1);
|
||||||
assertEquals(recentArticles.getCount(), 1);
|
assertEquals(recentArticles.getCount(), 1);
|
||||||
ArticleData articleData = recentArticles.getArticleDatas().get(0);
|
ArticleData articleData = recentArticles.getArticleDatas().get(0);
|
||||||
@ -138,10 +151,12 @@ public class ArticleQueryServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_query_article_by_tag() {
|
public void should_query_article_by_tag() {
|
||||||
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, user.getId());
|
Article anotherArticle =
|
||||||
|
new Article("new article", "desc", "body", Arrays.asList("test"), user.getId());
|
||||||
articleRepository.save(anotherArticle);
|
articleRepository.save(anotherArticle);
|
||||||
|
|
||||||
ArticleDataList recentArticles = queryService.findRecentArticles("spring", null, null, new Page(), user);
|
ArticleDataList recentArticles =
|
||||||
|
queryService.findRecentArticles("spring", null, null, new Page(), user);
|
||||||
assertEquals(recentArticles.getArticleDatas().size(), 1);
|
assertEquals(recentArticles.getArticleDatas().size(), 1);
|
||||||
assertEquals(recentArticles.getCount(), 1);
|
assertEquals(recentArticles.getCount(), 1);
|
||||||
assertEquals(recentArticles.getArticleDatas().get(0).getId(), article.getId());
|
assertEquals(recentArticles.getArticleDatas().get(0).getId(), article.getId());
|
||||||
@ -158,7 +173,8 @@ public class ArticleQueryServiceTest {
|
|||||||
FollowRelation followRelation = new FollowRelation(anotherUser.getId(), user.getId());
|
FollowRelation followRelation = new FollowRelation(anotherUser.getId(), user.getId());
|
||||||
userRepository.saveRelation(followRelation);
|
userRepository.saveRelation(followRelation);
|
||||||
|
|
||||||
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), anotherUser);
|
ArticleDataList recentArticles =
|
||||||
|
queryService.findRecentArticles(null, null, null, new Page(), anotherUser);
|
||||||
assertEquals(recentArticles.getCount(), 1);
|
assertEquals(recentArticles.getCount(), 1);
|
||||||
ArticleData articleData = recentArticles.getArticleDatas().get(0);
|
ArticleData articleData = recentArticles.getArticleDatas().get(0);
|
||||||
assertTrue(articleData.getProfileData().isFollowing());
|
assertTrue(articleData.getProfileData().isFollowing());
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package io.spring.application.comment;
|
package io.spring.application.comment;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import io.spring.application.CommentQueryService;
|
import io.spring.application.CommentQueryService;
|
||||||
import io.spring.application.data.CommentData;
|
import io.spring.application.data.CommentData;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
@ -12,6 +15,9 @@ import io.spring.core.user.UserRepository;
|
|||||||
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
||||||
import io.spring.infrastructure.repository.MyBatisCommentRepository;
|
import io.spring.infrastructure.repository.MyBatisCommentRepository;
|
||||||
import io.spring.infrastructure.repository.MyBatisUserRepository;
|
import io.spring.infrastructure.repository.MyBatisUserRepository;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -20,27 +26,22 @@ 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.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
@MybatisTest
|
@MybatisTest
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@Import({MyBatisCommentRepository.class, MyBatisUserRepository.class, CommentQueryService.class, MyBatisArticleRepository.class})
|
@Import({
|
||||||
|
MyBatisCommentRepository.class,
|
||||||
|
MyBatisUserRepository.class,
|
||||||
|
CommentQueryService.class,
|
||||||
|
MyBatisArticleRepository.class
|
||||||
|
})
|
||||||
public class CommentQueryServiceTest {
|
public class CommentQueryServiceTest {
|
||||||
@Autowired
|
@Autowired private CommentRepository commentRepository;
|
||||||
private CommentRepository commentRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private UserRepository userRepository;
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private CommentQueryService commentQueryService;
|
||||||
private CommentQueryService commentQueryService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
@ -63,7 +64,7 @@ public class CommentQueryServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_read_comments_of_article() {
|
public void should_read_comments_of_article() {
|
||||||
Article article = new Article("title", "desc", "body", new String[]{"java"}, user.getId());
|
Article article = new Article("title", "desc", "body", Arrays.asList("java"), user.getId());
|
||||||
articleRepository.save(article);
|
articleRepository.save(article);
|
||||||
|
|
||||||
User user2 = new User("user2@email.com", "user2", "123", "", "");
|
User user2 = new User("user2@email.com", "user2", "123", "", "");
|
||||||
@ -77,6 +78,5 @@ public class CommentQueryServiceTest {
|
|||||||
|
|
||||||
List<CommentData> comments = commentQueryService.findByArticleId(article.getId(), user);
|
List<CommentData> comments = commentQueryService.findByArticleId(article.getId(), user);
|
||||||
assertEquals(comments.size(), 2);
|
assertEquals(comments.size(), 2);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,12 @@
|
|||||||
package io.spring.application.tag;
|
package io.spring.application.tag;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import io.spring.application.TagsQueryService;
|
import io.spring.application.TagsQueryService;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
||||||
|
import java.util.Arrays;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
|
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
|
||||||
@ -11,21 +14,17 @@ 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 static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@MybatisTest
|
@MybatisTest
|
||||||
@Import({TagsQueryService.class, MyBatisArticleRepository.class})
|
@Import({TagsQueryService.class, MyBatisArticleRepository.class})
|
||||||
public class TagsQueryServiceTest {
|
public class TagsQueryServiceTest {
|
||||||
@Autowired
|
@Autowired private TagsQueryService tagsQueryService;
|
||||||
private TagsQueryService tagsQueryService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_all_tags() {
|
public void should_get_all_tags() {
|
||||||
articleRepository.save(new Article("test", "test", "test", new String[]{"java"}, "123"));
|
articleRepository.save(new Article("test", "test", "test", Arrays.asList("java"), "123"));
|
||||||
assertTrue(tagsQueryService.allTags().contains("java"));
|
assertTrue(tagsQueryService.allTags().contains("java"));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,39 +1,40 @@
|
|||||||
package io.spring.core.article;
|
package io.spring.core.article;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ArticleTest {
|
public class ArticleTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_right_slug() {
|
public void should_get_right_slug() {
|
||||||
Article article = new Article("a new title", "desc", "body", new String[]{"java"}, "123");
|
Article article = new Article("a new title", "desc", "body", Arrays.asList("java"), "123");
|
||||||
assertThat(article.getSlug(), is("a-new-title"));
|
assertThat(article.getSlug(), is("a-new-title"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_right_slug_with_number_in_title() {
|
public void should_get_right_slug_with_number_in_title() {
|
||||||
Article article = new Article("a new title 2", "desc", "body", new String[]{"java"}, "123");
|
Article article = new Article("a new title 2", "desc", "body", Arrays.asList("java"), "123");
|
||||||
assertThat(article.getSlug(), is("a-new-title-2"));
|
assertThat(article.getSlug(), is("a-new-title-2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_lower_case_slug() {
|
public void should_get_lower_case_slug() {
|
||||||
Article article = new Article("A NEW TITLE", "desc", "body", new String[]{"java"}, "123");
|
Article article = new Article("A NEW TITLE", "desc", "body", Arrays.asList("java"), "123");
|
||||||
assertThat(article.getSlug(), is("a-new-title"));
|
assertThat(article.getSlug(), is("a-new-title"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_handle_other_language() {
|
public void should_handle_other_language() {
|
||||||
Article article = new Article("中文:标题", "desc", "body", new String[]{"java"}, "123");
|
Article article = new Article("中文:标题", "desc", "body", Arrays.asList("java"), "123");
|
||||||
assertThat(article.getSlug(), is("中文-标题"));
|
assertThat(article.getSlug(), is("中文-标题"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_handle_commas() {
|
public void should_handle_commas() {
|
||||||
Article article = new Article("what?the.hell,w", "desc", "body", new String[]{"java"}, "123");
|
Article article = new Article("what?the.hell,w", "desc", "body", Arrays.asList("java"), "123");
|
||||||
assertThat(article.getSlug(), is("what-the-hell-w"));
|
assertThat(article.getSlug(), is("what-the-hell-w"));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,10 +1,13 @@
|
|||||||
package io.spring.infrastructure.article;
|
package io.spring.infrastructure.article;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import io.spring.core.user.UserRepository;
|
import io.spring.core.user.UserRepository;
|
||||||
import io.spring.infrastructure.mybatis.mapper.ArticleMapper;
|
import io.spring.infrastructure.mybatis.mapper.ArticleMapper;
|
||||||
|
import java.util.Arrays;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -12,35 +15,29 @@ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabas
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureTestDatabase
|
@AutoConfigureTestDatabase
|
||||||
public class ArticleRepositoryTransactionTest {
|
public class ArticleRepositoryTransactionTest {
|
||||||
@Autowired
|
@Autowired private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private UserRepository userRepository;
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ArticleMapper articleMapper;
|
|
||||||
|
|
||||||
|
@Autowired private ArticleMapper articleMapper;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void transactional_test() {
|
public void transactional_test() {
|
||||||
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
Article article =
|
||||||
|
new Article("test", "desc", "body", Arrays.asList("java", "spring"), user.getId());
|
||||||
articleRepository.save(article);
|
articleRepository.save(article);
|
||||||
Article anotherArticle = new Article("test", "desc", "body", new String[]{"java", "spring", "other"}, user.getId());
|
Article anotherArticle =
|
||||||
|
new Article("test", "desc", "body", Arrays.asList("java", "spring", "other"), user.getId());
|
||||||
try {
|
try {
|
||||||
articleRepository.save(anotherArticle);
|
articleRepository.save(anotherArticle);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assertNull(articleMapper.findTag("other"));
|
assertNull(articleMapper.findTag("other"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package io.spring.infrastructure.article;
|
package io.spring.infrastructure.article;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
import io.spring.core.article.Tag;
|
import io.spring.core.article.Tag;
|
||||||
@ -7,6 +12,8 @@ import io.spring.core.user.User;
|
|||||||
import io.spring.core.user.UserRepository;
|
import io.spring.core.user.UserRepository;
|
||||||
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
import io.spring.infrastructure.repository.MyBatisArticleRepository;
|
||||||
import io.spring.infrastructure.repository.MyBatisUserRepository;
|
import io.spring.infrastructure.repository.MyBatisUserRepository;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -15,31 +22,21 @@ 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.Optional;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
@MybatisTest
|
@MybatisTest
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@Import({MyBatisArticleRepository.class, MyBatisUserRepository.class})
|
@Import({MyBatisArticleRepository.class, MyBatisUserRepository.class})
|
||||||
public class MyBatisArticleRepositoryTest {
|
public class MyBatisArticleRepositoryTest {
|
||||||
@Autowired
|
@Autowired private ArticleRepository articleRepository;
|
||||||
private ArticleRepository articleRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private UserRepository userRepository;
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
private Article article;
|
private Article article;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
article = new Article("test", "desc", "body", Arrays.asList("java", "spring"), user.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user