From 48b59fb81a461a0091d7ac90a441e93bb7ecbc20 Mon Sep 17 00:00:00 2001 From: Koda Date: Wed, 4 Oct 2017 18:51:44 +0900 Subject: [PATCH] Add integration test --- .../control/open/KnowledgeControl.java | 4 +- .../control/protect/NotificationControl.java | 7 +- .../integration/IntegrationCommon.java | 65 +++- .../IntegrationNotificationTest.java | 281 ++++++++++++++++++ .../integration/IntegrationSurveyTest.java | 3 +- 5 files changed, 352 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/support/project/knowledge/integration/IntegrationNotificationTest.java diff --git a/src/main/java/org/support/project/knowledge/control/open/KnowledgeControl.java b/src/main/java/org/support/project/knowledge/control/open/KnowledgeControl.java index f26c4a341..f05558c33 100644 --- a/src/main/java/org/support/project/knowledge/control/open/KnowledgeControl.java +++ b/src/main/java/org/support/project/knowledge/control/open/KnowledgeControl.java @@ -691,7 +691,7 @@ public Boundary likecomment() throws InvalidParamException { * @return * @throws ParseException */ - @Post + @Post(subscribeToken = "") public Boundary escape(KnowledgesEntity entity) throws ParseException { super.setSendEscapeHtml(false); entity.setTitle(sanitize(entity.getTitle())); @@ -706,7 +706,7 @@ public Boundary escape(KnowledgesEntity entity) throws ParseException { * @return * @throws ParseException */ - @Post + @Post(subscribeToken = "") public Boundary marked(KnowledgesEntity entity) throws ParseException { super.setSendEscapeHtml(false); entity.setTitle(sanitize(entity.getTitle())); diff --git a/src/main/java/org/support/project/knowledge/control/protect/NotificationControl.java b/src/main/java/org/support/project/knowledge/control/protect/NotificationControl.java index 1714078ec..02d22b122 100644 --- a/src/main/java/org/support/project/knowledge/control/protect/NotificationControl.java +++ b/src/main/java/org/support/project/knowledge/control/protect/NotificationControl.java @@ -13,7 +13,7 @@ public class NotificationControl extends Control { - @Get + @Get(publishToken = "knowledge") public Boundary list() throws InvalidParamException { int offset = getPathInteger(0); boolean all = "true".equals(getAttribute("all", "false")); @@ -39,6 +39,7 @@ public Boundary view() throws InvalidParamException { return sendError(HttpStatus.SC_403_FORBIDDEN, "FORBIDDEN"); } NotificationLogic.get().setStatus(getLoginUserId(), no, NotificationLogic.STATUS_READED); + notification.setStatus(NotificationLogic.STATUS_READED); setAttributeOnProperty(notification); setAttribute("no", no); return forward("view.jsp"); @@ -56,6 +57,7 @@ public Boundary previous() throws InvalidParamException { } no = notification.getNo(); NotificationLogic.get().setStatus(getLoginUserId(), no, NotificationLogic.STATUS_READED); + notification.setStatus(NotificationLogic.STATUS_READED); setAttributeOnProperty(notification); setAttribute("no", no); return forward("view.jsp"); @@ -72,12 +74,13 @@ public Boundary next() throws InvalidParamException { } no = notification.getNo(); NotificationLogic.get().setStatus(getLoginUserId(), no, NotificationLogic.STATUS_READED); + notification.setStatus(NotificationLogic.STATUS_READED); setAttributeOnProperty(notification); setAttribute("no", no); return forward("view.jsp"); } - @Post + @Post(subscribeToken = "knowledge") public Boundary markread() { String no = getAttribute("no", ""); NotificationLogic.get().markAllAsRead(no, getLoginUserId()); diff --git a/src/test/java/org/support/project/knowledge/integration/IntegrationCommon.java b/src/test/java/org/support/project/knowledge/integration/IntegrationCommon.java index 2637ca3f2..a8ba89052 100644 --- a/src/test/java/org/support/project/knowledge/integration/IntegrationCommon.java +++ b/src/test/java/org/support/project/knowledge/integration/IntegrationCommon.java @@ -20,18 +20,23 @@ import org.support.project.knowledge.config.AppConfig; import org.support.project.knowledge.config.AuthType; import org.support.project.knowledge.dao.NotifyQueuesDao; +import org.support.project.knowledge.dao.TemplateItemsDao; import org.support.project.knowledge.entity.CommentsEntity; import org.support.project.knowledge.entity.NotifyQueuesEntity; +import org.support.project.knowledge.logic.EventsLogic; +import org.support.project.knowledge.logic.KnowledgeLogic; +import org.support.project.knowledge.logic.TargetLogic; +import org.support.project.knowledge.logic.TemplateLogic; import org.support.project.knowledge.vo.LikeCount; import org.support.project.knowledge.vo.api.Knowledge; import org.support.project.web.bean.LoginedUser; +import org.support.project.web.bean.MessageResult; import org.support.project.web.bean.Msg; import org.support.project.web.bean.NameId; import org.support.project.web.boundary.ForwardBoundary; import org.support.project.web.boundary.JsonBoundary; import org.support.project.web.boundary.RedirectBoundary; import org.support.project.web.boundary.SendMessageBoundary; -import org.support.project.web.common.HttpStatus; import org.support.project.web.common.InvokeTarget; import org.support.project.web.dao.MailConfigsDao; import org.support.project.web.entity.MailConfigsEntity; @@ -104,6 +109,62 @@ protected T invoke(HttpServletRequest request, HttpServletResponse response, } return (T) result; } + + + /** + * 投稿 + * @return + * @throws Exception + * @throws NoSuchFieldException + * @throws IllegalAccessException + */ + protected MessageResult postKnowledge(String userKey, int publicFlag, int typeId, String viewers) throws Exception, NoSuchFieldException, IllegalAccessException { + // 登録画面へアクセスできること(パスのルーティングのみ確認) + StubHttpServletRequest request = new StubHttpServletRequest(); + StubHttpServletResponse response = new StubHttpServletResponse(request); + request.setServletPath("protect.knowledge/view_add"); + request.setMethod("get"); + DefaultAuthenticationLogicImpl auth = org.support.project.di.Container.getComp(DefaultAuthenticationLogicImpl.class); + auth.setSession(userKey, request, response); + + ForwardBoundary boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/knowledge/edit.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + + String csrfToken = (String) request.getAttribute(HttpRequestCheckLogic.REQ_ID_KEY); + Assert.assertNotNull(csrfToken); + + // 保存 + request.setServletPath("protect.knowledge/save"); + request.setMethod("post"); + request.addParameter(HttpRequestCheckLogic.REQ_ID_KEY, csrfToken); + request.addParameter("title", "タイトル"); + request.addParameter("content", "内容"); + request.addParameter("publicFlag", String.valueOf(publicFlag)); + request.addParameter("typeId", String.valueOf(typeId)); + + if (typeId == TemplateLogic.TYPE_ID_BOOKMARK) { + request.addParameter("item_" + TemplateItemsDao.ITEM_ID_BOOKMARK_URL, "https://information-supportproject.org/"); + } else if (typeId == TemplateLogic.TYPE_ID_EVENT) { + request.addParameter("item_" + EventsLogic.ITEM_NO_DATE, "2017-10-01"); + request.addParameter("item_" + EventsLogic.ITEM_NO_START, "10:00"); + request.addParameter("item_" + EventsLogic.ITEM_NO_END, "12:00"); + request.addParameter("item_" + EventsLogic.ITEM_NO_TIMEZONE, "Asia/Tokyo"); + request.addParameter("item_" + EventsLogic.ITEM_NO_THE_NUMBER_TO_BE_ACCEPTED, "10"); + } + + if (publicFlag == KnowledgeLogic.PUBLIC_FLAG_PROTECT) { + if (StringUtils.isNotEmpty(viewers)) { + request.addParameter("groups", viewers); + } + } + + JsonBoundary jsonBoundary = invoke(request, response, JsonBoundary.class); + MessageResult sendObject = (MessageResult) jsonBoundary.getObj(); + LOG.info(sendObject); + Assert.assertEquals(200, sendObject.getCode().intValue()); + return sendObject; + } + /** * CP確認 * @param userKey @@ -386,7 +447,7 @@ protected CommentsEntity getLatestComment(String userKey, long knowledgeId) thro */ protected void execNotificationQueue() throws Exception { List list = NotifyQueuesDao.get().selectAll(); - Assert.assertEquals(1, list.size()); + Assert.assertTrue(0 < list.size()); NotifyMailBat.main(null); list = NotifyQueuesDao.get().selectAll(); Assert.assertEquals(0, list.size()); diff --git a/src/test/java/org/support/project/knowledge/integration/IntegrationNotificationTest.java b/src/test/java/org/support/project/knowledge/integration/IntegrationNotificationTest.java new file mode 100644 index 000000000..4b56d3955 --- /dev/null +++ b/src/test/java/org/support/project/knowledge/integration/IntegrationNotificationTest.java @@ -0,0 +1,281 @@ +package org.support.project.knowledge.integration; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.support.project.common.log.Log; +import org.support.project.common.log.LogFactory; +import org.support.project.common.test.Order; +import org.support.project.common.util.PropertyUtil; +import org.support.project.common.util.StringJoinBuilder; +import org.support.project.knowledge.logic.KnowledgeLogic; +import org.support.project.knowledge.logic.TemplateLogic; +import org.support.project.web.boundary.ForwardBoundary; +import org.support.project.web.boundary.JsonBoundary; +import org.support.project.web.entity.NotificationsEntity; +import org.support.project.web.logic.impl.DefaultAuthenticationLogicImpl; +import org.support.project.web.test.stub.StubHttpServletRequest; +import org.support.project.web.test.stub.StubHttpServletResponse; + +/** + * + * @author koda + */ +public class IntegrationNotificationTest extends IntegrationCommon { + /** ログ */ + private static final Log LOG = LogFactory.getLog(IntegrationNotificationTest.class); + + private static final String POST_USER = "integration-test-user-01"; + private static final String READ_USER = "integration-test-user-02"; + + private List getList(String userKey, StubHttpServletRequest request, StubHttpServletResponse response, boolean all) throws Exception { + request.setServletPath("protect.notification/list"); + request.setMethod("get"); + DefaultAuthenticationLogicImpl auth = org.support.project.di.Container.getComp(DefaultAuthenticationLogicImpl.class); + auth.setSession(userKey, request, response); + if (all) { + request.addParameter("all", "true"); + } + + ForwardBoundary boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/list.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + + List notifications = (List) request.getAttribute("notifications"); + return notifications; + } + + /** + * ユーザを登録 + * + * @throws Exception + */ + @Test + @Order(order = 1) + public void testUserInsert() throws Exception { + LOG.info("ユーザ登録"); + addUser(POST_USER); + addUser(READ_USER); + } + + /** + * 「公開」の記事登録(通知の登録) + * @throws Exception + */ + @Test + @Order(order = 100) + public void testNotification() throws Exception { + // 投稿 + postKnowledge( + POST_USER, + KnowledgeLogic.PUBLIC_FLAG_PUBLIC, + TemplateLogic.TYPE_ID_KNOWLEDGE, + null); + // 通知 + execNotificationQueue(); + + // 通知にアクセス(List) + StubHttpServletRequest request = new StubHttpServletRequest(); + StubHttpServletResponse response = new StubHttpServletResponse(request); + List notifications = getList(READ_USER, request, response, false); + Assert.assertEquals(1, notifications.size()); + + NotificationsEntity notification = notifications.get(0); + Assert.assertEquals(0, notification.getStatus()); + + // 通知を表示 + request.setServletPath("protect.notification/view/" + notification.getNo()); + ForwardBoundary boundary2 = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary2, "path")); + + Assert.assertEquals(1, request.getAttribute("status")); + } + + + /** + * 「公開」の記事登録(通知の登録) + * @throws Exception + */ + @Test + @Order(order = 101) + public void testReadAll() throws Exception { + // 投稿 + for (int i = 0; i < 10; i++) { + postKnowledge( + POST_USER, + KnowledgeLogic.PUBLIC_FLAG_PUBLIC, + TemplateLogic.TYPE_ID_KNOWLEDGE, + null); + } + // 通知 + execNotificationQueue(); + + // 通知にアクセス(List) + StubHttpServletRequest request = new StubHttpServletRequest(); + StubHttpServletResponse response = new StubHttpServletResponse(request); + List notifications = getList(READ_USER, request, response, false); + Assert.assertEquals(10, notifications.size()); + + // 全て既読へ + StringJoinBuilder builder = new StringJoinBuilder<>(); + for (NotificationsEntity notificationsEntity : notifications) { + builder.append(notificationsEntity.getNo()); + } + + request.setServletPath("protect.notification/markread"); + request.setMethod("post"); + request.addParameter("no", builder.join(",")); + JsonBoundary jsonBoundary = invoke(request, response, JsonBoundary.class); + Assert.assertEquals("OK", jsonBoundary.getObj()); + + // 通知にアクセス(List) + notifications = getList(READ_USER, request, response, false); + Assert.assertEquals(0, notifications.size()); + + // 通知にアクセス(List) + notifications = getList(READ_USER, request, response, true); + Assert.assertEquals(11, notifications.size()); + } + + + /** + * 次へのボタンの操作 + * @throws Exception + */ + @Test + @Order(order = 102) + public void testNext() throws Exception { + // 投稿 + for (int i = 0; i < 3; i++) { + postKnowledge( + POST_USER, + KnowledgeLogic.PUBLIC_FLAG_PUBLIC, + TemplateLogic.TYPE_ID_KNOWLEDGE, + null); + } + // 通知 + execNotificationQueue(); + + // 通知にアクセス(List) + StubHttpServletRequest request = new StubHttpServletRequest(); + StubHttpServletResponse response = new StubHttpServletResponse(request); + List notifications = getList(READ_USER, request, response, false); + Assert.assertEquals(3, notifications.size()); + + // 通知を表示 + NotificationsEntity notification = notifications.get(2); + request.setServletPath("protect.notification/view/" + notification.getNo()); + ForwardBoundary boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + Assert.assertEquals(1, request.getAttribute("status")); + Assert.assertEquals(notification.getNo(), request.getAttribute("no")); + + // Nextをクリック + request.setServletPath("protect.notification/next/" + notification.getNo()); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + Assert.assertEquals(1, request.getAttribute("status")); + notification = notifications.get(1); + Assert.assertEquals(notification.getNo(), request.getAttribute("no")); + + // Nextをクリック + request.setServletPath("protect.notification/next/" + notification.getNo()); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + Assert.assertEquals(1, request.getAttribute("status")); + notification = notifications.get(0); + Assert.assertEquals(notification.getNo(), request.getAttribute("no")); + + // Nextをクリック + request.setServletPath("protect.notification/next/" + notification.getNo()); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/not_found.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + + // リストの先頭に戻ってNextをクリック + notification = notifications.get(2); + request.setServletPath("protect.notification/next/" + notification.getNo()); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/not_found.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + + // 全てを表示のモードでNextをクリック + request.setServletPath("protect.notification/next/" + notification.getNo()); + request.addParameter("all", "true"); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + Assert.assertEquals(1, request.getAttribute("status")); + notification = notifications.get(1); + Assert.assertEquals(notification.getNo(), request.getAttribute("no")); + } + + + /** + * 前へのボタンの操作 + * @throws Exception + */ + @Test + @Order(order = 103) + public void testPrevious() throws Exception { + // 投稿 + for (int i = 0; i < 3; i++) { + postKnowledge( + POST_USER, + KnowledgeLogic.PUBLIC_FLAG_PUBLIC, + TemplateLogic.TYPE_ID_KNOWLEDGE, + null); + } + // 通知 + execNotificationQueue(); + + // 通知にアクセス(List) + StubHttpServletRequest request = new StubHttpServletRequest(); + StubHttpServletResponse response = new StubHttpServletResponse(request); + List notifications = getList(READ_USER, request, response, false); + Assert.assertEquals(3, notifications.size()); + + // 通知を表示 + NotificationsEntity notification = notifications.get(0); + request.setServletPath("protect.notification/view/" + notification.getNo()); + ForwardBoundary boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + Assert.assertEquals(1, request.getAttribute("status")); + Assert.assertEquals(notification.getNo(), request.getAttribute("no")); + + // previousをクリック + request.setServletPath("protect.notification/previous/" + notification.getNo()); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + Assert.assertEquals(1, request.getAttribute("status")); + notification = notifications.get(1); + Assert.assertEquals(notification.getNo(), request.getAttribute("no")); + + // previousをクリック + request.setServletPath("protect.notification/previous/" + notification.getNo()); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + Assert.assertEquals(1, request.getAttribute("status")); + notification = notifications.get(2); + Assert.assertEquals(notification.getNo(), request.getAttribute("no")); + + // previousをクリック + request.setServletPath("protect.notification/previous/" + notification.getNo()); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/not_found.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + + // リストの先頭に戻ってpreviousをクリック + notification = notifications.get(0); + request.setServletPath("protect.notification/previous/" + notification.getNo()); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/not_found.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + + // 全てを表示のモードでpreviousをクリック + request.setServletPath("protect.notification/previous/" + notification.getNo()); + request.addParameter("all", "true"); + boundary = invoke(request, response, ForwardBoundary.class); + Assert.assertEquals("/WEB-INF/views/protect/notification/view.jsp", PropertyUtil.getPrivateFeildOnReflection(String.class, boundary, "path")); + Assert.assertEquals(1, request.getAttribute("status")); + notification = notifications.get(1); + Assert.assertEquals(notification.getNo(), request.getAttribute("no")); + } + + +} diff --git a/src/test/java/org/support/project/knowledge/integration/IntegrationSurveyTest.java b/src/test/java/org/support/project/knowledge/integration/IntegrationSurveyTest.java index 73235a362..0df049c6a 100644 --- a/src/test/java/org/support/project/knowledge/integration/IntegrationSurveyTest.java +++ b/src/test/java/org/support/project/knowledge/integration/IntegrationSurveyTest.java @@ -16,7 +16,6 @@ import org.support.project.web.bean.Msg; import org.support.project.web.boundary.ForwardBoundary; import org.support.project.web.boundary.JsonBoundary; -import org.support.project.web.boundary.SendMessageBoundary; import org.support.project.web.common.HttpStatus; import org.support.project.web.logic.HttpRequestCheckLogic; import org.support.project.web.logic.impl.DefaultAuthenticationLogicImpl; @@ -55,7 +54,7 @@ public void testUserInsert() throws Exception { */ @Test @Order(order = 100) - public void testPostEventPublic() throws Exception { + public void testPostPublic() throws Exception { // 登録画面へアクセスできること(パスのルーティングのみ確認) StubHttpServletRequest request = new StubHttpServletRequest(); StubHttpServletResponse response = new StubHttpServletResponse(request);