Skip to content

Commit

Permalink
Added tests for parsingService
Browse files Browse the repository at this point in the history
  • Loading branch information
This-Is-Ko committed Oct 8, 2023
1 parent a818546 commit 27a3def
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 18 deletions.
18 changes: 0 additions & 18 deletions src/test/java/com/ko/footballupdater/unit/FbrefDataSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,6 @@ public void testParsePlayerMatchData_success() throws IOException {
assertEquals(0, result.getAssists());
}

// @Test
// public void testParseSquadDataForTeam() {
// // Implement your test for parseSquadDataForTeam here
// // Mock dependencies and verify the expected behavior
//
// // Example:
// Team team = new Team(/* create a Team instance here */);
// DataSource dataSource = new DataSource(/* create a DataSource instance here */);
//
// // Mock external dependencies or expectations
//
// // Call the method to be tested
// List<Player> result = fbrefDataSource.parseSquadDataForTeam(team, dataSource);
//
// // Assert the expected behavior or result
// assertNotNull(result); // Add more assertions based on your test requirements
// }

private String readHtmlFile(String fileName) throws IOException {
StringBuilder contentBuilder = new StringBuilder();
BufferedReader in = new BufferedReader(new FileReader(fileName));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package com.ko.footballupdater.unit.services;

import com.ko.footballupdater.datasource.DataSourceParser;
import com.ko.footballupdater.datasource.FbrefDataSource;
import com.ko.footballupdater.datasource.FotmobDataSource;
import com.ko.footballupdater.models.CheckedStatus;
import com.ko.footballupdater.models.DataSource;
import com.ko.footballupdater.models.DataSourceSiteName;
import com.ko.footballupdater.models.DataSourceType;
import com.ko.footballupdater.models.Player;
import com.ko.footballupdater.models.PlayerMatchPerformanceStats;
import com.ko.footballupdater.services.ParsingService;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.springframework.test.util.ReflectionTestUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

public class ParsingServiceTest {

@InjectMocks
private ParsingService parsingService;

private DataSourceParser fbrefDataSource = mock(FbrefDataSource.class);

private DataSourceParser fotmobDataSource = mock(FotmobDataSource.class);

private List<DataSourceParser> dataSourceParsers = new ArrayList<>(){{
add(fbrefDataSource);
add(fotmobDataSource);
}};

private List<DataSourceSiteName> dataSourcePriority = new ArrayList<>(){{
add(DataSourceSiteName.FOTMOB);
add(DataSourceSiteName.FBREF);
}};

private MockedStatic<Jsoup> jsoupMockedStatic = mockStatic(Jsoup.class);

@BeforeEach
void setUp() throws IOException {
MockitoAnnotations.openMocks(this);
ReflectionTestUtils.setField(parsingService, "dataSourceParsers", dataSourceParsers);
ReflectionTestUtils.setField(parsingService, "dataSourcePriority", dataSourcePriority);

Connection connection = mock(Connection.class);
when(Jsoup.connect(anyString())).thenReturn(connection);
when(connection.get()).thenReturn(new Document("url"));

when(fotmobDataSource.parsePlayerMatchData(any(Player.class), any(Document.class))).thenReturn(new PlayerMatchPerformanceStats(DataSourceSiteName.FOTMOB));
when(fotmobDataSource.getDataSourceSiteName()).thenReturn(DataSourceSiteName.FOTMOB);
when(fbrefDataSource.parsePlayerMatchData(any(Player.class), any(Document.class))).thenReturn(new PlayerMatchPerformanceStats(DataSourceSiteName.FBREF));
when(fbrefDataSource.getDataSourceSiteName()).thenReturn(DataSourceSiteName.FBREF);
}

@AfterEach
void after() {
jsoupMockedStatic.close();
}

@Test
void testParsePlayerMatchData_MultipleDataSourcesAvailable_Successful() {
// Priority FOTMOB, FBREF
// return FOTMOB successful
Set<DataSource> dataSources = new HashSet<>();
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FBREF, "url"));
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FOTMOB, "url"));
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNotNull(result);
assertEquals(DataSourceSiteName.FOTMOB, result.getDataSourceSiteName());
}

