mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 21:20:45 +00:00
Merge pull request #76 from Kingsrook/feature/q-instance-validator-plugins
Add plugin support in QInstanceValidator, with first implementation b…
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2022. Kingsrook, LLC
|
||||
* Copyright (C) 2021-2024. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
@ -38,6 +38,7 @@ import com.kingsrook.qqq.backend.core.actions.processes.BackendStep;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QInstanceValidationException;
|
||||
import com.kingsrook.qqq.backend.core.instances.validation.plugins.AlwaysFailsProcessValidatorPlugin;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.processes.ProcessSummaryLineInterface;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.processes.RunBackendStepInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.processes.RunBackendStepOutput;
|
||||
@ -97,7 +98,7 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||
** Unit test for QInstanceValidator.
|
||||
**
|
||||
*******************************************************************************/
|
||||
class QInstanceValidatorTest extends BaseTest
|
||||
public class QInstanceValidatorTest extends BaseTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
@ -367,6 +368,35 @@ class QInstanceValidatorTest extends BaseTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_validatorPlugins()
|
||||
{
|
||||
try
|
||||
{
|
||||
QInstanceValidator.addValidatorPlugin(new AlwaysFailsProcessValidatorPlugin());
|
||||
|
||||
////////////////////////////////////////
|
||||
// make sure our always failer fails. //
|
||||
////////////////////////////////////////
|
||||
assertValidationFailureReasonsAllowingExtraReasons((qInstance) -> {
|
||||
}, "always fail");
|
||||
}
|
||||
finally
|
||||
{
|
||||
QInstanceValidator.removeAllValidatorPlugins();
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// make sure if remove all plugins, we don't fail //
|
||||
////////////////////////////////////////////////////
|
||||
assertValidationSuccess((qInstance) -> {});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Test that a table with no fields fails.
|
||||
**
|
||||
@ -1949,9 +1979,9 @@ class QInstanceValidatorTest extends BaseTest
|
||||
** failed validation with reasons that match the supplied vararg-reasons (but allow
|
||||
** more reasons - e.g., helpful when one thing we're testing causes other errors).
|
||||
*******************************************************************************/
|
||||
private void assertValidationFailureReasonsAllowingExtraReasons(Consumer<QInstance> setup, String... reasons)
|
||||
public static void assertValidationFailureReasonsAllowingExtraReasons(Consumer<QInstance> setup, String... expectedReasons)
|
||||
{
|
||||
assertValidationFailureReasons(setup, true, reasons);
|
||||
assertValidationFailureReasons(setup, true, expectedReasons);
|
||||
}
|
||||
|
||||
|
||||
@ -1961,9 +1991,9 @@ class QInstanceValidatorTest extends BaseTest
|
||||
** failed validation with reasons that match the supplied vararg-reasons (and
|
||||
** require that exact # of reasons).
|
||||
*******************************************************************************/
|
||||
private void assertValidationFailureReasons(Consumer<QInstance> setup, String... reasons)
|
||||
public static void assertValidationFailureReasons(Consumer<QInstance> setup, String... expectedReasons)
|
||||
{
|
||||
assertValidationFailureReasons(setup, false, reasons);
|
||||
assertValidationFailureReasons(setup, false, expectedReasons);
|
||||
}
|
||||
|
||||
|
||||
@ -1971,7 +2001,7 @@ class QInstanceValidatorTest extends BaseTest
|
||||
/*******************************************************************************
|
||||
** Implementation for the overloads of this name.
|
||||
*******************************************************************************/
|
||||
private void assertValidationFailureReasons(Consumer<QInstance> setup, boolean allowExtraReasons, String... reasons)
|
||||
public static void assertValidationFailureReasons(Consumer<QInstance> setup, boolean allowExtraReasons, String... expectedReasons)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -1982,17 +2012,27 @@ class QInstanceValidatorTest extends BaseTest
|
||||
}
|
||||
catch(QInstanceValidationException e)
|
||||
{
|
||||
if(!allowExtraReasons)
|
||||
{
|
||||
int noOfReasons = e.getReasons() == null ? 0 : e.getReasons().size();
|
||||
assertEquals(reasons.length, noOfReasons, "Expected number of validation failure reasons.\nExpected reasons: " + String.join(",", reasons)
|
||||
+ "\nActual reasons: " + (noOfReasons > 0 ? String.join("\n", e.getReasons()) : "--"));
|
||||
}
|
||||
assertValidationFailureReasons(allowExtraReasons, e.getReasons(), expectedReasons);
|
||||
}
|
||||
}
|
||||
|
||||
for(String reason : reasons)
|
||||
{
|
||||
assertReason(reason, e);
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public static void assertValidationFailureReasons(boolean allowExtraReasons, List<String> actualReasons, String... expectedReasons)
|
||||
{
|
||||
if(!allowExtraReasons)
|
||||
{
|
||||
int noOfReasons = actualReasons == null ? 0 : actualReasons.size();
|
||||
assertEquals(expectedReasons.length, noOfReasons, "Expected number of validation failure reasons.\nExpected reasons: " + String.join(",", expectedReasons)
|
||||
+ "\nActual reasons: " + (noOfReasons > 0 ? String.join("\n", actualReasons) : "--"));
|
||||
}
|
||||
|
||||
for(String reason : expectedReasons)
|
||||
{
|
||||
assertReason(reason, actualReasons);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2022,11 +2062,11 @@ class QInstanceValidatorTest extends BaseTest
|
||||
** the list of reasons in the QInstanceValidationException.
|
||||
**
|
||||
*******************************************************************************/
|
||||
private void assertReason(String reason, QInstanceValidationException e)
|
||||
public static void assertReason(String reason, List<String> actualReasons)
|
||||
{
|
||||
assertNotNull(e.getReasons(), "Expected there to be a reason for the failure (but there was not)");
|
||||
assertThat(e.getReasons())
|
||||
.withFailMessage("Expected any of:\n%s\nTo match: [%s]", e.getReasons(), reason)
|
||||
assertNotNull(actualReasons, "Expected there to be a reason for the failure (but there was not)");
|
||||
assertThat(actualReasons)
|
||||
.withFailMessage("Expected any of:\n%s\nTo match: [%s]", actualReasons, reason)
|
||||
.anyMatch(s -> s.contains(reason));
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2024. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qqq.backend.core.instances.validation.plugins;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.instances.QInstanceValidator;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.processes.QProcessMetaData;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** test instance of a validator plugin, that always adds an error
|
||||
*******************************************************************************/
|
||||
public class AlwaysFailsProcessValidatorPlugin implements QInstanceValidatorPluginInterface<QProcessMetaData>
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public void validate(QProcessMetaData object, QInstance qInstance, QInstanceValidator qInstanceValidator)
|
||||
{
|
||||
qInstanceValidator.getErrors().add("I always fail.");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2024. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qqq.backend.core.instances.validation.plugins;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.BaseTest;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.instances.QInstanceValidator;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.processes.implementations.basepull.BasepullConfiguration;
|
||||
import com.kingsrook.qqq.backend.core.processes.implementations.basepull.ExtractViaBasepullQueryStep;
|
||||
import com.kingsrook.qqq.backend.core.processes.implementations.etl.streamedwithfrontend.ExtractViaQueryStep;
|
||||
import com.kingsrook.qqq.backend.core.processes.implementations.etl.streamedwithfrontend.LoadViaInsertStep;
|
||||
import com.kingsrook.qqq.backend.core.processes.implementations.etl.streamedwithfrontend.StreamedETLWithFrontendProcess;
|
||||
import com.kingsrook.qqq.backend.core.processes.implementations.etl.streamedwithfrontend.StreamedETLWithFrontendProcessTest;
|
||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static com.kingsrook.qqq.backend.core.instances.QInstanceValidatorTest.assertValidationFailureReasons;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Unit test for BasepullExtractStepValidator
|
||||
*******************************************************************************/
|
||||
class BasepullExtractStepValidatorTest extends BaseTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testNoExtractStepAtAllFails()
|
||||
{
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
QInstanceValidator validator = new QInstanceValidator();
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// turns out, our "basepullTestProcess" doesn't have an extract step, so that makes this condition fire. //
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
new BasepullExtractStepValidator().validate(qInstance.getProcess(TestUtils.PROCESS_NAME_BASEPULL), qInstance, validator);
|
||||
assertValidationFailureReasons(false, validator.getErrors(), "does not have a field with a default value that is a BasepullExtractStepInterface CodeReference");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testExtractViaQueryNotBasePull()
|
||||
{
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
QInstanceValidator validator = new QInstanceValidator();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// set up a streamed-etl process, but with an ExtractViaQueryStep instead of a basepull - it should fail! //
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
new BasepullExtractStepValidator().validate(StreamedETLWithFrontendProcess.defineProcessMetaData(
|
||||
TestUtils.TABLE_NAME_SHAPE,
|
||||
TestUtils.TABLE_NAME_PERSON_MEMORY,
|
||||
ExtractViaQueryStep.class,
|
||||
StreamedETLWithFrontendProcessTest.TestTransformShapeToPersonStep.class,
|
||||
LoadViaInsertStep.class).withBasepullConfiguration(new BasepullConfiguration()), qInstance, validator);
|
||||
assertValidationFailureReasons(false, validator.getErrors(), "does not have a field with a default value that is a BasepullExtractStepInterface CodeReference");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testExtractViaBasepullQueryPasses()
|
||||
{
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
QInstanceValidator validator = new QInstanceValidator();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// set up a streamed-etl process, with an ExtractViaBasepullQueryStep as expected - should pass //
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
new BasepullExtractStepValidator().validate(StreamedETLWithFrontendProcess.defineProcessMetaData(
|
||||
TestUtils.TABLE_NAME_SHAPE,
|
||||
TestUtils.TABLE_NAME_PERSON_MEMORY,
|
||||
ExtractViaBasepullQueryStep.class,
|
||||
StreamedETLWithFrontendProcessTest.TestTransformShapeToPersonStep.class,
|
||||
LoadViaInsertStep.class).withBasepullConfiguration(new BasepullConfiguration()), qInstance, validator);
|
||||
assertThat(validator.getErrors()).isNullOrEmpty();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user