|
|
|
@ -41,62 +41,66 @@ class TeacherOpenCourseQuestionLogControllerTest extends BaseTest {
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
void addQuestionLog() throws Exception {
|
|
|
|
|
// 通过查询数据库来random实体类属性
|
|
|
|
|
// 随机取一个学生的登录信息
|
|
|
|
|
List<ResourcesQuestionSnapshot> 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 ");
|
|
|
|
|
try {
|
|
|
|
|
// 通过查询数据库来random实体类属性
|
|
|
|
|
// 随机取一个学生的登录信息
|
|
|
|
|
List<ResourcesQuestionSnapshot> 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<Long, TreeSet<String>> answerRandomMap = new ConcurrentHashMap<>();
|
|
|
|
|
questionSnapshots.stream().forEach(item -> {
|
|
|
|
|
TreeSet<String> answer = new TreeSet<>();
|
|
|
|
|
// 单选
|
|
|
|
|
if (item.getQuestionType().equals(1)) {
|
|
|
|
|
String[] simpleOptions = ObjectUtil.isEmpty(item.getQuestionOptionE())
|
|
|
|
|
? new String[]{"A", "B", "C", "D"} : new String[]{"A", "B", "C", "D", "E"};
|
|
|
|
|
answer.add(RandomUtil.randomEle(simpleOptions));
|
|
|
|
|
}
|
|
|
|
|
// 多选
|
|
|
|
|
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<String> randomAnswerSet = RandomUtil
|
|
|
|
|
.randomEleSet(Arrays.asList(multipleOptions), RandomUtil.randomInt(2, multipleOptions.length));
|
|
|
|
|
//排序去重 逗号分割
|
|
|
|
|
answer.add(join(new TreeSet<>(randomAnswerSet).toArray(), ","));
|
|
|
|
|
}
|
|
|
|
|
// 判断
|
|
|
|
|
if (item.getQuestionType().equals(3)) {
|
|
|
|
|
answer.add(RandomUtil.randomEle(Arrays.asList("对", "错")));
|
|
|
|
|
}
|
|
|
|
|
answerRandomMap.put(item.getResourcesQuestionSnapshotId(), answer);
|
|
|
|
|
});
|
|
|
|
|
// 随机取题目ID和答案
|
|
|
|
|
Map<Long, TreeSet<String>> answerRandomMap = new ConcurrentHashMap<>();
|
|
|
|
|
questionSnapshots.stream().forEach(item -> {
|
|
|
|
|
TreeSet<String> answer = new TreeSet<>();
|
|
|
|
|
// 单选
|
|
|
|
|
if (item.getQuestionType().equals(1)) {
|
|
|
|
|
String[] simpleOptions = ObjectUtil.isEmpty(item.getQuestionOptionE())
|
|
|
|
|
? new String[]{"A", "B", "C", "D"} : new String[]{"A", "B", "C", "D", "E"};
|
|
|
|
|
answer.add(RandomUtil.randomEle(simpleOptions));
|
|
|
|
|
}
|
|
|
|
|
// 多选
|
|
|
|
|
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<String> randomAnswerSet = RandomUtil
|
|
|
|
|
.randomEleSet(Arrays.asList(multipleOptions), RandomUtil.randomInt(2, multipleOptions.length));
|
|
|
|
|
//排序去重 逗号分割
|
|
|
|
|
answer.add(join(new TreeSet<>(randomAnswerSet).toArray(), ","));
|
|
|
|
|
}
|
|
|
|
|
// 判断
|
|
|
|
|
if (item.getQuestionType().equals(3)) {
|
|
|
|
|
answer.add(RandomUtil.randomEle(Arrays.asList("对", "错")));
|
|
|
|
|
}
|
|
|
|
|
answerRandomMap.put(item.getResourcesQuestionSnapshotId(), answer);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 随机取一个学生的登录信息
|
|
|
|
|
putEnvLoginInfoStudent();
|
|
|
|
|
// 随机取一个学生的登录信息
|
|
|
|
|
putStudentLoginInfoToEnv();
|
|
|
|
|
|
|
|
|
|
// 随机来源类型
|
|
|
|
|
TeacherOpenCourseQuestionFromTypeEnum fromTypeEnum = CollectionUtils
|
|
|
|
|
.firstElement(randomSet(TeacherOpenCourseQuestionFromTypeEnum.class));
|
|
|
|
|
// 随机来源类型
|
|
|
|
|
TeacherOpenCourseQuestionFromTypeEnum fromTypeEnum = CollectionUtils
|
|
|
|
|
.firstElement(randomSet(TeacherOpenCourseQuestionFromTypeEnum.class));
|
|
|
|
|
|
|
|
|
|
//构造请求参数
|
|
|
|
|
RequestBuilder rb = MockMvcRequestBuilders.post(MODEL + "/addQuestionLog.json")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
.param("fromType", fromTypeEnum.name())
|
|
|
|
|
.content(toJsonStr(answerRandomMap));
|
|
|
|
|
//发送请求,验证返回结果
|
|
|
|
|
String result = mvc.perform(rb)
|
|
|
|
|
.andExpect(status().isOk())
|
|
|
|
|
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0"))
|
|
|
|
|
.andReturn().getResponse().getContentAsString();
|
|
|
|
|
//构造请求参数
|
|
|
|
|
RequestBuilder rb = MockMvcRequestBuilders.post(MODEL + "/addQuestionLog.json")
|
|
|
|
|
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
.param("fromType", fromTypeEnum.name())
|
|
|
|
|
.content(toJsonStr(answerRandomMap));
|
|
|
|
|
//发送请求,验证返回结果
|
|
|
|
|
String result = mvc.perform(rb)
|
|
|
|
|
.andExpect(status().isOk())
|
|
|
|
|
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0"))
|
|
|
|
|
.andReturn().getResponse().getContentAsString();
|
|
|
|
|
|
|
|
|
|
// 换回去原始的登录方式
|
|
|
|
|
clearEnvLoginInfo();
|
|
|
|
|
|
|
|
|
|
System.out.println(result);
|
|
|
|
|
System.out.println(result);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}finally {
|
|
|
|
|
// 清除登录信息
|
|
|
|
|
clearEnvLoginInfo();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|