diff --git a/api/src/main/java/io/github/wulkanowy/api/Api.java b/api/src/main/java/io/github/wulkanowy/api/Client.java similarity index 83% rename from api/src/main/java/io/github/wulkanowy/api/Api.java rename to api/src/main/java/io/github/wulkanowy/api/Client.java index fc9c03ee..61cbef36 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Api.java +++ b/api/src/main/java/io/github/wulkanowy/api/Client.java @@ -7,15 +7,19 @@ import org.jsoup.nodes.Document; import java.io.IOException; 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; } - public Map getCookies() { + void setCookies(Cookies cookies) { + this.cookies = cookies; + } + + Map getCookies() { return cookies.getItems(); } diff --git a/api/src/main/java/io/github/wulkanowy/api/SnP.java b/api/src/main/java/io/github/wulkanowy/api/SnP.java index f89b71cf..413f4b7d 100644 --- a/api/src/main/java/io/github/wulkanowy/api/SnP.java +++ b/api/src/main/java/io/github/wulkanowy/api/SnP.java @@ -22,8 +22,6 @@ public interface SnP { void storeContextCookies() throws IOException, NotLoggedInErrorException; - Cookies getCookiesObject(); - String getRowDataChildValue(Element e, int index); Document getSnPPageDocument(String url) throws IOException; diff --git a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java index 06c2d38f..0f9ef8fb 100644 --- a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java +++ b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -10,7 +10,7 @@ import java.util.List; 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"; @@ -20,7 +20,9 @@ public class StudentAndParent extends Api implements SnP { 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"; @@ -28,13 +30,13 @@ public class StudentAndParent extends Api implements SnP { private String id; - public StudentAndParent(Cookies cookies, String symbol) { - this.cookies = cookies; + public StudentAndParent(Client client, String symbol) { + this.client = client; this.symbol = symbol; } - public StudentAndParent(Cookies cookies, String symbol, String id) { - this(cookies, symbol); + public StudentAndParent(Client client, String symbol, String id) { + this(client, symbol); this.id = id; } @@ -78,7 +80,7 @@ public class StudentAndParent extends Api implements SnP { } public void storeContextCookies() throws IOException, NotLoggedInErrorException { - getPageByUrl(getSnpPageUrl()); + client.getPageByUrl(getSnpPageUrl()); } 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 - Document startPage = getPageByUrl(getStartPageUrl()); + Document startPage = client.getPageByUrl(getStartPageUrl()); Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first(); if (null == studentTileLink) { @@ -116,7 +118,7 @@ public class StudentAndParent extends Api implements SnP { } public Document getSnPPageDocument(String url) throws IOException { - return getPageByUrl(getBaseUrl() + return client.getPageByUrl(getBaseUrl() .replace(SYMBOL_PLACEHOLDER, getSymbol()) .replace("{ID}", getId()) + url); } diff --git a/api/src/main/java/io/github/wulkanowy/api/Vulcan.java b/api/src/main/java/io/github/wulkanowy/api/Vulcan.java index e6707e91..34e9e7c7 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Vulcan.java +++ b/api/src/main/java/io/github/wulkanowy/api/Vulcan.java @@ -21,7 +21,7 @@ import io.github.wulkanowy.api.timetable.Timetable; import io.github.wulkanowy.api.user.BasicInformation; import io.github.wulkanowy.api.user.FamilyInformation; -public class Vulcan extends Api { +public class Vulcan { private String id; @@ -35,25 +35,27 @@ public class Vulcan extends Api { private String email; - public Vulcan login(Cookies cookies, String symbol) { - this.cookies = cookies; - this.symbol = symbol; + private Client client = new Client(); - 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) throws BadCredentialsException, AccountPermissionException, LoginErrorException, IOException, VulcanOfflineException { - Login login = getLoginObject(); setFullEndpointInfo(email); login.setProtocolSchema(protocolSchema); login.setLogHost(logHost); - String realSymbol = login.login(this.email, password, symbol); - - login(login.getCookiesObject(), realSymbol); + this.symbol = login.login(this.email, password, symbol); return this; } @@ -80,11 +82,7 @@ public class Vulcan extends Api { return email; } - public Login getLoginObject() { - return new Login(new Cookies()); - } - - public Vulcan setFullEndpointInfo(String email) { + private void setFullEndpointInfo(String email) { String[] creds = email.split("\\\\"); this.email = email; @@ -96,12 +94,10 @@ public class Vulcan extends Api { this.logHost = url[1]; this.email = creds[2]; } - - return this; } public SnP getStudentAndParent() throws IOException, NotLoggedInErrorException { - if (null == getCookiesObject()) { + if (0 == client.getCookies().size()) { throw new NotLoggedInErrorException(); } @@ -109,23 +105,23 @@ public class Vulcan extends Api { return snp; } - snp = createSnp(cookies, symbol, id); + snp = createSnp(client, symbol, id); snp.setLogHost(logHost); snp.setProtocolSchema(protocolSchema); snp.storeContextCookies(); - this.cookies = snp.getCookiesObject(); +// this.cookies = client.getCookiesObject(); return snp; } - public SnP createSnp(Cookies cookies, String symbol, String id) { + SnP createSnp(Client client, String symbol, String 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 { diff --git a/api/src/main/java/io/github/wulkanowy/api/login/Login.java b/api/src/main/java/io/github/wulkanowy/api/login/Login.java index 7908bfd1..bcf1c0e5 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/Login.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/Login.java @@ -8,10 +8,9 @@ import org.jsoup.select.Elements; import java.io.IOException; import java.util.regex.Pattern; -import io.github.wulkanowy.api.Api; -import io.github.wulkanowy.api.Cookies; +import io.github.wulkanowy.api.Client; -public class Login extends Api { +public class Login { private static final String loginPageUrl = "{schema}://cufs.{host}/{symbol}/Account/LogOn" + "?ReturnUrl=%2F{symbol}%2FFS%2FLS%3Fwa%3Dwsignin1.0%26wtrealm%3D" + @@ -27,8 +26,10 @@ public class Login extends Api { private String symbol = "Default"; - public Login(Cookies cookies) { - this.cookies = cookies; + private Client client; + + public Login(Client client) { + this.client = client; } public void setProtocolSchema(String schema) { @@ -66,7 +67,7 @@ public class Login extends Api { throws IOException, BadCredentialsException { this.symbol = symbol; - Document html = postPageByUrl(getLoginPageUrl(), new String[][]{ + Document html = client.postPageByUrl(getLoginPageUrl(), new String[][]{ {"LoginName", email}, {"Password", password} }); @@ -82,7 +83,7 @@ public class Login extends Api { throws IOException, LoginErrorException, AccountPermissionException, VulcanOfflineException { this.symbol = findSymbol(defaultSymbol, certificate); - Document html = postPageByUrl(getLoginEndpointPageUrl(), new String[][]{ + Document html = client.postPageByUrl(getLoginEndpointPageUrl(), new String[][]{ {"wa", "wsignin1.0"}, {"wresult", certificate} }); diff --git a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java index e8ca1342..d9fffcbb 100644 --- a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java @@ -14,7 +14,7 @@ import io.github.wulkanowy.api.login.NotLoggedInErrorException; public class StudentAndParentTest { - private StudentAndParent snp; + private Client client; @Before public void setUp() throws Exception { @@ -22,32 +22,21 @@ public class StudentAndParentTest { getClass().getResourceAsStream("OcenyWszystkie-semester.html")); Document gradesPageDocument = Jsoup.parse(input); - snp = Mockito.mock(StudentAndParent.class); - - 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.anyList())).thenCallRealMethod(); + client = Mockito.mock(Client.class); + Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(gradesPageDocument); } @Test 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("id123", snp.getId()); } @Test public void getSnpPageUrlWithIdTest() throws Exception { - Mockito.when(snp.getSnpPageUrl()).thenCallRealMethod(); Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/123456/", - snp.getSnpPageUrl()); + (new StudentAndParent(client, "symbol", "123456")).getSnpPageUrl()); } @Test @@ -55,11 +44,9 @@ public class StudentAndParentTest { String input = FixtureHelper.getAsString(getClass().getResourceAsStream("Start.html")); Document startPageDocument = Jsoup.parse(input); - Mockito.when(snp.getPageByUrl(Mockito.anyString())).thenReturn(startPageDocument); - Mockito.when(snp.getStartPageUrl()).thenReturn("http://wulkan.io"); - Mockito.when(snp.getId()).thenCallRealMethod(); + Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(startPageDocument); + StudentAndParent snp = new StudentAndParent(client, "symbol"); - Mockito.when(snp.getSnpPageUrl()).thenCallRealMethod(); Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/534213/Start/Index/", snp.getSnpPageUrl()); } @@ -70,35 +57,36 @@ public class StudentAndParentTest { FixtureHelper.getAsString(getClass().getResourceAsStream("OcenyWszystkie-semester.html")) ); - Mockito.when(snp.getPageByUrl(Mockito.anyString())).thenReturn(wrongPageDocument); - Mockito.when(snp.getStartPageUrl()).thenReturn("http://wulkan.io"); - Mockito.when(snp.getId()).thenCallRealMethod(); - - Mockito.when(snp.getSnpPageUrl()).thenCallRealMethod(); + Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(wrongPageDocument); + StudentAndParent snp = new StudentAndParent(client, "symbol"); snp.getSnpPageUrl(); } @Test public void getExtractedIDStandardTest() throws Exception { + StudentAndParent snp = new StudentAndParent(client, "symbol"); Assert.assertEquals("123456", snp.getExtractedIdFromUrl("https://uonetplus-opiekun" + ".vulcan.net.pl/powiat/123456/Start/Index/")); } @Test public void getExtractedIDDemoTest() throws Exception { + StudentAndParent snp = new StudentAndParent(client, "symbol"); Assert.assertEquals("demo12345", snp.getExtractedIdFromUrl("https://uonetplus-opiekundemo" + ".vulcan.net.pl/demoupowiat/demo12345/Start/Index/")); } @Test(expected = NotLoggedInErrorException.class) public void getExtractedIDNotLoggedTest() throws Exception { + StudentAndParent snp = new StudentAndParent(client, "symbol"); Assert.assertEquals("123", snp.getExtractedIdFromUrl("https://uonetplus" + ".vulcan.net.pl/powiat/")); } @Test public void getSemestersTest() throws Exception { + SnP snp = new StudentAndParent(client, "symbol", "123456"); List semesters = snp.getSemesters(); 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("1500100901").setId("2").setCurrent(true)); + SnP snp = new StudentAndParent(client, ""); Assert.assertTrue(snp.getCurrentSemester(semesters).isCurrent()); Assert.assertEquals("2", snp.getCurrentSemester(semesters).getId()); Assert.assertEquals("1500100901", snp.getCurrentSemester(semesters).getNumber()); @@ -125,6 +114,7 @@ public class StudentAndParentTest { @Test public void getCurrentSemesterFromEmptyTest() throws Exception { + SnP snp = new StudentAndParent(client, ""); List semesters = new ArrayList<>(); Assert.assertNull(snp.getCurrentSemester(semesters)); diff --git a/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java b/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java index 7a87ed6e..1fee85e8 100644 --- a/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java @@ -1,199 +1,52 @@ 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 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.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; @Before public void setUp() throws Exception { - StudentAndParent snp = Mockito.mock(StudentAndParent.class); - vulcan = Mockito.mock(Vulcan.class); - Mockito.when(vulcan.getStudentAndParent()) - .thenReturn(snp); + vulcan = new Vulcan(); + vulcan.setClient(Mockito.mock(Client.class)); + vulcan.setLogin(Mockito.mock(Login.class)); } @Test 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"); + Assert.assertEquals("http", vulcan.getProtocolSchema()); Assert.assertEquals("fakelog.net", vulcan.getLogHost()); 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) public void getStudentAndParentNotLoggedInTest() throws Exception { - Mockito.when(vulcan.getStudentAndParent()).thenCallRealMethod(); vulcan.getStudentAndParent(); } @Test public void createSnPTest() throws Exception { - Vulcan vulcan = new Vulcan(); - vulcan.login(new Cookies(), "testSymbol"); + vulcan.login("wulkanowy@wulkanowy.io", "wulkanowy123", "wulkan"); - Assert.assertThat(vulcan.createSnp(new Cookies(), "testSymbol", null), - CoreMatchers.instanceOf(StudentAndParent.class)); + SnP snp1 = vulcan.createSnp(Mockito.mock(Client.class), "testSymbol", null); + 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) public void getAttendanceExceptionText() throws Exception { - Mockito.when(vulcan.getAttendanceTable()).thenCallRealMethod(); - Mockito.when(vulcan.getStudentAndParent()).thenThrow(NotLoggedInErrorException.class); - 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)); - } } diff --git a/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java b/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java index e277ed19..335ab84b 100644 --- a/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java @@ -7,66 +7,41 @@ import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; -import io.github.wulkanowy.api.Cookies; +import io.github.wulkanowy.api.Client; import io.github.wulkanowy.api.FixtureHelper; public class LoginTest { - public String getFixtureAsString(String fixtureFileName) { + private String getFixtureAsString(String fixtureFileName) { return FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)); } - public Login getSetUpLogin(String fixtureFileName) throws Exception { - Document tablePageDocument = Jsoup.parse(getFixtureAsString(fixtureFileName)); + private Client getClient(String fixtureFileName) throws Exception { + Document doc = Jsoup.parse(getFixtureAsString(fixtureFileName)); - Login login = Mockito.mock(Login.class); - Mockito.when(login.postPageByUrl(Mockito.anyString(), Mockito.any(String[][].class)) - ).thenReturn(tablePageDocument); - Mockito.when(login.getLoginPageUrl()).thenReturn(""); - Mockito.when(login.getLoginEndpointPageUrl()).thenReturn("asdf"); - return login; + Client client = Mockito.mock(Client.class); + Mockito.when(client.postPageByUrl(Mockito.anyString(), Mockito.any(String[][].class))).thenReturn(doc); + + return client; } @Test public void loginTest() throws Exception { - Login login = getSetUpLogin("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(""); - Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenReturn("d123"); - Assert.assertEquals("d123", login.login("a@a", "pswd", "d123")); - } + Login login = new Login(getClient("Logowanie-success.html")); - @Test - 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")); + Assert.assertEquals("d123", login.login("a@a", "pswd", "d123")); } @Test(expected = BadCredentialsException.class) public void sendWrongCredentialsTest() throws Exception { - Login login = getSetUpLogin("Logowanie-error.html"); - Mockito.when(login.sendCredentials( - Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); + Login login = new Login(getClient("Logowanie-error.html")); login.sendCredentials("a@a", "pswd", "d123"); } @Test public void sendCredentialsCertificateTest() throws Exception { - Login login = getSetUpLogin("Logowanie-certyfikat.html"); - Mockito.when(login.sendCredentials( - Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); - Mockito.when(login.getLoginPageUrl()).thenReturn("http://a.a"); + Login login = new Login(getClient("Logowanie-certyfikat.html")); Assert.assertEquals( getFixtureAsString("cert.xml").replaceAll("\\s+",""), @@ -76,49 +51,44 @@ public class LoginTest { @Test public void sendCertificateNotDefaultSymbolSuccessTest() throws Exception { - Login login = getSetUpLogin("Logowanie-success.html"); - Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); - Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); - Assert.assertEquals("wulkanowyschool321", login.sendCertificate("", "wulkanowyschool321")); + Login login = new Login(getClient("Logowanie-success.html")); + + Assert.assertEquals("wulkanowyschool321", + login.sendCertificate("", "wulkanowyschool321")); } @Test public void sendCertificateDefaultSymbolSuccessTest() throws Exception { - Login login = getSetUpLogin("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(); + Login login = new Login(getClient("Logowanie-success.html")); + Assert.assertEquals("demo12345", login.sendCertificate(getFixtureAsString("cert.xml"), "Default")); } @Test(expected = AccountPermissionException.class) public void sendCertificateAccountPermissionTest() throws Exception { - Login login = getSetUpLogin("Logowanie-brak-dostepu.html"); - Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); - Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); + Login login = new Login(getClient("Logowanie-brak-dostepu.html")); + login.sendCertificate(getFixtureAsString("cert.xml"), "demo123"); } @Test(expected = LoginErrorException.class) public void sendCertificateLoginErrorTest() throws Exception { - Login login = getSetUpLogin("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 login = new Login(getClient("Logowanie-certyfikat.html")); // change to other document + login.sendCertificate(getFixtureAsString("cert.xml"), "demo123"); } @Test(expected = VulcanOfflineException.class) public void sendCertificateVulcanOfflineTest() throws Exception { - Login login = getSetUpLogin("PrzerwaTechniczna.html"); - Mockito.when(login.findSymbol(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); - Mockito.when(login.sendCertificate(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); + Login login = new Login(getClient("PrzerwaTechniczna.html")); + login.sendCertificate(getFixtureAsString("cert.xml"), "demo123"); } @Test public void findSymbolInCertificateTest() throws Exception { - Login login = new Login(new Cookies()); + Login login = new Login(getClient("Logowanie-certyfikat.html")); String certificate = getFixtureAsString("cert.xml"); @@ -127,7 +97,7 @@ public class LoginTest { @Test public void findSymbolInInvalidCertificateTest() throws Exception { - Login login = new Login(new Cookies()); + Login login = new Login(getClient("Logowanie-certyfikat.html")); Assert.assertEquals("", login.findSymbolInCertificate("")); // change to real cert with empty symbols }