Mocked List Delete

This commit is contained in:
Adam Ding 2020-11-30 17:25:40 -05:00
parent bf1b0e0a98
commit c5313f5753
3 changed files with 50 additions and 1 deletions

View File

@ -63,6 +63,16 @@ public class TestListAdder {
Map<String, Object> ignore = new HashMap<>();
Map<String, Object> body = TestInputUtils.addBody(ignore);
body.put("name", "aname");
try {
Object rawIDReturn = listAdder.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID");
if (!(rawIDReturn.getClass() == Integer.class)) {
assert false;
return;
}
} catch(SQLException throwables) {
assert shouldThrow;
throwables.printStackTrace();
}
if(injector.getStatementString() == null) {
assert(false);
return;

View File

@ -1,4 +1,9 @@
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import static org.mockito.Mockito.*;
import java.security.AccessControlException;
import java.sql.SQLException;
@ -16,7 +21,7 @@ public class TestListDelete {
@Test
public void testListDeleterError() {
testListDeleterCore(true, true);
testListDeleterCoreMock(true, true);
}
public void testListDeleterCore(boolean shouldThrow, boolean hasAccess) {
@ -50,4 +55,38 @@ public class TestListDelete {
assert !hasAccess;
}
}
public void testListDeleterCoreMock(boolean shouldThrow, boolean hasAccess) {
StatementInjector injector;
ArrayList<Object> rsReturns = new ArrayList<>();
rsReturns.add("cognitoID");
try {
if (!hasAccess) {
rsReturns = null;
}
injector = new StatementInjector(null, rsReturns, shouldThrow);
} catch (SQLException throwables) {
throwables.printStackTrace();
assert false; //Error in test infrastructure
return;
}
ListDeleter testMock = Mockito.spy(new ListDeleter(injector, "cognitoID"));
Map<String, Object> body = (Map<String, Object>) TestBasicHandler.buildFullSampleMap().get("body");
HashMap<String, String> queryParams = (HashMap<String, String>) TestBasicHandler.buildFullSampleMap().get("body");
queryParams.put("id", "30");
try {
when(testMock.conductAction(body, queryParams, "cognitoID")).thenReturn(shouldThrow);
Object rawIDReturn = testMock.conductAction(body, queryParams, "cognitoID");
assert !shouldThrow;
assert (rawIDReturn == null);
assert (injector.getStatementString().equals("SELECT * FROM List WHERE (owner = ? AND listID = ?);DELETE FROM ListSharee where listID = ?;DELETE FROM ListProduct where listID = ?;DELETE FROM List WHERE listID = ?;[30]"));
} catch (SQLException throwables) {
assert shouldThrow ;
} catch (AccessControlException accessControlException) {
assert !hasAccess;
}
}
}