Optimize session handling (#63)

* [APP] Change way the Vulcan is configured (#65)
This commit is contained in:
Mikołaj Pich
2018-03-11 19:16:20 +01:00
committed by Rafał Borcz
parent a0313827ce
commit 3aca34340d
57 changed files with 521 additions and 454 deletions

View File

@ -5,44 +5,102 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import io.github.wulkanowy.api.login.Login;
public class Client {
private String protocol;
private String protocol = "https";
private String host;
private String host = "vulcan.net.pl";
private String symbol;
private String email;
private String password;
private String symbol = "Default";
private Login login;
private Date lastSuccessRequest = new Date();
private Cookies cookies = new Cookies();
Client(String protocol, String host, String symbol) {
this.protocol = protocol;
this.host = host;
Client(String email, String password, String symbol) {
this.email = email;
this.password = password;
this.symbol = symbol;
setFullEndpointInfo(email);
}
String getHost() {
return host;
private void setFullEndpointInfo(String info) {
String[] creds = info.split("\\\\");
email = info;
if (creds.length > 2) {
String[] url = creds[0].split("://");
protocol = url[0];
host = url[1];
email = creds[2];
}
}
private void login() throws IOException, VulcanException {
if (isLoggedIn()) {
return;
}
this.symbol = getLogin().login(email, password, symbol);
}
private boolean isLoggedIn() {
return getCookies().size() > 0 &&
29 > TimeUnit.MILLISECONDS.toMinutes(new Date().getTime() - lastSuccessRequest.getTime());
}
Login getLogin() {
if (null != login) {
return login;
}
login = new Login(this);
return login;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
Map<String, String> getCookies() {
private Map<String, String> getCookies() {
return cookies.getItems();
}
private String getFilledUrl(String url) {
String getHost() {
return host;
}
String getFilledUrl(String url) {
return url
.replace("{schema}", protocol)
.replace("{host}", host.replace(":", "%253A"))
.replace("{symbol}", symbol == null ? "Default" : symbol);
.replace("{symbol}", symbol);
}
Document getPageByUrl(String url) throws IOException {
Document getPageByUrl(String url) throws IOException, VulcanException {
login();
Connection.Response response = Jsoup.connect(getFilledUrl(url))
.followRedirects(true)
.cookies(getCookies())
@ -50,10 +108,10 @@ public class Client {
this.cookies.addItems(response.cookies());
return response.parse();
return checkForErrors(response.parse());
}
public Document postPageByUrl(String url, String[][] params) throws IOException {
public Document postPageByUrl(String url, String[][] params) throws IOException, VulcanException {
Connection connection = Jsoup.connect(getFilledUrl(url));
for (String[] data : params) {
@ -68,10 +126,12 @@ public class Client {
this.cookies.addItems(response.cookies());
return response.parse();
return checkForErrors(response.parse());
}
public String getJsonStringByUrl(String url) throws IOException {
public String getJsonStringByUrl(String url) throws IOException, VulcanException {
login();
Connection.Response response = Jsoup.connect(getFilledUrl(url))
.followRedirects(true)
.ignoreContentType(true)
@ -83,7 +143,9 @@ public class Client {
return response.body();
}
public String postJsonStringByUrl(String url, String[][] params) throws IOException {
public String postJsonStringByUrl(String url, String[][] params) throws IOException, VulcanException {
login();
Connection connection = Jsoup.connect(getFilledUrl(url));
for (String[] data : params) {
@ -101,4 +163,18 @@ public class Client {
return response.body();
}
Document checkForErrors(Document doc) throws VulcanException {
if ("Przerwa techniczna".equals(doc.select("title").text())) {
throw new VulcanOfflineException();
}
if ("Zaloguj się".equals(doc.select(".loginButton").text())) {
throw new NotLoggedInErrorException();
}
lastSuccessRequest = new Date();
return doc;
}
}

View File

@ -3,21 +3,15 @@ package io.github.wulkanowy.api;
import java.util.HashMap;
import java.util.Map;
public class Cookies {
class Cookies {
private Map<String, String> jar = new HashMap<>();
public Map<String, String> getItems() {
Map<String, String> getItems() {
return jar;
}
public Cookies setItems(Map<String, String> items) {
this.jar = items;
return this;
}
public Cookies addItems(Map<String, String> items) {
this.jar.putAll(items);
return this;
void addItems(Map<String, String> items) {
jar.putAll(items);
}
}

View File

@ -0,0 +1,4 @@
package io.github.wulkanowy.api;
public class NotLoggedInErrorException extends VulcanException {
}

View File

@ -6,19 +6,17 @@ import org.jsoup.nodes.Element;
import java.io.IOException;
import java.util.List;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
public interface SnP {
String getId();
void storeContextCookies() throws IOException, NotLoggedInErrorException;
StudentAndParent storeContextCookies() throws IOException, VulcanException;
String getRowDataChildValue(Element e, int index);
Document getSnPPageDocument(String url) throws IOException;
Document getSnPPageDocument(String url) throws IOException, VulcanException;
List<Semester> getSemesters() throws IOException;
List<Semester> getSemesters() throws IOException, VulcanException;
List<Semester> getSemesters(Document gradesPage);

View File

@ -8,8 +8,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
public class StudentAndParent implements SnP {
private static final String START_PAGE_URL = "{schema}://uonetplus.{host}/{symbol}/Start.mvc/Index";
@ -22,12 +20,8 @@ public class StudentAndParent implements SnP {
private String id;
StudentAndParent(Client client) {
this.client = client;
}
StudentAndParent(Client client, String id) {
this(client);
this.client = client;
this.id = id;
}
@ -39,11 +33,12 @@ public class StudentAndParent implements SnP {
return id;
}
public void storeContextCookies() throws IOException, NotLoggedInErrorException {
public StudentAndParent storeContextCookies() throws IOException, VulcanException {
client.getPageByUrl(getSnpHomePageUrl());
return this;
}
String getSnpHomePageUrl() throws IOException, NotLoggedInErrorException {
String getSnpHomePageUrl() throws IOException, VulcanException {
if (null != getId()) {
return getBaseUrl();
}
@ -77,11 +72,11 @@ public class StudentAndParent implements SnP {
return e.select(".daneWiersz .wartosc").get(index - 1).text();
}
public Document getSnPPageDocument(String url) throws IOException {
public Document getSnPPageDocument(String url) throws IOException, VulcanException {
return client.getPageByUrl(getBaseUrl() + url);
}
public List<Semester> getSemesters() throws IOException {
public List<Semester> getSemesters() throws IOException, VulcanException {
return getSemesters(getSnPPageDocument(GRADES_PAGE_URL));
}

View File

@ -7,12 +7,6 @@ import io.github.wulkanowy.api.attendance.AttendanceTable;
import io.github.wulkanowy.api.exams.ExamsWeek;
import io.github.wulkanowy.api.grades.GradesList;
import io.github.wulkanowy.api.grades.SubjectsList;
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.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.login.VulcanOfflineException;
import io.github.wulkanowy.api.messages.Messages;
import io.github.wulkanowy.api.notes.AchievementsList;
import io.github.wulkanowy.api.notes.NotesList;
@ -26,171 +20,92 @@ public class Vulcan {
private String id;
private String symbol;
private SnP snp;
private String protocolSchema = "https";
private String logHost = "vulcan.net.pl";
private String email;
private Client client;
private Login login;
public void setClient(Client client) {
this.client = client;
}
public void setLogin(Login login) {
this.login = login;
}
public void login(String email, String password, String symbol)
throws BadCredentialsException, AccountPermissionException,
LoginErrorException, IOException, VulcanOfflineException {
setFullEndpointInfo(email);
login = getLogin();
this.symbol = login.login(this.email, password, symbol);
}
public Vulcan login(String email, String password, String symbol, String id)
throws BadCredentialsException, AccountPermissionException,
LoginErrorException, IOException, VulcanOfflineException {
login(email, password, symbol);
public void setCredentials(String email, String password, String symbol, String id) {
client = new Client(email, password, symbol);
this.id = id;
return this;
}
String getProtocolSchema() {
return protocolSchema;
}
String getLogHost() {
return logHost;
}
public String getEmail() {
return email;
}
public String getSymbol() {
return symbol;
}
private void setFullEndpointInfo(String email) {
String[] creds = email.split("\\\\");
this.email = email;
if (creds.length >= 2) {
String[] url = creds[0].split("://");
this.protocolSchema = url[0];
this.logHost = url[1];
this.email = creds[2];
public Client getClient() throws NotLoggedInErrorException {
if (null == client) {
throw new NotLoggedInErrorException();
}
}
protected Client getClient() {
if (null != client) {
return client;
}
client = new Client(getProtocolSchema(), getLogHost(), symbol);
return client;
}
protected Login getLogin() {
if (null != login) {
return login;
}
public String getSymbol() throws NotLoggedInErrorException {
return getClient().getSymbol();
login = new Login(getClient());
return login;
}
public SnP getStudentAndParent() throws IOException, NotLoggedInErrorException {
if (0 == getClient().getCookies().size()) {
throw new NotLoggedInErrorException();
public SnP getStudentAndParent() throws IOException, VulcanException {
if (null != this.snp) {
return this.snp;
}
if (null != snp) {
return snp;
}
this.snp = new StudentAndParent(getClient(), id).storeContextCookies();
snp = createSnp(getClient(), id);
snp.storeContextCookies();
return snp;
return this.snp;
}
SnP createSnp(Client client, String id) {
if (null == id) {
return new StudentAndParent(client);
}
return new StudentAndParent(client, id);
public String getId() throws IOException, VulcanException {
return getStudentAndParent().getId();
}
public AttendanceStatistics getAttendanceStatistics() throws IOException, NotLoggedInErrorException {
return new AttendanceStatistics(getStudentAndParent());
}
public AttendanceTable getAttendanceTable() throws IOException, NotLoggedInErrorException {
public AttendanceTable getAttendanceTable() throws IOException, VulcanException {
return new AttendanceTable(getStudentAndParent());
}
public ExamsWeek getExamsList() throws IOException, NotLoggedInErrorException {
public AttendanceStatistics getAttendanceStatistics() throws IOException, VulcanException {
return new AttendanceStatistics(getStudentAndParent());
}
public ExamsWeek getExamsList() throws IOException, VulcanException {
return new ExamsWeek(getStudentAndParent());
}
public GradesList getGradesList() throws IOException, NotLoggedInErrorException {
public GradesList getGradesList() throws IOException, VulcanException {
return new GradesList(getStudentAndParent());
}
public SubjectsList getSubjectsList() throws IOException, NotLoggedInErrorException {
public SubjectsList getSubjectsList() throws IOException, VulcanException {
return new SubjectsList(getStudentAndParent());
}
public AchievementsList getAchievementsList() throws IOException, NotLoggedInErrorException {
public AchievementsList getAchievementsList() throws IOException, VulcanException {
return new AchievementsList(getStudentAndParent());
}
public NotesList getNotesList() throws IOException, NotLoggedInErrorException {
public NotesList getNotesList() throws IOException, VulcanException {
return new NotesList(getStudentAndParent());
}
public SchoolInfo getSchoolInfo() throws IOException, NotLoggedInErrorException {
public SchoolInfo getSchoolInfo() throws IOException, VulcanException {
return new SchoolInfo(getStudentAndParent());
}
public TeachersInfo getTeachersInfo() throws IOException, NotLoggedInErrorException {
public TeachersInfo getTeachersInfo() throws IOException, VulcanException {
return new TeachersInfo(getStudentAndParent());
}
public Timetable getTimetable() throws IOException, NotLoggedInErrorException {
public Timetable getTimetable() throws IOException, VulcanException {
return new Timetable(getStudentAndParent());
}
public BasicInformation getBasicInformation() throws IOException, NotLoggedInErrorException {
public BasicInformation getBasicInformation() throws IOException, VulcanException {
return new BasicInformation(getStudentAndParent());
}
public FamilyInformation getFamilyInformation() throws IOException, NotLoggedInErrorException {
public FamilyInformation getFamilyInformation() throws IOException, VulcanException {
return new FamilyInformation(getStudentAndParent());
}
public Messages getMessages() {
public Messages getMessages() throws VulcanException {
return new Messages(getClient());
}
}

View File

@ -0,0 +1,4 @@
package io.github.wulkanowy.api;
public abstract class VulcanException extends Exception {
}

View File

@ -0,0 +1,4 @@
package io.github.wulkanowy.api;
public class VulcanOfflineException extends VulcanException {
}

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.api.generic.Month;
import io.github.wulkanowy.api.generic.Subject;
@ -22,15 +23,15 @@ public class AttendanceStatistics {
this.snp = snp;
}
public Types getTypesTable() throws IOException {
public Types getTypesTable() throws IOException, VulcanException {
return getTypesTable("");
}
public Types getTypesTable(String tick) throws IOException {
public Types getTypesTable(String tick) throws IOException, VulcanException {
return getTypesTable(tick, -1);
}
public List<Subject> getSubjectList() throws IOException {
public List<Subject> getSubjectList() throws IOException, VulcanException {
Element mainContainer = snp.getSnPPageDocument(attendancePageUrl)
.select(".mainContainer #idPrzedmiot").first();
@ -46,7 +47,7 @@ public class AttendanceStatistics {
return subjectList;
}
public Types getTypesTable(String tick, Integer subjectId) throws IOException {
public Types getTypesTable(String tick, Integer subjectId) throws IOException, VulcanException {
Element mainContainer = snp.getSnPPageDocument((attendancePageUrl
+ "?data={tick}&idPrzedmiot={subject}")
.replace("{tick}", tick)

View File

@ -12,26 +12,28 @@ import java.util.List;
import java.util.Locale;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.api.generic.Day;
import io.github.wulkanowy.api.generic.Lesson;
import io.github.wulkanowy.api.generic.Week;
public class AttendanceTable {
private SnP snp;
private final static String ATTENDANCE_PAGE_URL = "Frekwencja.mvc?data=";
private SnP snp;
public AttendanceTable(SnP snp) {
this.snp = snp;
}
public Week<Day> getWeekTable() throws IOException, ParseException {
public Week<Day> getWeekTable() throws IOException, ParseException, VulcanException {
return getWeekTable("");
}
public Week<Day> getWeekTable(String tick) throws IOException, ParseException {
public Week<Day> getWeekTable(String tick) throws IOException, ParseException, VulcanException {
Element table = snp.getSnPPageDocument(ATTENDANCE_PAGE_URL + tick)
.select(".mainContainer .presentData").first();
Elements headerCells = table.select("thead th");

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.api.generic.Week;
public class ExamsWeek {
@ -21,11 +22,11 @@ public class ExamsWeek {
this.snp = snp;
}
public Week<ExamDay> getCurrent() throws IOException {
public Week<ExamDay> getCurrent() throws IOException, VulcanException {
return getWeek("", true);
}
public Week<ExamDay> getWeek(String tick, final boolean onlyNotEmpty) throws IOException {
public Week<ExamDay> getWeek(String tick, final boolean onlyNotEmpty) throws IOException, VulcanException {
Document examsPage = snp.getSnPPageDocument(EXAMS_PAGE_URL + tick);
Elements examsDays = examsPage.select(".mainContainer > div:not(.navigation)");

View File

@ -16,6 +16,7 @@ import java.util.regex.Pattern;
import io.github.wulkanowy.api.Semester;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
public class GradesList {
@ -33,11 +34,11 @@ public class GradesList {
return GRADES_PAGE_URL;
}
public List<Grade> getAll() throws IOException, ParseException {
public List<Grade> getAll() throws IOException, ParseException, VulcanException {
return getAll("");
}
public List<Grade> getAll(String semester) throws IOException, ParseException {
public List<Grade> getAll(String semester) throws IOException, ParseException, VulcanException {
Document gradesPage = snp.getSnPPageDocument(getGradesPageUrl() + semester);
Elements gradesRows = gradesPage.select(".ocenySzczegoly-table > tbody > tr");
Semester currentSemester = snp.getCurrentSemester(snp.getSemesters(gradesPage));

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
public class SubjectsList {
@ -20,7 +21,7 @@ public class SubjectsList {
this.snp = snp;
}
public List<Subject> getAll() throws IOException {
public List<Subject> getAll() throws IOException, VulcanException {
Document subjectPage = snp.getSnPPageDocument(SUBJECTS_PAGE_URL);
Elements rows = subjectPage.select(".ocenyZwykle-table > tbody > tr");

View File

@ -1,4 +1,6 @@
package io.github.wulkanowy.api.login;
public class AccountPermissionException extends Exception {
import io.github.wulkanowy.api.VulcanException;
public class AccountPermissionException extends VulcanException {
}

View File

@ -1,4 +1,6 @@
package io.github.wulkanowy.api.login;
public class BadCredentialsException extends Exception {
import io.github.wulkanowy.api.VulcanException;
public class BadCredentialsException extends VulcanException {
}

View File

@ -8,6 +8,7 @@ import org.jsoup.select.Elements;
import java.io.IOException;
import io.github.wulkanowy.api.Client;
import io.github.wulkanowy.api.VulcanException;
public class Login {
@ -27,16 +28,13 @@ public class Login {
this.client = client;
}
public String login(String email, String password, String symbol)
throws BadCredentialsException, LoginErrorException,
AccountPermissionException, IOException, VulcanOfflineException {
public String login(String email, String password, String symbol) throws VulcanException, IOException {
String certificate = sendCredentials(email, password, symbol);
return sendCertificate(certificate, symbol);
}
String sendCredentials(String email, String password, String symbol)
throws IOException, BadCredentialsException {
String sendCredentials(String email, String password, String symbol) throws IOException, VulcanException {
this.symbol = symbol;
Document html = client.postPageByUrl(LOGIN_PAGE_URL, new String[][]{
@ -51,25 +49,20 @@ public class Login {
return html.select("input[name=wresult]").attr("value");
}
String sendCertificate(String certificate, String defaultSymbol)
throws IOException, LoginErrorException, AccountPermissionException, VulcanOfflineException {
String sendCertificate(String certificate, String defaultSymbol) throws IOException, VulcanException {
this.symbol = findSymbol(defaultSymbol, certificate);
client.setSymbol(this.symbol);
Document html = client.postPageByUrl(LOGIN_ENDPOINT_PAGE_URL, new String[][]{
String title = client.postPageByUrl(LOGIN_ENDPOINT_PAGE_URL, new String[][]{
{"wa", "wsignin1.0"},
{"wresult", certificate}
});
}).select("title").text();
if (html.getElementsByTag("title").text().equals("Logowanie")) {
if ("Logowanie".equals(title)) {
throw new AccountPermissionException();
}
if (html.getElementsByTag("title").text().equals("Przerwa techniczna")) {
throw new VulcanOfflineException();
}
if (!html.select("title").text().equals("Uonet+")) {
if (!"Uonet+".equals(title)) {
throw new LoginErrorException();
}
@ -85,7 +78,8 @@ public class Login {
}
String findSymbolInCertificate(String certificate) {
Elements els = Jsoup.parse(certificate.replaceAll(":", ""), "", Parser.xmlParser())
Elements els = Jsoup
.parse(certificate.replaceAll(":", ""), "", Parser.xmlParser())
.select("[AttributeName=\"UserInstance\"] samlAttributeValue");
if (els.isEmpty()) {
@ -94,4 +88,4 @@ public class Login {
return els.get(1).text();
}
}
}

View File

@ -1,4 +1,6 @@
package io.github.wulkanowy.api.login;
import io.github.wulkanowy.api.NotLoggedInErrorException;
public class LoginErrorException extends NotLoggedInErrorException {
}

View File

@ -1,4 +0,0 @@
package io.github.wulkanowy.api.login;
public class NotLoggedInErrorException extends Exception {
}

View File

@ -1,4 +0,0 @@
package io.github.wulkanowy.api.login;
public class VulcanOfflineException extends Exception {
}

View File

@ -1,4 +1,6 @@
package io.github.wulkanowy.api.messages;
class BadRequestException extends Exception {
import io.github.wulkanowy.api.VulcanException;
class BadRequestException extends VulcanException {
}

View File

@ -7,7 +7,8 @@ import java.io.IOException;
import java.util.List;
import io.github.wulkanowy.api.Client;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.NotLoggedInErrorException;
import io.github.wulkanowy.api.VulcanException;
public class Messages {
@ -37,19 +38,19 @@ public class Messages {
this.client = client;
}
public List<Message> getReceived() throws IOException, NotLoggedInErrorException, BadRequestException {
public List<Message> getReceived() throws IOException, VulcanException {
return getMessages(RECEIVED_URL);
}
public List<Message> getSent() throws IOException, NotLoggedInErrorException, BadRequestException {
public List<Message> getSent() throws IOException, VulcanException {
return getMessages(SENT_URL);
}
public List<Message> getDeleted() throws IOException, NotLoggedInErrorException, BadRequestException {
public List<Message> getDeleted() throws IOException, VulcanException {
return getMessages(DELETED_URL);
}
private List<Message> getMessages(String url) throws IOException, NotLoggedInErrorException, BadRequestException {
private List<Message> getMessages(String url) throws IOException, VulcanException {
String res = client.getJsonStringByUrl(url);
List<Message> messages;
@ -67,7 +68,7 @@ public class Messages {
return messages;
}
public Message getMessage(int id, int folder) throws IOException, BadRequestException, NotLoggedInErrorException {
public Message getMessage(int id, int folder) throws IOException, VulcanException {
String res = client.postJsonStringByUrl(MESSAGE_URL, new String[][]{
{"idWiadomosc", String.valueOf(id)},
{"Folder", String.valueOf(folder)}

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
public class AchievementsList {
@ -21,7 +22,7 @@ public class AchievementsList {
this.snp = snp;
}
public List<String> getAllAchievements() throws IOException {
public List<String> getAllAchievements() throws IOException, VulcanException {
Element pageFragment = snp.getSnPPageDocument(NOTES_PAGE_URL)
.select(".mainContainer > div").get(1);
Elements items = pageFragment.select("article");

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
public class NotesList {
@ -21,7 +22,7 @@ public class NotesList {
this.snp = snp;
}
public List<Note> getAllNotes() throws IOException {
public List<Note> getAllNotes() throws IOException, VulcanException {
Element pageFragment = snp.getSnPPageDocument(NOTES_PAGE_URL)
.select(".mainContainer > div").get(0);
Elements items = pageFragment.select("article");

View File

@ -5,6 +5,7 @@ import org.jsoup.nodes.Element;
import java.io.IOException;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
public class SchoolInfo {
@ -16,7 +17,7 @@ public class SchoolInfo {
this.snp = snp;
}
public SchoolData getSchoolData() throws IOException {
public SchoolData getSchoolData() throws IOException, VulcanException {
Element e = snp.getSnPPageDocument(SCHOOL_PAGE_URL)
.select(".mainContainer > article").get(0);

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
public class TeachersInfo {
@ -20,7 +21,7 @@ public class TeachersInfo {
this.snp = snp;
}
public TeachersData getTeachersData() throws IOException {
public TeachersData getTeachersData() throws IOException, VulcanException {
Document doc = snp.getSnPPageDocument(SCHOOL_PAGE_URL);
Elements rows = doc.select(".mainContainer > table tbody tr");
String description = doc.select(".mainContainer > p").first().text();

View File

@ -13,6 +13,7 @@ import java.util.List;
import java.util.Locale;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.api.generic.Day;
import io.github.wulkanowy.api.generic.Lesson;
import io.github.wulkanowy.api.generic.Week;
@ -27,11 +28,11 @@ public class Timetable {
this.snp = snp;
}
public Week<Day> getWeekTable() throws IOException, ParseException {
public Week<Day> getWeekTable() throws IOException, ParseException, VulcanException {
return getWeekTable("");
}
public Week<Day> getWeekTable(final String tick) throws IOException, ParseException {
public Week<Day> getWeekTable(final String tick) throws IOException, ParseException, VulcanException {
Element table = snp.getSnPPageDocument(TIMETABLE_PAGE_URL + tick)
.select(".mainContainer .presentData").first();

View File

@ -6,6 +6,7 @@ import org.jsoup.nodes.Element;
import java.io.IOException;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
public class BasicInformation {
@ -21,7 +22,7 @@ public class BasicInformation {
this.snp = snp;
}
public Document getStudentDataPageDocument() throws IOException {
public Document getStudentDataPageDocument() throws IOException, VulcanException {
if (null == studentDataPageDocument) {
studentDataPageDocument = snp.getSnPPageDocument(STUDENT_DATA_PAGE_URL);
}
@ -29,7 +30,7 @@ public class BasicInformation {
return studentDataPageDocument;
}
public PersonalData getPersonalData() throws IOException {
public PersonalData getPersonalData() throws IOException, VulcanException {
Element e = getStudentDataPageDocument().select(CONTENT_QUERY).get(0);
String name = snp.getRowDataChildValue(e, 1);
@ -48,7 +49,7 @@ public class BasicInformation {
.setParentsNames(snp.getRowDataChildValue(e, 7));
}
public AddressData getAddressData() throws IOException {
public AddressData getAddressData() throws IOException, VulcanException {
Element e = getStudentDataPageDocument().select(CONTENT_QUERY).get(1);
return new AddressData()
@ -58,7 +59,7 @@ public class BasicInformation {
}
public ContactDetails getContactDetails() throws IOException {
public ContactDetails getContactDetails() throws IOException, VulcanException {
Element e = getStudentDataPageDocument().select(CONTENT_QUERY).get(2);
return new ContactDetails()

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.SnP;
import io.github.wulkanowy.api.VulcanException;
public class FamilyInformation {
@ -19,7 +20,7 @@ public class FamilyInformation {
this.snp = snp;
}
public List<FamilyMember> getFamilyMembers() throws IOException {
public List<FamilyMember> getFamilyMembers() throws IOException, VulcanException {
Elements membersElements = snp.getSnPPageDocument(STUDENT_DATA_PAGE_URL)
.select(".mainContainer > article:nth-of-type(n+4)");

View File

@ -0,0 +1,80 @@
package io.github.wulkanowy.api;
import org.hamcrest.CoreMatchers;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Assert;
import org.junit.Test;
import io.github.wulkanowy.api.login.Login;
public class ClientTest {
private String getFixtureAsString(String fixtureFileName) {
return FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName));
}
@Test
public void setFullEndpointInfoTest() throws Exception {
Client client = new Client("http://fakelog.net\\\\admin", "pass", "Default");
Assert.assertEquals("fakelog.net", client.getHost());
Assert.assertEquals("Default", client.getSymbol());
}
@Test
public void checkForNoErrorsTest() throws Exception {
Client client = new Client("", "", "");
Document doc = Jsoup.parse(getFixtureAsString("login/Logowanie-success.html"));
Assert.assertEquals(doc, client.checkForErrors(doc));
}
@Test(expected = VulcanOfflineException.class)
public void checkForErrorsOffline() throws Exception {
Client client = new Client("", "", "");
Document doc = Jsoup.parse(getFixtureAsString("login/PrzerwaTechniczna.html"));
client.checkForErrors(doc);
}
@Test(expected = NotLoggedInErrorException.class)
public void checkForErrors() throws Exception {
Client client = new Client("", "", "");
Document doc = Jsoup.parse(getFixtureAsString("login/Logowanie-notLoggedIn.html"));
client.checkForErrors(doc);
}
@Test
public void getClientTest() throws Exception {
Client client = new Client("", "", "");
Assert.assertThat(client.getLogin(), CoreMatchers.instanceOf(Login.class));
}
@Test
public void getClientTwiceTest() throws Exception {
Client client = new Client("", "", "");
Assert.assertEquals(client.getLogin(), client.getLogin());
}
@Test
public void getFilledUrlTest() throws Exception {
Client client = new Client("http://fakelog.cf\\\\admin", "", "symbol123");
Assert.assertEquals("http://uonetplus.fakelog.cf/symbol123/LoginEndpoint.aspx",
client.getFilledUrl("{schema}://uonetplus.{host}/{symbol}/LoginEndpoint.aspx"));
}
@Test
public void getSymbolTest() throws Exception {
Client client = new Client("", "", "symbol4321");
Assert.assertEquals("symbol4321", client.getSymbol());
}
}

View File

@ -10,8 +10,6 @@ import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
public class StudentAndParentTest {
private Client client;
@ -45,7 +43,7 @@ public class StudentAndParentTest {
Mockito.when(client.getHost()).thenReturn("vulcan.net.pl");
Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(startPageDocument);
StudentAndParent snp = new StudentAndParent(client);
StudentAndParent snp = new StudentAndParent(client, null);
Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/534213/Start/Index/",
snp.getSnpHomePageUrl());
@ -58,7 +56,7 @@ public class StudentAndParentTest {
);
Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(wrongPageDocument);
StudentAndParent snp = new StudentAndParent(client);
StudentAndParent snp = new StudentAndParent(client, null);
snp.getSnpHomePageUrl();
}

View File

@ -1,86 +1,31 @@
package io.github.wulkanowy.api;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.HashMap;
import java.util.Map;
import io.github.wulkanowy.api.login.Login;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
public class VulcanTest {
private Vulcan vulcan;
@Test(expected = NotLoggedInErrorException.class)
public void getClientWithoutLoginTest() throws Exception {
Vulcan vulcan = new Vulcan();
@Before
public void setUp() throws Exception {
vulcan = new Vulcan();
vulcan.setClient(Mockito.mock(Client.class));
vulcan.setLogin(Mockito.mock(Login.class));
vulcan.getClient();
}
@Test
public void setFullEndpointInfoTest() throws Exception {
vulcan.login("http://fakelog.net\\\\admin", "pass", "Default", "123");
public void getClientTest() throws Exception {
Vulcan vulcan = new Vulcan();
vulcan.setCredentials("email", "password", "symbol", null);
Assert.assertEquals("http", vulcan.getProtocolSchema());
Assert.assertEquals("fakelog.net", vulcan.getLogHost());
Assert.assertEquals("admin", vulcan.getEmail());
Assert.assertThat(vulcan.getClient(), CoreMatchers.instanceOf(Client.class));
}
@Test
public void getClientTwiceTest() throws Exception {
Vulcan vulcan = new Vulcan();
Assert.assertTrue(vulcan.getClient().equals(vulcan.getClient()));
}
vulcan.setCredentials("email", "password", "symbol", null);
@Test
public void getLoginTwiceTest() throws Exception {
Vulcan vulcan = new Vulcan();
Assert.assertTrue(vulcan.getLogin().equals(vulcan.getLogin()));
}
@Test(expected = NotLoggedInErrorException.class)
public void getStudentAndParentNotLoggedInTest() throws Exception {
vulcan.getStudentAndParent();
}
@Test
public void getStudentAndParentTwiceTest() throws Exception {
Client client = Mockito.mock(Client.class);
Map<String, String> cookies = new HashMap<>();
cookies.put("test", "test");
Mockito.when(client.getCookies()).thenReturn(cookies);
SnP snp = Mockito.mock(StudentAndParent.class);
Mockito.doNothing().when(snp).storeContextCookies();
Vulcan vulcan = Mockito.mock(Vulcan.class);
Mockito.when(vulcan.getClient()).thenReturn(client);
Mockito.when(vulcan.getStudentAndParent()).thenCallRealMethod();
Mockito.when(vulcan.createSnp(Mockito.any(Client.class), Mockito.any())).thenReturn(snp);
vulcan.getStudentAndParent();
vulcan.getStudentAndParent();
}
@Test
public void createSnPTest() throws Exception {
vulcan.login("wulkanowy@wulkanowy.io", "wulkanowy123", "wulkan");
SnP snp1 = vulcan.createSnp(Mockito.mock(Client.class), null);
Assert.assertEquals(null, snp1.getId());
SnP snp2 = vulcan.createSnp(Mockito.mock(Client.class), "wulkan");
Assert.assertEquals("wulkan", snp2.getId());
}
@Test(expected = NotLoggedInErrorException.class)
public void getAttendanceExceptionText() throws Exception {
vulcan.getAttendanceTable();
Assert.assertEquals(vulcan.getClient(), vulcan.getClient());
}
}

View File

@ -1,6 +1,5 @@
package io.github.wulkanowy.api.login;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Assert;
@ -79,13 +78,6 @@ public class LoginTest {
login.sendCertificate(getFixtureAsString("cert.xml"), "demo123");
}
@Test(expected = VulcanOfflineException.class)
public void sendCertificateVulcanOfflineTest() throws Exception {
Login login = new Login(getClient("PrzerwaTechniczna.html"));
login.sendCertificate(getFixtureAsString("cert.xml"), "demo123");
}
@Test
public void findSymbolInCertificateTest() throws Exception {
Login login = new Login(getClient("Logowanie-certyfikat.html"));

View File

@ -8,7 +8,7 @@ import java.util.List;
import io.github.wulkanowy.api.Client;
import io.github.wulkanowy.api.FixtureHelper;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.NotLoggedInErrorException;
public class MessagesTest {

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dziennik UONET+</title>
</head>
<body>
<div id="MainPage_InfoPage">
<div class="startScreen">
<div class="topBar">
<div class="loginBox">
<div>
<a href="/LoginEndpoint.aspx" class="loginButton"></a>
<a href="/LoginEndpoint.aspx" class="loginButton">Zaloguj się</a>
</div>
</div>
</div>
<div class="bottomBar"><span>Uonet+ wersja 17.09.0007.26300</span></div>
</div>
</div>
</body>
</html>