Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
koda-masaru committed Oct 3, 2017
1 parent a4b0727 commit 9fec5d9
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Boundary index() {
* 指定のユーザのLdap認証設定を設定する
* @return
*/
@Get
@Get(publishToken="csrf")
public Boundary config() {
String key = getParam("key");
if (StringUtils.isEmpty(key)) {
Expand All @@ -67,7 +67,7 @@ public Boundary config() {
return forward("config.jsp");
}

@Post
@Post(subscribeToken="csrf")
public Boundary connect() {
String key = getParam("key");
if (StringUtils.isEmpty(key)) {
Expand Down Expand Up @@ -133,7 +133,7 @@ public Boundary connect() {
}
}

@Post
@Post(subscribeToken="csrf")
public Boundary disconnect() {
String key = getParam("key");
if (StringUtils.isEmpty(key)) {
Expand All @@ -153,7 +153,7 @@ public Boundary disconnect() {
return index();
}

@Post
@Post(subscribeToken="csrf")
public Boundary update() {
String key = getParam("key");
if (StringUtils.isEmpty(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.support.project.common.bean.ValidateError;
Expand All @@ -29,6 +28,7 @@
import org.support.project.knowledge.logic.activity.Activity;
import org.support.project.knowledge.logic.activity.ActivityLogic;
import org.support.project.knowledge.vo.SurveyReport;
import org.support.project.web.bean.Msg;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.common.HttpStatus;
import org.support.project.web.config.MessageStatus;
Expand Down Expand Up @@ -138,12 +138,12 @@ public Boundary list() throws InvalidParamException {
* @return
* @throws InvalidParamException
*/
@Get
@Get(publishToken = "survey")
public Boundary load() throws InvalidParamException {
Long id = super.getPathLong(new Long(-1));
SurveysEntity entity = SurveyLogic.get().loadSurvey(id, getLoginUserId());
if (entity == null) {
return sendError(404, null);
return send(new Msg("survey data is not exists."));
}
return send(entity);
}
Expand All @@ -153,7 +153,7 @@ public Boundary load() throws InvalidParamException {
* @return
* @throws InvalidParamException
*/
@Delete
@Delete(subscribeToken = "survey")
public Boundary delete() throws InvalidParamException {
Long id = super.getPathLong(new Long(-1));
SurveyLogic.get().deleteSurvey(id);
Expand All @@ -172,7 +172,7 @@ public Boundary delete() throws InvalidParamException {
* @throws IllegalAccessException
* @throws InstantiationException
*/
@Post(subscribeToken = "knowledge", checkReqToken = true)
@Post(subscribeToken = "survey", checkReqToken = true)
public Boundary answer() throws InstantiationException, IllegalAccessException, JSONException, IOException, InvalidParamException {
String id = getParam("knowledgeId");
if (!StringUtils.isLong(id)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private DateFormat gateDayFormat() {
/**
* 現在のTokenの状態を見る
*/
@Get
@Get(publishToken = "csrf")
public Boundary index() {
LOG.trace("access to index");
String expires = "";
Expand All @@ -52,7 +52,7 @@ public Boundary index() {
/**
* Tokenの発行/更新
*/
@Post
@Post(subscribeToken = "csrf")
public Boundary save() {
LOG.trace("access to save");
String expires = getParam("expires");
Expand Down Expand Up @@ -81,7 +81,7 @@ public Boundary save() {
/**
* Tokenの削除
*/
@Post
@Post(subscribeToken = "csrf")
public Boundary delete() {
TokensEntity entity = TokensDao.get().selectOnUserId(getLoginUserId());
if (entity != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.common.util.RandomUtil;
import org.support.project.di.Container;
import org.support.project.di.DI;
import org.support.project.di.Instance;
Expand All @@ -27,9 +26,10 @@ private int getPoint() {
if (point != 0) {
return point;
}
int[] points = {1,1,2,2,2,2,3,3,3,4};
this.point = points[RandomUtil.randamNum(0, 10)]; // ランダムで値を増減してみた
return point;
//int[] points = {1,1,2,2,2,2,3,3,3,4};
//this.point = points[RandomUtil.randamNum(0, 10)]; // ランダムで値を増減してみた
//return point;
return 3;
}

@Override
Expand Down
30 changes: 17 additions & 13 deletions src/main/webapp/js/knowledge-view-survey.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
$(document).ready(function() {
var knowledgeId = $('#knowledgeId').val();
$.ajax({
type : 'GET',
url : _CONTEXT + '/protect.survey/load/' + knowledgeId
}).done(function(data) {
console.log(data);
$('#btnAnswerSurvey').removeClass('hide');
$('#modalAnswerSurveyLabel').text(data.title);
$('#surveyDescription').text(data.description);
document.__add_Template_Edit_Item(data);
}).fail(function(err) {
console.log(err);
});
if (_LOGIN_USER_ID) {
$.ajax({
type : 'GET',
url : _CONTEXT + '/protect.survey/load/' + knowledgeId
}).done(function(data) {
console.log(data);
if (data.msg) {
return;
}
$('#btnAnswerSurvey').removeClass('hide');
$('#modalAnswerSurveyLabel').text(data.title);
$('#surveyDescription').text(data.description);
document.__add_Template_Edit_Item(data);
}).fail(function(err) {
console.log(err);
});
}

$('#saveSurveyButton').click(function(){
$('#answerForm').submit();
});


// フォームのサブミットは禁止
$('#answerForm').submit(function(event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
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.knowledge.dao.NotifyQueuesDao;
import org.support.project.knowledge.entity.NotifyQueuesEntity;
import org.support.project.knowledge.logic.KnowledgeLogic;
import org.support.project.knowledge.logic.TemplateLogic;
import org.support.project.web.bean.MessageResult;
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;
import org.support.project.web.test.stub.StubHttpServletRequest;
import org.support.project.web.test.stub.StubHttpServletResponse;

/**
*
* @author koda
*/
public class IntegrationSurveyTest extends IntegrationCommon {
/** ログ */
private static final Log LOG = LogFactory.getLog(IntegrationSurveyTest.class);

private static final String POST_USER = "integration-test-user-01";
private static final String ANSWER_USER = "integration-test-user-02";

private static long knowledgeId;

/**
* ユーザを登録
*
* @throws Exception
*/
@Test
@Order(order = 1)
public void testUserInsert() throws Exception {
LOG.info("ユーザ登録");
addUser(POST_USER);
addUser(ANSWER_USER);
}

/**
* 「公開」の記事登録
* @throws Exception
*/
@Test
@Order(order = 100)
public void testPostEventPublic() throws Exception {
// 登録画面へアクセスできること(パスのルーティングのみ確認)
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(POST_USER, 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(KnowledgeLogic.PUBLIC_FLAG_PUBLIC));
request.addParameter("typeId", String.valueOf(TemplateLogic.TYPE_ID_KNOWLEDGE));

JsonBoundary jsonBoundary = invoke(request, response, JsonBoundary.class);
MessageResult sendObject = (MessageResult) jsonBoundary.getObj();
LOG.info(sendObject);
Assert.assertEquals(200, sendObject.getCode().intValue());

knowledgeId = new Long(sendObject.getResult());
}

/**
* 投稿後の確認
* @throws Exception
*/
@Test
@Order(order = 101)
public void testAssertAfterPost() throws Exception {
assertKnowledgeCP(POST_USER, knowledgeId, 50);
assertCP(POST_USER, 50);
assertKnowledgeCP(ANSWER_USER, knowledgeId, 1);
assertCP(ANSWER_USER, 1);
assertCP(POST_USER, 1);

execNotificationQueue();
assertNotificationCount(POST_USER, 1);
assertNotificationCount(ANSWER_USER, 1);
}

@Test
@Order(order = 200)
public void testCreateSurvey() throws Exception {
StubHttpServletRequest request = new StubHttpServletRequest();
StubHttpServletResponse response = new StubHttpServletResponse(request);

request.setServletPath("protect.survey/load/" + knowledgeId);
request.setMethod("get");
JsonBoundary msg = invoke(request, response, JsonBoundary.class);
Msg result = (Msg) msg.getObj();
Assert.assertEquals("survey data is not exists.", result.getMsg());

request.setServletPath("protect.survey/save");
request.setMethod("post");
DefaultAuthenticationLogicImpl auth = org.support.project.di.Container.getComp(DefaultAuthenticationLogicImpl.class);
auth.setSession(POST_USER, request, response);

String csrfToken = (String) request.getAttribute(HttpRequestCheckLogic.REQ_ID_KEY);
request.addParameter(HttpRequestCheckLogic.REQ_ID_KEY, csrfToken);

request.addParameter("knowledgeId", String.valueOf(knowledgeId));
request.addParameter("typeName", "アンケート");
request.addParameter("itemType", "text_item0");
request.addParameter("title_item0", "入力項目1");

JsonBoundary boundary = invoke(request, response, JsonBoundary.class);
MessageResult messageResult = (MessageResult) boundary.getObj();
LOG.info(messageResult);
Assert.assertEquals(HttpStatus.SC_200_OK, messageResult.getCode().intValue());
}


/**
* アンケート作成後の確認
* @throws Exception
*/
@Test
@Order(order = 201)
public void testAssertAfterCreateSurvey() throws Exception {
assertKnowledgeCP(POST_USER, knowledgeId, 0);
assertCP(POST_USER, 0);
assertKnowledgeCP(ANSWER_USER, knowledgeId, 0);
assertCP(ANSWER_USER, 0);

List<NotifyQueuesEntity> list = NotifyQueuesDao.get().selectAll();
Assert.assertEquals(0, list.size());
assertNotificationCount(POST_USER, 0);
assertNotificationCount(ANSWER_USER, 0);
}

/**
* アンケートに回答
* @throws Exception
*/
@Test
@Order(order = 300)
public void testAnswerSurvey() throws Exception {
StubHttpServletRequest request = new StubHttpServletRequest();
StubHttpServletResponse response = new StubHttpServletResponse(request);

request.setServletPath("protect.survey/load/" + knowledgeId);
request.setMethod("get");
JsonBoundary survey = invoke(request, response, JsonBoundary.class);
LOG.info(survey);
Assert.assertNotNull(survey);

request.setServletPath("protect.survey/answer");
request.setMethod("post");
DefaultAuthenticationLogicImpl auth = org.support.project.di.Container.getComp(DefaultAuthenticationLogicImpl.class);
auth.setSession(ANSWER_USER, request, response);

String csrfToken = (String) request.getAttribute(HttpRequestCheckLogic.REQ_ID_KEY);
request.addParameter(HttpRequestCheckLogic.REQ_ID_KEY, csrfToken);

request.addParameter("knowledgeId", String.valueOf(knowledgeId));
request.addParameter("item_0", "回答");

JsonBoundary boundary = invoke(request, response, JsonBoundary.class);
MessageResult messageResult = (MessageResult) boundary.getObj();
LOG.info(messageResult);
Assert.assertEquals(HttpStatus.SC_200_OK, messageResult.getCode().intValue());
}

/**
* アンケート作成後の確認
* @throws Exception
*/
@Test
@Order(order = 301)
public void testAssertAfterAnswer() throws Exception {
assertKnowledgeCP(POST_USER, knowledgeId, 3);
assertCP(POST_USER, 3);
assertCP(ANSWER_USER, 3);

List<NotifyQueuesEntity> list = NotifyQueuesDao.get().selectAll();
Assert.assertEquals(0, list.size());
assertNotificationCount(POST_USER, 0);
assertNotificationCount(ANSWER_USER, 0);
}



}

0 comments on commit 9fec5d9

Please sign in to comment.