Add ability to change grades list semester (#10)

This commit is contained in:
Mikołaj Pich 2017-08-04 14:06:37 +02:00 committed by GitHub
parent 89d235dd8a
commit 0aa083c8aa
7 changed files with 193 additions and 12 deletions

View File

@ -0,0 +1,35 @@
package io.github.wulkanowy.api;
public class Semester {
private String number = "";
private String id = "";
private boolean isCurrent = false;
public String getNumber() {
return number;
}
public Semester setNumber(String number) {
this.number = number;
return this;
}
public String getId() {
return id;
}
public Semester setId(String id) {
this.id = id;
return this;
}
public boolean isCurrent() {
return isCurrent;
}
public Semester setCurrent(boolean current) {
isCurrent = current;
return this;
}
}

View File

@ -4,8 +4,11 @@ import org.jsoup.Connection;
import org.jsoup.Jsoup;
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 java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -15,15 +18,22 @@ public class StudentAndParent extends Vulcan {
private String startPageUrl = "https://uonetplus.vulcan.net.pl/{locationID}/Start.mvc/Index";
private String locationID;
private String gradesPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/"
+ "Oceny/Wszystkie";
private String uonetPlusOpiekunUrl;
private String locationID = "";
private String uonetPlusOpiekunUrl = "";
public StudentAndParent(Cookies cookies, String locID) throws IOException {
this.cookies = cookies;
this.locationID = locID;
}
public String getGradesPageUrl() {
return gradesPageUrl;
}
public StudentAndParent setUp() throws IOException {
startPageUrl = startPageUrl.replace("{locationID}", locationID);
@ -67,4 +77,42 @@ public class StudentAndParent extends Vulcan {
public String getRowDataChildValue(Element e, int index) {
return e.select(".daneWiersz .wartosc").get(index - 1).text();
}
public List<Semester> getSemesters() throws IOException, LoginErrorException {
String url = getGradesPageUrl();
url = url.replace("{locationID}", getLocationID());
url = url.replace("{ID}", getID());
Document gradesPage = getPageByUrl(url);
Elements semesterOptions = gradesPage.select("#okresyKlasyfikacyjneDropDownList option");
List<Semester> semesters = new ArrayList<>();
for (Element e : semesterOptions) {
Semester semester = new Semester()
.setId(e.text())
.setNumber(e.attr("value"));
if ("selected".equals(e.attr("selected"))) {
semester.setCurrent(true);
}
semesters.add(semester);
}
return semesters;
}
public Semester getCurrentSemester(List<Semester> semesterList)
throws IOException, LoginErrorException {
Semester current = null;
for (Semester s : semesterList) {
if (s.isCurrent()) {
current = s;
break;
}
}
return current;
}
}

View File

@ -1,5 +1,9 @@
package io.github.wulkanowy.api;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.Map;
public abstract class Vulcan {
@ -23,4 +27,11 @@ public abstract class Vulcan {
this.cookies.addItems(cookies);
return this.cookies;
}
public Document getPageByUrl(String url) throws IOException {
return Jsoup.connect(url)
.followRedirects(true)
.cookies(getCookies())
.get();
}
}

View File

@ -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;
@ -20,8 +19,8 @@ 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 String gradesPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}"
+ "/Oceny/Wszystkie?details=2&okres=";
private List<Grade> grades = new ArrayList<>();
@ -30,14 +29,20 @@ public class GradesList extends Vulcan {
this.snp = snp;
}
public String getGradesPageUrl() {
return gradesPageUrl;
}
public List<Grade> getAll() throws IOException, LoginErrorException {
gradesPageUrl = gradesPageUrl.replace("{locationID}", snp.getLocationID());
gradesPageUrl = gradesPageUrl.replace("{ID}", snp.getID());
return getAll(snp.getCurrentSemester(snp.getSemesters()).getNumber());
}
Document marksPage = Jsoup.connect(gradesPageUrl)
.cookies(getCookies())
.get();
public List<Grade> getAll(String semester) throws IOException, LoginErrorException {
String url = getGradesPageUrl();
url = url.replace("{locationID}", snp.getLocationID());
url = url.replace("{ID}", snp.getID());
Document marksPage = getPageByUrl(url + semester);
Elements marksRows = marksPage.select(".ocenySzczegoly-table > tbody > tr");
for (Element row : marksRows) {

View File

@ -14,8 +14,8 @@ public class Notes extends Vulcan {
private StudentAndParent snp = null;
private String notesPageUrl =
"https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/UwagiOsiagniecia.mvc/Wszystkie";
private String notesPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/"
+ "UwagiOsiagniecia.mvc/Wszystkie";
public Notes(Cookies cookies, StudentAndParent snp) {
this.cookies = cookies;

View File

@ -0,0 +1,61 @@
package io.github.wulkanowy.api;
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.ArrayList;
import java.util.List;
public class StudentAndParentTest {
private String fixtureFileName = "OcenyWszystkie-semester.html";
private StudentAndParent snp;
@Before
public void setUp() throws Exception {
String input = FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName));
Document gradesPageDocument = Jsoup.parse(input);
snp = Mockito.mock(StudentAndParent.class);
PowerMockito.whenNew(StudentAndParent.class)
.withArguments(Mockito.any(Cookies.class), Mockito.anyString()).thenReturn(snp);
Mockito.when(snp.getPageByUrl(Mockito.anyString())).thenReturn(gradesPageDocument);
Mockito.when(snp.getGradesPageUrl()).thenReturn("http://example.null");
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();
}
@Test
public void getSemestersTest() throws Exception {
List<Semester> semesters = snp.getSemesters();
Assert.assertEquals(2, semesters.size());
Assert.assertEquals("1", semesters.get(0).getId());
Assert.assertEquals("1234", semesters.get(0).getNumber());
Assert.assertFalse(semesters.get(0).isCurrent());
Assert.assertEquals("2", semesters.get(1).getId());
Assert.assertEquals("1235", semesters.get(1).getNumber());
Assert.assertTrue(semesters.get(1).isCurrent());
}
@Test
public void getCurrentSemesterTest() throws Exception {
List<Semester> semesters = new ArrayList<>();
semesters.add(new Semester().setNumber("1500100900").setId("1").setCurrent(false));
semesters.add(new Semester().setNumber("1500100901").setId("2").setCurrent(true));
Assert.assertTrue(snp.getCurrentSemester(semesters).isCurrent());
Assert.assertEquals("2", snp.getCurrentSemester(semesters).getId());
Assert.assertEquals("1500100901", snp.getCurrentSemester(semesters).getNumber());
}
}

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>Witryna ucznia i rodzica Oceny</title>
</head>
<body>
<main class="mainContainer">
<h1>Oceny</h1>
<div class="filters">
<div>
<label for="okresyKlasyfikacyjneDropDownList">Okres klasyfikacyjny:</label>
<select id="okresyKlasyfikacyjneDropDownList" name="okresyKlasyfikacyjneDropDownList">
<option value="1234">1</option>
<option selected="selected" value="1235">2</option>
</select>
</div>
</main>
<footer>wersja: 17.05.0000.24042</footer>
</body>
</html>