Search in sources :

Example 26 with Bindings

use of javax.script.Bindings in project OpenAM by OpenRock.

the class OidcClaimsExtensionTest method testProfileScope.

@Test
public void testProfileScope() throws Exception {
    // Given
    Bindings variables = testBindings(asSet("profile"));
    when(identity.getAttribute("givenname")).thenReturn(asSet("joe"));
    when(identity.getAttribute("sn")).thenReturn(asSet("bloggs"));
    when(identity.getAttribute("preferredtimezone")).thenReturn(asSet("Europe/London"));
    when(identity.getAttribute("preferredlocale")).thenReturn(asSet("en"));
    when(identity.getAttribute("cn")).thenReturn(asSet("Joe Bloggs"));
    // When
    UserInfoClaims result = scriptEvaluator.evaluateScript(script, variables);
    // Then
    assertThat(result.getValues()).containsOnly(entry("given_name", "joe"), entry("family_name", "bloggs"), entry("name", "Joe Bloggs"), entry("zoneinfo", "Europe/London"), entry("locale", "en"));
    assertThat(result.getCompositeScopes()).hasSize(1);
    ArrayList<String> hashProfile = (ArrayList<String>) result.getCompositeScopes().get("profile");
    assertThat(hashProfile).contains("zoneinfo", "name", "locale", "family_name", "given_name");
    assertThat(hashProfile).hasSize(5);
}
Also used : UserInfoClaims(org.forgerock.oauth2.core.UserInfoClaims) ArrayList(java.util.ArrayList) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.testng.annotations.Test)

Example 27 with Bindings

use of javax.script.Bindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldOverrideScriptVariablesWithParameterVariables.

@Test
public void shouldOverrideScriptVariablesWithParameterVariables() throws Exception {
    // Given
    String varName = "testVar";
    String paramValue = "param value";
    String scriptValue = "script value";
    Bindings scriptBindings = new SimpleBindings();
    scriptBindings.put(varName, scriptValue);
    ScriptObject script = getJavascript(scriptBindings, varName);
    Bindings paramBindings = new SimpleBindings();
    paramBindings.put(varName, paramValue);
    // When
    String result = testEvaluator.evaluateScript(script, paramBindings);
    // Then
    assertThat(result).isEqualTo(paramValue);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 28 with Bindings

use of javax.script.Bindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldExposeScriptVariables.

@Test
public void shouldExposeScriptVariables() throws Exception {
    // Given
    String varName = "scriptVar";
    String value = "a script value";
    Bindings bindings = new SimpleBindings();
    bindings.put(varName, value);
    ScriptObject script = getJavascript(bindings, varName);
    // When
    String result = testEvaluator.evaluateScript(script, null);
    // Then
    assertThat(result).isEqualTo(value);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 29 with Bindings

use of javax.script.Bindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldSupportJSONParsing.

@Test
public void shouldSupportJSONParsing() throws Exception {
    ScriptObject script = getJavascript("var json = JSON.parse(x)", "json['a']");
    Bindings scope = new SimpleBindings();
    scope.put("x", "{\"a\" : 12}");
    Object result = testEvaluator.evaluateScript(script, scope);
    assertThat(result).isEqualTo(12);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 30 with Bindings

use of javax.script.Bindings in project OpenAM by OpenRock.

the class StandardScriptEvaluatorTest method shouldPassBindingsByReference.

/**
     * Ensure that binding scopes are passed by reference to the script engine so that any changes made by the script
     * are reflected in the final state of the bindings passed in.
     */
@Test
public void shouldPassBindingsByReference() throws Exception {
    // Given
    String varName = "state";
    Bindings scope = new SimpleBindings();
    scope.put(varName, "initial");
    String expected = "expected";
    ScriptObject script = getJavascript(varName + " = '" + expected + "'");
    // When
    testEvaluator.evaluateScript(script, scope);
    // Then
    assertThat(scope.get(varName)).isEqualTo(expected);
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Aggregations

Bindings (javax.script.Bindings)142 SimpleBindings (javax.script.SimpleBindings)74 Test (org.junit.Test)36 ScriptException (javax.script.ScriptException)27 ScriptContext (javax.script.ScriptContext)24 SimpleScriptContext (javax.script.SimpleScriptContext)21 SlingBindings (org.apache.sling.api.scripting.SlingBindings)18 Test (org.testng.annotations.Test)17 ScriptEngine (javax.script.ScriptEngine)15 CompiledScript (javax.script.CompiledScript)14 Resource (org.apache.sling.api.resource.Resource)11 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)7 ArrayList (java.util.ArrayList)7 PrintWriter (java.io.PrintWriter)6 ScriptEngineManager (javax.script.ScriptEngineManager)6