Skip to content

Commit

Permalink
Modify unit tests to support GraalJs execution
Browse files Browse the repository at this point in the history
* Add config to switch between script engines in unit tests
* Set Wrapper Factory based on the current script engine
* Add new testng test suite got graal js based script execution related tests
* Temporarily comment out GraphBasedSequenceHandlerClaimMappingsTest until fixed
  • Loading branch information
shanggeeth committed Feb 1, 2024
1 parent 6372b93 commit 7065b22
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public Object[][] filterOptionsDataProvider() {
{multipleOptionConfig, duplicateStepConfig(stepWithMultipleOptions), 3},
{multipleAndInvalidOptionConfig, duplicateStepConfig(stepWithMultipleOptions), 2},
{singleInvalidOptionConfig, duplicateStepConfig(stepWithMultipleOptions), 4},
{idpOnlyOptionConfig, duplicateStepConfig(stepWithMultipleOptions), 2},};
{idpOnlyOptionConfig, duplicateStepConfig(stepWithMultipleOptions), 2}, };
}

private StepConfig duplicateStepConfig(StepConfig stepConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,30 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest;
import org.wso2.carbon.identity.application.authentication.framework.MockAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.config.builder.FileBasedConfigurationBuilder;
import org.wso2.carbon.identity.application.authentication.framework.config.loader.UIBasedConfigurationLoader;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JSExecutionSupervisor;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsBaseGraphBuilderFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsFunctionRegistryImpl;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsWrapperFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsWrapperFactoryProvider;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.graaljs.JsGraalGraphBuilderFactory;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.graaljs.JsGraalWrapperFactory;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.handler.SubjectCallback;
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.common.model.ClaimMapping;
import org.wso2.carbon.idp.mgt.IdentityProviderManager;
import org.wso2.carbon.idp.mgt.dao.CacheBackedIdPMgtDAO;
Expand All @@ -55,6 +66,7 @@
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mock;

public class GraphBasedSequenceHandlerAbstractTest extends AbstractFrameworkTest {
Expand All @@ -68,13 +80,42 @@ public class GraphBasedSequenceHandlerAbstractTest extends AbstractFrameworkTest
protected static final String TEST_USER_1_ID = "4b4414e1-916b-4475-aaee-6b0751c29ff6";
protected GraphBasedSequenceHandler graphBasedSequenceHandler = new GraphBasedSequenceHandler();
protected UIBasedConfigurationLoader configurationLoader;
protected JsGraphBuilderFactory graphBuilderFactory;
protected JsBaseGraphBuilderFactory graphBuilderFactory;

@BeforeTest
public void setUpExecutionSupervisor() {

initMocks(this);
JSExecutionSupervisor jsExecutionSupervisor = new JSExecutionSupervisor(1, 500000L);
FrameworkServiceDataHolder.getInstance().setJsExecutionSupervisor(jsExecutionSupervisor);
}

@AfterTest
public void tearDownExecutionSupervisor() {

FrameworkServiceDataHolder.getInstance().getJsExecutionSupervisor().shutdown();
}

@BeforeClass
protected void setupSuite() {
@Parameters({"scriptEngine"})
protected void setupSuite(String scriptEngine) throws NoSuchFieldException, IllegalAccessException {

configurationLoader = new UIBasedConfigurationLoader();
graphBuilderFactory = new JsGraphBuilderFactory();
CarbonConstants.ENABLE_LEGACY_AUTHZ_RUNTIME = false;

if (scriptEngine.contentEquals(FrameworkConstants.JSAttributes.NASHORN)) {
graphBuilderFactory = new JsGraphBuilderFactory();
} else if (scriptEngine.contentEquals(FrameworkConstants.JSAttributes.GRAALJS)) {
graphBuilderFactory = new JsGraalGraphBuilderFactory();
}

Field wrapperFactory = JsWrapperFactoryProvider.class.getDeclaredField("jsWrapperBaseFactory");
wrapperFactory.setAccessible(true);
if (graphBuilderFactory instanceof JsGraphBuilderFactory) {
wrapperFactory.set(JsWrapperFactoryProvider.getInstance(), new JsWrapperFactory());
} else if (graphBuilderFactory instanceof JsGraalGraphBuilderFactory) {
wrapperFactory.set(JsWrapperFactoryProvider.getInstance(), new JsGraalWrapperFactory());
}

JsFunctionRegistryImpl jsFunctionRegistry = new JsFunctionRegistryImpl();
FrameworkServiceDataHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl;

import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.annotations.Test;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig;
Expand All @@ -29,23 +31,29 @@
import org.wso2.carbon.identity.common.testng.WithH2Database;
import org.wso2.carbon.identity.common.testng.WithRealmService;
import org.wso2.carbon.identity.common.testng.realm.UserStoreModel;
import org.wso2.carbon.identity.core.IdentityClaimManager;
import org.wso2.carbon.identity.core.internal.IdentityCoreServiceDataHolder;
import org.wso2.carbon.user.core.claim.Claim;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import java.util.Collections;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.mockito.Mockito.mock;
import static org.mockito.ArgumentMatchers.any;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.testng.Assert.assertEquals;

/**
* Tests the claim handling in the Javascript.
*/
@Test
@PowerMockIgnore({"javax.xml.*", "org.mockito.*", "org.apache.*", "org.w3c.*", })
@PrepareForTest({IdentityClaimManager.class})
@WithH2Database(jndiName = "jdbc/WSO2IdentityDB", files = {"dbScripts/h2.sql"})
@WithCarbonHome
@WithRealmService(injectToSingletons =
Expand All @@ -72,7 +80,21 @@ public void testHandleClaimHandling() throws Exception {

HttpServletResponse resp = mock(HttpServletResponse.class);

UserCoreUtil.setDomainInThreadLocal("test_domain");
Claim[] supportedClaims = new Claim[3];
Claim firstNameClaim = new Claim();
firstNameClaim.setClaimUri("http://wso2.org/claims/givenname");
supportedClaims[0] = firstNameClaim;
Claim lastNameClaim = new Claim();
lastNameClaim.setClaimUri("http://wso2.org/claims/lastname");
supportedClaims[1] = lastNameClaim;
Claim displayNameClaim = new Claim();
displayNameClaim.setClaimUri("http://wso2.org/claims/displayName");
supportedClaims[2] = displayNameClaim;

mockStatic(IdentityClaimManager.class);
IdentityClaimManager identityClaimManager = mock(IdentityClaimManager.class);
when(IdentityClaimManager.getInstance()).thenReturn(identityClaimManager);
when(identityClaimManager.getAllSupportedClaims(any())).thenReturn(supportedClaims);

RealmService currentRealmService = FrameworkServiceDataHolder.getInstance().getRealmService();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.wso2.carbon.identity.application.authentication.framework.MockAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsFunctionRegistryImpl;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.nashorn.JsNashornAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.base.JsBaseAuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException;
Expand Down Expand Up @@ -72,17 +72,17 @@
@WithRegistry(injectToSingletons = {FrameworkServiceDataHolder.class})
public class GraphBasedSequenceHandlerCustomFunctionsTest extends GraphBasedSequenceHandlerAbstractTest {

public static String customFunction1(JsNashornAuthenticationContext context) {
public static String customFunction1(JsBaseAuthenticationContext context) {

return "testResult1";
}

public static Boolean customBoolean(JsNashornAuthenticationContext context) {
public static Boolean customBoolean(JsBaseAuthenticationContext context) {

return true;
}

public static Boolean customBoolean2(JsNashornAuthenticationContext context, String value) {
public static Boolean customBoolean2(JsBaseAuthenticationContext context, String value) {

return true;
}
Expand All @@ -94,7 +94,7 @@ public void testHandleDynamicJavascript1() throws Exception {
JsFunctionRegistryImpl jsFunctionRegistrar = new JsFunctionRegistryImpl();
FrameworkServiceDataHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistrar);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "fn1",
(Function<JsNashornAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
(Function<JsBaseAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
::customFunction1);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "fn2", new CustomFunctionImpl2());

Expand All @@ -117,16 +117,13 @@ public void testHandleDynamicBoolean() throws Exception {
JsFunctionRegistry jsFunctionRegistrar = new JsFunctionRegistryImpl();
FrameworkServiceDataHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistrar);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "fn1",
(Function<JsNashornAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
(Function<JsBaseAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
::customFunction1);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction",
(Function<JsNashornAuthenticationContext, Boolean>) GraphBasedSequenceHandlerCustomFunctionsTest
(Function<JsBaseAuthenticationContext, Boolean>) GraphBasedSequenceHandlerCustomFunctionsTest
::customBoolean);

jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction2",
(BiFunction<JsNashornAuthenticationContext, String, Boolean>)
GraphBasedSequenceHandlerCustomFunctionsTest
::customBoolean2);
CustomBoolean2Impl customBoolean2 = new CustomBoolean2Impl();
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction2", customBoolean2);

ServiceProvider sp1 = getTestServiceProvider("js-sp-dynamic-1.xml");

Expand Down Expand Up @@ -169,14 +166,14 @@ public void testHandleDynamicOnFail() throws Exception {
JsFunctionRegistryImpl jsFunctionRegistrar = new JsFunctionRegistryImpl();
FrameworkServiceDataHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistrar);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "fn1",
(Function<JsNashornAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
(Function<JsBaseAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
::customFunction1);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction",
(Function<JsNashornAuthenticationContext, Boolean>) GraphBasedSequenceHandlerCustomFunctionsTest
(Function<JsBaseAuthenticationContext, Boolean>) GraphBasedSequenceHandlerCustomFunctionsTest
::customBoolean);

jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction2",
(BiFunction<JsNashornAuthenticationContext, String, Boolean>)
(BiFunction<JsBaseAuthenticationContext, String, Boolean>)
GraphBasedSequenceHandlerCustomFunctionsTest
::customBoolean2);

Expand All @@ -203,14 +200,14 @@ public void testHandleDynamicOnFallback() throws Exception {
JsFunctionRegistryImpl jsFunctionRegistrar = new JsFunctionRegistryImpl();
FrameworkServiceDataHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistrar);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "fn1",
(Function<JsNashornAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
(Function<JsBaseAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
::customFunction1);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction",
(Function<JsNashornAuthenticationContext, Boolean>) GraphBasedSequenceHandlerCustomFunctionsTest
(Function<JsBaseAuthenticationContext, Boolean>) GraphBasedSequenceHandlerCustomFunctionsTest
::customBoolean);

jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "getTrueFunction2",
(BiFunction<JsNashornAuthenticationContext, String, Boolean>)
(BiFunction<JsBaseAuthenticationContext, String, Boolean>)
GraphBasedSequenceHandlerCustomFunctionsTest
::customBoolean2);

Expand All @@ -235,7 +232,7 @@ public void testHandleDynamicJavascriptSerialization() throws Exception {
JsFunctionRegistry jsFunctionRegistrar = new JsFunctionRegistryImpl();
FrameworkServiceDataHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistrar);
jsFunctionRegistrar.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "fn1",
(Function<JsNashornAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
(Function<JsBaseAuthenticationContext, String>) GraphBasedSequenceHandlerCustomFunctionsTest
::customFunction1);

ServiceProvider sp1 = getTestServiceProvider("js-sp-dynamic-1.xml");
Expand Down Expand Up @@ -278,6 +275,7 @@ private AuthenticationContext processSequenceWithAcr(String[] acrArray)
return processAndGetAuthenticationContext(acrArray, sp1);
}

//
private AuthenticationContext processAndGetAuthenticationContext(String[] acrArray, ServiceProvider sp1)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, FrameworkException {

Expand Down Expand Up @@ -330,10 +328,16 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
}).when(request).getAttribute(Mockito.anyString());
}

@FunctionalInterface
public interface CustomBoolean2Interface extends Serializable {

boolean getTrueFunction2(JsBaseAuthenticationContext context, String param1);
}

@FunctionalInterface
public interface CustomFunctionInterface2 extends Serializable {

String customFunction2(JsNashornAuthenticationContext context, String param1, String param2);
String customFunction2(JsBaseAuthenticationContext context, String param1, String param2);
}

public static class MockFailingAuthenticator extends MockAuthenticator {
Expand Down Expand Up @@ -370,9 +374,17 @@ public AuthenticatorFlowStatus process(HttpServletRequest request, HttpServletRe

public class CustomFunctionImpl2 implements CustomFunctionInterface2 {

public String customFunction2(JsNashornAuthenticationContext context, String param1, String param2) {
public String customFunction2(JsBaseAuthenticationContext context, String param1, String param2) {

return "testResult2";
}
}

public class CustomBoolean2Impl implements CustomBoolean2Interface {

public boolean getTrueFunction2(JsBaseAuthenticationContext context, String param1) {

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry;
import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsFunctionRegistryImpl;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsNashornGraphBuilder;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilder;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.dao.impl.CacheBackedLongWaitStatusDAO;
import org.wso2.carbon.identity.application.authentication.framework.dao.impl.LongWaitStatusDAOImpl;
Expand Down Expand Up @@ -104,7 +104,7 @@ public void publishEvent(String siddhiAppName, String inStreamName, String outSt
AsyncProcess asyncProcess = new AsyncProcess((ctx, r) -> {
r.accept(ctx, Collections.emptyMap(), "onSuccess");
});
JsNashornGraphBuilder.addLongWaitProcess(asyncProcess, eventHandlers);
JsGraphBuilder.addLongWaitProcess(asyncProcess, eventHandlers);
}
}
}
Loading

0 comments on commit 7065b22

Please sign in to comment.