@Test
void testParsePlayerMatchData_MultipleDataSourcesAvailableForPlayer_DifferentOrder_Successful() {
// Priority FOTMOB, FBREF
// User datasource order is FBREF, FOTMOB
// return FOTMOB successful
Set<DataSource> dataSources = new HashSet<>();
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FOTMOB, "url"));
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FBREF, "url"));
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNotNull(result);
assertEquals(DataSourceSiteName.FOTMOB, result.getDataSourceSiteName());
}


@Test
void testParsePlayerMatchData_MultipleDataSourcesAvailable_DifferentPriorityOrder_Successful() {
dataSourcePriority = new ArrayList<>(){{
add(DataSourceSiteName.FBREF);
add(DataSourceSiteName.FOTMOB);
}};
ReflectionTestUtils.setField(parsingService, "dataSourcePriority", dataSourcePriority);
// Priority FBREF, FOTMOB
// return FBREF successful
Set<DataSource> dataSources = new HashSet<>();
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FBREF, "url"));
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FOTMOB, "url"));
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNotNull(result);
assertEquals(DataSourceSiteName.FBREF, result.getDataSourceSiteName());
}

@Test
void testParsePlayerMatchData_OneDataSourceAvailable_FOTMOB_Successful() {
// Priority FOTMOB, FBREF
// User datasource order is FOTMOB
// return FOTMOB successful
Set<DataSource> dataSources = new HashSet<>();
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FOTMOB, "url"));
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNotNull(result);
assertEquals(DataSourceSiteName.FOTMOB, result.getDataSourceSiteName());
}

@Test
void testParsePlayerMatchData_OneDataSourcesAvailable_FBREF_Successful() {
// Priority FOTMOB, FBREF
// User datasource order is FBREF
// return FBREF successful
Set<DataSource> dataSources = new HashSet<>();
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FBREF, "url"));
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNotNull(result);
assertEquals(DataSourceSiteName.FBREF, result.getDataSourceSiteName());
}

@Test
void testParsePlayerMatchData_MultipleDataSourcesAvailable_PriorityFails_Fallback_Successful() {
when(fotmobDataSource.parsePlayerMatchData(any(Player.class), any(Document.class))).thenReturn(null);
// Priority FOTMOB, FBREF
// FOTMOB failes, fallback to FBREF
// return FOTMOB successful
Set<DataSource> dataSources = new HashSet<>();
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FBREF, "url"));
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FOTMOB, "url"));
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNotNull(result);
assertEquals(DataSourceSiteName.FBREF, result.getDataSourceSiteName());
}

@Test
void testParsePlayerMatchData_NoDataSource_Unsuccessful() {
// Priority FOTMOB, FBREF
// return null
Set<DataSource> dataSources = new HashSet<>();
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNull(result);
}

@Test
void testParsePlayerMatchData_MultipleDataSourcesAvailable_AllDataSourcesFail_Unsuccessful() {
when(fotmobDataSource.parsePlayerMatchData(any(Player.class), any(Document.class))).thenReturn(null);
when(fbrefDataSource.parsePlayerMatchData(any(Player.class), any(Document.class))).thenReturn(null);
// Priority FOTMOB, FBREF
// FOTMOB failes, fallback to FBREF also fails
// return null
Set<DataSource> dataSources = new HashSet<>();
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FBREF, "url"));
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FOTMOB, "url"));
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNull(result);
}

@Test
void testParsePlayerMatchData_ErrorFromJsoupConnect_AllDataSources_Unsuccessful() throws IOException {
Connection newConnection = mock(Connection.class);
when(Jsoup.connect(anyString())).thenReturn(newConnection);
when(newConnection.get()).thenThrow(new IOException("error"));

Set<DataSource> dataSources = new HashSet<>();
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FBREF, "FBREF url"));
dataSources.add(new DataSource(DataSourceType.PLAYER, DataSourceSiteName.FOTMOB, "FOTMOB url"));
Player testPlayer = new Player(0, "Test Player Name", null, new Date(), null, dataSources, new CheckedStatus(0, DataSourceSiteName.FBREF));

PlayerMatchPerformanceStats result = parsingService.parsePlayerMatchData(testPlayer);

assertNull(result);
}

}

0 comments on commit 27a3def

Please sign in to comment.