Scripts and apis and all kinds of stuff

This commit is contained in:
2023-04-28 15:46:36 -05:00
parent 0b7c2db452
commit d35f150202
5 changed files with 60 additions and 46 deletions

View File

@ -74,11 +74,12 @@ public class TestScriptProcessStep implements BackendStep
// get inputs // // get inputs //
//////////////// ////////////////
Integer scriptId = input.getValueInteger("scriptId"); Integer scriptId = input.getValueInteger("scriptId");
String code = input.getValueString("code");
ScriptRevision scriptRevision = new ScriptRevision(); ScriptRevision scriptRevision = new ScriptRevision();
scriptRevision.setScriptId(scriptId); scriptRevision.setScriptId(scriptId);
scriptRevision.setContents(code); scriptRevision.setContents(input.getValueString("code"));
scriptRevision.setApiName(input.getValueString("apiName"));
scriptRevision.setApiVersion(input.getValueString("apiVersion"));
BuildScriptLogAndScriptLogLineExecutionLogger executionLogger = new BuildScriptLogAndScriptLogLineExecutionLogger(null, null); BuildScriptLogAndScriptLogLineExecutionLogger executionLogger = new BuildScriptLogAndScriptLogLineExecutionLogger(null, null);

View File

@ -26,6 +26,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.kingsrook.qqq.backend.core.actions.tables.CountAction; import com.kingsrook.qqq.backend.core.actions.tables.CountAction;
import com.kingsrook.qqq.backend.core.actions.tables.GetAction; import com.kingsrook.qqq.backend.core.actions.tables.GetAction;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction; import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
@ -47,6 +48,7 @@ import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance; import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey; import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils; import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.SleepUtils;
import com.kingsrook.qqq.backend.module.api.BaseTest; import com.kingsrook.qqq.backend.module.api.BaseTest;
import com.kingsrook.qqq.backend.module.api.TestUtils; import com.kingsrook.qqq.backend.module.api.TestUtils;
import com.kingsrook.qqq.backend.module.api.exceptions.RateLimitException; import com.kingsrook.qqq.backend.module.api.exceptions.RateLimitException;
@ -524,9 +526,20 @@ class BaseAPIActionUtilTest extends BaseTest
InsertOutput insertOutput = new InsertAction().execute(insertInput); InsertOutput insertOutput = new InsertAction().execute(insertInput);
assertEquals(6, insertOutput.getRecords().get(0).getValueInteger("id")); assertEquals(6, insertOutput.getRecords().get(0).getValueInteger("id"));
QueryInput queryInput = new QueryInput(); //////////////////////////////////////////////////////////////////////////////////////////
queryInput.setTableName(OutboundAPILog.TABLE_NAME); // the outbound api log is inserted async, so... do or do not, and sleep some if needed //
QueryOutput apiLogRecords = new QueryAction().execute(queryInput); //////////////////////////////////////////////////////////////////////////////////////////
QueryOutput apiLogRecords = null;
int tries = 0;
do
{
SleepUtils.sleep(10, TimeUnit.MILLISECONDS);
QueryInput queryInput = new QueryInput();
queryInput.setTableName(OutboundAPILog.TABLE_NAME);
apiLogRecords = new QueryAction().execute(queryInput);
}
while(apiLogRecords.getRecords().isEmpty() && tries++ < 10);
assertEquals(1, apiLogRecords.getRecords().size()); assertEquals(1, apiLogRecords.getRecords().size());
assertEquals("POST", apiLogRecords.getRecords().get(0).getValueString("method")); assertEquals("POST", apiLogRecords.getRecords().get(0).getValueString("method"));
} }

View File

