From 89d235dd8a33d76e4679d42eb7d40a1c26df6226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Wed, 2 Aug 2017 12:04:49 +0200 Subject: [PATCH] Add notes api (#8) * Add notes api with tests * Get county for snp direct from database --- app/build.gradle | 2 + .../dashboard/marks/MarksFragment.java | 12 ++- .../wulkanowy/activity/main/LoginTask.java | 14 ++-- .../java/io/github/wulkanowy/api/Cookies.java | 20 ++--- .../wulkanowy/api/StudentAndParent.java | 21 +++--- .../java/io/github/wulkanowy/api/Vulcan.java | 26 +++---- .../wulkanowy/api/grades/GradesList.java | 16 ++-- .../wulkanowy/api/grades/SubjectsList.java | 16 ++-- .../io/github/wulkanowy/api/login/Login.java | 8 +- .../wulkanowy/api/notes/AchievementsList.java | 32 ++++++++ .../io/github/wulkanowy/api/notes/Note.java | 48 ++++++++++++ .../io/github/wulkanowy/api/notes/Notes.java | 33 +++++++++ .../github/wulkanowy/api/notes/NotesList.java | 42 +++++++++++ .../wulkanowy/api/user/BasicInformation.java | 52 +++++-------- .../io/github/wulkanowy/api/user/User.java | 33 +++++++++ .../github/wulkanowy/api/FixtureHelper.java | 14 ++++ .../api/notes/AchievementsListTest.java | 55 ++++++++++++++ .../wulkanowy/api/notes/NotesListTest.java | 74 +++++++++++++++++++ .../api/notes/UwagiOsiagniecia-empty.html | 20 +++++ .../api/notes/UwagiOsiagniecia-filled.html | 65 ++++++++++++++++ 20 files changed, 514 insertions(+), 89 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/notes/Note.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/notes/Notes.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/notes/NotesList.java create mode 100644 app/src/main/java/io/github/wulkanowy/api/user/User.java create mode 100644 app/src/test/java/io/github/wulkanowy/api/FixtureHelper.java create mode 100644 app/src/test/java/io/github/wulkanowy/api/notes/AchievementsListTest.java create mode 100644 app/src/test/java/io/github/wulkanowy/api/notes/NotesListTest.java create mode 100644 app/src/test/resources/io/github/wulkanowy/api/notes/UwagiOsiagniecia-empty.html create mode 100644 app/src/test/resources/io/github/wulkanowy/api/notes/UwagiOsiagniecia-filled.html diff --git a/app/build.gradle b/app/build.gradle index e9db9542a..5b521a10c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,4 +41,6 @@ dependencies { compile 'org.apache.commons:commons-lang3:3.6' testCompile 'junit:junit:4.12' + testCompile 'org.powermock:powermock-api-mockito:1.6.1' + testCompile group: 'org.unitils', name: 'unitils-core', version: '3.3' } 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 0bee26489..0b782e231 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,10 +19,13 @@ 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.Grade; import io.github.wulkanowy.api.grades.GradesList; import io.github.wulkanowy.api.grades.Subject; import io.github.wulkanowy.api.grades.SubjectsList; +import io.github.wulkanowy.database.accounts.AccountData; +import io.github.wulkanowy.database.accounts.DatabaseAccount; public class MarksFragment extends Fragment { @@ -83,13 +86,18 @@ public class MarksFragment extends Fragment { try { Cookies cookies = new Cookies(); cookies.setItems(loginCookies); - SubjectsList subjectsList = new SubjectsList(cookies, "powiatjaroslawski"); + DatabaseAccount databaseAccount = new DatabaseAccount(mContext); + databaseAccount.open(); + AccountData accountData = databaseAccount.getAccount(1); + databaseAccount.close(); + StudentAndParent snp = new StudentAndParent(cookies, accountData.getCounty()).setUp(); + SubjectsList subjectsList = new SubjectsList(snp.getCookiesObject(), snp); List subjects = subjectsList.getAll(); for (Subject item : subjects) { subject.add(item.getName()); } - GradesList gradesList = new GradesList(cookies, "powiatjaroslawski"); + GradesList gradesList = new GradesList(snp.getCookiesObject(), snp); List grades = gradesList.getAll(); for (Grade item : grades) { Log.d("MarksFragment", item.getSubject() + ": " + item.getValue()); diff --git a/app/src/main/java/io/github/wulkanowy/activity/main/LoginTask.java b/app/src/main/java/io/github/wulkanowy/activity/main/LoginTask.java index 1b1e85b29..243b7b26a 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/main/LoginTask.java +++ b/app/src/main/java/io/github/wulkanowy/activity/main/LoginTask.java @@ -14,12 +14,14 @@ import java.io.ObjectOutputStream; import io.github.wulkanowy.R; import io.github.wulkanowy.activity.dashboard.DashboardActivity; import io.github.wulkanowy.api.Cookies; +import io.github.wulkanowy.api.StudentAndParent; import io.github.wulkanowy.api.login.AccountPermissionException; import io.github.wulkanowy.api.login.BadCredentialsException; import io.github.wulkanowy.api.login.Login; import io.github.wulkanowy.api.login.LoginErrorException; import io.github.wulkanowy.api.user.BasicInformation; import io.github.wulkanowy.api.user.PersonalData; +import io.github.wulkanowy.api.user.User; import io.github.wulkanowy.database.accounts.AccountData; import io.github.wulkanowy.database.accounts.DatabaseAccount; import io.github.wulkanowy.security.CryptoException; @@ -64,12 +66,11 @@ public class LoginTask extends AsyncTask { } catch (LoginErrorException e) { return R.string.login_denied; } - try { String cookiesPath = activity.getFilesDir().getPath() + "/cookies.txt"; FileOutputStream out = new FileOutputStream(cookiesPath); ObjectOutputStream outputStream = new ObjectOutputStream(out); - outputStream.writeObject(login.getJar()); + outputStream.writeObject(login.getCookies()); outputStream.flush(); } catch (IOException e) { return R.string.login_cookies_save_failed; @@ -77,8 +78,10 @@ public class LoginTask extends AsyncTask { if (save) { try { - BasicInformation userInfo = new BasicInformation(login.getCookies(), - credentials[2]); + StudentAndParent snp = new StudentAndParent(login.getCookiesObject(), + credentials[2]).setUp(); + User user = new User(snp.getCookiesObject(), snp); + BasicInformation userInfo = new BasicInformation(user, snp); PersonalData personalData = userInfo.getPersonalData(); String firstAndLastName = personalData.getFirstAndLastName(); @@ -117,7 +120,8 @@ public class LoginTask extends AsyncTask { Toast.makeText(activity, activity.getString(messageID), Toast.LENGTH_LONG).show(); - if (messageID == R.string.login_accepted || messageID == R.string.root_failed || messageID == R.string.encrypt_failed) { + if (messageID == R.string.login_accepted || messageID == R.string.root_failed + || messageID == R.string.encrypt_failed) { Intent intent = new Intent(activity, DashboardActivity.class); activity.startActivity(intent); } diff --git a/app/src/main/java/io/github/wulkanowy/api/Cookies.java b/app/src/main/java/io/github/wulkanowy/api/Cookies.java index 2617dffec..6801c61a1 100644 --- a/app/src/main/java/io/github/wulkanowy/api/Cookies.java +++ b/app/src/main/java/io/github/wulkanowy/api/Cookies.java @@ -6,15 +6,17 @@ public class Cookies { private Map cookies; - public void setItems(Map items) { - cookies = items; - } - - public void addItems(Map items) { - cookies.putAll(items); - } - - public Map getAll() { + public Map getItems() { return cookies; } + + public Cookies setItems(Map items) { + this.cookies = items; + return this; + } + + public Cookies addItems(Map items) { + this.cookies.putAll(items); + return this; + } } 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 d3fdc9278..c2dd0922f 100644 --- a/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java +++ b/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -20,20 +20,17 @@ public class StudentAndParent extends Vulcan { private String uonetPlusOpiekunUrl; public StudentAndParent(Cookies cookies, String locID) throws IOException { - super(cookies); - + this.cookies = cookies; this.locationID = locID; - - setUp(); } - private void setUp() throws IOException { + public StudentAndParent setUp() throws IOException { startPageUrl = startPageUrl.replace("{locationID}", locationID); // get link to uonetplus-opiekun.vulcan.net.pl module Document startPage = Jsoup.connect(startPageUrl) .followRedirects(true) - .cookies(getJar()) + .cookies(getCookies()) .get(); Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first(); uonetPlusOpiekunUrl = studentTileLink.attr("href"); @@ -41,13 +38,15 @@ public class StudentAndParent extends Vulcan { //get context module cookie Connection.Response res = Jsoup.connect(uonetPlusOpiekunUrl) .followRedirects(true) - .cookies(getJar()) + .cookies(getCookies()) .execute(); - addCookies(res.cookies()); + cookies.addItems(res.cookies()); + + return this; } - protected String getLocationID() { + public String getLocationID() { return locationID; } @@ -64,4 +63,8 @@ public class StudentAndParent extends Vulcan { return match; } + + public String getRowDataChildValue(Element e, int index) { + return e.select(".daneWiersz .wartosc").get(index - 1).text(); + } } diff --git a/app/src/main/java/io/github/wulkanowy/api/Vulcan.java b/app/src/main/java/io/github/wulkanowy/api/Vulcan.java index a62b389c1..e149d8e18 100644 --- a/app/src/main/java/io/github/wulkanowy/api/Vulcan.java +++ b/app/src/main/java/io/github/wulkanowy/api/Vulcan.java @@ -4,25 +4,23 @@ import java.util.Map; public abstract class Vulcan { - private Cookies cookies; + protected Cookies cookies; - public Vulcan(Cookies cookies) { - this.cookies = cookies; - } - - public Map getJar() { - return cookies.getAll(); - } - - public Cookies getCookies() { + public Cookies getCookiesObject() { return cookies; } - public void setCookies(Map items) { - cookies.setItems(items); + public Map getCookies() { + return cookies.getItems(); } - public void addCookies(Map items) { - cookies.addItems(items); + public Cookies setCookies(Map cookies) { + this.cookies.setItems(cookies); + return this.cookies; + } + + public Cookies addCookies(Map cookies) { + this.cookies.addItems(cookies); + return this.cookies; } } 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 0ace5af0e..a38b54547 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 @@ -13,25 +13,29 @@ import java.util.regex.Pattern; 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 GradesList extends StudentAndParent { +public class GradesList extends Vulcan { + + private StudentAndParent snp = null; private String gradesPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/Oceny/Wszystkie?details=2"; private List grades = new ArrayList<>(); - public GradesList(Cookies cookies, String locationID) throws IOException { - super(cookies, locationID); + public GradesList(Cookies cookies, StudentAndParent snp) { + this.cookies = cookies; + this.snp = snp; } public List getAll() throws IOException, LoginErrorException { - gradesPageUrl = gradesPageUrl.replace("{locationID}", getLocationID()); - gradesPageUrl = gradesPageUrl.replace("{ID}", getID()); + gradesPageUrl = gradesPageUrl.replace("{locationID}", snp.getLocationID()); + gradesPageUrl = gradesPageUrl.replace("{ID}", snp.getID()); Document marksPage = Jsoup.connect(gradesPageUrl) - .cookies(getJar()) + .cookies(getCookies()) .get(); Elements marksRows = marksPage.select(".ocenySzczegoly-table > tbody > tr"); 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 d24d66ec8..d39ff73f0 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 @@ -11,25 +11,29 @@ import java.util.List; 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 SubjectsList extends StudentAndParent { +public class SubjectsList extends Vulcan { + + private StudentAndParent snp = null; private String subjectsPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/Oceny/Wszystkie?details=1"; private List subjects = new ArrayList<>(); - public SubjectsList(Cookies cookies, String locationID) throws IOException { - super(cookies, locationID); + public SubjectsList(Cookies cookies, StudentAndParent snp) { + this.cookies = cookies; + this.snp = snp; } public List getAll() throws IOException, LoginErrorException { - subjectsPageUrl = subjectsPageUrl.replace("{locationID}", getLocationID()); - subjectsPageUrl = subjectsPageUrl.replace("{ID}", getID()); + subjectsPageUrl = subjectsPageUrl.replace("{locationID}", snp.getLocationID()); + subjectsPageUrl = subjectsPageUrl.replace("{ID}", snp.getID()); Document subjectPage = Jsoup.connect(subjectsPageUrl) - .cookies(getJar()) + .cookies(getCookies()) .get(); Elements rows = subjectPage.select(".ocenyZwykle-table > tbody > tr"); diff --git a/app/src/main/java/io/github/wulkanowy/api/login/Login.java b/app/src/main/java/io/github/wulkanowy/api/login/Login.java index bac67f764..a27928464 100644 --- a/app/src/main/java/io/github/wulkanowy/api/login/Login.java +++ b/app/src/main/java/io/github/wulkanowy/api/login/Login.java @@ -23,7 +23,7 @@ public class Login extends Vulcan { "https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx"; public Login(Cookies cookies) { - super(cookies); + this.cookies = cookies; } public boolean login(String email, String password, String county) @@ -61,7 +61,7 @@ public class Login extends Vulcan { certificatePageUrl = certificatePageUrl.replace("{locationID}", county); Document certificatePage = Jsoup.connect(certificatePageUrl) - .cookies(getJar()) + .cookies(getCookies()) .get(); return new String[]{ @@ -77,12 +77,12 @@ public class Login extends Vulcan { Connection.Response response = Jsoup.connect(loginEndpointPageUrl) .data("wa", protocolVersion) .data("wresult", certificate) - .cookies(getJar()) + .cookies(getCookies()) .followRedirects(true) .method(Connection.Method.POST) .execute(); - setCookies(response.cookies()); + addCookies(response.cookies()); Document html = response.parse(); if (html.getElementsByTag("title").text().equals("Logowanie")) { diff --git a/app/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java b/app/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java new file mode 100644 index 000000000..cff89f23a --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/notes/AchievementsList.java @@ -0,0 +1,32 @@ +package io.github.wulkanowy.api.notes; + +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.login.LoginErrorException; + +public class AchievementsList { + + private Notes notes = null; + + private List achievementsList = new ArrayList<>(); + + public AchievementsList(Notes notes) { + this.notes = notes; + } + + public List getAllAchievements() throws LoginErrorException, IOException { + Element pageFragment = notes.getNotesPageDocument().select(".mainContainer > div").get(1); + Elements items = pageFragment.select("article"); + + for (Element item : items) { + achievementsList.add(item.text()); + } + + return achievementsList; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/notes/Note.java b/app/src/main/java/io/github/wulkanowy/api/notes/Note.java new file mode 100644 index 000000000..b1fa68b93 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/notes/Note.java @@ -0,0 +1,48 @@ +package io.github.wulkanowy.api.notes; + +public class Note { + + private String date; + + private String teacher; + + private String category; + + private String content; + + public String getDate() { + return date; + } + + public Note setDate(String date) { + this.date = date; + return this; + } + + public String getTeacher() { + return teacher; + } + + public Note setTeacher(String teacher) { + this.teacher = teacher; + return this; + } + + public String getCategory() { + return category; + } + + public Note setCategory(String category) { + this.category = category; + return this; + } + + public String getContent() { + return content; + } + + public Note setContent(String content) { + this.content = content; + return this; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/notes/Notes.java b/app/src/main/java/io/github/wulkanowy/api/notes/Notes.java new file mode 100644 index 000000000..a05f011c9 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/notes/Notes.java @@ -0,0 +1,33 @@ +package io.github.wulkanowy.api.notes; + +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 Notes extends Vulcan { + + private StudentAndParent snp = null; + + private String notesPageUrl = + "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/UwagiOsiagniecia.mvc/Wszystkie"; + + public Notes(Cookies cookies, StudentAndParent snp) { + this.cookies = cookies; + this.snp = snp; + } + + public Document getNotesPageDocument() throws IOException, LoginErrorException { + notesPageUrl = notesPageUrl.replace("{locationID}", snp.getLocationID()); + notesPageUrl = notesPageUrl.replace("{ID}", snp.getID()); + + return Jsoup.connect(notesPageUrl) + .cookies(getCookies()) + .get(); + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/notes/NotesList.java b/app/src/main/java/io/github/wulkanowy/api/notes/NotesList.java new file mode 100644 index 000000000..05aa820ec --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/notes/NotesList.java @@ -0,0 +1,42 @@ +package io.github.wulkanowy.api.notes; + +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.StudentAndParent; +import io.github.wulkanowy.api.login.LoginErrorException; + +public class NotesList { + + private Notes notes = null; + private StudentAndParent snp = null; + + private List notesList = new ArrayList<>(); + + public NotesList(Notes notes, StudentAndParent snp) { + this.notes = notes; + this.snp = snp; + } + + public List getAllNotes() throws LoginErrorException, IOException { + Element pageFragment = notes.getNotesPageDocument().select(".mainContainer > div").get(0); + Elements items = pageFragment.select("article"); + Elements dates = pageFragment.select("h2"); + + int index = 0; + for (Element item : items) { + notesList.add(new Note() + .setDate(dates.get(index++).text()) + .setTeacher(snp.getRowDataChildValue(item, 1)) + .setCategory(snp.getRowDataChildValue(item, 2)) + .setContent(snp.getRowDataChildValue(item, 3)) + ); + } + + return notesList; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/user/BasicInformation.java b/app/src/main/java/io/github/wulkanowy/api/user/BasicInformation.java index c3d349b0f..a2a5affb2 100644 --- a/app/src/main/java/io/github/wulkanowy/api/user/BasicInformation.java +++ b/app/src/main/java/io/github/wulkanowy/api/user/BasicInformation.java @@ -1,62 +1,46 @@ package io.github.wulkanowy.api.user; -import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import java.io.IOException; -import io.github.wulkanowy.api.Cookies; import io.github.wulkanowy.api.StudentAndParent; import io.github.wulkanowy.api.login.LoginErrorException; -public class BasicInformation extends StudentAndParent { +public class BasicInformation { - private String studentDataPageUrl = - "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/Uczen.mvc/DanePodstawowe"; + private StudentAndParent snp = null; private Document studentDataPageDocument; - public BasicInformation(Cookies cookies, String locationID) + public BasicInformation(User user, StudentAndParent snp) throws IOException, LoginErrorException { - super(cookies, locationID); + this.snp = snp; - studentDataPageDocument = getPage(); - } - - private Document getPage() throws IOException, LoginErrorException { - studentDataPageUrl = studentDataPageUrl.replace("{locationID}", getLocationID()); - studentDataPageUrl = studentDataPageUrl.replace("{ID}", getID()); - - return Jsoup.connect(studentDataPageUrl) - .cookies(getJar()) - .get(); - } - - private String getRowDataChildValue(Element e, int index) { - return e.select(".daneWiersz .wartosc").get(index - 1).text(); + studentDataPageDocument = user.getPage(); } public PersonalData getPersonalData() { Element e = studentDataPageDocument.select(".mainContainer > article").get(0); return new PersonalData() - .setNames(getRowDataChildValue(e, 1)) - .setDateAndBirthPlace(getRowDataChildValue(e, 2)) - .setPesel(getRowDataChildValue(e, 3)) - .setGender(getRowDataChildValue(e, 4)) - .setPolishCitizenship(getRowDataChildValue(e, 5)) - .setFamilyName(getRowDataChildValue(e, 6)) - .setParentsNames(getRowDataChildValue(e, 7)); + .setNames(snp.getRowDataChildValue(e, 1)) + .setDateAndBirthPlace(snp.getRowDataChildValue(e, 2)) + .setPesel(snp.getRowDataChildValue(e, 3)) + .setGender(snp.getRowDataChildValue(e, 4)) + .setPolishCitizenship(snp.getRowDataChildValue(e, 5)) + .setFamilyName(snp.getRowDataChildValue(e, 6)) + .setParentsNames(snp.getRowDataChildValue(e, 7)); } public AddressData getAddresData() { Element e = studentDataPageDocument.select(".mainContainer > article").get(1); return new AddressData() - .setAddress(getRowDataChildValue(e, 1)) - .setRegisteredAddress(getRowDataChildValue(e, 2)) - .setCorrespondenceAddress(getRowDataChildValue(e, 3)); + .setAddress(snp.getRowDataChildValue(e, 1)) + .setRegisteredAddress(snp.getRowDataChildValue(e, 2)) + .setCorrespondenceAddress(snp.getRowDataChildValue(e, 3)); } @@ -64,8 +48,8 @@ public class BasicInformation extends StudentAndParent { Element e = studentDataPageDocument.select(".mainContainer > article").get(2); return new ContactDetails() - .setPhoneNumber(getRowDataChildValue(e, 1)) - .setCellPhoneNumber(getRowDataChildValue(e, 2)) - .setEmail(getRowDataChildValue(e, 3)); + .setPhoneNumber(snp.getRowDataChildValue(e, 1)) + .setCellPhoneNumber(snp.getRowDataChildValue(e, 2)) + .setEmail(snp.getRowDataChildValue(e, 3)); } } diff --git a/app/src/main/java/io/github/wulkanowy/api/user/User.java b/app/src/main/java/io/github/wulkanowy/api/user/User.java new file mode 100644 index 000000000..c75fb7da2 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/user/User.java @@ -0,0 +1,33 @@ +package io.github.wulkanowy.api.user; + +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 User extends Vulcan { + + private StudentAndParent snp = null; + + private String studentDataPageUrl = + "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/Uczen.mvc/DanePodstawowe"; + + public User(Cookies cookies, StudentAndParent snp) { + this.cookies = cookies; + this.snp = snp; + } + + public Document getPage() throws IOException, LoginErrorException { + studentDataPageUrl = studentDataPageUrl.replace("{locationID}", snp.getLocationID()); + studentDataPageUrl = studentDataPageUrl.replace("{ID}", snp.getID()); + + return Jsoup.connect(studentDataPageUrl) + .cookies(getCookies()) + .get(); + } +} diff --git a/app/src/test/java/io/github/wulkanowy/api/FixtureHelper.java b/app/src/test/java/io/github/wulkanowy/api/FixtureHelper.java new file mode 100644 index 000000000..c487fb320 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/api/FixtureHelper.java @@ -0,0 +1,14 @@ +package io.github.wulkanowy.api; + +import java.io.InputStream; +import java.util.Scanner; + +public class FixtureHelper { + + public static String getAsString(InputStream inputStream) { + Scanner s = new Scanner(inputStream).useDelimiter("\\A"); + String input = s.hasNext() ? s.next() : ""; + + return input; + } +} diff --git a/app/src/test/java/io/github/wulkanowy/api/notes/AchievementsListTest.java b/app/src/test/java/io/github/wulkanowy/api/notes/AchievementsListTest.java new file mode 100644 index 000000000..63775e209 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/api/notes/AchievementsListTest.java @@ -0,0 +1,55 @@ +package io.github.wulkanowy.api.notes; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; +import org.unitils.reflectionassert.ReflectionAssert; + +import java.util.ArrayList; +import java.util.List; + +import io.github.wulkanowy.api.FixtureHelper; + +public class AchievementsListTest { + + private String fixtureFilledFileName = "UwagiOsiagniecia-filled.html"; + + private String fixtureEmptyFileName = "UwagiOsiagniecia-empty.html"; + + private AchievementsList getSetUpAchievementsList(String fixtureFileName) throws Exception { + String input = FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)); + + Document notesPageDocument = Jsoup.parse(input); + + Notes notes = Mockito.mock(Notes.class); + Mockito.when(notes.getNotesPageDocument()).thenReturn(notesPageDocument); + + return new AchievementsList(notes); + } + + @Test + public void getAllAchievementsFilledTest() throws Exception { + List expectedList = new ArrayList<>(); + expectedList.add("I miejsce w ogólnopolskim konkursie ortograficznym"); + expectedList.add("III miejsce w ogólnopolskim konkursie plastycznym"); + + List actualList = getSetUpAchievementsList( + fixtureFilledFileName).getAllAchievements(); + + Assert.assertEquals(2, actualList.size()); + ReflectionAssert.assertReflectionEquals(expectedList, actualList); + } + + @Test + public void getAllAchievementsEmptyTest() throws Exception { + List expectedList = new ArrayList<>(); + + List actualList = getSetUpAchievementsList( + fixtureEmptyFileName).getAllAchievements(); + + Assert.assertEquals(0, actualList.size()); + ReflectionAssert.assertReflectionEquals(expectedList, actualList); + } +} diff --git a/app/src/test/java/io/github/wulkanowy/api/notes/NotesListTest.java b/app/src/test/java/io/github/wulkanowy/api/notes/NotesListTest.java new file mode 100644 index 000000000..8a1f79da1 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/api/notes/NotesListTest.java @@ -0,0 +1,74 @@ +package io.github.wulkanowy.api.notes; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; +import org.unitils.reflectionassert.ReflectionAssert; + +import java.util.ArrayList; +import java.util.List; + +import io.github.wulkanowy.api.FixtureHelper; +import io.github.wulkanowy.api.StudentAndParent; + +public class NotesListTest { + + private String fixtureFilledFileName = "UwagiOsiagniecia-filled.html"; + + private String fixtureEmptyFileName = "UwagiOsiagniecia-empty.html"; + + private NotesList getSetUpNotesList(String fixtureFileName) throws Exception { + String input = FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)); + + Document notesPageDocument = Jsoup.parse(input); + + Notes notes = Mockito.mock(Notes.class); + Mockito.when(notes.getNotesPageDocument()).thenReturn(notesPageDocument); + StudentAndParent snp = Mockito.mock(StudentAndParent.class); + Mockito.when(snp.getRowDataChildValue(Mockito.any(Element.class), + Mockito.anyInt())).thenCallRealMethod(); + + return new NotesList(notes, snp); + } + + @Test + public void getAllNotesFilledTest() throws Exception { + List expectedList = new ArrayList<>(); + expectedList.add(new Note() + .setDate("06.06.2017") + .setTeacher("Jan Kowalski [JK]") + .setCategory("Zaangażowanie społeczne") + .setContent("Pomoc przy pikniku charytatywnym") + ); + expectedList.add(new Note() + .setDate("01.12.2016") + .setTeacher("Ochocka Zofia [PZ]") + .setCategory("Reprezentowanie szkoły") + .setContent("Udział w przygotowaniu spektaklu") + ); + expectedList.add(new Note() + .setDate("01.10.2016") + .setTeacher("Kochański Leszek [KL]") + .setCategory("Zachowanie na lekcji") + .setContent("Przeszkadzanie w prowadzeniu lekcji") + ); + + List actualList = getSetUpNotesList(fixtureFilledFileName).getAllNotes(); + + Assert.assertEquals(3, actualList.size()); + ReflectionAssert.assertReflectionEquals(expectedList, actualList); + } + + @Test + public void getAllNotesWhenEmpty() throws Exception { + List actualList = getSetUpNotesList(fixtureEmptyFileName).getAllNotes(); + + List expectedList = new ArrayList<>(); + + Assert.assertEquals(0, actualList.size()); + ReflectionAssert.assertReflectionEquals(expectedList, actualList); + } +} diff --git a/app/src/test/resources/io/github/wulkanowy/api/notes/UwagiOsiagniecia-empty.html b/app/src/test/resources/io/github/wulkanowy/api/notes/UwagiOsiagniecia-empty.html new file mode 100644 index 000000000..b15bb6e70 --- /dev/null +++ b/app/src/test/resources/io/github/wulkanowy/api/notes/UwagiOsiagniecia-empty.html @@ -0,0 +1,20 @@ + + + + + Witryna ucznia i rodzica – Uwagi i osiągnięcia + + +
+
+

Uwagi

+

Brak informacji do wyświetlenia

+
+
+

Osiągnięcia

+

Brak informacji do wyświetlenia

+
+
+
wersja: 17.05.0000.24042
+ + diff --git a/app/src/test/resources/io/github/wulkanowy/api/notes/UwagiOsiagniecia-filled.html b/app/src/test/resources/io/github/wulkanowy/api/notes/UwagiOsiagniecia-filled.html new file mode 100644 index 000000000..50e740585 --- /dev/null +++ b/app/src/test/resources/io/github/wulkanowy/api/notes/UwagiOsiagniecia-filled.html @@ -0,0 +1,65 @@ + + + + + Witryna ucznia i rodzica – Uwagi i osiągnięcia + + +
+
+

Uwagi

+

06.06.2017

+
+
+
Nauczyciel:
+
Jan Kowalski [JK]
+
+
+
Kategoria:
+
Zaangażowanie społeczne
+
+
+
Treść:
+
Pomoc przy pikniku charytatywnym
+
+
+

01.12.2016

+
+
+
Nauczyciel:
+
Ochocka Zofia [PZ]
+
+
+
Kategoria:
+
Reprezentowanie szkoły
+
+
+
Treść:
+
Udział w przygotowaniu spektaklu
+
+
+

01.10.2016

+
+
+
Nauczyciel:
+
Kochański Leszek [KL]
+
+
+
Kategoria:
+
Zachowanie na lekcji
+
+
+
Treść:
+
Przeszkadzanie w prowadzeniu lekcji
+
+
+
+
+

Osiągnięcia

+
I miejsce w ogólnopolskim konkursie ortograficznym
+
III miejsce w ogólnopolskim konkursie plastycznym
+
+
+ + +