should update article
This commit is contained in:
@@ -6,6 +6,7 @@ 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 io.spring.core.user.User;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -22,7 +23,6 @@ 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.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
@@ -64,8 +64,6 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
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,
|
||||
@@ -102,14 +100,11 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
@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)
|
||||
@@ -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
|
||||
public void should_read_article_success() throws Exception {
|
||||
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();
|
||||
ArticleData articleData = new ArticleData(
|
||||
@@ -172,4 +156,100 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
||||
.then()
|
||||
.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", "", "");
|
||||
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);
|
||||
|
||||
Optional<ArticleData> optional = queryService.findById(article.getId(), user);
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@MybatisTest
|
||||
@@ -29,17 +30,18 @@ public class MyBatisArticleRepositoryTest {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
private User user;
|
||||
private Article article;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
|
||||
userRepository.save(user);
|
||||
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
|
||||
}
|
||||
|
||||
@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));
|
||||
@@ -47,4 +49,19 @@ public class MyBatisArticleRepositoryTest {
|
||||
assertThat(optional.get().getTags().contains(new Tag("java")), 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(""));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user