From 3d6515bcf20b3ab58c210a727a8b9252c40b1c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 6 Aug 2017 13:25:08 +0200 Subject: [PATCH] Add to Grade semester and symbol properties (#12) --- .../dashboard/marks/MarksFragment.java | 4 +- .../wulkanowy/api/StudentAndParent.java | 5 + .../io/github/wulkanowy/api/grades/Grade.java | 19 +-- .../github/wulkanowy/api/grades/Grades.java | 28 +++++ .../wulkanowy/api/grades/GradesList.java | 49 ++++---- .../wulkanowy/api/grades/SubjectsList.java | 5 +- .../wulkanowy/api/StudentAndParentTest.java | 5 +- .../wulkanowy/api/grades/GradesListTest.java | 95 +++++++++++++++ .../api/grades/OcenyWszystkie-filled.html | 108 ++++++++++++++++++ 9 files changed, 280 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/api/grades/Grades.java create mode 100644 app/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java create mode 100644 app/src/test/resources/io/github/wulkanowy/api/grades/OcenyWszystkie-filled.html diff --git a/app/src/main/java/io/github/wulkanowy/activity/dashboard/marks/MarksFragment.java b/app/src/main/java/io/github/wulkanowy/activity/dashboard/marks/MarksFragment.java index 5f2065808..3f3da013a 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/dashboard/marks/MarksFragment.java +++ b/app/src/main/java/io/github/wulkanowy/activity/dashboard/marks/MarksFragment.java @@ -19,6 +19,7 @@ import java.util.Map; import io.github.wulkanowy.R; import io.github.wulkanowy.api.Cookies; import io.github.wulkanowy.api.StudentAndParent; +import io.github.wulkanowy.api.grades.Grades; import io.github.wulkanowy.api.grades.GradesList; import io.github.wulkanowy.api.grades.Subject; import io.github.wulkanowy.api.grades.SubjectsList; @@ -105,7 +106,8 @@ public class MarksFragment extends Fragment { subjectsName.add(subject.getName()); } - GradesList gradesList = new GradesList(snp.getCookiesObject(), snp); + Grades gradesObject = new Grades(snp.getCookiesObject(), snp); + GradesList gradesList = new GradesList(gradesObject, snp); GradesDatabase gradesDatabase = new GradesDatabase(mContext); gradesDatabase.open(); gradesDatabase.put(gradesList.getAll()); diff --git a/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java b/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java index d6640c895..02103cf07 100644 --- a/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java +++ b/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -84,6 +84,11 @@ public class StudentAndParent extends Vulcan { url = url.replace("{ID}", getID()); Document gradesPage = getPageByUrl(url); + + return getSemesters(gradesPage); + } + + public List getSemesters(Document gradesPage) { Elements semesterOptions = gradesPage.select("#okresyKlasyfikacyjneDropDownList option"); List semesters = new ArrayList<>(); diff --git a/app/src/main/java/io/github/wulkanowy/api/grades/Grade.java b/app/src/main/java/io/github/wulkanowy/api/grades/Grade.java index b362da8c0..c2f85d339 100644 --- a/app/src/main/java/io/github/wulkanowy/api/grades/Grade.java +++ b/app/src/main/java/io/github/wulkanowy/api/grades/Grade.java @@ -10,23 +10,23 @@ public class Grade { private int subjectID; - private String subject; + private String subject = ""; - private String value; + private String value = ""; - private String color; + private String color = ""; - private String symbol; + private String symbol = ""; - private String description; + private String description = ""; - private String weight; + private String weight = ""; - private String date; + private String date = ""; - private String teacher; + private String teacher = ""; - private String semester; + private String semester = ""; private boolean isNew; @@ -156,6 +156,7 @@ public class Grade { public Grade setIsNew(boolean isNew) { this.isNew = isNew; + return this; } diff --git a/app/src/main/java/io/github/wulkanowy/api/grades/Grades.java b/app/src/main/java/io/github/wulkanowy/api/grades/Grades.java new file mode 100644 index 000000000..aef658b37 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/grades/Grades.java @@ -0,0 +1,28 @@ +package io.github.wulkanowy.api.grades; + +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 Grades extends Vulcan { + + private StudentAndParent snp = null; + + public Grades(Cookies cookies, StudentAndParent snp) { + this.cookies = cookies; + this.snp = snp; + } + + //TODO: move to snp + public Document getGradesPageDocument(String url) throws IOException, LoginErrorException { + return getPageByUrl(url + .replace("{locationID}", snp.getLocationID()) + .replace("{ID}", snp.getID()) + ); + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/grades/GradesList.java b/app/src/main/java/io/github/wulkanowy/api/grades/GradesList.java index 82350f499..559acc2e6 100644 --- a/app/src/main/java/io/github/wulkanowy/api/grades/GradesList.java +++ b/app/src/main/java/io/github/wulkanowy/api/grades/GradesList.java @@ -10,22 +10,23 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import io.github.wulkanowy.api.Cookies; +import io.github.wulkanowy.api.Semester; import io.github.wulkanowy.api.StudentAndParent; import io.github.wulkanowy.api.Vulcan; import io.github.wulkanowy.api.login.LoginErrorException; public class GradesList extends Vulcan { + private Grades grades = null; private StudentAndParent snp = null; private String gradesPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}" + "/Oceny/Wszystkie?details=2&okres="; - private List grades = new ArrayList<>(); + private List gradesList = new ArrayList<>(); - public GradesList(Cookies cookies, StudentAndParent snp) { - this.cookies = cookies; + public GradesList(Grades grades, StudentAndParent snp) { + this.grades = grades; this.snp = snp; } @@ -34,39 +35,41 @@ public class GradesList extends Vulcan { } public List getAll() throws IOException, LoginErrorException { - return getAll(snp.getCurrentSemester(snp.getSemesters()).getNumber()); + return getAll(""); } public List getAll(String semester) throws IOException, LoginErrorException { - String url = getGradesPageUrl(); - url = url.replace("{locationID}", snp.getLocationID()); - url = url.replace("{ID}", snp.getID()); + Document gradesPage = grades.getGradesPageDocument(getGradesPageUrl() + semester); + Elements gradesRows = gradesPage.select(".ocenySzczegoly-table > tbody > tr"); + Semester currentSemester = snp.getCurrentSemester(snp.getSemesters(gradesPage)); - Document marksPage = getPageByUrl(url + semester); - Elements marksRows = marksPage.select(".ocenySzczegoly-table > tbody > tr"); - - for (Element row : marksRows) { - Pattern pattern = Pattern.compile("#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"); - Matcher matcher = pattern.matcher( - row.select("td:nth-child(2) span.ocenaCzastkowa").attr("style")); - String color; - if (!matcher.find()) { - color = "000000"; - } else { - color = matcher.group(1); + for (Element row : gradesRows) { + if ("Brak ocen".equals(row.select("td:nth-child(2)").text())) { + continue; } - grades.add(new Grade() + String descriptions = row.select("td:nth-child(3)").text(); + String symbol = descriptions.split(", ")[0]; + String description = descriptions.replaceFirst(symbol, "").replaceFirst(", ", ""); + + Pattern pattern = Pattern.compile("#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"); + Matcher matcher = pattern.matcher(row.select("td:nth-child(2) span.ocenaCzastkowa") + .attr("style")); + String color = matcher.find() ? matcher.group(1) : ""; + + gradesList.add(new Grade() .setSubject(row.select("td:nth-child(1)").text()) .setValue(row.select("td:nth-child(2)").text()) .setColor(color) - .setDescription(row.select("td:nth-child(3)").text()) + .setSymbol(symbol) + .setDescription(description) .setWeight(row.select("td:nth-child(4)").text()) .setDate(row.select("td:nth-child(5)").text()) .setTeacher(row.select("td:nth-child(6)").text()) + .setSemester(currentSemester.getNumber()) ); } - return grades; + return gradesList; } } diff --git a/app/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java b/app/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java index d39ff73f0..1de1fad1a 100644 --- a/app/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java +++ b/app/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java @@ -1,6 +1,5 @@ package io.github.wulkanowy.api.grades; -import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; @@ -32,9 +31,7 @@ public class SubjectsList extends Vulcan { subjectsPageUrl = subjectsPageUrl.replace("{locationID}", snp.getLocationID()); subjectsPageUrl = subjectsPageUrl.replace("{ID}", snp.getID()); - Document subjectPage = Jsoup.connect(subjectsPageUrl) - .cookies(getCookies()) - .get(); + Document subjectPage = getPageByUrl(subjectsPageUrl); Elements rows = subjectPage.select(".ocenyZwykle-table > tbody > tr"); diff --git a/app/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java b/app/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java index 5b92d23c0..a01ee7a7a 100644 --- a/app/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java +++ b/app/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java @@ -10,6 +10,7 @@ import org.powermock.api.mockito.PowerMockito; import java.util.ArrayList; import java.util.List; + public class StudentAndParentTest { private String fixtureFileName = "OcenyWszystkie-semester.html"; @@ -30,7 +31,9 @@ public class StudentAndParentTest { Mockito.when(snp.getLocationID()).thenReturn("symbol"); Mockito.when(snp.getID()).thenReturn("123456"); Mockito.when(snp.getSemesters()).thenCallRealMethod(); - Mockito.when(snp.getCurrentSemester(Mockito.anyListOf(Semester.class))).thenCallRealMethod(); + Mockito.when(snp.getSemesters(Mockito.any(Document.class))).thenCallRealMethod(); + Mockito.when(snp.getCurrentSemester(Mockito.anyListOf(Semester.class))) + .thenCallRealMethod(); } @Test diff --git a/app/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java b/app/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java new file mode 100644 index 000000000..319e365f8 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java @@ -0,0 +1,95 @@ +package io.github.wulkanowy.api.grades; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; + +import java.util.List; + +import io.github.wulkanowy.api.FixtureHelper; +import io.github.wulkanowy.api.Semester; +import io.github.wulkanowy.api.StudentAndParent; + +public class GradesListTest { + + private String fixtureFileName = "OcenyWszystkie-filled.html"; + + private GradesList gradesList; + + @Before + public void setUp() throws Exception { + String input = FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)); + Document gradesPageDocument = Jsoup.parse(input); + + StudentAndParent snp = Mockito.mock(StudentAndParent.class); + PowerMockito.whenNew(StudentAndParent.class).withAnyArguments().thenReturn(snp); + Mockito.when(snp.getLocationID()).thenReturn("symbol"); + Mockito.when(snp.getID()).thenReturn("123456"); + Mockito.when(snp.getGradesPageUrl()).thenReturn("http://example.null"); + Mockito.when(snp.getSemesters()).thenCallRealMethod(); + Mockito.when(snp.getSemesters(Mockito.any(Document.class))).thenCallRealMethod(); + Mockito.when(snp.getCurrentSemester(Mockito.anyListOf(Semester.class))) + .thenCallRealMethod(); + + Grades grades = Mockito.mock(Grades.class); + PowerMockito.whenNew(Grades.class).withAnyArguments().thenReturn(grades); + Mockito.when(grades.getGradesPageDocument(Mockito.anyString())) + .thenReturn(gradesPageDocument); + + gradesList = new GradesList(grades, snp); + } + + @Test + public void getAllTest() throws Exception { + List grades = gradesList.getAll(); + Assert.assertEquals(6, grades.size()); // 2 items are skipped + + Grade grade1 = grades.get(0); + Assert.assertEquals("Zajęcia z wychowawcą", grade1.getSubject()); + Assert.assertEquals("5", grade1.getValue()); + Assert.assertEquals("000000", grade1.getColor()); + Assert.assertEquals("A1", grade1.getSymbol()); + Assert.assertEquals("Dzień Kobiet w naszej klasie", grade1.getDescription()); + Assert.assertEquals("1,00", grade1.getWeight()); + Assert.assertEquals("21.03.2017", grade1.getDate()); + Assert.assertEquals("Patryk Maciejewski", grade1.getTeacher()); + Assert.assertEquals("7654321", grade1.getSemester()); + + Grade grade2 = grades.get(3); + Assert.assertEquals("Język angielski", grade2.getSubject()); + Assert.assertEquals("5", grade2.getValue()); + Assert.assertEquals("1289F7", grade2.getColor()); + Assert.assertEquals("BW3", grade2.getSymbol()); + Assert.assertEquals("Writing", grade2.getDescription()); + Assert.assertEquals("3,00", grade2.getWeight()); + Assert.assertEquals("02.06.2017", grade2.getDate()); + Assert.assertEquals("Oliwia Woźniak", grade2.getTeacher()); + Assert.assertEquals("7654321", grade2.getSemester()); + + Grade grade3 = grades.get(4); + Assert.assertEquals("Wychowanie fizyczne", grade3.getSubject()); + Assert.assertEquals("1", grade3.getValue()); + Assert.assertEquals("6ECD07", grade3.getColor()); + Assert.assertEquals("STR", grade3.getSymbol()); + Assert.assertEquals("", grade3.getDescription()); + Assert.assertEquals("8,00", grade3.getWeight()); + Assert.assertEquals("02.04.2017", grade3.getDate()); + Assert.assertEquals("Klaudia Dziedzic", grade3.getTeacher()); + Assert.assertEquals("7654321", grade3.getSemester()); + + Grade grade4 = grades.get(5); + Assert.assertEquals("Język polski", grade4.getSubject()); + Assert.assertEquals("1", grade4.getValue()); + Assert.assertEquals("6ECD07", grade4.getColor()); + Assert.assertEquals("K", grade4.getSymbol()); + Assert.assertEquals("Kordian", grade4.getDescription()); + Assert.assertEquals("5,00", grade4.getWeight()); + Assert.assertEquals("06.02.2017", grade4.getDate()); + Assert.assertEquals("Amelia Stępień", grade4.getTeacher()); + Assert.assertEquals("7654321", grade4.getSemester()); + } +} diff --git a/app/src/test/resources/io/github/wulkanowy/api/grades/OcenyWszystkie-filled.html b/app/src/test/resources/io/github/wulkanowy/api/grades/OcenyWszystkie-filled.html new file mode 100644 index 000000000..a229d1d53 --- /dev/null +++ b/app/src/test/resources/io/github/wulkanowy/api/grades/OcenyWszystkie-filled.html @@ -0,0 +1,108 @@ + + + + + Witryna ucznia i rodzica – Oceny + + +
+

Oceny

+
+
+ + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PrzedmiotOcena cząstkowaOpisWagaDataNauczyciel
ZachowanieBrak ocen
Zajęcia z wychowawcą + 5 + A1, Dzień Kobiet w naszej klasie1,0021.03.2017Patryk Maciejewski
Edukacja dla bezpieczeństwa + 4- + S1, PIERWSZA POMOC I RESUSCYTACJA5,0031.03.2017Weronika Ratajczak
Fizyka + 2 + O, Odpowiedź3,0025.06.2017Jakub Michalak
Język angielski + 5 + BW3, Writing3,0002.06.2017Oliwia Woźniak
Wiedza o społeczeństwieBrak ocen
Wychowanie fizyczne1STR8,0002.04.2017Klaudia Dziedzic
Język polski1K, Kordian5,0006.02.2017Amelia Stępień
+
+ + +