From 2c9a894840ab9e923b62b14a10726042e65c85bb Mon Sep 17 00:00:00 2001 From: Mlxa0324 Date: Thu, 29 Sep 2022 16:37:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=B1=BB=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=9A=8F=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...erOpenCourseQuestionLogControllerTest.java | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/web/src/test/java/com/ibeetl/jlw/web/TeacherOpenCourseQuestionLogControllerTest.java b/web/src/test/java/com/ibeetl/jlw/web/TeacherOpenCourseQuestionLogControllerTest.java index 08dfd963..78cd922a 100644 --- a/web/src/test/java/com/ibeetl/jlw/web/TeacherOpenCourseQuestionLogControllerTest.java +++ b/web/src/test/java/com/ibeetl/jlw/web/TeacherOpenCourseQuestionLogControllerTest.java @@ -2,8 +2,11 @@ package com.ibeetl.jlw.web; import base.BaseTest; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.RandomUtil; import com.ibeetl.admin.test.util.test.TestEnvUtil; +import com.ibeetl.jlw.dao.ResourcesQuestionSnapshotDao; import com.ibeetl.jlw.dao.StudentDao; +import com.ibeetl.jlw.entity.ResourcesQuestionSnapshot; import com.ibeetl.jlw.entity.Student; import com.ibeetl.jlw.enums.TeacherOpenCourseQuestionLogFromTypeEnum; import org.junit.Assert; @@ -15,9 +18,12 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import java.util.List; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import static cn.hutool.json.JSONUtil.toJsonStr; import static com.ibeetl.admin.test.util.test.RandomUtils.randomSet; +import static org.apache.commons.lang3.StringUtils.join; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; class TeacherOpenCourseQuestionLogControllerTest extends BaseTest { @@ -27,6 +33,8 @@ class TeacherOpenCourseQuestionLogControllerTest extends BaseTest { @Resource StudentDao studentDao; + @Resource + ResourcesQuestionSnapshotDao resourcesQuestionSnapshotDao; /** * 和题目有关的做题提交方法 @@ -38,6 +46,39 @@ class TeacherOpenCourseQuestionLogControllerTest extends BaseTest { void addQuestionLog() throws Exception { String params = "{\"1574684340254842880\": [\"错\"], \"1574684340271620097\": [\"D\",\"A\", \"C\"]}"; + // 通过查询数据库来random实体类属性 + // 随机取一个学生的登录信息 + List questionSnapshots = resourcesQuestionSnapshotDao.execute( + "select t.* \n" + + "from resources_question_snapshot t \n" + + "where t.question_status = 1\t\n" + + "order by RAND() \n" + + "limit 10 "); + + // 随机取题目ID和答案 + Map> answerRandomMap = new ConcurrentHashMap<>(); + questionSnapshots.stream().forEach(item -> { + TreeSet answer = new TreeSet<>(); + // 单选 + if (item.getQuestionType().equals(1)) { + answer.add(RandomUtil.randomEle(new String[]{"A", "B", "C", "D"})); + } + // 多选 + if (item.getQuestionType().equals(2)) { + String[] multipleOptions = ObjectUtil.isEmpty(item.getQuestionOptionE()) + ? new String[]{"A", "B", "C", "D"} : new String[]{"A", "B", "C", "D", "E"}; + Set randomAnswerSet = RandomUtil + .randomEleSet(Arrays.asList(multipleOptions), RandomUtil.randomInt(2, multipleOptions.length)); + // 取前N项 + answer.add(join(new TreeSet<>(randomAnswerSet).toArray(), ",")); + } + // 判断 + if (item.getQuestionType().equals(3)) { + answer.add(RandomUtil.randomEle(Arrays.asList("对", "错"))); + } + answerRandomMap.put(item.getResourcesQuestionSnapshotId(), answer); + }); + // 随机取一个学生的登录信息 List studentList = studentDao.execute( "select org_id, user_id " + @@ -60,7 +101,7 @@ class TeacherOpenCourseQuestionLogControllerTest extends BaseTest { RequestBuilder rb = MockMvcRequestBuilders.post(MODEL + "/addQuestionLog.json") .contentType(MediaType.APPLICATION_JSON_VALUE) .param("fromType", fromTypeEnum.name()) - .content(params); + .content(toJsonStr(answerRandomMap)); //发送请求,验证返回结果 String result = mvc.perform(rb) .andExpect(status().isOk())