#39 Added some tests
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package de.rwu.easydrop.api;
|
||||
|
||||
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 de.rwu.easydrop.api.client.AmazonProductDataSource;
|
||||
|
||||
public class AmazonProductDataSourceTest {
|
||||
|
||||
private AmazonProductDataSource dataSource;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
dataSource = new AmazonProductDataSource(
|
||||
"https://www.example.com/api",
|
||||
"my-api-key");
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
// Arrange
|
||||
String baseUrl = "https://www.example.com/api";
|
||||
String apiKey = "my-api-key";
|
||||
|
||||
// Assert
|
||||
try {
|
||||
Field baseUrlField = AmazonProductDataSource.class.getDeclaredField("baseUrl");
|
||||
baseUrlField.setAccessible(true);
|
||||
Assertions.assertEquals(baseUrl, baseUrlField.get(dataSource));
|
||||
|
||||
Field apiKeyField = AmazonProductDataSource.class.getDeclaredField("apiKey");
|
||||
apiKeyField.setAccessible(true);
|
||||
Assertions.assertEquals(apiKey, apiKeyField.get(dataSource));
|
||||
} catch (NoSuchFieldException e) {
|
||||
Assertions.fail();
|
||||
} catch (IllegalAccessException e) {
|
||||
Assertions.fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateApiUrl() throws MalformedURLException {
|
||||
// Test case 1
|
||||
String productId1 = "12345";
|
||||
URL expectedUrl1 = new URL(
|
||||
"https://www.example.com/api/products/2020-08-26/products/12345/offers?productRegion=DE&locale=de_DE");
|
||||
URL createdUrl1 = dataSource.createApiUrl(productId1);
|
||||
Assertions.assertEquals(expectedUrl1, createdUrl1);
|
||||
|
||||
// Test case 2
|
||||
String productId2 = "67890";
|
||||
URL expectedUrl2 = new URL(
|
||||
"https://www.example.com/api/products/2020-08-26/products/67890/offers?productRegion=DE&locale=de_DE");
|
||||
URL createdUrl2 = dataSource.createApiUrl(productId2);
|
||||
Assertions.assertEquals(expectedUrl2, createdUrl2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user