Skip to content

Commit

Permalink
Add SVPlanDemoTest (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Sep 4, 2016
1 parent 4998136 commit 9e5e509
Show file tree
Hide file tree
Showing 3 changed files with 773 additions and 3 deletions.
12 changes: 9 additions & 3 deletions parser/src/main/java/me/vertretungsplan/parser/SVPlanParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import me.vertretungsplan.objects.SubstitutionSchedule;
import me.vertretungsplan.objects.SubstitutionScheduleData;
import me.vertretungsplan.objects.SubstitutionScheduleDay;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -48,13 +49,19 @@ public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONEx
String encoding = data.getString("encoding");
List<Document> docs = new ArrayList<>();

SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);

for (int i = 0; i < urls.length(); i++) {
JSONObject url = urls.getJSONObject(i);
loadUrl(url.getString("url"), encoding, docs);
}

SubstitutionSchedule v = parseSVPlanSchedule(docs);
return v;
}

@NotNull
SubstitutionSchedule parseSVPlanSchedule(List<Document> docs) throws IOException, JSONException {
SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);

for (Document doc : docs) {
if (doc.select(".svp").size() > 0) {
for (Element svp:doc.select(".svp")) {
Expand All @@ -67,7 +74,6 @@ public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONEx

v.setClasses(getAllClasses());
v.setTeachers(getAllTeachers());

return v;
}

Expand Down
72 changes: 72 additions & 0 deletions parser/src/test/java/me/vertretungsplan/parser/SVPlanDemoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class SVPlanDemoTest extends BaseDemoTest {
private String html;

private SVPlanParser parser;

@Before
public void setUp() throws JSONException {
html = readResource("/svplan/svplan.html");
SubstitutionScheduleData scheduleData = new SubstitutionScheduleData();
scheduleData.setData(new JSONObject());
parser = new SVPlanParser(scheduleData, null);
}

@Test
public void demoTest() throws IOException, JSONException {
List<Document> docs = new ArrayList<>();
docs.add(Jsoup.parse(html));
SubstitutionSchedule schedule = parser.parseSVPlanSchedule(docs);

assertEquals(new LocalDateTime(2016, 9, 2, 11, 10), schedule.getLastChange());
assertEquals(5, schedule.getDays().size());

SubstitutionScheduleDay day = schedule.getDays().get(0);

assertEquals(new LocalDate(2016, 9, 2), day.getDate());
assertEquals(14, day.getSubstitutions().size());
assertEquals(1, day.getMessages().size());
assertEquals("Ordnungsdienst:9BR<br>", day.getMessages().get(0));

for (Substitution subst : day.getSubstitutions()) {
assertTrue(subst.getClasses().size() == 1);
assertNotEmpty(subst.getLesson());
assertNullOrNotEmpty(subst.getPreviousSubject());
assertNotEmpty(subst.getSubject());
assertNullOrNotEmpty(subst.getRoom());
assertNullOrNotEmpty(subst.getTeacher());
assertNullOrNotEmpty(subst.getPreviousTeacher());
assertNullOrNotEmpty(subst.getDesc());
assertNotEmpty(subst.getType());
}
}
}
Loading

0 comments on commit 9e5e509

Please sign in to comment.