From 849ce7825d28b206518c9e6c66691bc7e66a53eb Mon Sep 17 00:00:00 2001 From: Marvin Scham Date: Tue, 23 May 2023 08:36:05 +0200 Subject: [PATCH] #39 Added some tests --- .../api/AmazonProductDataSourceTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/test/java/de/rwu/easydrop/api/AmazonProductDataSourceTest.java diff --git a/src/test/java/de/rwu/easydrop/api/AmazonProductDataSourceTest.java b/src/test/java/de/rwu/easydrop/api/AmazonProductDataSourceTest.java new file mode 100644 index 0000000..ff472fe --- /dev/null +++ b/src/test/java/de/rwu/easydrop/api/AmazonProductDataSourceTest.java @@ -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); + } +}