102 lines
3.7 KiB
Java
102 lines
3.7 KiB
Java
package de.rwu.easydrop.api.client;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
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;
|
|
|
|
class EbayItemDataSourceTest {
|
|
private EbayItemDataSource demoDataSource;
|
|
|
|
private static String demoApiKey = "my-api-key";
|
|
private static String demoApiUrl = "https://www.example.com/api";
|
|
private static String demoDataOrigin = "eBay";
|
|
private static String demoQuery = "iPhone";
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
demoDataSource = new EbayItemDataSource(demoApiUrl, demoApiKey);
|
|
MockitoAnnotations.openMocks(this);
|
|
}
|
|
|
|
@Test
|
|
void testConstructor() {
|
|
// Assert
|
|
try {
|
|
Field baseUrlField = EbayItemDataSource.class.getDeclaredField("baseUrl");
|
|
baseUrlField.setAccessible(true);
|
|
Assertions.assertEquals(demoApiUrl, baseUrlField.get(demoDataSource));
|
|
|
|
Field apiKeyField = EbayItemDataSource.class.getDeclaredField("apiKey");
|
|
apiKeyField.setAccessible(true);
|
|
Assertions.assertEquals(demoApiKey, apiKeyField.get(demoDataSource));
|
|
} catch (NoSuchFieldException e) {
|
|
Assertions.fail();
|
|
} catch (IllegalAccessException e) {
|
|
Assertions.fail();
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void createApiUrl_ValidSearchQuery_ReturnsValidUrl() throws MalformedURLException {
|
|
String searchQuery = demoQuery;
|
|
URL apiUrl = demoDataSource.createApiUrl(searchQuery);
|
|
|
|
assertNotNull(apiUrl);
|
|
assertEquals("https://www.example.com/api/buy/browse/v1/item_summary/search?q=iPhone&limit=1&offset=0",
|
|
apiUrl.toString());
|
|
}
|
|
|
|
@Test
|
|
void buildProductDTO_ValidJson_ReturnsValidProductDTO() {
|
|
ProductDTO product = new ProductDTO(demoQuery, demoDataOrigin);
|
|
String json = "{\"itemSummaries\":[{\"shippingOptions\":[{\"guaranteedDelivery\":true,\"shippingCost\":{\"value\":10}}],\"price\":{\"value\":999.99},\"seller\":{\"username\":\"seller123\"}}]}";
|
|
|
|
ProductDTO result = demoDataSource.buildProductDTO(product, json);
|
|
|
|
assertEquals(demoDataOrigin, result.getDataOrigin());
|
|
assertEquals(true, result.isAvailable());
|
|
assertEquals(999.99, result.getCurrentPrice());
|
|
assertEquals(10.0, result.getDeliveryPrice());
|
|
assertEquals("seller123", result.getMerchant());
|
|
}
|
|
|
|
@Test
|
|
void buildProductDTO_InvalidJson_ReturnsProductDTOWithDefaults() {
|
|
ProductDTO product = new ProductDTO(demoQuery, demoDataOrigin);
|
|
String json = "{\"itemSummaries\":[]}"; // Empty JSON to simulate missing data
|
|
|
|
ProductDTO result = demoDataSource.buildProductDTO(product, json);
|
|
|
|
assertEquals("eBay", result.getDataOrigin());
|
|
assertEquals(false, result.isAvailable()); // Default value for boolean
|
|
assertEquals(0.0, result.getCurrentPrice()); // Default value for double
|
|
assertEquals(0.0, result.getDeliveryPrice()); // Default value for double
|
|
assertEquals(null, result.getMerchant()); // Default value for String
|
|
}
|
|
|
|
@Test
|
|
void getDataOrigin_ReturnsExpectedDataOrigin() {
|
|
String dataOrigin = demoDataSource.getDataOrigin();
|
|
assertEquals(demoDataOrigin, dataOrigin);
|
|
}
|
|
|
|
@Test
|
|
void getApiKey_ReturnsExpectedApiKey() {
|
|
String apiKey = demoDataSource.getApiKey();
|
|
assertEquals(demoApiKey, apiKey);
|
|
}
|
|
}
|