Implementation of QContext everywhere, instead of passing QInstance and QSession in all ActionInputs

This commit is contained in:
2023-01-15 19:30:57 -06:00
parent 69a6104393
commit d3fa1df56f
219 changed files with 1955 additions and 1581 deletions

View File

@ -89,7 +89,7 @@ public class BaseAPIActionUtil
{
private final QLogger LOG = QLogger.getLogger(BaseAPIActionUtil.class);
protected QSession session;
protected QSession session; // todo not commit - delete!!
protected APIBackendMetaData backendMetaData;
protected AbstractTableActionInput actionInput;
@ -116,7 +116,7 @@ public class BaseAPIActionUtil
}
catch(Exception e)
{
LOG.error(session, "Error in API count", e);
LOG.error("Error in API count", e);
throw new QException("Error executing count: " + e.getMessage(), e);
}
}
@ -149,7 +149,7 @@ public class BaseAPIActionUtil
}
catch(Exception e)
{
LOG.error(session, "Error in API get", e);
LOG.error("Error in API get", e);
throw new QException("Error executing get: " + e.getMessage(), e);
}
}
@ -166,7 +166,7 @@ public class BaseAPIActionUtil
if(CollectionUtils.nullSafeIsEmpty(insertInput.getRecords()))
{
LOG.debug(session, "Insert request called with 0 records. Returning with no-op");
LOG.debug("Insert request called with 0 records. Returning with no-op");
return (insertOutput);
}
@ -207,7 +207,7 @@ public class BaseAPIActionUtil
}
catch(Exception e)
{
LOG.error(session, "Error in API Insert for [" + table.getName() + "]", e);
LOG.error("Error in API Insert for [" + table.getName() + "]", e);
throw new QException("Error executing insert: " + e.getMessage(), e);
}
@ -265,7 +265,7 @@ public class BaseAPIActionUtil
///////////////////////////////////////////////////////////////////
if(queryInput.getAsyncJobCallback().wasCancelRequested())
{
LOG.info(session, "Breaking query job, as requested.");
LOG.info("Breaking query job, as requested.");
return (queryOutput);
}
@ -280,7 +280,7 @@ public class BaseAPIActionUtil
}
catch(Exception e)
{
LOG.error(session, "Error in API Query", e);
LOG.error("Error in API Query", e);
throw new QException("Error executing query: " + e.getMessage(), e);
}
}
@ -298,7 +298,7 @@ public class BaseAPIActionUtil
if(CollectionUtils.nullSafeIsEmpty(updateInput.getRecords()))
{
LOG.debug(session, "Update request called with 0 records. Returning with no-op");
LOG.debug("Update request called with 0 records. Returning with no-op");
return (updateOutput);
}
@ -325,7 +325,7 @@ public class BaseAPIActionUtil
catch(Exception e)
{
String errorMessage = "An unexpected error occurred updating entities.";
LOG.error(session, errorMessage, e);
LOG.error(errorMessage, e);
throw (new QException(errorMessage, e));
}
@ -343,7 +343,7 @@ public class BaseAPIActionUtil
}
catch(Exception e)
{
LOG.error(session, "Error in API Update for [" + table.getName() + "]", e);
LOG.error("Error in API Update for [" + table.getName() + "]", e);
throw new QException("Error executing update: " + e.getMessage(), e);
}
}
@ -483,7 +483,7 @@ public class BaseAPIActionUtil
int statusCode = response.getStatusCode();
String resultString = response.getContent();
String errorMessage = "HTTP " + request.getMethod() + " for table [" + table.getName() + "] failed with status " + statusCode + ": " + resultString;
LOG.error(session, errorMessage);
LOG.error(errorMessage);
if("GET".equals(request.getMethod()))
{
@ -605,7 +605,7 @@ public class BaseAPIActionUtil
String fullURL = backendMetaData.getBaseUrl() + "oauth/token";
String postBody = "grant_type=client_credentials&client_id=" + backendMetaData.getClientId() + "&client_secret=" + backendMetaData.getClientSecret();
LOG.info(session, "Fetching OAuth2 token from " + fullURL);
LOG.info("Fetching OAuth2 token from " + fullURL);
try(CloseableHttpClient client = HttpClients.custom().setConnectionManager(new PoolingHttpClientConnectionManager()).build())
{
@ -624,7 +624,7 @@ public class BaseAPIActionUtil
JSONObject resultJSON = new JSONObject(resultString);
accessToken = (resultJSON.getString("access_token"));
LOG.debug(session, "Fetched access token: " + accessToken);
LOG.debug("Fetched access token: " + accessToken);
///////////////////////////////////////////////////////////////////////////////////////////////////
// stash the access token in the backendMetaData, from which it will be used for future requests //
@ -638,7 +638,7 @@ public class BaseAPIActionUtil
catch(Exception e)
{
String errorMessage = "Error getting OAuth Token";
LOG.warn(session, errorMessage, e);
LOG.warn(errorMessage, e);
throw (new OAuthCredentialsException(errorMessage, e));
}
}
@ -853,10 +853,10 @@ public class BaseAPIActionUtil
setupContentTypeInRequest(request);
setupAdditionalHeaders(request);
LOG.info(session, "Making [" + request.getMethod() + "] request to URL [" + request.getURI() + "] on table [" + table.getName() + "].");
LOG.info("Making [" + request.getMethod() + "] request to URL [" + request.getURI() + "] on table [" + table.getName() + "].");
if("POST".equals(request.getMethod()))
{
LOG.info(session, "POST contents [" + ((HttpPost) request).getEntity().toString() + "]");
LOG.info("POST contents [" + ((HttpPost) request).getEntity().toString() + "]");
}
try(CloseableHttpResponse response = httpClient.execute(request))
@ -873,26 +873,26 @@ public class BaseAPIActionUtil
handleResponseError(table, request, qResponse);
}
LOG.info(session, "Received successful response with code [" + qResponse.getStatusCode() + "] and content [" + qResponse.getContent() + "].");
LOG.info("Received successful response with code [" + qResponse.getStatusCode() + "] and content [" + qResponse.getContent() + "].");
return (qResponse);
}
}
catch(OAuthCredentialsException oce)
{
LOG.error(session, "OAuth Credential failure for [" + table.getName() + "]");
LOG.error("OAuth Credential failure for [" + table.getName() + "]");
throw (oce);
}
catch(OAuthExpiredTokenException oete)
{
if(!caughtAnOAuthExpiredToken)
{
LOG.info(session, "OAuth Expired token for [" + table.getName() + "] - retrying");
LOG.info("OAuth Expired token for [" + table.getName() + "] - retrying");
backendMetaData.withCustomValue("accessToken", null);
caughtAnOAuthExpiredToken = true;
}
else
{
LOG.info(session, "OAuth Expired token for [" + table.getName() + "] even after a retry. Giving up.");
LOG.info("OAuth Expired token for [" + table.getName() + "] even after a retry. Giving up.");
throw (oete);
}
}
@ -901,11 +901,11 @@ public class BaseAPIActionUtil
rateLimitsCaught++;
if(rateLimitsCaught > getMaxAllowedRateLimitErrors())
{
LOG.error(session, "Giving up POST to [" + table.getName() + "] after too many rate-limit errors (" + getMaxAllowedRateLimitErrors() + ")");
LOG.error("Giving up POST to [" + table.getName() + "] after too many rate-limit errors (" + getMaxAllowedRateLimitErrors() + ")");
throw (new QException(rle));
}
LOG.warn(session, "Caught RateLimitException [#" + rateLimitsCaught + "] during HTTP request to [" + request.getURI() + "] on table [" + table.getName() + "] - sleeping [" + sleepMillis + "]...");
LOG.warn("Caught RateLimitException [#" + rateLimitsCaught + "] during HTTP request to [" + request.getURI() + "] on table [" + table.getName() + "] - sleeping [" + sleepMillis + "]...");
SleepUtils.sleep(sleepMillis, TimeUnit.MILLISECONDS);
sleepMillis *= 2;
}
@ -919,7 +919,7 @@ public class BaseAPIActionUtil
catch(Exception e)
{
String message = "An unknown error occurred trying to make an HTTP request to [" + request.getURI() + "] on table [" + table.getName() + "].";
LOG.error(session, message, e);
LOG.error(message, e);
throw (new QException(message, e));
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright © 2022-2023. Nutrifresh Services <contact@nutrifreshservices.com>. All Rights Reserved.
*/
package com.kingsrook.qqq.backend.module.api;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
/*******************************************************************************
**
*******************************************************************************/
public class BaseTest
{
private static final Logger LOG = LogManager.getLogger(BaseTest.class);
/*******************************************************************************
**
*******************************************************************************/
@BeforeEach
void baseBeforeEach()
{
QContext.init(TestUtils.defineInstance(), new QSession());
}
/*******************************************************************************
**
*******************************************************************************/
@AfterEach
void baseAfterEach()
{
QContext.clear();
}
/*******************************************************************************
**
*******************************************************************************/
protected static void reInitInstanceInContext(QInstance qInstance)
{
if(qInstance.equals(QContext.getQInstance()))
{
LOG.warn("Unexpected condition - the same qInstance that is already in the QContext was passed into reInit. You probably want a new QInstance object instance.");
}
QContext.init(qInstance, new QSession());
}
}

View File

@ -44,7 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
**
*******************************************************************************/
@DisabledOnOs(OS.LINUX)
public class EasyPostApiTest
public class EasyPostApiTest extends BaseTest
{
/*******************************************************************************
@ -67,8 +67,7 @@ public class EasyPostApiTest
.withValue("carrier", "USPS")
.withValue("trackingNo", "EZ4000000004");
InsertInput insertInput = new InsertInput(TestUtils.defineInstance());
insertInput.setSession(new QSession());
InsertInput insertInput = new InsertInput();
insertInput.setTableName("easypostTracker");
insertInput.setRecords(List.of(record));
InsertOutput insertOutput = new InsertAction().execute(insertInput);
@ -88,8 +87,7 @@ public class EasyPostApiTest
QRecord record1 = new QRecord().withValue("carrier", "USPS").withValue("trackingNo", "EZ1000000001");
QRecord record2 = new QRecord().withValue("carrier", "USPS").withValue("trackingNo", "EZ2000000002");
InsertInput insertInput = new InsertInput(TestUtils.defineInstance());
insertInput.setSession(new QSession());
InsertInput insertInput = new InsertInput();
insertInput.setTableName("easypostTracker");
insertInput.setRecords(List.of(record1, record2));
InsertOutput insertOutput = new InsertAction().execute(insertInput);
@ -109,8 +107,7 @@ public class EasyPostApiTest
@Test
void testPostTrackerEmptyInput() throws QException
{
InsertInput insertInput = new InsertInput(TestUtils.defineInstance());
insertInput.setSession(new QSession());
InsertInput insertInput = new InsertInput();
insertInput.setTableName("easypostTracker");
insertInput.setRecords(List.of());
new InsertAction().execute(insertInput);
@ -131,13 +128,13 @@ public class EasyPostApiTest
QInstance instance = TestUtils.defineInstance();
QBackendMetaData backend = instance.getBackend(TestUtils.EASYPOST_BACKEND_NAME);
((APIBackendMetaData) backend).setApiKey("not-valid");
reInitInstanceInContext(instance);
QRecord record = new QRecord()
.withValue("carrier", "USPS")
.withValue("trackingNo", "EZ1000000001");
InsertInput insertInput = new InsertInput(instance);
insertInput.setSession(new QSession());
InsertInput insertInput = new InsertInput();
insertInput.setTableName("easypostTracker");
insertInput.setRecords(List.of(record));
InsertOutput insertOutput = new InsertAction().execute(insertInput);
@ -159,8 +156,7 @@ public class EasyPostApiTest
.withValue("carrier", "USPS")
.withValue("trackingNo", "Not-Valid-Tracking-No");
InsertInput insertInput = new InsertInput(TestUtils.defineInstance());
insertInput.setSession(new QSession());
InsertInput insertInput = new InsertInput();
insertInput.setTableName("easypostTracker");
insertInput.setRecords(List.of(record));
InsertOutput insertOutput = new InsertAction().execute(insertInput);

View File

@ -26,12 +26,12 @@ import com.kingsrook.qqq.backend.core.instances.QMetaDataVariableInterpreter;
import com.kingsrook.qqq.backend.core.model.metadata.QAuthenticationType;
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.authentication.QAuthenticationMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeUsage;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.authentication.QAuthenticationMetaData;
import com.kingsrook.qqq.backend.module.api.model.AuthorizationType;
import com.kingsrook.qqq.backend.module.api.model.metadata.APIBackendMetaData;
import com.kingsrook.qqq.backend.module.api.model.metadata.APITableBackendDetails;

View File

@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.module.api.model.metadata;
import com.kingsrook.qqq.backend.core.instances.QInstanceValidator;
import com.kingsrook.qqq.backend.module.api.BaseTest;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -31,7 +32,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/*******************************************************************************
** Unit test for APIBackendMetaData
*******************************************************************************/
class APIBackendMetaDataTest
class APIBackendMetaDataTest extends BaseTest
{
/*******************************************************************************

View File

@ -22,6 +22,7 @@
package com.kingsrook.qqq.backend.module.api.utils;
import com.kingsrook.qqq.backend.module.api.BaseTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -29,7 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/*******************************************************************************
** Unit test for QueryStringBuilder
*******************************************************************************/
class QueryStringBuilderTest
class QueryStringBuilderTest extends BaseTest
{
/*******************************************************************************