001package edu.pdx.cs.joy.grader;
002
003import org.junit.jupiter.api.Test;
004
005import java.io.*;
006
007import static org.hamcrest.CoreMatchers.equalTo;
008import static org.hamcrest.CoreMatchers.hasItem;
009import static org.hamcrest.CoreMatchers.hasItems;
010import static org.hamcrest.MatcherAssert.assertThat;
011
012public class D2LSurveyResponsesCSVParserTest {
013
014  @Test
015  public void canParseFirstLineOfCSV() throws IOException {
016    Reader reader = createReaderWithLines("Section #,Q #,Q Type,Q Title,Q Text,Bonus?,Difficulty,Answer,Answer Match,# Responses");
017    D2LSurveyResponsesCSVParser parser = new D2LSurveyResponsesCSVParser(reader);
018
019    assertThat(parser.questionColumnIndex, equalTo(4));
020    assertThat(parser.responseColumnIndex, equalTo(7));
021
022  }
023
024  @Test
025  public void canParseOneResponseLine() throws IOException {
026    String question = "What is your response?";
027    String response = "This is my response";
028    Reader reader = createReaderWithLines("Q Text,Answer", question + "," + response);
029
030    D2LSurveyResponsesCSVParser parser = new D2LSurveyResponsesCSVParser(reader);
031    SurveyResponsesFromD2L responses = parser.getSurveyResponses();
032    assertThat(responses.getResponsesTo(question), hasItem(response));
033  }
034
035  @Test
036  public void canParseTwoResponseLines() throws IOException {
037    String question = "What is your response?";
038    String response1 = "This is my response";
039    String response2 = "This is my other response";
040    Reader reader = createReaderWithLines("Q Text,Answer", question + "," + response1, question + "," + response2);
041
042    D2LSurveyResponsesCSVParser parser = new D2LSurveyResponsesCSVParser(reader);
043    SurveyResponsesFromD2L responses = parser.getSurveyResponses();
044    assertThat(responses.getResponsesTo(question), hasItems(response1, response2));
045  }
046
047  @Test
048  public void canParseMultipleQuestion() throws IOException {
049    String question1 = "This is my first question?";
050    String response1 = "This is my response";
051    String question2 = "This is my second question?";
052    String response2 = "This is my other response";
053    Reader reader = createReaderWithLines("Q Text,Answer", question1 + "," + response1, question2 + "," + response2);
054
055    D2LSurveyResponsesCSVParser parser = new D2LSurveyResponsesCSVParser(reader);
056    SurveyResponsesFromD2L responses = parser.getSurveyResponses();
057    assertThat(responses.getResponsesTo(question1), hasItems(response1));
058    assertThat(responses.getResponsesTo(question2), hasItems(response2));
059  }
060
061  private Reader createReaderWithLines(String... lines) {
062    StringWriter sw = new StringWriter();
063    PrintWriter pw = new PrintWriter(sw);
064    for (String line : lines) {
065      pw.println(line);
066    }
067
068    return new StringReader(sw.toString());
069  }
070}