should create article success
This commit is contained in:
parent
b1e8632c3b
commit
00f778c087
@ -31,6 +31,7 @@ dependencies {
|
|||||||
compile('org.springframework.boot:spring-boot-starter-web')
|
compile('org.springframework.boot:spring-boot-starter-web')
|
||||||
compile('io.jsonwebtoken:jjwt:0.7.0')
|
compile('io.jsonwebtoken:jjwt:0.7.0')
|
||||||
compile('org.springframework.boot:spring-boot-starter-security')
|
compile('org.springframework.boot:spring-boot-starter-security')
|
||||||
|
compile('joda-time:joda-time:2.9.9')
|
||||||
compileOnly('org.projectlombok:lombok')
|
compileOnly('org.projectlombok:lombok')
|
||||||
runtime('com.h2database:h2')
|
runtime('com.h2database:h2')
|
||||||
testCompile 'io.rest-assured:rest-assured:3.0.2'
|
testCompile 'io.rest-assured:rest-assured:3.0.2'
|
||||||
|
67
src/main/java/io/spring/api/ArticlesApi.java
Normal file
67
src/main/java/io/spring/api/ArticlesApi.java
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package io.spring.api;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
import io.spring.api.exception.InvalidRequestException;
|
||||||
|
import io.spring.application.article.ArticleQueryService;
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import io.spring.core.article.ArticleRepository;
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.hibernate.validator.constraints.NotBlank;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(path = "/articles")
|
||||||
|
public class ArticlesApi {
|
||||||
|
private ArticleRepository articleRepository;
|
||||||
|
private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public ArticlesApi(ArticleRepository articleRepository, ArticleQueryService articleQueryService) {
|
||||||
|
this.articleRepository = articleRepository;
|
||||||
|
this.articleQueryService = articleQueryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity createArticle(@Valid @RequestBody NewArticleParam newArticleParam,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
@AuthenticationPrincipal User user) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
throw new InvalidRequestException(bindingResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
Article article = new Article(
|
||||||
|
articleRepository.toSlug(
|
||||||
|
newArticleParam.getTitle()),
|
||||||
|
newArticleParam.getTitle(),
|
||||||
|
newArticleParam.getDescription(),
|
||||||
|
newArticleParam.getBody(),
|
||||||
|
newArticleParam.getTagList(),
|
||||||
|
user.getId());
|
||||||
|
articleRepository.save(article);
|
||||||
|
return ResponseEntity.ok(articleQueryService.findById(article.getId(), user).get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@JsonRootName("article")
|
||||||
|
@NoArgsConstructor
|
||||||
|
class NewArticleParam {
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
private String title;
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
private String description;
|
||||||
|
@NotBlank(message = "can't be empty")
|
||||||
|
private String body;
|
||||||
|
private String[] tagList;
|
||||||
|
}
|
31
src/main/java/io/spring/application/article/ArticleData.java
Normal file
31
src/main/java/io/spring/application/article/ArticleData.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
import io.spring.application.profile.ProfileData;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.joda.time.DateTime;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonRootName("article")
|
||||||
|
public class ArticleData {
|
||||||
|
private String id;
|
||||||
|
private String slug;
|
||||||
|
private String title;
|
||||||
|
private String description;
|
||||||
|
private String body;
|
||||||
|
private boolean favorited;
|
||||||
|
private int favoritesCount;
|
||||||
|
private DateTime createdAt;
|
||||||
|
private DateTime updatedAt;
|
||||||
|
private List<String> tagList;
|
||||||
|
@JsonProperty("author")
|
||||||
|
private ProfileData profileData;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
@Component
|
||||||
|
public interface ArticleFavoritesQueryService {
|
||||||
|
boolean isUserFavorite(@Param("userId") String userId, @Param("articleId") String articleId);
|
||||||
|
|
||||||
|
int articleFavoriteCount(@Param("articleId") String articleId);
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import io.spring.application.profile.UserRelationshipQueryService;
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ArticleQueryService {
|
||||||
|
private ArticleReadService articleReadService;
|
||||||
|
private UserRelationshipQueryService userRelationshipQueryService;
|
||||||
|
private ArticleFavoritesQueryService articleFavoritesQueryService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public ArticleQueryService(ArticleReadService articleReadService,
|
||||||
|
UserRelationshipQueryService userRelationshipQueryService,
|
||||||
|
ArticleFavoritesQueryService articleFavoritesQueryService) {
|
||||||
|
this.articleReadService = articleReadService;
|
||||||
|
this.userRelationshipQueryService = userRelationshipQueryService;
|
||||||
|
this.articleFavoritesQueryService = articleFavoritesQueryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<ArticleData> findById(String id, User user) {
|
||||||
|
ArticleData articleData = articleReadService.ofId(id);
|
||||||
|
if (articleData == null) {
|
||||||
|
return Optional.empty();
|
||||||
|
} else {
|
||||||
|
articleData.setFavorited(articleFavoritesQueryService.isUserFavorite(user.getId(), id));
|
||||||
|
articleData.setFavoritesCount(articleFavoritesQueryService.articleFavoriteCount(id));
|
||||||
|
articleData.getProfileData().setFollowing(
|
||||||
|
userRelationshipQueryService.isUserFollowing(
|
||||||
|
user.getId(),
|
||||||
|
articleData.getProfileData().getId()));
|
||||||
|
return Optional.of(articleData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Mapper
|
||||||
|
public interface ArticleReadService {
|
||||||
|
ArticleData ofId(@Param("id") String id);
|
||||||
|
}
|
18
src/main/java/io/spring/application/profile/ProfileData.java
Normal file
18
src/main/java/io/spring/application/profile/ProfileData.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package io.spring.application.profile;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ProfileData {
|
||||||
|
@JsonIgnore
|
||||||
|
private String id;
|
||||||
|
private String username;
|
||||||
|
private String bio;
|
||||||
|
private String image;
|
||||||
|
private boolean following;
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package io.spring.application.profile;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Mapper
|
||||||
|
public interface UserRelationshipQueryService {
|
||||||
|
boolean isUserFollowing(@Param("userId") String userId, @Param("anotherUserId") String anotherUserId);
|
||||||
|
}
|
@ -11,10 +11,8 @@ import javax.persistence.Id;
|
|||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Entity
|
|
||||||
@JsonRootName("user")
|
@JsonRootName("user")
|
||||||
public class UserData {
|
public class UserData {
|
||||||
@Id
|
|
||||||
private String id;
|
private String id;
|
||||||
private String email;
|
private String email;
|
||||||
private String username;
|
private String username;
|
||||||
|
@ -16,12 +16,12 @@ public class UserQueryService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UserWithToken fetchNewAuthenticatedUser(String username) {
|
public UserWithToken fetchNewAuthenticatedUser(String username) {
|
||||||
UserData userData = userReadService.findOne(username);
|
UserData userData = userReadService.findByUsername(username);
|
||||||
return new UserWithToken(userData, jwtService.toToken(userData));
|
return new UserWithToken(userData, jwtService.toToken(userData));
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserWithToken fetchCurrentUser(String username, String token) {
|
public UserWithToken fetchCurrentUser(String username, String token) {
|
||||||
return new UserWithToken(userReadService.findOne(username), token);
|
return new UserWithToken(userReadService.findByUsername(username), token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package io.spring.application.user;
|
package io.spring.application.user;
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
public interface UserReadService extends CrudRepository<UserData, String> {
|
@Component
|
||||||
|
@Mapper
|
||||||
|
public interface UserReadService {
|
||||||
|
|
||||||
|
UserData findByUsername(@Param("username") String username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
41
src/main/java/io/spring/core/article/Article.java
Normal file
41
src/main/java/io/spring/core/article/Article.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package io.spring.core.article;
|
||||||
|
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
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
|
||||||
|
@NoArgsConstructor
|
||||||
|
@EqualsAndHashCode(of = {"id"})
|
||||||
|
public class Article {
|
||||||
|
private String userId;
|
||||||
|
private String id;
|
||||||
|
private String slug;
|
||||||
|
private String title;
|
||||||
|
private String description;
|
||||||
|
private String body;
|
||||||
|
private List<Tag> tags;
|
||||||
|
private DateTime createdAt;
|
||||||
|
private DateTime updatedAt;
|
||||||
|
|
||||||
|
public Article(String slug, String title, String description, String body, String[] tagList, String userId) {
|
||||||
|
this.id = UUID.randomUUID().toString();
|
||||||
|
this.slug = slug;
|
||||||
|
this.title = title;
|
||||||
|
this.description = description;
|
||||||
|
this.body = body;
|
||||||
|
this.tags = Arrays.stream(tagList).collect(toSet()).stream().map(Tag::new).collect(toList());
|
||||||
|
this.userId = userId;
|
||||||
|
this.createdAt = new DateTime();
|
||||||
|
this.updatedAt = new DateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
src/main/java/io/spring/core/article/ArticleRepository.java
Normal file
11
src/main/java/io/spring/core/article/ArticleRepository.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package io.spring.core.article;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface ArticleRepository {
|
||||||
|
String toSlug(String title);
|
||||||
|
|
||||||
|
void save(Article article);
|
||||||
|
|
||||||
|
Optional<Article> findById(String id);
|
||||||
|
}
|
20
src/main/java/io/spring/core/article/Tag.java
Normal file
20
src/main/java/io/spring/core/article/Tag.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package io.spring.core.article;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@EqualsAndHashCode(of = "name")
|
||||||
|
public class Tag {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Tag(String name) {
|
||||||
|
this.id = UUID.randomUUID().toString();
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -9,7 +9,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@EqualsAndHashCode(of = {"username"})
|
@EqualsAndHashCode(of = {"id"})
|
||||||
public class User {
|
public class User {
|
||||||
private String id;
|
private String id;
|
||||||
private String email;
|
private String email;
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package io.spring.infrastructure.article;
|
||||||
|
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import io.spring.core.article.Tag;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Mapper
|
||||||
|
public interface ArticleMapper {
|
||||||
|
void insert(@Param("article") Article article);
|
||||||
|
|
||||||
|
Article findById(@Param("id") String id);
|
||||||
|
|
||||||
|
boolean findTag(@Param("tagName") String tagName);
|
||||||
|
|
||||||
|
void insertTag(@Param("tag") Tag tag);
|
||||||
|
|
||||||
|
void insertArticleTagRelation(@Param("articleId") String articleId, @Param("tagId") String tagId);
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package io.spring.infrastructure.article;
|
||||||
|
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import io.spring.core.article.ArticleRepository;
|
||||||
|
import io.spring.core.article.Tag;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class MyBatisArticleRepository implements ArticleRepository {
|
||||||
|
private ArticleMapper articleMapper;
|
||||||
|
|
||||||
|
public MyBatisArticleRepository(ArticleMapper articleMapper) {
|
||||||
|
this.articleMapper = articleMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toSlug(String title) {
|
||||||
|
return title.toLowerCase().replace(' ', '-');
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(Article article) {
|
||||||
|
articleMapper.insert(article);
|
||||||
|
for (Tag tag : article.getTags()) {
|
||||||
|
if (!articleMapper.findTag(tag.getName())) {
|
||||||
|
articleMapper.insertTag(tag);
|
||||||
|
}
|
||||||
|
articleMapper.insertArticleTagRelation(article.getId(), tag.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Article> findById(String id) {
|
||||||
|
return Optional.ofNullable(articleMapper.findById(id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package io.spring.infrastructure.mybatis;
|
||||||
|
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.apache.ibatis.type.MappedTypes;
|
||||||
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
import org.joda.time.DateTime;
|
||||||
|
|
||||||
|
import java.sql.CallableStatement;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
@MappedTypes(DateTime.class)
|
||||||
|
public class DateTimeHandler implements TypeHandler<DateTime> {
|
||||||
|
|
||||||
|
private static final Calendar UTC_CALENDAR = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setParameter(PreparedStatement ps, int i, DateTime parameter, JdbcType jdbcType) throws SQLException {
|
||||||
|
ps.setTimestamp(i, parameter != null ? new Timestamp(parameter.getMillis()) : null, UTC_CALENDAR);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DateTime getResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
|
Timestamp timestamp = rs.getTimestamp(columnName, UTC_CALENDAR);
|
||||||
|
return timestamp != null ? new DateTime(timestamp.getTime()) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DateTime getResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
|
Timestamp timestamp = rs.getTimestamp(columnIndex, UTC_CALENDAR);
|
||||||
|
return timestamp != null ? new DateTime(timestamp.getTime()) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DateTime getResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
|
Timestamp ts = cs.getTimestamp(columnIndex, UTC_CALENDAR);
|
||||||
|
return ts != null ? new DateTime(ts.getTime()) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,4 +4,4 @@ image.default=https://static.productionready.io/images/smiley-cyrus.jpg
|
|||||||
jwt.secret=nRvyYC4soFxBdZ-F-5Nnzz5USXstR1YylsTd-mA0aKtI9HUlriGrtkf-TiuDapkLiUCogO3JOK7kwZisrHp6wA
|
jwt.secret=nRvyYC4soFxBdZ-F-5Nnzz5USXstR1YylsTd-mA0aKtI9HUlriGrtkf-TiuDapkLiUCogO3JOK7kwZisrHp6wA
|
||||||
jwt.sessionTime=86400
|
jwt.sessionTime=86400
|
||||||
mybatis.config-location=classpath:mybatis-config.xml
|
mybatis.config-location=classpath:mybatis-config.xml
|
||||||
mybatis.mapper-locations=mapper/*Mapper.xml
|
mybatis.mapper-locations=mapper/*.xml
|
@ -6,3 +6,34 @@ create table users (
|
|||||||
bio text,
|
bio text,
|
||||||
image varchar(511)
|
image varchar(511)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
create table articles (
|
||||||
|
id varchar(255) primary key,
|
||||||
|
user_id varchar(255),
|
||||||
|
slug varchar(255) UNIQUE,
|
||||||
|
title varchar(255),
|
||||||
|
description text,
|
||||||
|
body text,
|
||||||
|
created_at TIMESTAMP NOT NULL,
|
||||||
|
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
create table article_favorites (
|
||||||
|
article_id varchar(255) not null,
|
||||||
|
user_id varchar(255) not null
|
||||||
|
);
|
||||||
|
|
||||||
|
create table follows (
|
||||||
|
user_id varchar(255) not null,
|
||||||
|
follow_id varchar(255) not null
|
||||||
|
);
|
||||||
|
|
||||||
|
create table tags (
|
||||||
|
id varchar(255) primary key,
|
||||||
|
name varchar(255)
|
||||||
|
);
|
||||||
|
|
||||||
|
create table article_tags (
|
||||||
|
article_id varchar(255) not null,
|
||||||
|
tag_id varchar(255) not null
|
||||||
|
);
|
||||||
|
11
src/main/resources/mapper/ArticleFavoritesQueryService.xml
Normal file
11
src/main/resources/mapper/ArticleFavoritesQueryService.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="io.spring.application.article.ArticleFavoritesQueryService">
|
||||||
|
<select id="isUserFavorite" resultType="java.lang.Boolean">
|
||||||
|
select count(1) from article_favorites where user_id = #{userId} and article_id = #{articleId}
|
||||||
|
</select>
|
||||||
|
<select id="articleFavoriteCount" resultType="java.lang.Integer">
|
||||||
|
select count(1) from article_favorites where article_id = #{articleId}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
</mapper>
|
60
src/main/resources/mapper/ArticleMapper.xml
Normal file
60
src/main/resources/mapper/ArticleMapper.xml
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="io.spring.infrastructure.article.ArticleMapper">
|
||||||
|
<insert id="insert">
|
||||||
|
insert into articles(id, slug, title, description, body, user_id, created_at, updated_at)
|
||||||
|
values(
|
||||||
|
#{article.id},
|
||||||
|
#{article.slug},
|
||||||
|
#{article.title},
|
||||||
|
#{article.description},
|
||||||
|
#{article.body},
|
||||||
|
#{article.userId},
|
||||||
|
#{article.createdAt},
|
||||||
|
#{article.updatedAt})
|
||||||
|
</insert>
|
||||||
|
<insert id="insertTag">
|
||||||
|
insert into tags (id, name) values (#{tag.id}, #{tag.name})
|
||||||
|
</insert>
|
||||||
|
<insert id="insertArticleTagRelation">
|
||||||
|
insert into article_tags (article_id, tag_id) values(#{articleId}, #{tagId})
|
||||||
|
</insert>
|
||||||
|
<select id="findById" resultMap="article">
|
||||||
|
select
|
||||||
|
A.id articleId,
|
||||||
|
A.slug articleSlug,
|
||||||
|
A.title articleTitle,
|
||||||
|
A.description articleDescription,
|
||||||
|
A.body articleBody,
|
||||||
|
A.user_id articleUserId,
|
||||||
|
A.created_at articleCreatedAt,
|
||||||
|
A.updated_at articleUpdatedAt,
|
||||||
|
T.id tagId,
|
||||||
|
T.name tagName
|
||||||
|
from articles A
|
||||||
|
left join article_tags AT on A.id = AT.article_id
|
||||||
|
left join tags T on T.id = AT.tag_id
|
||||||
|
where A.id = #{id}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="findTag" resultType="java.lang.Boolean">
|
||||||
|
select count(*) from tags where name = #{tagName}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<resultMap id="article" type="io.spring.core.article.Article">
|
||||||
|
<id column="articleId" property="id"/>
|
||||||
|
<result column="articleUserId" property="userId"/>
|
||||||
|
<result column="articleTitle" property="title"/>
|
||||||
|
<result column="articleSlug" property="slug"/>
|
||||||
|
<result column="articleDescription" property="description"/>
|
||||||
|
<result column="articleBody" property="body"/>
|
||||||
|
<result column="articleCreatedAt" property="createdAt"/>
|
||||||
|
<result column="articleUpdatedAt" property="updatedAt"/>
|
||||||
|
<collection property="tags" ofType="arraylist" resultMap="tag"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<resultMap id="tag" type="io.spring.core.article.Tag">
|
||||||
|
<id column="tagId" property="id"/>
|
||||||
|
<result column="tagName" property="name"/>
|
||||||
|
</resultMap>
|
||||||
|
</mapper>
|
44
src/main/resources/mapper/ArticleReadService.xml
Normal file
44
src/main/resources/mapper/ArticleReadService.xml
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="io.spring.application.article.ArticleReadService">
|
||||||
|
<select id="ofId" resultMap="articleData">
|
||||||
|
select
|
||||||
|
A.id articleId,
|
||||||
|
A.slug articleSlug,
|
||||||
|
A.title articleTitle,
|
||||||
|
A.description articleDescription,
|
||||||
|
A.body articleBody,
|
||||||
|
A.created_at articleCreatedAt,
|
||||||
|
A.updated_at articleUpdatedAt,
|
||||||
|
T.name as tagName,
|
||||||
|
U.id userId,
|
||||||
|
U.username userUsername,
|
||||||
|
U.bio userBio,
|
||||||
|
U.image userImage
|
||||||
|
from
|
||||||
|
articles A
|
||||||
|
left join article_tags AT on A.id = AT.article_id
|
||||||
|
left join tags T on T.id = AT.article_id
|
||||||
|
left join users U on U.id = A.user_id
|
||||||
|
where A.id = #{id}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<resultMap id="articleData" type="io.spring.application.article.ArticleData">
|
||||||
|
<id column="articleId" property="id"/>
|
||||||
|
<result column="articleSlug" property="slug"/>
|
||||||
|
<result column="articleTitle" property="title"/>
|
||||||
|
<result column="articleDescription" property="description"/>
|
||||||
|
<result column="articleBody" property="body"/>
|
||||||
|
<result column="articleCreatedAt" property="createdAt"/>
|
||||||
|
<result column="articleUpdatedAt" property="updatedAt"/>
|
||||||
|
<association property="profileData" resultMap="profileData"/>
|
||||||
|
<collection property="tagList" ofType="arraylist" column="tagName" javaType="string"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<resultMap id="profileData" type="io.spring.application.profile.ProfileData">
|
||||||
|
<id column="userId" property="id"/>
|
||||||
|
<result column="userUsername" property="username"/>
|
||||||
|
<result column="userBio" property="bio"/>
|
||||||
|
<result column="userImage" property="image"/>
|
||||||
|
</resultMap>
|
||||||
|
</mapper>
|
7
src/main/resources/mapper/UserReadService.xml
Normal file
7
src/main/resources/mapper/UserReadService.xml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="io.spring.application.user.UserReadService">
|
||||||
|
<select id="findByUsername" resultType="io.spring.application.user.UserData">
|
||||||
|
select * from users where username = #{username}
|
||||||
|
</select>
|
||||||
|
</mapper>
|
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="io.spring.application.profile.UserRelationshipQueryService">
|
||||||
|
<select id="isUserFollowing" resultType="java.lang.Boolean">
|
||||||
|
select count(1) from follows where user_id = #{userId} and follow_id = #{anotherUserId}
|
||||||
|
</select>
|
||||||
|
</mapper>
|
@ -17,6 +17,11 @@
|
|||||||
<setting name="useGeneratedKeys" value="true"/>
|
<setting name="useGeneratedKeys" value="true"/>
|
||||||
</settings>
|
</settings>
|
||||||
|
|
||||||
|
<typeHandlers>
|
||||||
|
<typeHandler handler="io.spring.infrastructure.mybatis.DateTimeHandler"
|
||||||
|
javaType="org.joda.time.DateTime"/>
|
||||||
|
</typeHandlers>
|
||||||
|
|
||||||
<!-- Continue going here -->
|
<!-- Continue going here -->
|
||||||
|
|
||||||
</configuration>
|
</configuration>
|
||||||
|
134
src/test/java/io/spring/api/ArticlesApiTest.java
Normal file
134
src/test/java/io/spring/api/ArticlesApiTest.java
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
package io.spring.api;
|
||||||
|
|
||||||
|
import io.restassured.RestAssured;
|
||||||
|
import io.spring.application.article.ArticleData;
|
||||||
|
import io.spring.application.article.ArticleQueryService;
|
||||||
|
import io.spring.application.profile.ProfileData;
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import io.spring.core.article.ArticleRepository;
|
||||||
|
import org.joda.time.DateTime;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static io.restassured.RestAssured.given;
|
||||||
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||||
|
public class ArticlesApiTest extends TestWithCurrentUser {
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private ArticleRepository articleRepository;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
|
protected String email;
|
||||||
|
protected String username;
|
||||||
|
protected String defaultAvatar;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
RestAssured.port = port;
|
||||||
|
email = "john@jacob.com";
|
||||||
|
username = "johnjacob";
|
||||||
|
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||||
|
userFixture(email, username, defaultAvatar);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_create_article_success() throws Exception {
|
||||||
|
String title = "How to train your dragon";
|
||||||
|
String slug = "how-to-train-your-dragon";
|
||||||
|
String description = "Ever wonder how?";
|
||||||
|
String body = "You have to believe";
|
||||||
|
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
||||||
|
Map<String, Object> param = prepareParam(title, description, body, tagList);
|
||||||
|
|
||||||
|
when(articleRepository.toSlug(eq(title))).thenReturn(slug);
|
||||||
|
|
||||||
|
ArticleData articleData = new ArticleData(
|
||||||
|
"123",
|
||||||
|
slug,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
body,
|
||||||
|
false,
|
||||||
|
0,
|
||||||
|
new DateTime(),
|
||||||
|
new DateTime(),
|
||||||
|
Arrays.asList(tagList),
|
||||||
|
new ProfileData("userid", user.getUsername(), user.getBio(), user.getImage(), false));
|
||||||
|
|
||||||
|
when(articleQueryService.findById(any(), any())).thenReturn(Optional.of(articleData));
|
||||||
|
|
||||||
|
given()
|
||||||
|
.contentType("application/json")
|
||||||
|
.header("Authorization", "Token " + token)
|
||||||
|
.body(param)
|
||||||
|
.when()
|
||||||
|
.post("/articles")
|
||||||
|
.then()
|
||||||
|
.statusCode(200)
|
||||||
|
.body("article.title", equalTo(title))
|
||||||
|
.body("article.favorited", equalTo(false))
|
||||||
|
.body("article.body", equalTo(body))
|
||||||
|
.body("article.favoritesCount", equalTo(0))
|
||||||
|
.body("article.author.username", equalTo(user.getUsername()))
|
||||||
|
.body("article.author.id", equalTo(null));
|
||||||
|
|
||||||
|
verify(articleRepository).save(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_get_error_message_with_wrong_parameter() throws Exception {
|
||||||
|
String title = "How to train your dragon";
|
||||||
|
String slug = "how-to-train-your-dragon";
|
||||||
|
String description = "Ever wonder how?";
|
||||||
|
String body = "";
|
||||||
|
String[] tagList = {"reactjs", "angularjs", "dragons"};
|
||||||
|
Map<String, Object> param = prepareParam(title, description, body, tagList);
|
||||||
|
|
||||||
|
when(articleRepository.toSlug(eq(title))).thenReturn(slug);
|
||||||
|
|
||||||
|
given()
|
||||||
|
.contentType("application/json")
|
||||||
|
.header("Authorization", "Token " + token)
|
||||||
|
.body(param)
|
||||||
|
.when()
|
||||||
|
.post("/articles")
|
||||||
|
.then()
|
||||||
|
.statusCode(422)
|
||||||
|
.body("errors.body[0]", equalTo("can't be empty"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private HashMap<String, Object> prepareParam(final String title, final String description, final String body, final String[] tagList) {
|
||||||
|
return new HashMap<String, Object>() {{
|
||||||
|
put("article", new HashMap<String, Object>() {{
|
||||||
|
put("title", title);
|
||||||
|
put("description", description);
|
||||||
|
put("body", body);
|
||||||
|
put("tagList", tagList);
|
||||||
|
}});
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
}
|
@ -12,56 +12,35 @@ import org.junit.runner.RunWith;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.RestAssured.given;
|
||||||
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 static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.mockito.Matchers.eq;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
public class CurrentUserApiTest {
|
public class CurrentUserApiTest extends TestWithCurrentUser {
|
||||||
@MockBean
|
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private UserReadService userReadService;
|
|
||||||
|
|
||||||
@LocalServerPort
|
@LocalServerPort
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
@Autowired
|
protected String email;
|
||||||
private JwtService jwtService;
|
protected String username;
|
||||||
private User user;
|
protected String defaultAvatar;
|
||||||
private UserData userData;
|
|
||||||
private String token;
|
|
||||||
private String email;
|
|
||||||
private String username;
|
|
||||||
private String defaultAvatar;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
RestAssured.port = port;
|
||||||
email = "john@jacob.com";
|
email = "john@jacob.com";
|
||||||
username = "johnjacob";
|
username = "johnjacob";
|
||||||
|
|
||||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||||
user = new User(email, username, "123", "", defaultAvatar);
|
userFixture(email, username, defaultAvatar);
|
||||||
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
|
|
||||||
|
|
||||||
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
|
||||||
when(userReadService.findOne(eq(username))).thenReturn(userData);
|
|
||||||
|
|
||||||
token = jwtService.toToken(userData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
39
src/test/java/io/spring/api/TestWithCurrentUser.java
Normal file
39
src/test/java/io/spring/api/TestWithCurrentUser.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package io.spring.api;
|
||||||
|
|
||||||
|
import io.spring.application.JwtService;
|
||||||
|
import io.spring.application.user.UserData;
|
||||||
|
import io.spring.application.user.UserReadService;
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
import io.spring.core.user.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
class TestWithCurrentUser {
|
||||||
|
@MockBean
|
||||||
|
protected UserRepository userRepository;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
protected UserReadService userReadService;
|
||||||
|
|
||||||
|
protected User user;
|
||||||
|
protected UserData userData;
|
||||||
|
protected String token;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JwtService jwtService;
|
||||||
|
|
||||||
|
protected void userFixture(String email, String username, String defaultAvatar) {
|
||||||
|
user = new User(email, username, "123", "", defaultAvatar);
|
||||||
|
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
|
||||||
|
|
||||||
|
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
||||||
|
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
|
||||||
|
|
||||||
|
token = jwtService.toToken(userData);
|
||||||
|
}
|
||||||
|
}
|
@ -55,7 +55,7 @@ public class UsersApiTest {
|
|||||||
|
|
||||||
when(jwtService.toToken(any())).thenReturn("123");
|
when(jwtService.toToken(any())).thenReturn("123");
|
||||||
UserData userData = new UserData("123", email, username, "", defaultAvatar);
|
UserData userData = new UserData("123", email, username, "", defaultAvatar);
|
||||||
when(userReadService.findOne(eq(username))).thenReturn(userData);
|
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
|
||||||
|
|
||||||
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());
|
||||||
@ -178,7 +178,7 @@ public class UsersApiTest {
|
|||||||
UserData userData = new UserData("123", email, username, "", defaultAvatar);
|
UserData userData = new UserData("123", email, username, "", defaultAvatar);
|
||||||
|
|
||||||
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
|
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
|
||||||
when(userReadService.findOne(eq(username))).thenReturn(userData);
|
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
|
||||||
when(jwtService.toToken(any())).thenReturn("123");
|
when(jwtService.toToken(any())).thenReturn("123");
|
||||||
|
|
||||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
Map<String, Object> param = new HashMap<String, Object>() {{
|
||||||
@ -212,7 +212,7 @@ public class UsersApiTest {
|
|||||||
UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
||||||
|
|
||||||
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
|
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
|
||||||
when(userReadService.findOne(eq(username))).thenReturn(userData);
|
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
|
||||||
|
|
||||||
Map<String, Object> param = new HashMap<String, Object>() {{
|
Map<String, Object> param = new HashMap<String, Object>() {{
|
||||||
put("user", new HashMap<String, Object>() {{
|
put("user", new HashMap<String, Object>() {{
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
package io.spring.application.article;
|
||||||
|
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import io.spring.core.article.ArticleRepository;
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
import io.spring.core.user.UserRepository;
|
||||||
|
import io.spring.infrastructure.article.MyBatisArticleRepository;
|
||||||
|
import io.spring.infrastructure.user.MyBatisUserRepository;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@MybatisTest
|
||||||
|
@Import({ArticleQueryService.class, MyBatisUserRepository.class, MyBatisArticleRepository.class})
|
||||||
|
public class ArticleQueryServiceTest {
|
||||||
|
@Autowired
|
||||||
|
private ArticleQueryService queryService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ArticleRepository articleRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_fetch_article_success() throws Exception {
|
||||||
|
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
Article article = new Article("test", "test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||||
|
articleRepository.save(article);
|
||||||
|
|
||||||
|
Optional<ArticleData> optional = queryService.findById(article.getId(), user);
|
||||||
|
assertThat(optional.isPresent(), is(true));
|
||||||
|
ArticleData fetched = optional.get();
|
||||||
|
assertThat(fetched.getFavoritesCount(), is(0));
|
||||||
|
assertThat(fetched.isFavorited(), is(false));
|
||||||
|
assertThat(fetched.getCreatedAt(), notNullValue());
|
||||||
|
assertThat(fetched.getUpdatedAt(), notNullValue());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package io.spring.infrastructure.article;
|
||||||
|
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import io.spring.core.article.ArticleRepository;
|
||||||
|
import io.spring.core.article.Tag;
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
import io.spring.core.user.UserRepository;
|
||||||
|
import io.spring.infrastructure.user.MyBatisUserRepository;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@MybatisTest
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@Import({MyBatisArticleRepository.class, MyBatisUserRepository.class})
|
||||||
|
public class MyBatisArticleRepositoryTest {
|
||||||
|
@Autowired
|
||||||
|
private ArticleRepository articleRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_create_and_fetch_article_success() throws Exception {
|
||||||
|
Article article = new Article("test", "test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||||
|
articleRepository.save(article);
|
||||||
|
Optional<Article> optional = articleRepository.findById(article.getId());
|
||||||
|
assertThat(optional.isPresent(), is(true));
|
||||||
|
assertThat(optional.get(), is(article));
|
||||||
|
assertThat(optional.get().getTags().contains(new Tag("java")), is(true));
|
||||||
|
assertThat(optional.get().getTags().contains(new Tag("spring")), is(true));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user