Skip to content

Commit

Permalink
fix: data_provider, multiplying redundant calls of u test functions, …
Browse files Browse the repository at this point in the history
…listener has been added;
  • Loading branch information
smansoft committed Jan 6, 2022
1 parent b999e8c commit fab2f75
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 7 deletions.
71 changes: 69 additions & 2 deletions server/src/test/java/io/jans/as/server/ConfigurableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String, Integer> calls = new HashMap<String, Integer>(); // 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);
}

}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions server/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<suite name="jansAuthServer" parallel="false">

<listeners>
<listener class-name="io.jans.as.server.JansUnitTestsTestsListener" />
</listeners>

<test name="Unit Tests" enabled="true">
<classes>
<class name="io.jans.as.server.service.MTLSServiceTest" />
Expand Down

0 comments on commit fab2f75

Please sign in to comment.