Skip to content

Commit

Permalink
Add CSVDemoTest (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Sep 4, 2016
1 parent cd68614 commit 2c2889d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions parser/src/main/java/me/vertretungsplan/parser/CSVParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import me.vertretungsplan.objects.SubstitutionScheduleData;
import me.vertretungsplan.objects.SubstitutionScheduleDay;
import org.apache.http.client.fluent.Request;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -42,6 +43,11 @@ public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONEx
String url = data.getString("url");
String response = executor.execute(Request.Get(url)).returnContent().asString();

return parseCSV(response);
}

@NotNull
SubstitutionSchedule parseCSV(String response) throws JSONException, IOException {
SubstitutionSchedule schedule = SubstitutionSchedule.fromData(scheduleData);

String[] lines = response.split("\n");
Expand Down
68 changes: 68 additions & 0 deletions parser/src/test/java/me/vertretungsplan/parser/CSVDemoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* substitution-schedule-parser - Java library for parsing schools' substitution schedules
* Copyright (c) 2016 Johan v. Forstner
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package me.vertretungsplan.parser;

import me.vertretungsplan.objects.Substitution;
import me.vertretungsplan.objects.SubstitutionSchedule;
import me.vertretungsplan.objects.SubstitutionScheduleData;
import me.vertretungsplan.objects.SubstitutionScheduleDay;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class CSVDemoTest extends BaseDemoTest {
private String csv;
private CSVParser parser;

@Before
public void setUp() throws JSONException {
csv = readResource("/csv/csv.csv");
SubstitutionScheduleData scheduleData = new SubstitutionScheduleData();
JSONObject data = new JSONObject();
data.put("skipLines", 1);
data.put("separator", "\\|");
JSONArray columns = new JSONArray();
columns.put("ignore").put("day").put("stand").put("class").put("lesson").put("subject").put("teacher").put
("room").put("desc-type");
data.put("columns", columns);
data.put("classes", new JSONArray());
scheduleData.setData(data);
parser = new CSVParser(scheduleData, null);
}

@Test
public void demoTest() throws IOException, JSONException {
SubstitutionSchedule schedule = parser.parseCSV(csv);
assertEquals(2, schedule.getDays().size());

SubstitutionScheduleDay day = schedule.getDays().get(0);
assertEquals(new LocalDate(2016, 9, 5), day.getDate());
assertEquals(new LocalDateTime(2016, 9, 2, 8, 16), day.getLastChange());
assertEquals(1, day.getSubstitutions().size());
assertEquals(0, day.getMessages().size());

Substitution subst = day.getSubstitutions().iterator().next();
assertEquals(1, subst.getClasses().size());
assertEquals("05A", subst.getClasses().iterator().next());
assertEquals("6", subst.getLesson());
assertEquals("!Ma", subst.getSubject());
assertEquals("!DROE", subst.getTeacher());
assertEquals("!B203", subst.getRoom());
assertEquals("für Mu REN", subst.getDesc());
assertEquals("Vertretung", subst.getType());
}
}
3 changes: 3 additions & 0 deletions parser/src/test/resources/csv/csv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Offset|Tag|LastMod|Klasse|Stunde|Fach|Lehrer|Raum|Info
1|5.9.2016|02.09.2016, 08:16|05A|6|!Ma|!DROE|!B203|für Mu REN
2|6.9.2016|01.09.2016, 13:50|06A|3|!MNT|!NA|!D203|für Ku HU

0 comments on commit 2c2889d

Please sign in to comment.