mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-17 20:50:44 +00:00
Scripts and apis and all kinds of stuff
This commit is contained in:
@ -74,11 +74,12 @@ public class TestScriptProcessStep implements BackendStep
|
||||
// get inputs //
|
||||
////////////////
|
||||
Integer scriptId = input.getValueInteger("scriptId");
|
||||
String code = input.getValueString("code");
|
||||
|
||||
ScriptRevision scriptRevision = new ScriptRevision();
|
||||
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);
|
||||
|
||||
|
@ -26,6 +26,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
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.GetAction;
|
||||
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.tables.UniqueKey;
|
||||
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.TestUtils;
|
||||
import com.kingsrook.qqq.backend.module.api.exceptions.RateLimitException;
|
||||
@ -524,9 +526,20 @@ class BaseAPIActionUtilTest extends BaseTest
|
||||
InsertOutput insertOutput = new InsertAction().execute(insertInput);
|
||||
assertEquals(6, insertOutput.getRecords().get(0).getValueInteger("id"));
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
// the outbound api log is inserted async, so... do or do not, and sleep some if needed //
|
||||
//////////////////////////////////////////////////////////////////////////////////////////
|
||||
QueryOutput apiLogRecords = null;
|
||||
int tries = 0;
|
||||
do
|
||||
{
|
||||
SleepUtils.sleep(10, TimeUnit.MILLISECONDS);
|
||||
QueryInput queryInput = new QueryInput();
|
||||
queryInput.setTableName(OutboundAPILog.TABLE_NAME);
|
||||
QueryOutput apiLogRecords = new QueryAction().execute(queryInput);
|
||||
apiLogRecords = new QueryAction().execute(queryInput);
|
||||
}
|
||||
while(apiLogRecords.getRecords().isEmpty() && tries++ < 10);
|
||||
|
||||
assertEquals(1, apiLogRecords.getRecords().size());
|
||||
assertEquals("POST", apiLogRecords.getRecords().get(0).getValueString("method"));
|
||||
}
|
||||
|
@ -33,10 +33,8 @@
|
||||
|
||||
<properties>
|
||||
<!-- 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>
|
||||
|
||||
<dependencies>
|
||||
|
@ -102,7 +102,7 @@ public class ApiImplementation
|
||||
{
|
||||
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();
|
||||
|
||||
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
|
||||
{
|
||||
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.INSERT);
|
||||
QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.INSERT);
|
||||
String tableName = table.getName();
|
||||
|
||||
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
|
||||
{
|
||||
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_INSERT);
|
||||
QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_INSERT);
|
||||
String tableName = table.getName();
|
||||
|
||||
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
|
||||
{
|
||||
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.GET);
|
||||
QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.GET);
|
||||
String tableName = table.getName();
|
||||
|
||||
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
|
||||
{
|
||||
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.UPDATE);
|
||||
QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.UPDATE);
|
||||
String tableName = table.getName();
|
||||
|
||||
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
|
||||
{
|
||||
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_UPDATE);
|
||||
QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_UPDATE);
|
||||
String tableName = table.getName();
|
||||
|
||||
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
|
||||
{
|
||||
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.DELETE);
|
||||
QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.DELETE);
|
||||
String tableName = table.getName();
|
||||
|
||||
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
|
||||
{
|
||||
QTableMetaData table = validateTableAndVersion(tableApiName, apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_DELETE);
|
||||
QTableMetaData table = validateTableAndVersion(apiInstanceMetaData, version, tableApiName, ApiOperation.BULK_DELETE);
|
||||
String tableName = table.getName();
|
||||
|
||||
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);
|
||||
LogPair[] logPairs = new LogPair[] { logPair("apiName", apiInstanceMetaData.getName()), logPair("version", version), logPair("tableApiName", tableApiName), logPair("operation", operation) };
|
||||
|
||||
if(table == null)
|
||||
{
|
||||
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()))
|
||||
{
|
||||
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);
|
||||
if(apiTableMetaDataContainer == null)
|
||||
{
|
||||
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());
|
||||
if(apiTableMetaData == null)
|
||||
{
|
||||
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()))
|
||||
{
|
||||
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)))
|
||||
{
|
||||
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()))
|
||||
{
|
||||
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);
|
||||
@ -1038,13 +1036,13 @@ public class ApiImplementation
|
||||
if(CollectionUtils.nullSafeIsEmpty(supportedVersions) || !supportedVersions.contains(requestApiVersion))
|
||||
{
|
||||
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))
|
||||
{
|
||||
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);
|
||||
|
@ -147,25 +147,29 @@ class QJavalinApiHandlerTest extends BaseTest
|
||||
@Test
|
||||
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(method, BASE_URL + "/api/" + VERSION + "/notATable/").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(method, 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/foo").asString());
|
||||
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("get", BASE_URL + "/api/" + VERSION + "/person/1/2").asString());
|
||||
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("get", BASE_URL + "/api/foo").asString());
|
||||
assertErrorResponse(HttpStatus.OK_200, null, Unirest.request("get", BASE_URL + "/api/").asString()); // this path returns the doc site for a GET
|
||||
|
||||
if(method.equals("get"))
|
||||
{
|
||||
//////////////////////////////////////////////
|
||||
// this path returns the doc site for a GET //
|
||||
//////////////////////////////////////////////
|
||||
assertErrorResponse(HttpStatus.OK_200, null, Unirest.request(method, BASE_URL + "/api/").asString());
|
||||
}
|
||||
else
|
||||
{
|
||||
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 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());
|
||||
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.NOT_FOUND_404, "Could not find any resources at path", Unirest.request("patch", BASE_URL + "/api/" + VERSION + "/notATable/").asString());
|
||||
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("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());
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user