From fab2f75e98c540f61825f8cfcd2bcd0b04237251 Mon Sep 17 00:00:00 2001 From: SMan Date: Wed, 5 Jan 2022 19:56:38 -0600 Subject: [PATCH] fix: data_provider, multiplying redundant calls of u test functions, listener has been added; --- .../io/jans/as/server/ConfigurableTest.java | 71 ++++++++++++++++++- .../as/server/JansUnitTestsTestsListener.java | 64 +++++++++++++++++ .../as/server/comp/CryptoProviderTest.java | 15 ++-- server/src/test/resources/testng.xml | 4 ++ 4 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 server/src/test/java/io/jans/as/server/JansUnitTestsTestsListener.java diff --git a/server/src/test/java/io/jans/as/server/ConfigurableTest.java b/server/src/test/java/io/jans/as/server/ConfigurableTest.java index ba41264ce7b..fc56bada6e3 100644 --- a/server/src/test/java/io/jans/as/server/ConfigurableTest.java +++ b/server/src/test/java/io/jans/as/server/ConfigurableTest.java @@ -27,9 +27,11 @@ import java.util.Properties; /** - * Base class for all seam test which requre external configuration + * Base class for all seam test which require external configuration * - * @author Yuriy Movchan Date: 05/16/2016 + * @author Yuriy Movchan + * @author Sergey Manoylo + * @version December 29, 2021 */ @ArquillianSuiteDeployment public abstract class ConfigurableTest extends Arquillian { @@ -83,4 +85,69 @@ public void initTestSuite(ITestContext context) throws IOException { initialized = true; } + /** + * Data Provider, that returns correct arrays, which should be used, when JEE testing platform Arquillian is used. + * + * @author Sergey Manoylo + * @version December 29, 2021 + */ + public static class ArquillianDataProvider { + + private static final Map calls = new HashMap(); // contains map: data provider - number of call (counter value) of the function 'provide' + + /** + * Constructor. + * + * Private, so only public static functions should be called. + * + */ + private ArquillianDataProvider() { + } + + /** + * Returns Array of Data, that should be used by testing framework TestNG. + * + * This function returns two-dimensional array, when this function + * is called 1-st time for some defined provider name. + * + * This function returns row of the two-dimensional array, when this function + * is called 2-nd - N-st time. + * Number of the row of the two-dimensional array, == (number of call - 1). + * + * @param providerName Provider Name. + * @param providerData Data of the provider (two-dimensional array). + * @return current array (two-dimensional array or row). + */ + public synchronized static Object[][] provide(final String providerName, final Object[][] providerData) { + if (calls.containsKey(providerName)) { + // get instance and increase calls counter + Object[][] testCase = new Object[][] { providerData[calls.get(providerName)] }; + calls.put(providerName, (calls.get(providerName) + 1) % providerData.length); + return testCase; + } + else { + calls.put(providerName, 0); + return providerData; + } + } + + /** + * Clears counter of calls for all providers. + * + */ + public synchronized static void initCalls() { + calls.clear(); + } + + /** + * Clears counter of calls for some provider. + * + * @param providerName Provider Name. + */ + public synchronized static void initCalls(final String providerName) { + calls.put(providerName, 0); + } + + } + } diff --git a/server/src/test/java/io/jans/as/server/JansUnitTestsTestsListener.java b/server/src/test/java/io/jans/as/server/JansUnitTestsTestsListener.java new file mode 100644 index 00000000000..195e6e81e2b --- /dev/null +++ b/server/src/test/java/io/jans/as/server/JansUnitTestsTestsListener.java @@ -0,0 +1,64 @@ +/** + * + */ +package io.jans.as.server; + +import org.testng.ITestContext; +import org.testng.ITestListener; +import org.testng.ITestResult; +import org.testng.Reporter; + +import com.google.common.base.Throwables; + +/** + * @author Sergey Manoylo + * @version December 29, 2021 + */ +public class JansUnitTestsTestsListener implements ITestListener { + + @Override + public void onFinish(ITestContext context) { + } + + @Override + public void onStart(ITestContext context) { + } + + @Override + public void onTestFailedButWithinSuccessPercentage(ITestResult context) { + } + + @Override + public void onTestFailure(ITestResult result) { + Reporter.log("Test FAILED: " + result.getName() + "." + result.getMethod().getMethodName(), true); + Object[] parameters = result.getParameters(); + if(parameters != null) { + Reporter.log("Test Parameters: ", true); + for(Object parameter : parameters) { + Reporter.log("parameter = " + parameter, true); + } + } + Throwable throwable = result.getThrowable(); + if(throwable != null) { + Reporter.log("", true); + Reporter.log("Exception: ", true); + Reporter.log(Throwables.getStackTraceAsString(result.getThrowable()), true); + Reporter.log("", true); + } + } + + @Override + public void onTestSkipped(ITestResult result) { + } + + @Override + public void onTestStart(ITestResult result) { + Reporter.log("Test STARTED: " + result.getName() + "." + result.getMethod().getMethodName(), true); + } + + @Override + public void onTestSuccess(ITestResult result) { + Reporter.log("Test SUCCESS: " + result.getName() + "." + result.getMethod().getMethodName(), true); + Reporter.log("", true); + } +} diff --git a/server/src/test/java/io/jans/as/server/comp/CryptoProviderTest.java b/server/src/test/java/io/jans/as/server/comp/CryptoProviderTest.java index 11ded872cb0..0a52d0999a4 100644 --- a/server/src/test/java/io/jans/as/server/comp/CryptoProviderTest.java +++ b/server/src/test/java/io/jans/as/server/comp/CryptoProviderTest.java @@ -50,6 +50,12 @@ public class CryptoProviderTest extends ConfigurableTest { private final static String SIGNING_INPUT = "Signing Input"; private final static String SHARED_SECRET = "secret"; + private final static Object[][] DEF_EC_ALGS_DATA = new Object[][] { + { Algorithm.ES256 }, + { Algorithm.ES384 }, + { Algorithm.ES512 }, + }; + private static Long expirationTime; private static String hs256Signature; private static String hs384Signature; @@ -388,11 +394,10 @@ public void testDeleteKeyES512() { @DataProvider(name = "GenerateKeysDataProvider") public Object[][] testGenerateKeysDataProvider() { - return new Object[][] { - { Algorithm.ES256 }, - { Algorithm.ES384 }, - { Algorithm.ES512 }, - }; + return ConfigurableTest.ArquillianDataProvider.provide( + "CryptoProviderTest#GenerateKeysDataProvider", + DEF_EC_ALGS_DATA + ); } @Test(dependsOnMethods = {"testDeleteKeyRS256", "testDeleteKeyRS384", "testDeleteKeyRS512", diff --git a/server/src/test/resources/testng.xml b/server/src/test/resources/testng.xml index 9a40bdc8eb6..2a02293dbf6 100644 --- a/server/src/test/resources/testng.xml +++ b/server/src/test/resources/testng.xml @@ -2,6 +2,10 @@ + + + +