Use Client as dependency instead inherit from Api (#50)

This commit is contained in:
Mikołaj Pich 2018-02-22 17:07:04 +01:00 committed by Rafał Borcz
parent 66fe25d9cc
commit 5b52b5d398
8 changed files with 97 additions and 283 deletions

View File

@ -7,15 +7,19 @@ import org.jsoup.nodes.Document;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
public abstract class Api { public class Client {
protected Cookies cookies = new Cookies(); private Cookies cookies = new Cookies();
public Cookies getCookiesObject() { Cookies getCookiesObject() {
return cookies; return cookies;
} }
public Map<String, String> getCookies() { void setCookies(Cookies cookies) {
this.cookies = cookies;
}
Map<String, String> getCookies() {
return cookies.getItems(); return cookies.getItems();
} }

View File

@ -22,8 +22,6 @@ public interface SnP {
void storeContextCookies() throws IOException, NotLoggedInErrorException; void storeContextCookies() throws IOException, NotLoggedInErrorException;
Cookies getCookiesObject();
String getRowDataChildValue(Element e, int index); String getRowDataChildValue(Element e, int index);
Document getSnPPageDocument(String url) throws IOException; Document getSnPPageDocument(String url) throws IOException;

View File

@ -10,7 +10,7 @@ import java.util.List;
import io.github.wulkanowy.api.login.NotLoggedInErrorException; import io.github.wulkanowy.api.login.NotLoggedInErrorException;
public class StudentAndParent extends Api implements SnP { public class StudentAndParent implements SnP {
private static final String startPageUrl = "{schema}://uonetplus.{host}/{symbol}/Start.mvc/Index"; private static final String startPageUrl = "{schema}://uonetplus.{host}/{symbol}/Start.mvc/Index";
@ -20,7 +20,9 @@ public class StudentAndParent extends Api implements SnP {
private static final String GRADES_PAGE_URL = "Oceny/Wszystkie"; private static final String GRADES_PAGE_URL = "Oceny/Wszystkie";
protected String logHost = "vulcan.net.pl"; private Client client;
private String logHost = "vulcan.net.pl";
private String protocolSchema = "https"; private String protocolSchema = "https";
@ -28,13 +30,13 @@ public class StudentAndParent extends Api implements SnP {
private String id; private String id;
public StudentAndParent(Cookies cookies, String symbol) { public StudentAndParent(Client client, String symbol) {
this.cookies = cookies; this.client = client;
this.symbol = symbol; this.symbol = symbol;
} }
public StudentAndParent(Cookies cookies, String symbol, String id) { public StudentAndParent(Client client, String symbol, String id) {
this(cookies, symbol); this(client, symbol);
this.id = id; this.id = id;
} }
@ -78,7 +80,7 @@ public class StudentAndParent extends Api implements SnP {
} }
public void storeContextCookies() throws IOException, NotLoggedInErrorException { public void storeContextCookies() throws IOException, NotLoggedInErrorException {
getPageByUrl(getSnpPageUrl()); client.getPageByUrl(getSnpPageUrl());
} }
public String getSnpPageUrl() throws IOException, NotLoggedInErrorException { public String getSnpPageUrl() throws IOException, NotLoggedInErrorException {
@ -87,7 +89,7 @@ public class StudentAndParent extends Api implements SnP {
} }
// get url to uonetplus-opiekun.vulcan.net.pl // get url to uonetplus-opiekun.vulcan.net.pl
Document startPage = getPageByUrl(getStartPageUrl()); Document startPage = client.getPageByUrl(getStartPageUrl());
Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first(); Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first();
if (null == studentTileLink) { if (null == studentTileLink) {
@ -116,7 +118,7 @@ public class StudentAndParent extends Api implements SnP {
} }
public Document getSnPPageDocument(String url) throws IOException { public Document getSnPPageDocument(String url) throws IOException {
return getPageByUrl(getBaseUrl() return client.getPageByUrl(getBaseUrl()
.replace(SYMBOL_PLACEHOLDER, getSymbol()) .replace(SYMBOL_PLACEHOLDER, getSymbol())
.replace("{ID}", getId()) + url); .replace("{ID}", getId()) + url);
} }

View File

@ -21,7 +21,7 @@ import io.github.wulkanowy.api.timetable.Timetable;
import io.github.wulkanowy.api.user.BasicInformation; import io.github.wulkanowy.api.user.BasicInformation;
import io.github.wulkanowy.api.user.FamilyInformation; import io.github.wulkanowy.api.user.FamilyInformation;
public class Vulcan extends Api { public class Vulcan {
private String id; private String id;
@ -35,25 +35,27 @@ public class Vulcan extends Api {
private String email; private String email;
public Vulcan login(Cookies cookies, String symbol) { private Client client = new Client();
this.cookies = cookies;
this.symbol = symbol;
return this; private Login login = new Login(client);
public void setClient(Client client) {
this.client = client;
}
public void setLogin(Login login) {
this.login = login;
} }
public Vulcan login(String email, String password, String symbol) public Vulcan login(String email, String password, String symbol)
throws BadCredentialsException, AccountPermissionException, throws BadCredentialsException, AccountPermissionException,
LoginErrorException, IOException, VulcanOfflineException { LoginErrorException, IOException, VulcanOfflineException {
Login login = getLoginObject();
setFullEndpointInfo(email); setFullEndpointInfo(email);
login.setProtocolSchema(protocolSchema); login.setProtocolSchema(protocolSchema);
login.setLogHost(logHost); login.setLogHost(logHost);
String realSymbol = login.login(this.email, password, symbol); this.symbol = login.login(this.email, password, symbol);
login(login.getCookiesObject(), realSymbol);
return this; return this;
} }
@ -80,11 +82,7 @@ public class Vulcan extends Api {
return email; return email;
} }
public Login getLoginObject() { private void setFullEndpointInfo(String email) {
return new Login(new Cookies());
}
public Vulcan setFullEndpointInfo(String email) {
String[] creds = email.split("\\\\"); String[] creds = email.split("\\\\");
this.email = email; this.email = email;
@ -96,12 +94,10 @@ public class Vulcan extends Api {
this.logHost = url[1]; this.logHost = url[1];
this.email = creds[2]; this.email = creds[2];
} }
return this;
} }
public SnP getStudentAndParent() throws IOException, NotLoggedInErrorException { public SnP getStudentAndParent() throws IOException, NotLoggedInErrorException {
if (null == getCookiesObject()) { if (0 == client.getCookies().size()) {
throw new NotLoggedInErrorException(); throw new NotLoggedInErrorException();
} }
@ -109,23 +105,23 @@ public class Vulcan extends Api {
return snp; return snp;
} }
snp = createSnp(cookies, symbol, id); snp = createSnp(client, symbol, id);
snp.setLogHost(logHost); snp.setLogHost(logHost);
snp.setProtocolSchema(protocolSchema); snp.setProtocolSchema(protocolSchema);
snp.storeContextCookies(); snp.storeContextCookies();
this.cookies = snp.getCookiesObject(); // this.cookies = client.getCookiesObject();
return snp; return snp;
} }
public SnP createSnp(Cookies cookies, String symbol, String id) { SnP createSnp(Client client, String symbol, String id) {
if (null == id) { if (null == id) {
return new StudentAndParent(cookies, symbol); return new StudentAndParent(client, symbol);
} }
return new StudentAndParent(cookies, symbol, id); return new StudentAndParent(client, symbol, id);
} }
public AttendanceStatistics getAttendanceStatistics() throws IOException, NotLoggedInErrorException { public AttendanceStatistics getAttendanceStatistics() throws IOException, NotLoggedInErrorException {

View File

@ -8,10 +8,9 @@ import org.jsoup.select.Elements;
import java.io.IOException; import java.io.IOException;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import io.github.wulkanowy.api.Api; import io.github.wulkanowy.api.Client;
import io.github.wulkanowy.api.Cookies;
public class Login extends Api { public class Login {
private static final String loginPageUrl = "{schema}://cufs.{host}/{symbol}/Account/LogOn" + private static final String loginPageUrl = "{schema}://cufs.{host}/{symbol}/Account/LogOn" +
"?ReturnUrl=%2F{symbol}%2FFS%2FLS%3Fwa%3Dwsignin1.0%26wtrealm%3D" + "?ReturnUrl=%2F{symbol}%2FFS%2FLS%3Fwa%3Dwsignin1.0%26wtrealm%3D" +
@ -27,8 +26,10 @@ public class Login extends Api {
private String symbol = "Default"; private String symbol = "Default";
public Login(Cookies cookies) { private Client client;
this.cookies = cookies;
public Login(Client client) {
this.client = client;
} }
public void setProtocolSchema(String schema) { public void setProtocolSchema(String schema) {
@ -66,7 +67,7 @@ public class Login extends Api {
throws IOException, BadCredentialsException { throws IOException, BadCredentialsException {
this.symbol = symbol; this.symbol = symbol;
Document html = postPageByUrl(getLoginPageUrl(), new String[][]{ Document html = client.postPageByUrl(getLoginPageUrl(), new String[][]{
{"LoginName", email}, {"LoginName", email},
{"Password", password} {"Password", password}
}); });
@ -82,7 +83,7 @@ public class Login extends Api {
throws IOException, LoginErrorException, AccountPermissionException, VulcanOfflineException { throws IOException, LoginErrorException, AccountPermissionException, VulcanOfflineException {
this.symbol = findSymbol(defaultSymbol, certificate); this.symbol = findSymbol(defaultSymbol, certificate);
Document html = postPageByUrl(getLoginEndpointPageUrl(), new String[][]{ Document html = client.postPageByUrl(getLoginEndpointPageUrl(), new String[][]{
{"wa", "wsignin1.0"}, {"wa", "wsignin1.0"},
{"wresult", certificate} {"wresult", certificate}
}); });

View File

@ -14,7 +14,7 @@ import io.github.wulkanowy.api.login.NotLoggedInErrorException;
public class StudentAndParentTest { public class StudentAndParentTest {
private StudentAndParent snp; private Client client;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -22,32 +22,21 @@ public class StudentAndParentTest {
getClass().getResourceAsStream("OcenyWszystkie-semester.html")); getClass().getResourceAsStream("OcenyWszystkie-semester.html"));
Document gradesPageDocument = Jsoup.parse(input); Document gradesPageDocument = Jsoup.parse(input);
snp = Mockito.mock(StudentAndParent.class); client = Mockito.mock(Client.class);
Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(gradesPageDocument);
Mockito.when(snp.getSnPPageDocument(Mockito.anyString())).thenReturn(gradesPageDocument);
Mockito.when(snp.getExtractedIdFromUrl(Mockito.anyString())).thenCallRealMethod();
Mockito.when(snp.getLogHost()).thenReturn("vulcan.net.pl");
Mockito.when(snp.getBaseUrl()).thenReturn("https://uonetplus-opiekun.vulcan.net.pl/symbol/123456/");
Mockito.when(snp.getSymbol()).thenReturn("symbol");
Mockito.when(snp.getId()).thenReturn("123456");
Mockito.when(snp.getSemesters()).thenCallRealMethod();
Mockito.when(snp.getGradesPageUrl()).thenReturn("http://wulkanowy.null");
Mockito.when(snp.getSemesters(Mockito.any(Document.class))).thenCallRealMethod();
Mockito.when(snp.getCurrentSemester(Mockito.<Semester>anyList())).thenCallRealMethod();
} }
@Test @Test
public void snpTest() throws Exception { public void snpTest() throws Exception {
StudentAndParent snp = new StudentAndParent(new Cookies(), "demo123", "id123"); StudentAndParent snp = new StudentAndParent(client, "demo123", "id123");
Assert.assertEquals("demo123", snp.getSymbol()); Assert.assertEquals("demo123", snp.getSymbol());
Assert.assertEquals("id123", snp.getId()); Assert.assertEquals("id123", snp.getId());
} }
@Test @Test
public void getSnpPageUrlWithIdTest() throws Exception { public void getSnpPageUrlWithIdTest() throws Exception {
Mockito.when(snp.getSnpPageUrl()).thenCallRealMethod();
Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/123456/", Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/123456/",
snp.getSnpPageUrl()); (new StudentAndParent(client, "symbol", "123456")).getSnpPageUrl());
} }
@Test @Test
@ -55,11 +44,9 @@ public class StudentAndParentTest {
String input = FixtureHelper.getAsString(getClass().getResourceAsStream("Start.html")); String input = FixtureHelper.getAsString(getClass().getResourceAsStream("Start.html"));
Document startPageDocument = Jsoup.parse(input); Document startPageDocument = Jsoup.parse(input);
Mockito.when(snp.getPageByUrl(Mockito.anyString())).thenReturn(startPageDocument); Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(startPageDocument);
Mockito.when(snp.getStartPageUrl()).thenReturn("http://wulkan.io"); StudentAndParent snp = new StudentAndParent(client, "symbol");
Mockito.when(snp.getId()).thenCallRealMethod();
Mockito.when(snp.getSnpPageUrl()).thenCallRealMethod();
Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/534213/Start/Index/", Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/534213/Start/Index/",
snp.getSnpPageUrl()); snp.getSnpPageUrl());
} }
@ -70,35 +57,36 @@ public class StudentAndParentTest {
FixtureHelper.getAsString(getClass().getResourceAsStream("OcenyWszystkie-semester.html")) FixtureHelper.getAsString(getClass().getResourceAsStream("OcenyWszystkie-semester.html"))
); );
Mockito.when(snp.getPageByUrl(Mockito.anyString())).thenReturn(wrongPageDocument); Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(wrongPageDocument);
Mockito.when(snp.getStartPageUrl()).thenReturn("http://wulkan.io"); StudentAndParent snp = new StudentAndParent(client, "symbol");
Mockito.when(snp.getId()).thenCallRealMethod();
Mockito.when(snp.getSnpPageUrl()).thenCallRealMethod();
snp.getSnpPageUrl(); snp.getSnpPageUrl();
} }
@Test @Test
public void getExtractedIDStandardTest() throws Exception { public void getExtractedIDStandardTest() throws Exception {
StudentAndParent snp = new StudentAndParent(client, "symbol");
Assert.assertEquals("123456", snp.getExtractedIdFromUrl("https://uonetplus-opiekun" Assert.assertEquals("123456", snp.getExtractedIdFromUrl("https://uonetplus-opiekun"
+ ".vulcan.net.pl/powiat/123456/Start/Index/")); + ".vulcan.net.pl/powiat/123456/Start/Index/"));
} }
@Test @Test
public void getExtractedIDDemoTest() throws Exception { public void getExtractedIDDemoTest() throws Exception {
StudentAndParent snp = new StudentAndParent(client, "symbol");
Assert.assertEquals("demo12345", snp.getExtractedIdFromUrl("https://uonetplus-opiekundemo" Assert.assertEquals("demo12345", snp.getExtractedIdFromUrl("https://uonetplus-opiekundemo"
+ ".vulcan.net.pl/demoupowiat/demo12345/Start/Index/")); + ".vulcan.net.pl/demoupowiat/demo12345/Start/Index/"));
} }
@Test(expected = NotLoggedInErrorException.class) @Test(expected = NotLoggedInErrorException.class)
public void getExtractedIDNotLoggedTest() throws Exception { public void getExtractedIDNotLoggedTest() throws Exception {
StudentAndParent snp = new StudentAndParent(client, "symbol");
Assert.assertEquals("123", snp.getExtractedIdFromUrl("https://uonetplus" Assert.assertEquals("123", snp.getExtractedIdFromUrl("https://uonetplus"
+ ".vulcan.net.pl/powiat/")); + ".vulcan.net.pl/powiat/"));
} }
@Test @Test
public void getSemestersTest() throws Exception { public void getSemestersTest() throws Exception {
SnP snp = new StudentAndParent(client, "symbol", "123456");
List<Semester> semesters = snp.getSemesters(); List<Semester> semesters = snp.getSemesters();
Assert.assertEquals(2, semesters.size()); Assert.assertEquals(2, semesters.size());
@ -118,6 +106,7 @@ public class StudentAndParentTest {
semesters.add(new Semester().setNumber("1500100900").setId("1").setCurrent(false)); semesters.add(new Semester().setNumber("1500100900").setId("1").setCurrent(false));
semesters.add(new Semester().setNumber("1500100901").setId("2").setCurrent(true)); semesters.add(new Semester().setNumber("1500100901").setId("2").setCurrent(true));
SnP snp = new StudentAndParent(client, "");
Assert.assertTrue(snp.getCurrentSemester(semesters).isCurrent()); Assert.assertTrue(snp.getCurrentSemester(semesters).isCurrent());
Assert.assertEquals("2", snp.getCurrentSemester(semesters).getId()); Assert.assertEquals("2", snp.getCurrentSemester(semesters).getId());
Assert.assertEquals("1500100901", snp.getCurrentSemester(semesters).getNumber()); Assert.assertEquals("1500100901", snp.getCurrentSemester(semesters).getNumber());
@ -125,6 +114,7 @@ public class StudentAndParentTest {
@Test @Test
public void getCurrentSemesterFromEmptyTest() throws Exception { public void getCurrentSemesterFromEmptyTest() throws Exception {
SnP snp = new StudentAndParent(client, "");
List<Semester> semesters = new ArrayList<>(); List<Semester> semesters = new ArrayList<>();
Assert.assertNull(snp.getCurrentSemester(semesters)); Assert.assertNull(snp.getCurrentSemester(semesters));

View File

@ -1,199 +1,52 @@
package io.github.wulkanowy.api; package io.github.wulkanowy.api;
import org.hamcrest.CoreMatchers;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import io.github.wulkanowy.api.attendance.AttendanceStatistics;
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.Login; import io.github.wulkanowy.api.login.Login;
import io.github.wulkanowy.api.login.NotLoggedInErrorException; import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.notes.AchievementsList;
import io.github.wulkanowy.api.notes.NotesList;
import io.github.wulkanowy.api.school.SchoolInfo;
import io.github.wulkanowy.api.school.TeachersInfo;
import io.github.wulkanowy.api.timetable.Timetable;
import io.github.wulkanowy.api.user.BasicInformation;
import io.github.wulkanowy.api.user.FamilyInformation;
public class VulcanTest extends Vulcan { public class VulcanTest {
private Vulcan vulcan; private Vulcan vulcan;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
StudentAndParent snp = Mockito.mock(StudentAndParent.class); vulcan = new Vulcan();
vulcan = Mockito.mock(Vulcan.class); vulcan.setClient(Mockito.mock(Client.class));
Mockito.when(vulcan.getStudentAndParent()) vulcan.setLogin(Mockito.mock(Login.class));
.thenReturn(snp);
} }
@Test @Test
public void setFullEndpointInfoTest() throws Exception { public void setFullEndpointInfoTest() throws Exception {
SnP snp = new StudentAndParent(new Cookies(), "Default");
Login login = Mockito.mock(Login.class);
Vulcan vulcan = Mockito.mock(Vulcan.class);
Mockito.when(vulcan.login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(vulcan.login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(vulcan.getLoginObject()).thenReturn(login);
Mockito.when(vulcan.createSnp(Mockito.any(Cookies.class), Mockito.anyString(), Mockito.anyString())).thenReturn(snp);
Mockito.when(vulcan.getCookiesObject()).thenCallRealMethod();
Mockito.when(vulcan.getStudentAndParent()).thenCallRealMethod();
Mockito.when(vulcan.setFullEndpointInfo(Mockito.anyString())).thenCallRealMethod();
Mockito.when(vulcan.getProtocolSchema()).thenCallRealMethod();
Mockito.when(vulcan.getLogHost()).thenCallRealMethod();
Mockito.when(vulcan.getEmail()).thenCallRealMethod();
vulcan.login("http://fakelog.net\\\\admin", "pass", "Default", "123"); vulcan.login("http://fakelog.net\\\\admin", "pass", "Default", "123");
Assert.assertEquals("http", vulcan.getProtocolSchema()); Assert.assertEquals("http", vulcan.getProtocolSchema());
Assert.assertEquals("fakelog.net", vulcan.getLogHost()); Assert.assertEquals("fakelog.net", vulcan.getLogHost());
Assert.assertEquals("admin", vulcan.getEmail()); Assert.assertEquals("admin", vulcan.getEmail());
} }
@Test
public void getLoginObjectTest() throws Exception {
Mockito.when(vulcan.getLoginObject()).thenCallRealMethod();
Assert.assertThat(vulcan.getLoginObject(), CoreMatchers.instanceOf(Login.class));
}
@Test
public void getStudentAndParentTest() throws Exception {
Cookies cookies = new Cookies();
Vulcan vulcan = Mockito.mock(Vulcan.class);
Mockito.when(vulcan.getCookiesObject()).thenReturn(cookies);
StudentAndParent snp = Mockito.mock(StudentAndParent.class);
Mockito.doNothing().when(snp).storeContextCookies();
Mockito.when(snp.getCookiesObject()).thenReturn(cookies);
Mockito.when(vulcan.createSnp( // nullable because method uses class vars, refactor?
Mockito.nullable(Cookies.class), Mockito.nullable(String.class), Mockito.nullable(String.class)
)).thenReturn(snp);
Mockito.when(vulcan.getStudentAndParent()).thenCallRealMethod();
SnP vulcanSnP = vulcan.getStudentAndParent();
Assert.assertEquals(snp, vulcanSnP);
Assert.assertEquals(vulcanSnP, vulcan.getStudentAndParent());
}
@Test(expected = NotLoggedInErrorException.class) @Test(expected = NotLoggedInErrorException.class)
public void getStudentAndParentNotLoggedInTest() throws Exception { public void getStudentAndParentNotLoggedInTest() throws Exception {
Mockito.when(vulcan.getStudentAndParent()).thenCallRealMethod();
vulcan.getStudentAndParent(); vulcan.getStudentAndParent();
} }
@Test @Test
public void createSnPTest() throws Exception { public void createSnPTest() throws Exception {
Vulcan vulcan = new Vulcan(); vulcan.login("wulkanowy@wulkanowy.io", "wulkanowy123", "wulkan");
vulcan.login(new Cookies(), "testSymbol");
Assert.assertThat(vulcan.createSnp(new Cookies(), "testSymbol", null), SnP snp1 = vulcan.createSnp(Mockito.mock(Client.class), "testSymbol", null);
CoreMatchers.instanceOf(StudentAndParent.class)); Assert.assertEquals(snp1.getId(), null);
SnP snp2 = vulcan.createSnp(Mockito.mock(Client.class), "testSymbol", "wulkan");
Assert.assertEquals(snp2.getId(), "wulkan");
Assert.assertThat(vulcan.createSnp(new Cookies(), "testSymbol", "testId"),
CoreMatchers.instanceOf(StudentAndParent.class));
} }
@Test(expected = NotLoggedInErrorException.class) @Test(expected = NotLoggedInErrorException.class)
public void getAttendanceExceptionText() throws Exception { public void getAttendanceExceptionText() throws Exception {
Mockito.when(vulcan.getAttendanceTable()).thenCallRealMethod();
Mockito.when(vulcan.getStudentAndParent()).thenThrow(NotLoggedInErrorException.class);
vulcan.getAttendanceTable(); vulcan.getAttendanceTable();
} }
@Test
public void getAttendanceTest() throws Exception {
Mockito.when(vulcan.getAttendanceTable()).thenCallRealMethod();
Assert.assertThat(vulcan.getAttendanceTable(),
CoreMatchers.instanceOf(AttendanceTable.class));
}
@Test
public void getAttendanceStatisticTest() throws Exception {
Mockito.when(vulcan.getAttendanceStatistics()).thenCallRealMethod();
Assert.assertThat(vulcan.getAttendanceStatistics(),
CoreMatchers.instanceOf(AttendanceStatistics.class));
}
@Test
public void getExamsListTest() throws Exception {
Mockito.when(vulcan.getExamsList()).thenCallRealMethod();
Assert.assertThat(vulcan.getExamsList(),
CoreMatchers.instanceOf(ExamsWeek.class));
}
@Test
public void getGradesListTest() throws Exception {
Mockito.when(vulcan.getGradesList()).thenCallRealMethod();
Assert.assertThat(vulcan.getGradesList(),
CoreMatchers.instanceOf(GradesList.class));
}
@Test
public void getSubjectListTest() throws Exception {
Mockito.when(vulcan.getSubjectsList()).thenCallRealMethod();
Assert.assertThat(vulcan.getSubjectsList(),
CoreMatchers.instanceOf(SubjectsList.class));
}
@Test
public void getAchievementsListTest() throws Exception {
Mockito.when(vulcan.getAchievementsList()).thenCallRealMethod();
Assert.assertThat(vulcan.getAchievementsList(),
CoreMatchers.instanceOf(AchievementsList.class));
}
@Test
public void getNotesListTest() throws Exception {
Mockito.when(vulcan.getNotesList()).thenCallRealMethod();
Assert.assertThat(vulcan.getNotesList(),
CoreMatchers.instanceOf(NotesList.class));
}
@Test
public void getSchoolInfoTest() throws Exception {
Mockito.when(vulcan.getSchoolInfo()).thenCallRealMethod();
Assert.assertThat(vulcan.getSchoolInfo(),
CoreMatchers.instanceOf(SchoolInfo.class));
}
@Test
public void getTeachersInfoTest() throws Exception {
Mockito.when(vulcan.getTeachersInfo()).thenCallRealMethod();
Assert.assertThat(vulcan.getTeachersInfo(),
CoreMatchers.instanceOf(TeachersInfo.class));
}
@Test
public void getTimetableTest() throws Exception {
Mockito.when(vulcan.getTimetable()).thenCallRealMethod();
Assert.assertThat(vulcan.getTimetable(),
CoreMatchers.instanceOf(Timetable.class));
}
@Test
public void getBasicInformationTest() throws Exception {
Mockito.when(vulcan.getBasicInformation()).thenCallRealMethod();
Assert.assertThat(vulcan.getBasicInformation(),
CoreMatchers.instanceOf(BasicInformation.class));
}
@Test
public void getFamilyInformationTest() throws Exception {
Mockito.when(vulcan.getFamilyInformation()).thenCallRealMethod();
Assert.assertThat(vulcan.getFamilyInformation(),
CoreMatchers.instanceOf(FamilyInformation.class));
}
} }

View File

@ -7,66 +7,41 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import io.github.wulkanowy.api.Cookies; import io.github.wulkanowy.api.Client;
import io.github.wulkanowy.api.FixtureHelper; import io.github.wulkanowy.api.FixtureHelper;
public class LoginTest { public class LoginTest {
public String getFixtureAsString(String fixtureFileName) { private String getFixtureAsString(String fixtureFileName) {
return FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)); return FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName));
} }
public Login getSetUpLogin(String fixtureFileName) throws Exception { private Client getClient(String fixtureFileName) throws Exception {
Document tablePageDocument = Jsoup.parse(getFixtureAsString(fixtureFileName)); Document doc = Jsoup.parse(getFixtureAsString(fixtureFileName));
Login login = Mockito.mock(Login.class); Client client = Mockito.mock(Client.class);
Mockito.when(login.postPageByUrl(Mockito.anyString(), Mockito.any(String[][].class)) Mockito.when(client.postPageByUrl(Mockito.anyString(), Mockito.any(String[][].class))).thenReturn(doc);
).thenReturn(tablePageDocument);
Mockito.when(login.getLoginPageUrl()).thenReturn(""); return client;
Mockito.when(login.getLoginEndpointPageUrl()).thenReturn("asdf");
return login;
} }
@Test @Test
public void loginTest() throws Exception { public void loginTest() throws Exception {
Login login = getSetUpLogin("Logowanie-success.html"); Login login = new Login(getClient("Logowanie-success.html"));
Mockito.when(login.login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenCallRealMethod();
Mockito.when(login.sendCredentials(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenReturn("<xml>");
Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenReturn("d123");
Assert.assertEquals("d123", login.login("a@a", "pswd", "d123"));
}
@Test Assert.assertEquals("d123", login.login("a@a", "pswd", "d123"));
public void loginDefaultTest() throws Exception {
Login login = getSetUpLogin("Logowanie-success.html");
Mockito.when(login.getLoginEndpointPageUrl()).thenReturn("asdf");
Mockito.when(login.login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenCallRealMethod();
Mockito.when(login.sendCredentials(Mockito.anyString(), Mockito.anyString(), Mockito.eq("Default")))
.thenReturn(getFixtureAsString("cert.xml"));
Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.findSymbolInCertificate(Mockito.anyString())).thenCallRealMethod();
Assert.assertEquals("demo12345", login.login("a@a", "pswd", "Default"));
} }
@Test(expected = BadCredentialsException.class) @Test(expected = BadCredentialsException.class)
public void sendWrongCredentialsTest() throws Exception { public void sendWrongCredentialsTest() throws Exception {
Login login = getSetUpLogin("Logowanie-error.html"); Login login = new Login(getClient("Logowanie-error.html"));
Mockito.when(login.sendCredentials(
Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
login.sendCredentials("a@a", "pswd", "d123"); login.sendCredentials("a@a", "pswd", "d123");
} }
@Test @Test
public void sendCredentialsCertificateTest() throws Exception { public void sendCredentialsCertificateTest() throws Exception {
Login login = getSetUpLogin("Logowanie-certyfikat.html"); Login login = new Login(getClient("Logowanie-certyfikat.html"));
Mockito.when(login.sendCredentials(
Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.getLoginPageUrl()).thenReturn("http://a.a");
Assert.assertEquals( Assert.assertEquals(
getFixtureAsString("cert.xml").replaceAll("\\s+",""), getFixtureAsString("cert.xml").replaceAll("\\s+",""),
@ -76,49 +51,44 @@ public class LoginTest {
@Test @Test
public void sendCertificateNotDefaultSymbolSuccessTest() throws Exception { public void sendCertificateNotDefaultSymbolSuccessTest() throws Exception {
Login login = getSetUpLogin("Logowanie-success.html"); Login login = new Login(getClient("Logowanie-success.html"));
Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); Assert.assertEquals("wulkanowyschool321",
Assert.assertEquals("wulkanowyschool321", login.sendCertificate("", "wulkanowyschool321")); login.sendCertificate("", "wulkanowyschool321"));
} }
@Test @Test
public void sendCertificateDefaultSymbolSuccessTest() throws Exception { public void sendCertificateDefaultSymbolSuccessTest() throws Exception {
Login login = getSetUpLogin("Logowanie-success.html"); Login login = new Login(getClient("Logowanie-success.html"));
Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.findSymbolInCertificate(Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Assert.assertEquals("demo12345", Assert.assertEquals("demo12345",
login.sendCertificate(getFixtureAsString("cert.xml"), "Default")); login.sendCertificate(getFixtureAsString("cert.xml"), "Default"));
} }
@Test(expected = AccountPermissionException.class) @Test(expected = AccountPermissionException.class)
public void sendCertificateAccountPermissionTest() throws Exception { public void sendCertificateAccountPermissionTest() throws Exception {
Login login = getSetUpLogin("Logowanie-brak-dostepu.html"); Login login = new Login(getClient("Logowanie-brak-dostepu.html"));
Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
login.sendCertificate(getFixtureAsString("cert.xml"), "demo123"); login.sendCertificate(getFixtureAsString("cert.xml"), "demo123");
} }
@Test(expected = LoginErrorException.class) @Test(expected = LoginErrorException.class)
public void sendCertificateLoginErrorTest() throws Exception { public void sendCertificateLoginErrorTest() throws Exception {
Login login = getSetUpLogin("Logowanie-certyfikat.html"); // change to other document Login login = new Login(getClient("Logowanie-certyfikat.html")); // change to other document
Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
login.sendCertificate(getFixtureAsString("cert.xml"), "demo123"); login.sendCertificate(getFixtureAsString("cert.xml"), "demo123");
} }
@Test(expected = VulcanOfflineException.class) @Test(expected = VulcanOfflineException.class)
public void sendCertificateVulcanOfflineTest() throws Exception { public void sendCertificateVulcanOfflineTest() throws Exception {
Login login = getSetUpLogin("PrzerwaTechniczna.html"); Login login = new Login(getClient("PrzerwaTechniczna.html"));
Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod();
login.sendCertificate(getFixtureAsString("cert.xml"), "demo123"); login.sendCertificate(getFixtureAsString("cert.xml"), "demo123");
} }
@Test @Test
public void findSymbolInCertificateTest() throws Exception { public void findSymbolInCertificateTest() throws Exception {
Login login = new Login(new Cookies()); Login login = new Login(getClient("Logowanie-certyfikat.html"));
String certificate = getFixtureAsString("cert.xml"); String certificate = getFixtureAsString("cert.xml");
@ -127,7 +97,7 @@ public class LoginTest {
@Test @Test
public void findSymbolInInvalidCertificateTest() throws Exception { public void findSymbolInInvalidCertificateTest() throws Exception {
Login login = new Login(new Cookies()); Login login = new Login(getClient("Logowanie-certyfikat.html"));
Assert.assertEquals("", login.findSymbolInCertificate("<xml></xml>")); // change to real cert with empty symbols Assert.assertEquals("", login.findSymbolInCertificate("<xml></xml>")); // change to real cert with empty symbols
} }