From 59d6edb818c766e4f70776a0cf19de60119b84f2 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 May 2019 15:42:08 +0400 Subject: [PATCH] #120 refactor --- .../java/ru/ulstu/grant/model/GrantDto.java | 7 +++ .../java/ru/ulstu/grant/page/KiasPage.java | 54 ++++++++++++------ .../ru/ulstu/grant/service/KiasService.java | 49 +++++++--------- src/test/java/IndexKiasTest.java | 56 ------------------- src/test/java/grant/KiasPage.java | 41 -------------- 5 files changed, 63 insertions(+), 144 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index 5a561b1..9e59d86 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -12,6 +12,7 @@ import ru.ulstu.project.model.ProjectDto; import ru.ulstu.user.model.UserDto; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -96,6 +97,12 @@ public class GrantDto extends NameContainer { this.papers = convert(grant.getPapers(), PaperDto::new); } + public GrantDto(String grantTitle, Date deadLineDate) { + this.title = grantTitle; + deadlines.add(new Deadline(deadLineDate, "Окончание приёма заявок")); + status = Grant.GrantStatus.LOADED_FROM_KIAS; + } + public Integer getId() { return id; } diff --git a/src/main/java/ru/ulstu/grant/page/KiasPage.java b/src/main/java/ru/ulstu/grant/page/KiasPage.java index 4353684..8a6e444 100644 --- a/src/main/java/ru/ulstu/grant/page/KiasPage.java +++ b/src/main/java/ru/ulstu/grant/page/KiasPage.java @@ -3,34 +3,31 @@ package ru.ulstu.grant.page; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import ru.ulstu.grant.model.GrantDto; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.NoSuchElementException; public class KiasPage { private WebDriver driver; + public KiasPage(WebDriver webDriver) { this.driver = webDriver; } - public List getGrants() { - List grants = new ArrayList<>(); + public List getKiasGrants() throws ParseException { + List newGrants = new ArrayList<>(); do { - grants.addAll(getPageOfGrants()); - } - while (checkPagination()); - - return grants; + newGrants.addAll(getGrantsFromPage()); + } while (goToNextPage()); //проверка существования следующей страницы с грантами + return newGrants; } - public List getPageOfGrants() { - WebElement listContest = driver.findElement(By.tagName("tBody")); - List grants = listContest.findElements(By.cssSelector("tr.tr")); - return grants; - } - - public boolean checkPagination() { + private boolean goToNextPage() { try { if (driver.findElements(By.id("js-ctrlNext")).size() > 0) { driver.findElement(By.id("js-ctrlNext")).click(); @@ -42,11 +39,34 @@ public class KiasPage { return false; } - public String getGrantTitle(WebElement grant) { + private List getPageOfGrants() { + WebElement listContest = driver.findElement(By.tagName("tBody")); + List grants = listContest.findElements(By.cssSelector("tr.tr")); + return grants; + } + + private String getGrantTitle(WebElement grant) { return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText(); } - public String getFirstDeadline(WebElement grant) { - return grant.findElement(By.xpath("./td[5]")).getText(); + private List getGrantsFromPage() throws ParseException { + List grants = new ArrayList<>(); + for (WebElement grantElement : getPageOfGrants()) { + GrantDto grantDto = new GrantDto( + getGrantTitle(grantElement), + parseDeadLineDate(grantElement)); + grants.add(grantDto); + } + return grants; + } + + private Date parseDeadLineDate(WebElement grantElement) throws ParseException { + String deadlineDate = getFirstDeadline(grantElement); //10.06.2019 23:59 + SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + return formatter.parse(deadlineDate); + } + + private String getFirstDeadline(WebElement grantElement) { + return grantElement.findElement(By.xpath("./td[5]")).getText(); } } diff --git a/src/main/java/ru/ulstu/grant/service/KiasService.java b/src/main/java/ru/ulstu/grant/service/KiasService.java index 449b948..6881292 100644 --- a/src/main/java/ru/ulstu/grant/service/KiasService.java +++ b/src/main/java/ru/ulstu/grant/service/KiasService.java @@ -1,22 +1,17 @@ package ru.ulstu.grant.service; import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.springframework.stereotype.Service; -import ru.ulstu.deadline.model.Deadline; -import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.page.KiasPage; import ru.ulstu.user.service.UserService; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; -import java.util.Date; import java.util.List; @Service @@ -24,7 +19,6 @@ public class KiasService { private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s"; private final static String CONTEST_STATUS_ID = "1"; private final static String CONTEST_TYPE = "-1"; - private final static int CONTEST_YEAR = Calendar.getInstance().get(Calendar.YEAR); private final static String DRIVER_LOCATION = "drivers/%s"; private final static String WINDOWS_DRIVER = "chromedriver.exe"; @@ -38,33 +32,28 @@ public class KiasService { } public List getNewGrantsDto() throws ParseException { + WebDriver webDriver = getDriver(); + Integer leaderId = userService.findOneByLoginIgnoreCase("admin").getId(); + List grants = new ArrayList<>(); + for (Integer year : generateGrantYears()) { + webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year)); + grants.addAll(new KiasPage(webDriver).getKiasGrants()); + } + grants.forEach(grantDto -> grantDto.setLeaderId(leaderId)); + webDriver.quit(); + return grants; + } + + private List generateGrantYears() { + return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR), + Calendar.getInstance().get(Calendar.YEAR) + 1); + } + + private WebDriver getDriver() { System.setProperty(DRIVER_TYPE, getDriverExecutablePath()); final ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("--headless"); - WebDriver webDriver = new ChromeDriver(chromeOptions); - webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR)); - KiasPage kiasPage = new KiasPage(webDriver); - List kiasGrants = new ArrayList<>(); - List newGrants = new ArrayList<>(); - do { - kiasGrants.addAll(kiasPage.getPageOfGrants()); - for (WebElement grant : kiasGrants) { - GrantDto grantDto = new GrantDto(); - grantDto.setTitle(kiasPage.getGrantTitle(grant)); - String deadlineDate = kiasPage.getFirstDeadline(grant); //10.06.2019 23:59 - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); - Date date = formatter.parse(deadlineDate); - Deadline deadline = new Deadline(date, "Окончание приёма заявок"); - grantDto.setDeadlines(Arrays.asList(deadline)); - grantDto.setLeaderId(userService.findOneByLoginIgnoreCase("admin").getId()); - grantDto.setStatus(Grant.GrantStatus.LOADED_FROM_KIAS); - newGrants.add(grantDto); - } - kiasGrants.clear(); - } - while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами - webDriver.quit(); - return newGrants; + return new ChromeDriver(chromeOptions); } private String getDriverExecutablePath() { diff --git a/src/test/java/IndexKiasTest.java b/src/test/java/IndexKiasTest.java index 73dc646..8228e76 100644 --- a/src/test/java/IndexKiasTest.java +++ b/src/test/java/IndexKiasTest.java @@ -1,67 +1,11 @@ -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import core.PageObject; import core.TestTemplate; -import grant.KiasPage; import org.junit.runner.RunWith; -import org.openqa.selenium.WebElement; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import ru.ulstu.NgTrackerApplication; -import ru.ulstu.deadline.model.Deadline; -import ru.ulstu.grant.model.Grant; -import ru.ulstu.grant.model.GrantDto; -import ru.ulstu.user.repository.UserRepository; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class IndexKiasTest extends TestTemplate { - private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s"; - private final static String CONTEST_STATUS_ID = "1"; - private final static String CONTEST_TYPE = "-1"; - private final static String CONTEST_YEAR = "2019"; - private final Map> navigationHolder = ImmutableMap.of( - new KiasPage(), Arrays.asList("Поиск по конкурсам", - String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR)) - ); - - @Autowired - UserRepository userRepository; - - public List getNewGrantsDto() throws ParseException { - Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); - getContext().goTo(page.getValue().get(1)); - KiasPage kiasPage = (KiasPage) getContext().initPage(page.getKey()); - List kiasGrants = new ArrayList<>(); - List newGrants = new ArrayList<>(); - do { - kiasGrants.addAll(kiasPage.getPageOfGrants()); - for (WebElement grant : kiasGrants) { - GrantDto grantDto = new GrantDto(); - grantDto.setTitle(kiasPage.getGrantTitle(grant)); - String deadlineDate = kiasPage.getFirstDeadline(grant); //10.06.2019 23:59 - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); - Date date = formatter.parse(deadlineDate); - Deadline deadline = new Deadline(date, "Окончание приёма заявок"); - grantDto.setDeadlines(Arrays.asList(deadline)); - grantDto.setLeaderId(userRepository.findOneByLoginIgnoreCase("admin").getId()); - grantDto.setStatus(Grant.GrantStatus.LOADED_FROM_KIAS); - newGrants.add(grantDto); - } - kiasGrants.clear(); - } - while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами - - return newGrants; - } } diff --git a/src/test/java/grant/KiasPage.java b/src/test/java/grant/KiasPage.java index 7723b01..5864511 100644 --- a/src/test/java/grant/KiasPage.java +++ b/src/test/java/grant/KiasPage.java @@ -2,51 +2,10 @@ package grant; import core.PageObject; import org.openqa.selenium.By; -import org.openqa.selenium.WebElement; - -import java.util.ArrayList; -import java.util.List; -import java.util.NoSuchElementException; public class KiasPage extends PageObject { @Override public String getSubTitle() { return driver.findElement(By.tagName("h1")).getText(); } - - public List getGrants() { - List grants = new ArrayList<>(); - do { - grants.addAll(getPageOfGrants()); - } - while (checkPagination()); - - return grants; - } - - public List getPageOfGrants() { - WebElement listContest = driver.findElement(By.tagName("tBody")); - List grants = listContest.findElements(By.cssSelector("tr.tr")); - return grants; - } - - public boolean checkPagination() { - try { - if (driver.findElements(By.id("js-ctrlNext")).size() > 0) { - driver.findElement(By.id("js-ctrlNext")).click(); - return true; - } - } catch (NoSuchElementException e) { - return false; - } - return false; - } - - public String getGrantTitle(WebElement grant) { - return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText(); - } - - public String getFirstDeadline(WebElement grant) { - return grant.findElement(By.xpath("./td[5]")).getText(); - } }