should update article
This commit is contained in:
parent
ca2683abab
commit
6c41584abe
@ -1,25 +1,37 @@
|
|||||||
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.ResourceNotFoundException;
|
import io.spring.api.exception.ResourceNotFoundException;
|
||||||
|
import io.spring.application.AuthorizationService;
|
||||||
import io.spring.application.article.ArticleData;
|
import io.spring.application.article.ArticleData;
|
||||||
import io.spring.application.article.ArticleQueryService;
|
import io.spring.application.article.ArticleQueryService;
|
||||||
|
import io.spring.core.article.ArticleRepository;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
|
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.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
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;
|
||||||
|
|
||||||
@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;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ArticleApi(ArticleQueryService articleQueryService) {
|
public ArticleApi(ArticleQueryService articleQueryService, ArticleRepository articleRepository) {
|
||||||
this.articleQueryService = articleQueryService;
|
this.articleQueryService = articleQueryService;
|
||||||
|
this.articleRepository = articleRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@ -27,4 +39,30 @@ public class ArticleApi {
|
|||||||
@AuthenticationPrincipal User user) {
|
@AuthenticationPrincipal User user) {
|
||||||
return articleQueryService.findBySlug(slug, user).map(ResponseEntity::ok).orElseThrow(ResourceNotFoundException::new);
|
return articleQueryService.findBySlug(slug, user).map(ResponseEntity::ok).orElseThrow(ResourceNotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
public ResponseEntity<ArticleData> updateArticle(@PathVariable("slug") String slug,
|
||||||
|
@AuthenticationPrincipal User user,
|
||||||
|
@Valid @RequestBody UpdateArticleParam updateArticleParam) {
|
||||||
|
return articleRepository.findBySlug(slug).map(article -> {
|
||||||
|
if (!AuthorizationService.canUpdateArticle(user, article)) {
|
||||||
|
throw new NoAuthorizationException();
|
||||||
|
}
|
||||||
|
article.update(
|
||||||
|
updateArticleParam.getTitle(),
|
||||||
|
updateArticleParam.getDescription(),
|
||||||
|
updateArticleParam.getBody());
|
||||||
|
articleRepository.save(article);
|
||||||
|
return ResponseEntity.ok(articleQueryService.findBySlug(slug, user).get());
|
||||||
|
}).orElseThrow(ResourceNotFoundException::new);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@JsonRootName("article")
|
||||||
|
class UpdateArticleParam {
|
||||||
|
private String title = "";
|
||||||
|
private String body = "";
|
||||||
|
private String description = "";
|
||||||
}
|
}
|
||||||
|
@ -41,8 +41,6 @@ public class ArticlesApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Article article = new Article(
|
Article article = new Article(
|
||||||
articleRepository.toSlug(
|
|
||||||
newArticleParam.getTitle()),
|
|
||||||
newArticleParam.getTitle(),
|
newArticleParam.getTitle(),
|
||||||
newArticleParam.getDescription(),
|
newArticleParam.getDescription(),
|
||||||
newArticleParam.getBody(),
|
newArticleParam.getBody(),
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package io.spring.api.exception;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||||
|
public class NoAuthorizationException extends RuntimeException {
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package io.spring.application;
|
||||||
|
|
||||||
|
import io.spring.core.article.Article;
|
||||||
|
import io.spring.core.user.User;
|
||||||
|
|
||||||
|
public class AuthorizationService {
|
||||||
|
public static boolean canUpdateArticle(User user, Article article) {
|
||||||
|
return user.getId().equals(article.getUserId());
|
||||||
|
}
|
||||||
|
}
|
@ -26,9 +26,9 @@ public class Article {
|
|||||||
private DateTime createdAt;
|
private DateTime createdAt;
|
||||||
private DateTime updatedAt;
|
private DateTime updatedAt;
|
||||||
|
|
||||||
public Article(String slug, String title, String description, String body, String[] tagList, String userId) {
|
public Article(String title, String description, String body, String[] tagList, String userId) {
|
||||||
this.id = UUID.randomUUID().toString();
|
this.id = UUID.randomUUID().toString();
|
||||||
this.slug = slug;
|
this.slug = toSlug(title);
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.body = body;
|
this.body = body;
|
||||||
@ -38,4 +38,20 @@ public class Article {
|
|||||||
this.updatedAt = new DateTime();
|
this.updatedAt = new DateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void update(String title, String description, String body) {
|
||||||
|
if (!"".equals(title)) {
|
||||||
|
this.title = title;
|
||||||
|
this.slug = toSlug(title);
|
||||||
|
}
|
||||||
|
if (!"".equals(description)) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
if (!"".equals(body)) {
|
||||||
|
this.body = body;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String toSlug(String title) {
|
||||||
|
return title.toLowerCase().replace(' ', '-');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,10 @@ package io.spring.core.article;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface ArticleRepository {
|
public interface ArticleRepository {
|
||||||
String toSlug(String title);
|
|
||||||
|
|
||||||
void save(Article article);
|
void save(Article article);
|
||||||
|
|
||||||
Optional<Article> findById(String id);
|
Optional<Article> findById(String id);
|
||||||
|
|
||||||
|
Optional<Article> findBySlug(String slug);
|
||||||
}
|
}
|
||||||
|
@ -18,4 +18,8 @@ public interface ArticleMapper {
|
|||||||
void insertTag(@Param("tag") Tag tag);
|
void insertTag(@Param("tag") Tag tag);
|
||||||
|
|
||||||
void insertArticleTagRelation(@Param("articleId") String articleId, @Param("tagId") String tagId);
|
void insertArticleTagRelation(@Param("articleId") String articleId, @Param("tagId") String tagId);
|
||||||
|
|
||||||
|
Article findBySlug(@Param("slug") String slug);
|
||||||
|
|
||||||
|
void update(@Param("article") Article article);
|
||||||
}
|
}
|
||||||
|
@ -16,12 +16,15 @@ public class MyBatisArticleRepository implements ArticleRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toSlug(String title) {
|
public void save(Article article) {
|
||||||
return title.toLowerCase().replace(' ', '-');
|
if (articleMapper.findById(article.getId()) == null) {
|
||||||
|
createNew(article);
|
||||||
|
} else {
|
||||||
|
articleMapper.update(article);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void createNew(Article article) {
|
||||||
public void save(Article article) {
|
|
||||||
articleMapper.insert(article);
|
articleMapper.insert(article);
|
||||||
for (Tag tag : article.getTags()) {
|
for (Tag tag : article.getTags()) {
|
||||||
if (!articleMapper.findTag(tag.getName())) {
|
if (!articleMapper.findTag(tag.getName())) {
|
||||||
@ -35,4 +38,9 @@ public class MyBatisArticleRepository implements ArticleRepository {
|
|||||||
public Optional<Article> findById(String id) {
|
public Optional<Article> findById(String id) {
|
||||||
return Optional.ofNullable(articleMapper.findById(id));
|
return Optional.ofNullable(articleMapper.findById(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Article> findBySlug(String slug) {
|
||||||
|
return Optional.ofNullable(articleMapper.findBySlug(slug));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,27 +19,45 @@
|
|||||||
<insert id="insertArticleTagRelation">
|
<insert id="insertArticleTagRelation">
|
||||||
insert into article_tags (article_id, tag_id) values(#{articleId}, #{tagId})
|
insert into article_tags (article_id, tag_id) values(#{articleId}, #{tagId})
|
||||||
</insert>
|
</insert>
|
||||||
<select id="findById" resultMap="article">
|
<update id="update">
|
||||||
|
update articles
|
||||||
|
<set>
|
||||||
|
<if test="article.title != ''">title = #{article.title},</if>
|
||||||
|
<if test="article.title != ''">slug = #{article.slug},</if>
|
||||||
|
<if test="article.description != ''">description = #{article.description},</if>
|
||||||
|
<if test="article.body != ''">body = #{article.body}</if>
|
||||||
|
</set>
|
||||||
|
where id = #{article.id}
|
||||||
|
</update>
|
||||||
|
<sql id="selectArticle">
|
||||||
select
|
select
|
||||||
A.id articleId,
|
A.id articleId,
|
||||||
A.slug articleSlug,
|
A.slug articleSlug,
|
||||||
A.title articleTitle,
|
A.title articleTitle,
|
||||||
A.description articleDescription,
|
A.description articleDescription,
|
||||||
A.body articleBody,
|
A.body articleBody,
|
||||||
A.user_id articleUserId,
|
A.user_id articleUserId,
|
||||||
A.created_at articleCreatedAt,
|
A.created_at articleCreatedAt,
|
||||||
A.updated_at articleUpdatedAt,
|
A.updated_at articleUpdatedAt,
|
||||||
T.id tagId,
|
T.id tagId,
|
||||||
T.name tagName
|
T.name tagName
|
||||||
from articles A
|
from articles A
|
||||||
left join article_tags AT on A.id = AT.article_id
|
left join article_tags AT on A.id = AT.article_id
|
||||||
left join tags T on T.id = AT.tag_id
|
left join tags T on T.id = AT.tag_id
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<select id="findById" resultMap="article">
|
||||||
|
<include refid="selectArticle"/>
|
||||||
where A.id = #{id}
|
where A.id = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findTag" resultType="java.lang.Boolean">
|
<select id="findTag" resultType="java.lang.Boolean">
|
||||||
select count(*) from tags where name = #{tagName}
|
select count(*) from tags where name = #{tagName}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="findBySlug" resultMap="article">
|
||||||
|
<include refid="selectArticle"/>
|
||||||
|
where A.slug = #{slug}
|
||||||
|
</select>
|
||||||
|
|
||||||
<resultMap id="article" type="io.spring.core.article.Article">
|
<resultMap id="article" type="io.spring.core.article.Article">
|
||||||
<id column="articleId" property="id"/>
|
<id column="articleId" property="id"/>
|
||||||
|
@ -6,6 +6,7 @@ import io.spring.application.article.ArticleQueryService;
|
|||||||
import io.spring.application.profile.ProfileData;
|
import io.spring.application.profile.ProfileData;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
|
import io.spring.core.user.User;
|
||||||
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;
|
||||||
@ -22,7 +23,6 @@ import java.util.Optional;
|
|||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.RestAssured.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.junit.Assert.*;
|
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
@ -64,8 +64,6 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
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, tagList);
|
||||||
|
|
||||||
when(articleRepository.toSlug(eq(title))).thenReturn(slug);
|
|
||||||
|
|
||||||
ArticleData articleData = new ArticleData(
|
ArticleData articleData = new ArticleData(
|
||||||
"123",
|
"123",
|
||||||
slug,
|
slug,
|
||||||
@ -102,14 +100,11 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
@Test
|
@Test
|
||||||
public void should_get_error_message_with_wrong_parameter() throws Exception {
|
public void should_get_error_message_with_wrong_parameter() throws Exception {
|
||||||
String title = "How to train your dragon";
|
String title = "How to train your dragon";
|
||||||
String slug = "how-to-train-your-dragon";
|
|
||||||
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, tagList);
|
||||||
|
|
||||||
when(articleRepository.toSlug(eq(title))).thenReturn(slug);
|
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
.header("Authorization", "Token " + token)
|
.header("Authorization", "Token " + token)
|
||||||
@ -122,21 +117,10 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}});
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
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";
|
||||||
Article article = new Article(slug, "Test New Article", "Desc", "Body", new String[]{"java", "spring", "jpg"}, user.getId());
|
Article article = new Article("Test New Article", "Desc", "Body", new String[]{"java", "spring", "jpg"}, user.getId());
|
||||||
|
|
||||||
DateTime time = new DateTime();
|
DateTime time = new DateTime();
|
||||||
ArticleData articleData = new ArticleData(
|
ArticleData articleData = new ArticleData(
|
||||||
@ -172,4 +156,100 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
.then()
|
.then()
|
||||||
.statusCode(404);
|
.statusCode(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_update_article_content_success() throws Exception {
|
||||||
|
String title = "new-title";
|
||||||
|
String body = "new body";
|
||||||
|
String description = "new description";
|
||||||
|
Map<String, Object> updateParam = prepareUpdateParam(title, body, description);
|
||||||
|
|
||||||
|
Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, user.getId());
|
||||||
|
|
||||||
|
DateTime time = new DateTime();
|
||||||
|
ArticleData articleData = new ArticleData(
|
||||||
|
article.getId(),
|
||||||
|
article.getSlug(),
|
||||||
|
article.getTitle(),
|
||||||
|
article.getDescription(),
|
||||||
|
article.getBody(),
|
||||||
|
false,
|
||||||
|
0,
|
||||||
|
time,
|
||||||
|
time,
|
||||||
|
Arrays.asList("joda"),
|
||||||
|
new ProfileData(user.getId(), user.getUsername(), user.getBio(), user.getImage(), false));
|
||||||
|
|
||||||
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
|
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
|
||||||
|
|
||||||
|
given()
|
||||||
|
.contentType("application/json")
|
||||||
|
.header("Authorization", "Token " + token)
|
||||||
|
.body(updateParam)
|
||||||
|
.when()
|
||||||
|
.put("/articles/{slug}", article.getSlug())
|
||||||
|
.then()
|
||||||
|
.statusCode(200)
|
||||||
|
.body("article.slug", equalTo(articleData.getSlug()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_get_403_if_not_author_to_update_article() throws Exception {
|
||||||
|
String title = "new-title";
|
||||||
|
String body = "new body";
|
||||||
|
String description = "new description";
|
||||||
|
Map<String, Object> updateParam = prepareUpdateParam(title, body, description);
|
||||||
|
|
||||||
|
User anotherUser = new User("test@test.com", "test", "123123", "", "");
|
||||||
|
|
||||||
|
Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, anotherUser.getId());
|
||||||
|
|
||||||
|
DateTime time = new DateTime();
|
||||||
|
ArticleData articleData = new ArticleData(
|
||||||
|
article.getId(),
|
||||||
|
article.getSlug(),
|
||||||
|
article.getTitle(),
|
||||||
|
article.getDescription(),
|
||||||
|
article.getBody(),
|
||||||
|
false,
|
||||||
|
0,
|
||||||
|
time,
|
||||||
|
time,
|
||||||
|
Arrays.asList("joda"),
|
||||||
|
new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false));
|
||||||
|
|
||||||
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
|
when(articleQueryService.findBySlug(eq(article.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
|
||||||
|
|
||||||
|
given()
|
||||||
|
.contentType("application/json")
|
||||||
|
.header("Authorization", "Token " + token)
|
||||||
|
.body(updateParam)
|
||||||
|
.when()
|
||||||
|
.put("/articles/{slug}", article.getSlug())
|
||||||
|
.then()
|
||||||
|
.statusCode(403);
|
||||||
|
}
|
||||||
|
|
||||||
|
private HashMap<String, Object> prepareUpdateParam(final String title, final String body, final String description) {
|
||||||
|
return new HashMap<String, Object>() {{
|
||||||
|
put("article", new HashMap<String, Object>() {{
|
||||||
|
put("title", title);
|
||||||
|
put("body", body);
|
||||||
|
put("description", description);
|
||||||
|
}});
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}});
|
||||||
|
}};
|
||||||
|
}
|
||||||
}
|
}
|
@ -37,7 +37,7 @@ public class ArticleQueryServiceTest {
|
|||||||
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
Article article = new Article("test", "test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||||
articleRepository.save(article);
|
articleRepository.save(article);
|
||||||
|
|
||||||
Optional<ArticleData> optional = queryService.findById(article.getId(), user);
|
Optional<ArticleData> optional = queryService.findById(article.getId(), user);
|
||||||
|
@ -17,6 +17,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.CoreMatchers.not;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
@MybatisTest
|
@MybatisTest
|
||||||
@ -29,17 +30,18 @@ public class MyBatisArticleRepositoryTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
private User user;
|
private User user;
|
||||||
|
private Article article;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_create_and_fetch_article_success() throws Exception {
|
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);
|
articleRepository.save(article);
|
||||||
Optional<Article> optional = articleRepository.findById(article.getId());
|
Optional<Article> optional = articleRepository.findById(article.getId());
|
||||||
assertThat(optional.isPresent(), is(true));
|
assertThat(optional.isPresent(), is(true));
|
||||||
@ -47,4 +49,19 @@ public class MyBatisArticleRepositoryTest {
|
|||||||
assertThat(optional.get().getTags().contains(new Tag("java")), is(true));
|
assertThat(optional.get().getTags().contains(new Tag("java")), is(true));
|
||||||
assertThat(optional.get().getTags().contains(new Tag("spring")), is(true));
|
assertThat(optional.get().getTags().contains(new Tag("spring")), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_update_and_fetch_article_success() throws Exception {
|
||||||
|
articleRepository.save(article);
|
||||||
|
|
||||||
|
String newTitle = "new test 2";
|
||||||
|
article.update(newTitle, "", "");
|
||||||
|
articleRepository.save(article);
|
||||||
|
System.out.println(article.getSlug());
|
||||||
|
Optional<Article> optional = articleRepository.findBySlug(article.getSlug());
|
||||||
|
assertThat(optional.isPresent(), is(true));
|
||||||
|
Article fetched = optional.get();
|
||||||
|
assertThat(fetched.getTitle(), is(newTitle));
|
||||||
|
assertThat(fetched.getBody(), not(""));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user