|
| 1 | +package de.mygrades; |
| 2 | + |
| 3 | +import android.test.ApplicationTestCase; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import de.mygrades.database.dao.Action; |
| 8 | +import de.mygrades.database.dao.ActionDao; |
| 9 | +import de.mygrades.database.dao.DaoSession; |
| 10 | +import de.mygrades.database.dao.GradeEntry; |
| 11 | +import de.mygrades.database.dao.GradeEntryDao; |
| 12 | +import de.mygrades.database.dao.Overview; |
| 13 | +import de.mygrades.database.dao.University; |
| 14 | +import de.mygrades.database.dao.UniversityDao; |
| 15 | +import de.mygrades.main.processor.GradesProcessor; |
| 16 | +import de.mygrades.main.processor.LoginProcessor; |
| 17 | +import de.mygrades.main.processor.UniversityProcessor; |
| 18 | +import de.mygrades.util.AverageCalculator; |
| 19 | +import de.mygrades.util.Config; |
| 20 | + |
| 21 | +/** |
| 22 | + * Base class for application tests to test the rules against html fragments. |
| 23 | + * Furthermore it tests the correct functionality of the scraper and parser. |
| 24 | + */ |
| 25 | +public abstract class AbstractRuleTest extends ApplicationTestCase<MyGradesApplication> { |
| 26 | + |
| 27 | + protected GradesProcessor gradesProcessor; |
| 28 | + protected LoginProcessor loginProcessor; |
| 29 | + protected UniversityProcessor universityProcessor; |
| 30 | + protected DaoSession daoSession; |
| 31 | + protected AverageCalculator averageCalculator; |
| 32 | + |
| 33 | + protected List<University> universities; |
| 34 | + protected List<GradeEntry> gradeEntries; |
| 35 | + protected Overview overview; |
| 36 | + |
| 37 | + public AbstractRuleTest() { |
| 38 | + super(MyGradesApplication.class); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected void setUp() throws Exception { |
| 43 | + createApplication(); |
| 44 | + assertNotNull(getContext()); |
| 45 | + |
| 46 | + daoSession = ((MyGradesApplication) getContext().getApplicationContext()).getDaoSession(); |
| 47 | + assertNotNull(daoSession); |
| 48 | + daoSession.clear(); |
| 49 | + daoSession.getGradeEntryDao().deleteAll(); |
| 50 | + |
| 51 | + universityProcessor = new UniversityProcessor(getContext()); |
| 52 | + assertNotNull(universityProcessor); |
| 53 | + |
| 54 | + loginProcessor = new LoginProcessor(getContext()); |
| 55 | + assertNotNull(loginProcessor); |
| 56 | + |
| 57 | + gradesProcessor = new GradesProcessor(getContext()); |
| 58 | + assertNotNull(gradesProcessor); |
| 59 | + |
| 60 | + universityProcessor.getUniversities(true); |
| 61 | + getUniversitiesFromDatabase(); |
| 62 | + |
| 63 | + super.setUp(); // should be on top actually, but then the test is flaky :/ |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void tearDown() throws Exception { |
| 68 | + loginProcessor.logout(); |
| 69 | + loginProcessor = null; |
| 70 | + universities = null; |
| 71 | + gradesProcessor = null; |
| 72 | + loginProcessor = null; |
| 73 | + daoSession = null; |
| 74 | + averageCalculator = null; |
| 75 | + gradeEntries = null; |
| 76 | + overview = null; |
| 77 | + super.tearDown(); |
| 78 | + } |
| 79 | + |
| 80 | + private void getUniversitiesFromDatabase() { |
| 81 | + UniversityDao universityDao = daoSession.getUniversityDao(); |
| 82 | + universities = universityDao.queryBuilder() |
| 83 | + .where(UniversityDao.Properties.Published.eq(true)) |
| 84 | + .list(); |
| 85 | + } |
| 86 | + |
| 87 | + private University getUniversityByName(String name) { |
| 88 | + for (University u : universities) { |
| 89 | + if (u.getName().toLowerCase().contains(name.toLowerCase())) { |
| 90 | + return u; |
| 91 | + } |
| 92 | + } |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + protected void scrape(String universityName, String url, int ruleIndex) { |
| 97 | + scrape(universityName, url, "", "", ruleIndex); |
| 98 | + } |
| 99 | + |
| 100 | + protected void scrape(String universityName, String url, String username, String password, int ruleIndex) { |
| 101 | + University university = getUniversityByName(universityName); |
| 102 | + assertNotNull(university); |
| 103 | + long universityId = university.getUniversityId(); |
| 104 | + long ruleId = university.getRules().get(ruleIndex).getRuleId(); // TODO: sort rules or get rule by name |
| 105 | + universityProcessor.getDetailedUniversity(universityId); |
| 106 | + overwriteUrl(ruleId, url); |
| 107 | + loginProcessor.loginAndScrapeForGrades(username, password, universityId, ruleId); |
| 108 | + gradeEntries = daoSession.getGradeEntryDao().loadAll(); |
| 109 | + averageCalculator = new AverageCalculator(false); |
| 110 | + averageCalculator.calculateFromGradeEntries(gradeEntries); |
| 111 | + } |
| 112 | + |
| 113 | + protected void scrapeForOverview(String examId) { |
| 114 | + GradeEntry gradeEntry = daoSession.getGradeEntryDao().queryBuilder() |
| 115 | + .where(GradeEntryDao.Properties.ExamId.eq(examId)).build().unique(); |
| 116 | + assertNotNull(gradeEntry); |
| 117 | + String gradeHash = gradeEntry.getHash(); |
| 118 | + gradesProcessor.scrapeForOverview(gradeHash); |
| 119 | + gradeEntry.refresh(); |
| 120 | + overview = gradeEntry.getOverview(); |
| 121 | + assertNotNull(overview); |
| 122 | + } |
| 123 | + |
| 124 | + private void overwriteUrl(long ruleId, String url) { |
| 125 | + Action action = daoSession.getActionDao().queryBuilder() |
| 126 | + .where(ActionDao.Properties.RuleId.eq(ruleId)) |
| 127 | + .where(ActionDao.Properties.Position.eq(0)) |
| 128 | + .build().unique(); |
| 129 | + assertNotNull(action); |
| 130 | + action.setUrl(Config.getTestServerUrl() + url); |
| 131 | + action.update(); |
| 132 | + } |
| 133 | +} |
0 commit comments