0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * The contents of this file are subject to the terms of the Common Development and
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * Distribution License (the License). You may not use this file except in compliance with the
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * specific language governing permission and limitations under the License.
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * When distributing Covered Software, include this CDDL Header Notice in each file and include
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * Header, with the fields enclosed by brackets [] replaced by your own identifying
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * information: "Portions copyright [year] [name of copyright owner]".
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste * Copyright 2015 ForgeRock AS.
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Joosteimport static org.assertj.core.api.Assertions.assertThat;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Joosteimport static org.forgerock.openam.scripting.ScriptConstants.ENGINE_CONFIGURATION;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Joosteimport static org.forgerock.openam.scripting.ScriptConstants.ScriptContext;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Joosteimport static org.forgerock.openam.scripting.ScriptConstants.ScriptContext.AUTHENTICATION_SERVER_SIDE;
ece5a262d20a50d0abf584d0f7ec73929ede9cfdJaco Joosteimport static org.forgerock.openam.scripting.ScriptConstants.ScriptContext.POLICY_CONDITION;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste private ScriptEngineConfigurator engineConfigurator;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste private ScriptEngineConfigurator mockEngineConfigurator;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste mockEngineConfigurator = mock(ScriptEngineConfigurator.class);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste engineConfigurator = new ScriptEngineConfigurator(logger);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste List<Pattern> result = engineConfigurator.compilePatternList(patterns);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste Set<String> patterns = Collections.singleton("");
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste List<Pattern> result = engineConfigurator.compilePatternList(patterns);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste Set<String> patterns = Collections.singleton("java.lang.*");
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste List<Pattern> result = engineConfigurator.compilePatternList(patterns);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("java.lang.Foo").matches()).isTrue();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("java.util.Foo").matches()).isFalse();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste Set<String> patterns = Collections.singleton("java.*.Foo*");
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste List<Pattern> result = engineConfigurator.compilePatternList(patterns);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("java.lang.FooBar").matches()).isTrue();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("java.foo.FooBar").matches()).isTrue();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("java.FooBar").matches()).isFalse();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("java.foo.BarFoo").matches()).isFalse();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste Set<String> patterns = Collections.singleton("java.lang.*");
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste List<Pattern> result = engineConfigurator.compilePatternList(patterns);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("javaXlangXFoo").matches()).isFalse();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("java.lang.Foo").matches()).isTrue();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste Set<String> patterns = Collections.singleton("foo[1-9]");
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste List<Pattern> result = engineConfigurator.compilePatternList(patterns);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("foo6").matches()).isFalse();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(result.get(0).matcher("foo[1-9]").matches()).isTrue();
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste public void shouldUpdateEngineWithAuthenticationServerSideConfig() {
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste ScriptContext context = AUTHENTICATION_SERVER_SIDE;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste String serviceComponent = "/" + context.name() + "/" + ENGINE_CONFIGURATION;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste .globalConfigChanged(anyString(), anyString(), anyString(), anyString(), anyInt());
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste mockEngineConfigurator.globalConfigChanged(ScriptConstants.SERVICE_NAME, "", "", serviceComponent, 0);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste ArgumentCaptor<ScriptContext> resourceCaptor = ArgumentCaptor.forClass(ScriptContext.class);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste verify(mockEngineConfigurator, times(1)).updateConfig(resourceCaptor.capture());
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(resourceCaptor.getValue()).isEqualTo(context);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste public void shouldUpdateEngineWithAuthorisationEntitlementConditionConfig() {
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste String serviceComponent = "/" + context.name() + "/" + ENGINE_CONFIGURATION;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste .globalConfigChanged(anyString(), anyString(), anyString(), anyString(), anyInt());
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste mockEngineConfigurator.globalConfigChanged(ScriptConstants.SERVICE_NAME, "", "", serviceComponent, 0);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste ArgumentCaptor<ScriptContext> contextCaptor = ArgumentCaptor.forClass(ScriptContext.class);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste verify(mockEngineConfigurator, times(1)).updateConfig(contextCaptor.capture());
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(contextCaptor.getValue()).isEqualTo(context);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste public void shouldNotUpdateEngineWhenEngineConfigDidNotChange() {
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste String serviceComponent = "/" + context.name() + "/SomeOtherConfig";
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste .globalConfigChanged(anyString(), anyString(), anyString(), anyString(), anyInt());
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste mockEngineConfigurator.globalConfigChanged(ScriptConstants.SERVICE_NAME, "", "", serviceComponent, 0);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste verify(mockEngineConfigurator, times(0)).updateConfig(any(ScriptContext.class));
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste public void shouldLogErrorWhenContextNotFound() {
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste String serviceComponent = "/NO_SUCH_CONTEXT/" + ENGINE_CONFIGURATION;
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste engineConfigurator.globalConfigChanged(ScriptConstants.SERVICE_NAME, "", "", serviceComponent, 0);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste verify(logger, times(1)).error(stringCaptor.capture(), any(IllegalArgumentException.class));
0a915e9189b4078b8b3763451da6dc5d9b7edbdeJaco Jooste assertThat(stringCaptor.getValue()).isEqualTo("Script Context does not exist: NO_SUCH_CONTEXT");