@ -33,10 +33,8 @@
<properties> <properties>
<!-- props specifically to this module --> <!-- props specifically to this module -->
<!-- none at this time -->
<!-- todo - remove these!! -->
<coverage.instructionCoveredRatioMinimum>0.10</coverage.instructionCoveredRatioMinimum>
<coverage.classCoveredRatioMinimum>0.10</coverage.classCoveredRatioMinimum>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -102,7 +102,7 @@ public class ApiImplementation
{ {
List<String> badRequestMessages = new ArrayList<>(); List<String> badRequestMessages = new ArrayList<>();
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.QUERY_BY_QUERY_STRING); QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.QUERY_BY_QUERY_STRING);
String tableName = table.getName(); String tableName = table.getName();
QueryInput queryInput = new QueryInput(); QueryInput queryInput = new QueryInput();
@ -326,7 +326,7 @@ public class ApiImplementation
*******************************************************************************/ *******************************************************************************/
public static Map<String, Serializable> insert(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String body) throws QException public static Map<String, Serializable> insert(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String body) throws QException
{ {
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.INSERT); QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.INSERT);
String tableName = table.getName(); String tableName = table.getName();
InsertInput insertInput = new InsertInput(); InsertInput insertInput = new InsertInput();
@ -392,7 +392,7 @@ public class ApiImplementation
*******************************************************************************/ *******************************************************************************/
public static List<Map<String, Serializable>> bulkInsert(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String body) throws QException public static List<Map<String, Serializable>> bulkInsert(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String body) throws QException
{ {
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_INSERT); QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_INSERT);
String tableName = table.getName(); String tableName = table.getName();
InsertInput insertInput = new InsertInput(); InsertInput insertInput = new InsertInput();
@ -481,7 +481,7 @@ public class ApiImplementation
*******************************************************************************/ *******************************************************************************/
public static Map<String, Serializable> get(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String primaryKey) throws QException public static Map<String, Serializable> get(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String primaryKey) throws QException
{ {
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.GET); QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.GET);
String tableName = table.getName(); String tableName = table.getName();
GetInput getInput = new GetInput(); GetInput getInput = new GetInput();
@ -516,7 +516,7 @@ public class ApiImplementation
*******************************************************************************/ *******************************************************************************/
public static void update(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String primaryKey, String body) throws QException public static void update(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String primaryKey, String body) throws QException
{ {
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.UPDATE); QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.UPDATE);
String tableName = table.getName(); String tableName = table.getName();
UpdateInput updateInput = new UpdateInput(); UpdateInput updateInput = new UpdateInput();
@ -586,7 +586,7 @@ public class ApiImplementation
*******************************************************************************/ *******************************************************************************/
public static List<Map<String, Serializable>> bulkUpdate(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String body) throws QException public static List<Map<String, Serializable>> bulkUpdate(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String body) throws QException
{ {
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_UPDATE); QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_UPDATE);
String tableName = table.getName(); String tableName = table.getName();
UpdateInput updateInput = new UpdateInput(); UpdateInput updateInput = new UpdateInput();
@ -698,7 +698,7 @@ public class ApiImplementation
*******************************************************************************/ *******************************************************************************/
public static void delete(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String primaryKey) throws QException public static void delete(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String primaryKey) throws QException
{ {
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.DELETE); QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.DELETE);
String tableName = table.getName(); String tableName = table.getName();
DeleteInput deleteInput = new DeleteInput(); DeleteInput deleteInput = new DeleteInput();
@ -732,7 +732,7 @@ public class ApiImplementation
*******************************************************************************/ *******************************************************************************/
public static List<Map<String, Serializable>> bulkDelete(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String body) throws QException public static List<Map<String, Serializable>> bulkDelete(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, String body) throws QException
{ {
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_DELETE); QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_DELETE);
String tableName = table.getName(); String tableName = table.getName();
DeleteInput deleteInput = new DeleteInput(); DeleteInput deleteInput = new DeleteInput();
@ -982,55 +982,53 @@ public class ApiImplementation
/******************************************************************************* /*******************************************************************************
** **
*******************************************************************************/ *******************************************************************************/
public static QTableMetaData validateTableAndVersion(String path, ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, ApiOperation operation) throws QNotFoundException public static QTableMetaData validateTableAndVersion(ApiInstanceMetaData apiInstanceMetaData, String version, String tableApiName, ApiOperation operation) throws QNotFoundException
{ {
QNotFoundException qNotFoundException = new QNotFoundException("Could not find any resources at path " + path);
QTableMetaData table = getTableByApiName(apiInstanceMetaData.getName(), version, tableApiName); QTableMetaData table = getTableByApiName(apiInstanceMetaData.getName(), version, tableApiName);
LogPair[] logPairs = new LogPair[] { logPair("apiName", apiInstanceMetaData.getName()), logPair("version", version), logPair("tableApiName", tableApiName), logPair("operation", operation) }; LogPair[] logPairs = new LogPair[] { logPair("apiName", apiInstanceMetaData.getName()), logPair("version", version), logPair("tableApiName", tableApiName), logPair("operation", operation) };
if(table == null) if(table == null)
{ {
LOG.info("404 because table is null (tableApiName=" + tableApiName + ")", logPairs); LOG.info("404 because table is null (tableApiName=" + tableApiName + ")", logPairs);
throw (qNotFoundException); throw (new QNotFoundException("Could not find a table named " + tableApiName + " in this api."));
} }
if(BooleanUtils.isTrue(table.getIsHidden())) if(BooleanUtils.isTrue(table.getIsHidden()))
{ {
LOG.info("404 because table isHidden", logPairs); LOG.info("404 because table isHidden", logPairs);
throw (qNotFoundException); throw (new QNotFoundException("Could not find a table named " + tableApiName + " in this api."));
} }
ApiTableMetaDataContainer apiTableMetaDataContainer = ApiTableMetaDataContainer.of(table); ApiTableMetaDataContainer apiTableMetaDataContainer = ApiTableMetaDataContainer.of(table);
if(apiTableMetaDataContainer == null) if(apiTableMetaDataContainer == null)
{ {
LOG.info("404 because table apiMetaDataContainer is null", logPairs); LOG.info("404 because table apiMetaDataContainer is null", logPairs);
throw (qNotFoundException); throw (new QNotFoundException("Could not find a table named " + tableApiName + " in this api."));
} }
ApiTableMetaData apiTableMetaData = apiTableMetaDataContainer.getApiTableMetaData(apiInstanceMetaData.getName()); ApiTableMetaData apiTableMetaData = apiTableMetaDataContainer.getApiTableMetaData(apiInstanceMetaData.getName());
if(apiTableMetaData == null) if(apiTableMetaData == null)
{ {
LOG.info("404 because table apiMetaData is null", logPairs); LOG.info("404 because table apiMetaData is null", logPairs);
throw (qNotFoundException); throw (new QNotFoundException("Could not find a table named " + tableApiName + " in this api."));
} }
if(BooleanUtils.isTrue(apiTableMetaData.getIsExcluded())) if(BooleanUtils.isTrue(apiTableMetaData.getIsExcluded()))
{ {
LOG.info("404 because table is excluded", logPairs); LOG.info("404 because table is excluded", logPairs);
throw (qNotFoundException); throw (new QNotFoundException("Could not find a table named " + tableApiName + " in this api."));
} }
if(!operation.isOperationEnabled(List.of(apiInstanceMetaData, apiTableMetaData))) if(!operation.isOperationEnabled(List.of(apiInstanceMetaData, apiTableMetaData)))
{ {
LOG.info("404 because api operation is not enabled", logPairs); LOG.info("404 because api operation is not enabled", logPairs);
throw (qNotFoundException); throw (new QNotFoundException("Cannot perform operation [" + operation + "] on table named " + tableApiName + " in this api."));
} }
if(!table.isCapabilityEnabled(QContext.getQInstance().getBackendForTable(table.getName()), operation.getCapability())) if(!table.isCapabilityEnabled(QContext.getQInstance().getBackendForTable(table.getName()), operation.getCapability()))
{ {
LOG.info("404 because table capability is not enabled", logPairs); LOG.info("404 because table capability is not enabled", logPairs);
throw (qNotFoundException); throw (new QNotFoundException("Cannot perform operation [" + operation + "] on table named " + tableApiName + " in this api."));
} }
APIVersion requestApiVersion = new APIVersion(version); APIVersion requestApiVersion = new APIVersion(version);
@ -1038,13 +1036,13 @@ public class ApiImplementation
if(CollectionUtils.nullSafeIsEmpty(supportedVersions) || !supportedVersions.contains(requestApiVersion)) if(CollectionUtils.nullSafeIsEmpty(supportedVersions) || !supportedVersions.contains(requestApiVersion))
{ {
LOG.info("404 because requested version is not supported", logPairs); LOG.info("404 because requested version is not supported", logPairs);
throw (qNotFoundException); throw (new QNotFoundException(version + " is not a supported version in this api."));
} }
if(!apiTableMetaData.getApiVersionRange().includes(requestApiVersion)) if(!apiTableMetaData.getApiVersionRange().includes(requestApiVersion))
{ {
LOG.info("404 because table version range does not include requested version", logPairs); LOG.info("404 because table version range does not include requested version", logPairs);
throw (qNotFoundException); throw (new QNotFoundException(version + " is not a supported version for table " + tableApiName + " in this api."));
} }
return (table); return (table);

View File

@ -147,25 +147,29 @@ class QJavalinApiHandlerTest extends BaseTest
@Test @Test
void testRandom404s() void testRandom404s()
{ {
for(String method : new String[] { "get", "post", "patch", "delete" }) assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("get", BASE_URL + "/api/" + VERSION + "/notATable/").asString());
{ assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find a table named notATable in this api", Unirest.request("get", BASE_URL + "/api/" + VERSION + "/notATable/notAnId").asString());
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/" + VERSION + "/notATable/").asString()); assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("get", BASE_URL + "/api/" + VERSION + "/person/1/2").asString());
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/" + VERSION + "/notATable/notAnId").asString()); assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("get", BASE_URL + "/api/foo").asString());
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/" + VERSION + "/person/1/2").asString()); assertErrorResponse(HttpStatus.OK_200, null, Unirest.request("get", BASE_URL + "/api/").asString()); // this path returns the doc site for a GET
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/foo").asString());
if(method.equals("get")) assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find a table named notATable in this api", Unirest.request("post", BASE_URL + "/api/" + VERSION + "/notATable/").asString());
{ assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("post", BASE_URL + "/api/" + VERSION + "/notATable/notAnId").asString());
////////////////////////////////////////////// assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("post", BASE_URL + "/api/" + VERSION + "/person/1/2").asString());
// this path returns the doc site for a GET // assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("post", BASE_URL + "/api/foo").asString());
////////////////////////////////////////////// assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("post", BASE_URL + "/api/").asString());
assertErrorResponse(HttpStatus.OK_200, null, Unirest.request(method, BASE_URL + "/api/").asString());
} assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("patch", BASE_URL + "/api/" + VERSION + "/notATable/").asString());
else assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find a table named notATable in this api", Unirest.request("patch", BASE_URL + "/api/" + VERSION + "/notATable/notAnId").asString());
{ assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("patch", BASE_URL + "/api/" + VERSION + "/person/1/2").asString());
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/").asString()); assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("patch", BASE_URL + "/api/foo").asString());
} assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("patch", BASE_URL + "/api/").asString());
}
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("delete", BASE_URL + "/api/" + VERSION + "/notATable/").asString());
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find a table named notATable in this api", Unirest.request("delete", BASE_URL + "/api/" + VERSION + "/notATable/notAnId").asString());
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("delete", BASE_URL + "/api/" + VERSION + "/person/1/2").asString());
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("delete", BASE_URL + "/api/foo").asString());
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("delete", BASE_URL + "/api/").asString());
} }