Listify/Lambdas/Lists/Item/test/TestItemGetter.java
NMerz 508818c700 Lambda unit test infrastructure
Unit test infrastructure for testing GET and POST Lambdas
2020-10-08 18:34:06 -04:00

56 lines
2.0 KiB
Java

import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class TestItemGetter {
@Test
public void testItemGetterValid() {
conductItemGetterTest(false);
}
@Test
public void testItemGetterError() {
conductItemGetterTest(true);
}
public void conductItemGetterTest(boolean shouldThrow) {
ArrayList<Object> rsReturns = new ArrayList<>();
rsReturns.add(1);//ProductID
rsReturns.add(2);//chainID
rsReturns.add("aupc");
rsReturns.add("adescription");
rsReturns.add(BigDecimal.valueOf(.03));//Price
rsReturns.add("animageurl");
rsReturns.add("adepartment");
rsReturns.add(Timestamp.from(Instant.ofEpochMilli(1602192528688L)));//retrievedDate
rsReturns.add(5); // fetchCounts
StatementInjector injector = null;
try {
injector = new StatementInjector(null, rsReturns, shouldThrow);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
ItemGetter getter = new ItemGetter(injector, "id");
Map<String, Object> ignore = new HashMap<>();
HashMap<String, String> queryParams = TestInputUtils.addQueryParams(ignore);
queryParams.put("id", "1");
try {
Object conductReturn = getter.conductAction(TestInputUtils.addBody(ignore), queryParams, "cognitoID");
assert !shouldThrow;
assert (conductReturn.getClass() == Item.class);
Item itemReturn = (Item) conductReturn;
assert (itemReturn.toString().equals("Item{productID=1, chainID=2, upc='aupc', description='adescription', price=0.03, imageURL='animageurl', department='adepartment', retrievedDate=1602192528688, fetchCounts=5}"));
} catch (SQLException throwables) {
throwables.printStackTrace();
assert shouldThrow;
}
}
}