From 4539a62de912546b719b473030a698cf23835f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 5 Aug 2017 13:56:26 +0200 Subject: [PATCH] Add school api (#11) --- .../github/wulkanowy/api/school/School.java | 32 +++++ .../wulkanowy/api/school/SchoolData.java | 55 +++++++ .../wulkanowy/api/school/SchoolInfo.java | 31 ++++ .../github/wulkanowy/api/school/Subject.java | 24 ++++ .../wulkanowy/api/school/TeachersData.java | 36 +++++ .../wulkanowy/api/school/TeachersInfo.java | 41 ++++++ .../wulkanowy/api/school/SchoolInfoTest.java | 33 +++++ .../wulkanowy/api/school/SchoolTest.java | 30 ++++ .../api/school/TeachersInfoTest.java | 44 ++++++ .../github/wulkanowy/api/school/Szkola.html | 136 ++++++++++++++++++ 10 files changed, 462 insertions(+) create mode 100644 app/src/main/java/io/github/wulkanowy/api/school/School.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/school/SchoolData.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/school/Subject.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/school/TeachersData.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java create mode 100644 app/src/test/java/io/github/wulkanowy/api/school/SchoolInfoTest.java create mode 100644 app/src/test/java/io/github/wulkanowy/api/school/SchoolTest.java create mode 100644 app/src/test/java/io/github/wulkanowy/api/school/TeachersInfoTest.java create mode 100644 app/src/test/resources/io/github/wulkanowy/api/school/Szkola.html diff --git a/app/src/main/java/io/github/wulkanowy/api/school/School.java b/app/src/main/java/io/github/wulkanowy/api/school/School.java new file mode 100644 index 00000000..89b43627 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/school/School.java @@ -0,0 +1,32 @@ +package io.github.wulkanowy.api.school; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; + +import java.io.IOException; + +import io.github.wulkanowy.api.Cookies; +import io.github.wulkanowy.api.StudentAndParent; +import io.github.wulkanowy.api.Vulcan; +import io.github.wulkanowy.api.login.LoginErrorException; + +public class School extends Vulcan { + private StudentAndParent snp = null; + + private String schoolPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/" + + "Szkola.mvc/Nauczyciele"; + + public School(Cookies cookies, StudentAndParent snp) { + this.cookies = cookies; + this.snp = snp; + } + + public Document getSchoolPageDocument() throws IOException, LoginErrorException { + schoolPageUrl = schoolPageUrl.replace("{locationID}", snp.getLocationID()); + schoolPageUrl = schoolPageUrl.replace("{ID}", snp.getID()); + + return Jsoup.connect(schoolPageUrl) + .cookies(getCookies()) + .get(); + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/school/SchoolData.java b/app/src/main/java/io/github/wulkanowy/api/school/SchoolData.java new file mode 100644 index 00000000..24bb3107 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/school/SchoolData.java @@ -0,0 +1,55 @@ +package io.github.wulkanowy.api.school; + +public class SchoolData { + + private String name = ""; + private String address = ""; + private String phoneNumber = ""; + private String headmaster = ""; + private String[] pedagogue; + + public String getName() { + return name; + } + + public SchoolData setName(String name) { + this.name = name; + return this; + } + + public String getAddress() { + return address; + } + + public SchoolData setAddress(String address) { + this.address = address; + return this; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public SchoolData setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + return this; + } + + public String getHeadmaster() { + return headmaster; + } + + public SchoolData setHeadmaster(String headmaster) { + this.headmaster = headmaster; + return this; + } + + public String[] getPedagogues() { + return pedagogue; + } + + public SchoolData setPedagogue(String[] pedagogue) { + this.pedagogue = pedagogue; + return this; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java b/app/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java new file mode 100644 index 00000000..861a1be8 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/school/SchoolInfo.java @@ -0,0 +1,31 @@ +package io.github.wulkanowy.api.school; + +import org.jsoup.nodes.Element; + +import java.io.IOException; + +import io.github.wulkanowy.api.StudentAndParent; +import io.github.wulkanowy.api.Vulcan; +import io.github.wulkanowy.api.login.LoginErrorException; + +public class SchoolInfo extends Vulcan { + + private School school = null; + private StudentAndParent snp = null; + + public SchoolInfo(School school, StudentAndParent snp) { + this.school = school; + this.snp = snp; + } + + public SchoolData getSchoolData() throws IOException, LoginErrorException { + Element e = school.getSchoolPageDocument().select(".mainContainer > article").get(0); + + return new SchoolData() + .setName(snp.getRowDataChildValue(e, 1)) + .setAddress(snp.getRowDataChildValue(e, 2)) + .setPhoneNumber(snp.getRowDataChildValue(e, 3)) + .setHeadmaster(snp.getRowDataChildValue(e, 4)) + .setPedagogue(snp.getRowDataChildValue(e, 5).split(", ")); + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/school/Subject.java b/app/src/main/java/io/github/wulkanowy/api/school/Subject.java new file mode 100644 index 00000000..d8e0d417 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/school/Subject.java @@ -0,0 +1,24 @@ +package io.github.wulkanowy.api.school; + +public class Subject { + private String name = ""; + private String[] teachers; + + public String getName() { + return name; + } + + public Subject setName(String name) { + this.name = name; + return this; + } + + public String[] getTeachers() { + return teachers; + } + + public Subject setTeachers(String[] teachers) { + this.teachers = teachers; + return this; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/school/TeachersData.java b/app/src/main/java/io/github/wulkanowy/api/school/TeachersData.java new file mode 100644 index 00000000..73d88452 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/school/TeachersData.java @@ -0,0 +1,36 @@ +package io.github.wulkanowy.api.school; + +import java.util.List; + +public class TeachersData { + private String className = ""; + private String[] classTeacher; + private List subjects; + + public String getClassName() { + return className; + } + + public TeachersData setClassName(String className) { + this.className = className; + return this; + } + + public String[] getClassTeacher() { + return classTeacher; + } + + public TeachersData setClassTeacher(String[] classTeacher) { + this.classTeacher = classTeacher; + return this; + } + + public List getSubjects() { + return subjects; + } + + public TeachersData setSubjects(List subjects) { + this.subjects = subjects; + return this; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java b/app/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java new file mode 100644 index 00000000..4c330f73 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/school/TeachersInfo.java @@ -0,0 +1,41 @@ +package io.github.wulkanowy.api.school; + +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import io.github.wulkanowy.api.Vulcan; +import io.github.wulkanowy.api.login.LoginErrorException; + +public class TeachersInfo extends Vulcan { + + private School school = null; + + public TeachersInfo(School school) { + this.school = school; + } + + public TeachersData getTeachersData() throws IOException, LoginErrorException { + Document doc = school.getSchoolPageDocument(); + Elements rows = doc.select(".mainContainer > table tbody tr"); + String description = doc.select(".mainContainer > p").first().text(); + + List subjects = new ArrayList<>(); + + for (Element subject : rows) { + subjects.add(new Subject() + .setName(subject.select("td").get(1).text()) + .setTeachers(subject.select("td").get(2).text().split(", ")) + ); + } + + return new TeachersData() + .setClassName(description.split(", ")[0].split(": ")[1].trim()) + .setClassTeacher(description.split("Wychowawcy:")[1].trim().split(", ")) + .setSubjects(subjects); + } +} diff --git a/app/src/test/java/io/github/wulkanowy/api/school/SchoolInfoTest.java b/app/src/test/java/io/github/wulkanowy/api/school/SchoolInfoTest.java new file mode 100644 index 00000000..2875fd11 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/api/school/SchoolInfoTest.java @@ -0,0 +1,33 @@ +package io.github.wulkanowy.api.school; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class SchoolInfoTest extends SchoolTest { + + private SchoolInfo schoolInfo; + + @Before + public void setUp() throws Exception { + super.setUp(); + schoolInfo = new SchoolInfo(school, snp); + } + + @Test + public void getSchoolDataTest() throws Exception { + SchoolData schoolData = schoolInfo.getSchoolData(); + + Assert.assertEquals("Zespół Szkół nr 64", schoolData.getName()); + Assert.assertEquals("ul. Wiśniowa 128, 01-234 Rogalowo, Nibylandia", + schoolData.getAddress()); + Assert.assertEquals("55 5555555", schoolData.getPhoneNumber()); + Assert.assertEquals("Antoni Sobczyk", schoolData.getHeadmaster()); + Assert.assertArrayEquals(new String[]{ + "Zofia Czerwińska [ZC]", + "Aleksander Krzemiński [AK]", + "Karolina Kowalska [KK]", + "Bartek Dąbrowski [BD]" + }, schoolData.getPedagogues()); + } +} diff --git a/app/src/test/java/io/github/wulkanowy/api/school/SchoolTest.java b/app/src/test/java/io/github/wulkanowy/api/school/SchoolTest.java new file mode 100644 index 00000000..21358691 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/api/school/SchoolTest.java @@ -0,0 +1,30 @@ +package io.github.wulkanowy.api.school; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.junit.Before; +import org.mockito.Mockito; + +import io.github.wulkanowy.api.FixtureHelper; +import io.github.wulkanowy.api.StudentAndParent; + +public class SchoolTest { + + protected School school; + protected StudentAndParent snp; + private String fixtureFileName = "Szkola.html"; + + @Before + public void setUp() throws Exception { + String input = FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)); + + Document schoolPageDocument = Jsoup.parse(input); + + school = Mockito.mock(School.class); + Mockito.when(school.getSchoolPageDocument()).thenReturn(schoolPageDocument); + snp = Mockito.mock(StudentAndParent.class); + Mockito.when(snp.getRowDataChildValue(Mockito.any(Element.class), + Mockito.anyInt())).thenCallRealMethod(); + } +} diff --git a/app/src/test/java/io/github/wulkanowy/api/school/TeachersInfoTest.java b/app/src/test/java/io/github/wulkanowy/api/school/TeachersInfoTest.java new file mode 100644 index 00000000..1b79dd98 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/api/school/TeachersInfoTest.java @@ -0,0 +1,44 @@ +package io.github.wulkanowy.api.school; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class TeachersInfoTest extends SchoolTest { + + private TeachersInfo teachersInfo; + + @Before + public void setUp() throws Exception { + super.setUp(); + teachersInfo = new TeachersInfo(school); + } + + @Test + public void getTeachersDataTest() throws Exception { + TeachersData teachersData = teachersInfo.getTeachersData(); + + Assert.assertEquals("1a", teachersData.getClassName()); + Assert.assertArrayEquals(new String[]{ + "Karolina Kowalska [AN]", + "Antoni Sobczyk [AS]" + }, teachersData.getClassTeacher()); + + List subjects = teachersData.getSubjects(); + + Assert.assertEquals("Biologia", subjects.get(0).getName()); + Assert.assertArrayEquals(new String[]{"Karolina Kowalska [AN]"}, + subjects.get(0).getTeachers()); + Assert.assertEquals("Karolina Kowalska [AN]", + subjects.get(0).getTeachers()[0]); + + Assert.assertEquals("Język angielski", subjects.get(6).getName()); + Assert.assertArrayEquals(new String[]{ + "Karolina Kowalska [AN]", + "Mateusz Kowal [MK]", + "Amelia Mazur [AM]" + }, subjects.get(6).getTeachers()); + } +} diff --git a/app/src/test/resources/io/github/wulkanowy/api/school/Szkola.html b/app/src/test/resources/io/github/wulkanowy/api/school/Szkola.html new file mode 100644 index 00000000..05a698a0 --- /dev/null +++ b/app/src/test/resources/io/github/wulkanowy/api/school/Szkola.html @@ -0,0 +1,136 @@ + + + + + Witryna ucznia i rodzica – Szkoła i nauczyciele + + +
+

Szkoła

+
+
+ Nazwa szkoły: + Zespół Szkół nr 64 +
+
+ Adres szkoły: + ul. Wiśniowa 128, 01-234 Rogalowo, Nibylandia +
+
+ Telefon: + 55 5555555 +
+
+ Imię i nazwisko dyrektora: + Antoni Sobczyk +
+
+ Imię i nazwisko pedagoga: + Zofia Czerwińska [ZC], Aleksander Krzemiński [AK], Karolina Kowalska [KK], Bartek Dąbrowski [BD] +
+
+

Nauczyciele

+

+ Klasa: 1a, Wychowawcy: + Karolina Kowalska [AN], Antoni Sobczyk [AS]

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Lp.PrzedmiotNauczyciel
1BiologiaKarolina Kowalska [AN]
2ChemiaZofia Czerwińska [NA]
3Edukacja dla bezpieczeństwaAleksandra Krajewska [AK]
4FizykaStanisław Krupa [BS]
5GeografiaAleksandra Wójtowicz [AW]
6HistoriaSara Wierzbicka [KB]
7Język angielskiKarolina Kowalska [AN], Mateusz Kowal [MK], Amelia Mazur [AM]
8Język niemieckiMateusz Kowal [MK], Barbara Markowska [BM]
9Język polskiMichał Mazur [MM]
10MatematykaSzymon Wojciechowski [SW]
11PlastykaMichał Mazur [MM]
12ReligiaMaja Wiśniewska [M]
13Wiedza o społeczeństwieKarolina Kowalska [AN]
14Wychowanie do życia w rodzinieZofia Czerwińska [NA]
15Wychowanie fizyczneKarolina Kowalska [AN], Liliana Kowal [LK]
16Zajęcia techniczneBartek Dąbrowski [BD]
17Zajęcia z wychowawcąKarolina Kowalska [AN]
+
+ + +