replace subtaskInfo map with list for stepik
[idea/community.git] / python / educational-core / student / testSrc / com / jetbrains / edu / learning / stepic / StudyStepicFormatTest.java
1 package com.jetbrains.edu.learning.stepic;
2
3 import com.google.gson.Gson;
4 import com.google.gson.GsonBuilder;
5 import com.intellij.openapi.util.io.FileUtil;
6 import com.intellij.testFramework.PlatformTestUtil;
7 import com.intellij.util.containers.ContainerUtil;
8 import com.jetbrains.edu.learning.StudySerializationUtils;
9 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
10 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
11 import com.jetbrains.edu.learning.courseFormat.TaskFile;
12 import org.jetbrains.annotations.NotNull;
13 import org.junit.Test;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Map;
20
21 import static org.junit.Assert.*;
22
23
24 public class StudyStepicFormatTest {
25
26   @Test
27   public void fromFirstVersion() throws IOException {
28     doStepOptionsCreationTest("1.json");
29   }
30
31   @Test
32   public void fromSecondVersion() throws IOException {
33     doStepOptionsCreationTest("2.json");
34   }
35
36   @Test
37   public void testWithSubtasks() throws IOException {
38     StepicWrappers.StepOptions stepOptions = doStepOptionsCreationTest("3.json");
39     assertEquals(1, stepOptions.lastSubtaskIndex);
40   }
41
42
43   private static StepicWrappers.StepOptions doStepOptionsCreationTest(String fileName) throws IOException {
44     String responseString =
45       FileUtil.loadFile(new File(getTestDataPath(), fileName));
46     StepicWrappers.StepSource stepSource =
47       EduStepicClient.deserializeStepicResponse(StepicWrappers.StepContainer.class, responseString).steps.get(0);
48     StepicWrappers.StepOptions options = stepSource.block.options;
49     List<TaskFile> files = options.files;
50     assertTrue("Wrong number of task files", files.size() == 1);
51     List<AnswerPlaceholder> placeholders = files.get(0).getAnswerPlaceholders();
52     assertTrue("Wrong number of placeholders", placeholders.size() == 1);
53     Map<Integer, AnswerPlaceholderSubtaskInfo> infos = placeholders.get(0).getSubtaskInfos();
54     assertNotNull(infos);
55     assertEquals(Collections.singletonList("Type your name here."), infos.get(0).getHints());
56     assertEquals("Liana", infos.get(0).getPossibleAnswer());
57     return options;
58   }
59
60   @Test
61   public void testAvailableCourses() throws IOException {
62     String responseString = FileUtil.loadFile(new File(getTestDataPath(), "courses.json"));
63     StepicWrappers.CoursesContainer container =
64       EduStepicClient.deserializeStepicResponse(StepicWrappers.CoursesContainer.class, responseString);
65     assertNotNull(container.courses);
66     assertTrue("Incorrect number of courses", container.courses.size() == 4);
67     List<CourseInfo> filtered = ContainerUtil.filter(container.courses, info -> EduStepicConnector.canBeOpened(info));
68     assertEquals(ContainerUtil.newArrayList("Adaptive Python", "Introduction to Python", "format2"),
69                  ContainerUtil.map(filtered, CourseInfo::getName));
70   }
71
72   @Test
73   public void placeholderSerialization() throws IOException {
74     final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().
75       registerTypeAdapter(AnswerPlaceholder.class, new StudySerializationUtils.Json.StepicAnswerPlaceholderAdapter()).create();
76     AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
77     answerPlaceholder.setOffset(1);
78     answerPlaceholder.setLength(10);
79     AnswerPlaceholderSubtaskInfo info1 = createSubtaskInfo("type here", "answer1", ContainerUtil.list("hint 1", "hint 2"));
80     AnswerPlaceholderSubtaskInfo info2 = createSubtaskInfo("type here1", "answer2", ContainerUtil.list("hint 11", "hint 22"));
81     answerPlaceholder.setSubtaskInfos(ContainerUtil.newHashMap(ContainerUtil.list(0, 1), ContainerUtil.list(info1, info2)));
82     final String placeholderSerialization = gson.toJson(answerPlaceholder);
83     String expected = FileUtil.loadFile(new File(getTestDataPath(), "placeholder.json"));
84     assertEquals(expected, placeholderSerialization);
85
86   }
87
88   private static AnswerPlaceholderSubtaskInfo createSubtaskInfo(String placeholderText, String possibleAnswer, List<String> hints) {
89     AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
90     info.setPlaceholderText(placeholderText);
91     info.setPossibleAnswer(possibleAnswer);
92     info.setHints(hints);
93     info.setNeedInsertText(true);
94     return info;
95   }
96
97   @NotNull
98   private static String getTestDataPath() {
99     return FileUtil.join(PlatformTestUtil.getCommunityPath(), "python/educational-core/student/testData/stepic");
100   }
101 }