diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/URLCharityScraperTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/URLCharityScraperTest.java index d437891..82c28cb 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/URLCharityScraperTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/URLCharityScraperTest.java @@ -1,5 +1,9 @@ package ntnu.systemutvikling.team6.scraper; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; @@ -7,165 +11,149 @@ import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.WebDriverWait; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - class URLCharityScraperTest { - private WebDriver driver; - private URLCharityScraper scraper; + private WebDriver driver; + private URLCharityScraper scraper; - @BeforeEach - void setup() { - driver = mock(WebDriver.class); + @BeforeEach + void setup() { + driver = mock(WebDriver.class); - scraper = new URLCharityScraper("http://test", driver) { - @Override - protected WebDriverWait createWait() { - return mock(WebDriverWait.class); - } + scraper = + new URLCharityScraper("http://test", driver) { + @Override + protected WebDriverWait createWait() { + return mock(WebDriverWait.class); + } - @Override - protected void closeDriver() { - } + @Override + protected void closeDriver() {} }; - } - - @Test - void updateDescriptionShouldReturnCorrectDescriptionWithoutReadMore() { - WebElement p1 = mock(WebElement.class); - WebElement p2 = mock(WebElement.class); - - when(p1.getText()).thenReturn("Short description"); - when(p2.getText()).thenReturn(""); + } - when(scraper.findElements(By.cssSelector(".information div p"))) - .thenReturn(List.of(p1, p2)); + @Test + void updateDescriptionShouldReturnCorrectDescriptionWithoutReadMore() { + WebElement p1 = mock(WebElement.class); + WebElement p2 = mock(WebElement.class); - when(scraper.findElements(By.cssSelector("a.read-more"))) - .thenReturn(List.of()); + when(p1.getText()).thenReturn("Short description"); + when(p2.getText()).thenReturn(""); - when(scraper.findElements(By.cssSelector(".extra-info p"))) - .thenReturn(List.of()); + when(scraper.findElements(By.cssSelector(".information div p"))).thenReturn(List.of(p1, p2)); - scraper.updateDescription(); + when(scraper.findElements(By.cssSelector("a.read-more"))).thenReturn(List.of()); - String result = scraper.getDescription(); + when(scraper.findElements(By.cssSelector(".extra-info p"))).thenReturn(List.of()); - assertTrue(result.contains("Short description"), - "First paragraph should be 'Short description'"); - assertFalse(result.isBlank(), - "Second paragraph should be blank."); - } + scraper.updateDescription(); - @Test - void updateDescriptionShouldReturnCorrectDescriptionWithReadMore() { - WebElement p1 = mock(WebElement.class); - WebElement extra = mock(WebElement.class); - WebElement readMore = mock(WebElement.class); + String result = scraper.getDescription(); - when(p1.getText()).thenReturn("Intro"); - when(extra.getText()).thenReturn("Extra info"); + assertTrue( + result.contains("Short description"), "First paragraph should be 'Short description'"); + assertFalse(result.isBlank(), "Second paragraph should be blank."); + } - when(scraper.findElements(By.cssSelector(".information div p"))) - .thenReturn(List.of(p1)); + @Test + void updateDescriptionShouldReturnCorrectDescriptionWithReadMore() { + WebElement p1 = mock(WebElement.class); + WebElement extra = mock(WebElement.class); + WebElement readMore = mock(WebElement.class); - when(scraper.findElements(By.cssSelector("a.read-more"))) - .thenReturn(List.of(readMore)); + when(p1.getText()).thenReturn("Intro"); + when(extra.getText()).thenReturn("Extra info"); - when(scraper.findElement(By.cssSelector("a.read-more"))) - .thenReturn(readMore); + when(scraper.findElements(By.cssSelector(".information div p"))).thenReturn(List.of(p1)); - when(scraper.findElements(By.cssSelector(".extra-info p"))) - .thenReturn(List.of(extra)); + when(scraper.findElements(By.cssSelector("a.read-more"))).thenReturn(List.of(readMore)); - scraper.updateDescription(); + when(scraper.findElement(By.cssSelector("a.read-more"))).thenReturn(readMore); - String result = scraper.getDescription(); + when(scraper.findElements(By.cssSelector(".extra-info p"))).thenReturn(List.of(extra)); - verify(readMore).click(); - assertTrue(result.contains("Intro"), - "First paragraph should be 'Intro'"); - assertTrue(result.contains("Extra info"), - "Second paragraph should be 'Extra info'"); - } + scraper.updateDescription(); - @Test - void updateLogoShouldReturnCorrectLogoURL() { - WebElement logo = mock(WebElement.class); + String result = scraper.getDescription(); - when(scraper.findElement(By.cssSelector(".logo > img"))) - .thenReturn(logo); + verify(readMore).click(); + assertTrue(result.contains("Intro"), "First paragraph should be 'Intro'"); + assertTrue(result.contains("Extra info"), "Second paragraph should be 'Extra info'"); + } - when(logo.getAttribute("src")).thenReturn("logo.png"); + @Test + void updateLogoShouldReturnCorrectLogoURL() { + WebElement logo = mock(WebElement.class); - scraper.updateLogo(); + when(scraper.findElement(By.cssSelector(".logo > img"))).thenReturn(logo); - assertEquals("logo.png", scraper.getLogoURL()); - } + when(logo.getAttribute("src")).thenReturn("logo.png"); - @Test - void updateCategoriesShouldReturnCorrectCategories() { - WebElement c1 = mock(WebElement.class); - WebElement c2 = mock(WebElement.class); + scraper.updateLogo(); - when(c1.getText()).thenReturn("Health"); - when(c2.getText()).thenReturn("Education"); + assertEquals("logo.png", scraper.getLogoURL()); + } - when(scraper.findElements(By.cssSelector(".tag-label"))) - .thenReturn(List.of(c1, c2)); + @Test + void updateCategoriesShouldReturnCorrectCategories() { + WebElement c1 = mock(WebElement.class); + WebElement c2 = mock(WebElement.class); - scraper.updateCategories(); + when(c1.getText()).thenReturn("Health"); + when(c2.getText()).thenReturn("Education"); - assertEquals("Health,Education", scraper.getCategories(), - "Should return a string that lists categories with ',' as a delimiter."); - } + when(scraper.findElements(By.cssSelector(".tag-label"))).thenReturn(List.of(c1, c2)); - @Test - void updateKeyNumbersShouldReturnCorrectNumbers() { - WebElement e1 = mock(WebElement.class); - WebElement e2 = mock(WebElement.class); - WebElement e3 = mock(WebElement.class); + scraper.updateCategories(); - when(e1.getAttribute("data-percentage")).thenReturn("80"); - when(e2.getAttribute("data-percentage")).thenReturn("10"); - when(e3.getAttribute("data-percentage")).thenReturn("90"); + assertEquals( + "Health,Education", + scraper.getCategories(), + "Should return a string that lists categories with ',' as a delimiter."); + } - when(scraper.findElement(any(By.class))) - .thenReturn(e1, e2, e3); + @Test + void updateKeyNumbersShouldReturnCorrectNumbers() { + WebElement e1 = mock(WebElement.class); + WebElement e2 = mock(WebElement.class); + WebElement e3 = mock(WebElement.class); - scraper.updateKeyValues(); + when(e1.getAttribute("data-percentage")).thenReturn("80"); + when(e2.getAttribute("data-percentage")).thenReturn("10"); + when(e3.getAttribute("data-percentage")).thenReturn("90"); - System.out.println(scraper.getKeyValues()); + when(scraper.findElement(any(By.class))).thenReturn(e1, e2, e3); - assertEquals("80:10:90", scraper.getKeyValues(), - "Should return a string that lists key values with ':' as a delimiter."); - } + scraper.updateKeyValues(); - @Test - void scrapeCharityPageShouldCallAllRelevantMethods() { - URLCharityScraper spyScraper = spy(scraper); + System.out.println(scraper.getKeyValues()); - doNothing().when(spyScraper).updateDescription(); - doNothing().when(spyScraper).updateLogo(); - doNothing().when(spyScraper).updateCategories(); - doNothing().when(spyScraper).updateKeyValues(); + assertEquals( + "80:10:90", + scraper.getKeyValues(), + "Should return a string that lists key values with ':' as a delimiter."); + } - spyScraper.scrapeCharityPage(); + @Test + void scrapeCharityPageShouldCallAllRelevantMethods() { + URLCharityScraper spyScraper = spy(scraper); - // Url should be correct - verify(driver).get("http://test"); - // UpdateDescription should run - verify(spyScraper).updateDescription(); - // UpdateLogo should run - verify(spyScraper).updateLogo(); - // UpdateCategories should run - verify(spyScraper).updateCategories(); - // UpdateKeyValues should run - verify(spyScraper).updateKeyValues(); - } + doNothing().when(spyScraper).updateDescription(); + doNothing().when(spyScraper).updateLogo(); + doNothing().when(spyScraper).updateCategories(); + doNothing().when(spyScraper).updateKeyValues(); + spyScraper.scrapeCharityPage(); -} \ No newline at end of file + // Url should be correct + verify(driver).get("http://test"); + // UpdateDescription should run + verify(spyScraper).updateDescription(); + // UpdateLogo should run + verify(spyScraper).updateLogo(); + // UpdateCategories should run + verify(spyScraper).updateCategories(); + // UpdateKeyValues should run + verify(spyScraper).updateKeyValues(); + } +}