#37 Added eBay data src, updated context + tests
This commit is contained in:
@@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.exception.DataSourceException;
|
||||
|
||||
class AmazonProductDataSourceTest {
|
||||
|
||||
@@ -158,7 +159,7 @@ class AmazonProductDataSourceTest {
|
||||
when(mockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_NOT_FOUND);
|
||||
|
||||
// Invoke the method and verify the exception
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
DataSourceException exception = assertThrows(DataSourceException.class, () -> {
|
||||
dataSource.getProductDTOById(demoProductId);
|
||||
});
|
||||
|
||||
@@ -181,7 +182,7 @@ class AmazonProductDataSourceTest {
|
||||
when(mockConnection.getInputStream()).thenThrow(new IOException());
|
||||
|
||||
// Invoke the method and verify the exception
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
DataSourceException exception = assertThrows(DataSourceException.class, () -> {
|
||||
dataSource.getProductDTOById(demoProductId);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package de.rwu.easydrop.api.client;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import de.rwu.easydrop.util.Config;
|
||||
|
||||
class DataSourceFactoryTest {
|
||||
@Mock
|
||||
private Config config;
|
||||
|
||||
private DataSourceFactory dataSourceFactory;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws ConfigurationException {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
when(config.getProperty("AMAZON_API_URL")).thenReturn("https://api.amazon.com");
|
||||
when(config.getProperty("AMAZON_API_KEY")).thenReturn("amazon-api-key");
|
||||
when(config.getProperty("EBAY_API_URL")).thenReturn("https://api.ebay.com");
|
||||
when(config.getProperty("EBAY_API_KEY")).thenReturn("ebay-api-key");
|
||||
dataSourceFactory = new DataSourceFactory(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAmazonProductDataSource_ReturnsAmazonProductDataSource() {
|
||||
// Act
|
||||
AmazonProductDataSource dataSource = dataSourceFactory.createAmazonProductDataSource();
|
||||
|
||||
// Assert
|
||||
assertEquals("amazon-api-key", dataSource.getApiKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEbayItemDataSource_ReturnsEbayItemDataSource() {
|
||||
// Act
|
||||
EbayItemDataSource dataSource = dataSourceFactory.createEbayItemDataSource();
|
||||
|
||||
// Assert
|
||||
assertEquals("ebay-api-key", dataSource.getApiKey());
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,13 @@ package de.rwu.easydrop.api.client;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
@@ -12,10 +17,8 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.jayway.jsonpath.ReadContext;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.exception.DataSourceException;
|
||||
|
||||
class EbayItemDataSourceTest {
|
||||
private EbayItemDataSource demoDataSource;
|
||||
@@ -51,8 +54,7 @@ class EbayItemDataSourceTest {
|
||||
|
||||
@Test
|
||||
void createApiUrl_ValidSearchQuery_ReturnsValidUrl() throws MalformedURLException {
|
||||
String searchQuery = demoQuery;
|
||||
URL apiUrl = demoDataSource.createApiUrl(searchQuery);
|
||||
URL apiUrl = demoDataSource.createApiUrl(demoQuery);
|
||||
|
||||
assertNotNull(apiUrl);
|
||||
assertEquals("https://www.example.com/api/buy/browse/v1/item_summary/search?q=iPhone&limit=1&offset=0",
|
||||
@@ -98,4 +100,26 @@ class EbayItemDataSourceTest {
|
||||
String apiKey = demoDataSource.getApiKey();
|
||||
assertEquals(demoApiKey, apiKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetProductDTOById_failedRequest() throws IOException {
|
||||
// Set up the test environment
|
||||
|
||||
EbayItemDataSource dataSource = mock(EbayItemDataSource.class);
|
||||
URL mockURL = mock(URL.class);
|
||||
when(dataSource.getDataOrigin()).thenReturn(demoDataOrigin);
|
||||
when(dataSource.createApiUrl(demoQuery)).thenReturn(mockURL);
|
||||
when(dataSource.getProductDTOById(demoQuery)).thenCallRealMethod();
|
||||
HttpURLConnection mockConnection = mock(HttpURLConnection.class);
|
||||
when(mockURL.openConnection()).thenReturn(mockConnection);
|
||||
when(mockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_NOT_FOUND);
|
||||
|
||||
// Invoke the method and verify the exception
|
||||
DataSourceException exception = assertThrows(DataSourceException.class, () -> {
|
||||
dataSource.getProductDTOById(demoQuery);
|
||||
});
|
||||
|
||||
// Verify the exception message
|
||||
assertEquals("Nothing found: eBay API responded with error code 404", exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package de.rwu.easydrop.exception;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DataSourceExceptionTest {
|
||||
|
||||
@Test
|
||||
void constructor_WithMessage_SetsMessage() {
|
||||
// Arrange
|
||||
String message = "Data source error";
|
||||
|
||||
// Act
|
||||
DataSourceException exception = new DataSourceException(message);
|
||||
|
||||
// Assert
|
||||
assertEquals(message, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithMessageAndCause_SetsMessageAndCause() {
|
||||
// Arrange
|
||||
String message = "Data source error";
|
||||
Throwable cause = new IllegalArgumentException("Invalid argument");
|
||||
|
||||
// Act
|
||||
DataSourceException exception = new DataSourceException(message, cause);
|
||||
|
||||
// Assert
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithNullMessage_SetsNullMessage() {
|
||||
// Act
|
||||
DataSourceException exception = new DataSourceException(null);
|
||||
|
||||
// Assert
|
||||
assertEquals(null, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithNullCause_SetsNullCause() {
|
||||
// Act
|
||||
DataSourceException exception = new DataSourceException("Data source error", null);
|
||||
|
||||
// Assert
|
||||
assertEquals(null, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void throw_DataSourceException() {
|
||||
// Act and Assert
|
||||
assertThrows(DataSourceException.class, () -> {
|
||||
throw new DataSourceException("Data source error");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package de.rwu.easydrop.exception;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class InvalidProductExceptionTest {
|
||||
|
||||
@Test
|
||||
void constructor_WithMessage_SetsMessage() {
|
||||
// Arrange
|
||||
String message = "Invalid product data";
|
||||
|
||||
// Act
|
||||
InvalidProductException exception = new InvalidProductException(message);
|
||||
|
||||
// Assert
|
||||
assertEquals(message, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithMessageAndCause_SetsMessageAndCause() {
|
||||
// Arrange
|
||||
String message = "Invalid product data";
|
||||
Throwable cause = new IllegalArgumentException("Invalid argument");
|
||||
|
||||
// Act
|
||||
InvalidProductException exception = new InvalidProductException(message, cause);
|
||||
|
||||
// Assert
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithNullMessage_SetsNullMessage() {
|
||||
// Act
|
||||
InvalidProductException exception = new InvalidProductException(null);
|
||||
|
||||
// Assert
|
||||
assertEquals(null, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithNullCause_SetsNullCause() {
|
||||
// Act
|
||||
InvalidProductException exception = new InvalidProductException("Invalid product data", null);
|
||||
|
||||
// Assert
|
||||
assertEquals(null, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void throw_InvalidProductException() {
|
||||
// Act and Assert
|
||||
assertThrows(InvalidProductException.class, () -> {
|
||||
throw new InvalidProductException("Invalid product data");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user