Search in sources :

Example 1 with ScriptException

use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.

the class ScriptExceptionMappingHandlerTest method shouldTranslateMessageToAcceptLanguage.

@Test
public void shouldTranslateMessageToAcceptLanguage() throws Exception {
    // given
    final Context serverContext = getHttpServerContext("te");
    for (ScriptErrorCode errorCode : ScriptErrorCode.values()) {
        // when
        ResourceException re = mappingHandler.handleError(serverContext, null, new ScriptException(errorCode));
        // then
        assertNotNull(re);
    //            assertEquals("Test message", re.getMessage()); //TODO dont get this...
    }
}
Also used : Context(org.forgerock.services.context.Context) HttpContext(org.forgerock.json.resource.http.HttpContext) ClientContext(org.forgerock.services.context.ClientContext) ScriptException(org.forgerock.openam.scripting.ScriptException) ScriptErrorCode(org.forgerock.openam.scripting.ScriptConstants.ScriptErrorCode) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Example 2 with ScriptException

use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.

the class ScriptConfigurationServiceTest method shouldFailIfUuidDoesNotExistOnDelete.

@Test
public void shouldFailIfUuidDoesNotExistOnDelete() throws ScriptException {
    // given
    String uuid = "1234567890";
    when(dataStore.containsUuid(anyString())).thenReturn(false);
    // when
    try {
        service.delete(uuid);
        fail("shouldFailIfUuidDoesNotExistOnDelete");
    } catch (ScriptException e) {
        // then
        assertEquals(e.getScriptErrorCode(), SCRIPT_UUID_NOT_FOUND);
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 3 with ScriptException

use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.

the class ScriptResource method queryCollection.

@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler resultHandler) {
    final QueryFilter<JsonPointer> filter = request.getQueryFilter();
    try {
        final Set<ScriptConfiguration> configs;
        if (filter == null) {
            configs = serviceFactory.create(getContextSubject(context), getRealm(context)).getAll();
        } else {
            QueryFilter<String> stringQueryFilter = filter.accept(new QueryByStringFilterConverter(), null);
            configs = serviceFactory.create(getContextSubject(context), getRealm(context)).get(stringQueryFilter);
        }
        List<ResourceResponse> results = new ArrayList<>();
        for (ScriptConfiguration configuration : configs) {
            String id = configuration.getId();
            results.add(newResourceResponse(id, null, asJson(configuration)));
        }
        QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
        return QueryResponsePresentation.perform(resultHandler, request, results);
    } catch (ScriptException se) {
        return exceptionMappingHandler.handleError(context, request, se).asPromise();
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryByStringFilterConverter(org.forgerock.openam.rest.query.QueryByStringFilterConverter) ArrayList(java.util.ArrayList) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) JsonPointer(org.forgerock.json.JsonPointer)

Example 4 with ScriptException

use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.

the class ScriptConfigurationDataStoreTest method shouldFailIfAttemptingToDeleteDefaultScript.

@Test
public void shouldFailIfAttemptingToDeleteDefaultScript() throws Exception {
    // given
    String uuid = "1234567890";
    ServiceConfig defaultScriptSubConfig = mock(ServiceConfig.class);
    given(subGlobalConfig.getSubConfig(uuid)).willReturn(defaultScriptSubConfig);
    given(defaultScriptSubConfig.exists()).willReturn(true);
    // when
    try {
        dataStore.delete(uuid);
        fail("shouldFailIfAttemptingToDeleteDefaultScript");
    } catch (ScriptException e) {
        // then
        assertEquals(e.getScriptErrorCode(), RETRIEVE_FAILED);
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) ServiceConfig(com.sun.identity.sm.ServiceConfig) Test(org.testng.annotations.Test)

Example 5 with ScriptException

use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.

the class ScriptResource method actionCollection.

@Override
public Promise<ActionResponse, ResourceException> actionCollection(Context context, ActionRequest request) {
    if ("validate".equals(request.getAction())) {
        try {
            JsonValue json = request.getContent();
            SupportedScriptingLanguage language = getLanguageFromString(json.get(SCRIPT_LANGUAGE).asString());
            String script = json.get(SCRIPT_TEXT).asString();
            if (script == null) {
                throw new ScriptException(MISSING_SCRIPT);
            }
            List<ScriptError> scriptErrorList = scriptValidator.validateScript(new ScriptObject(EMPTY, decodeScript(script), language, null));
            if (scriptErrorList.isEmpty()) {
                return newResultPromise(newActionResponse(json(object(field("success", true)))));
            }
            Set<Object> errors = new HashSet<>();
            for (ScriptError error : scriptErrorList) {
                errors.add(object(field("line", error.getLineNumber()), field("column", error.getColumnNumber()), field("message", error.getMessage())));
            }
            return newResultPromise(newActionResponse(json(object(field("success", false), field("errors", errors)))));
        } catch (ScriptException se) {
            return exceptionMappingHandler.handleError(context, request, se).asPromise();
        }
    } else {
        return new NotSupportedException().asPromise();
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) ScriptObject(org.forgerock.openam.scripting.ScriptObject) ScriptError(org.forgerock.openam.scripting.ScriptError) JsonValue(org.forgerock.json.JsonValue) ScriptObject(org.forgerock.openam.scripting.ScriptObject) SupportedScriptingLanguage(org.forgerock.openam.scripting.SupportedScriptingLanguage) NotSupportedException(org.forgerock.json.resource.NotSupportedException) HashSet(java.util.HashSet)

Aggregations

ScriptException (org.forgerock.openam.scripting.ScriptException)14 Test (org.testng.annotations.Test)8 ScriptConfiguration (org.forgerock.openam.scripting.service.ScriptConfiguration)7 ScriptObject (org.forgerock.openam.scripting.ScriptObject)4 HashMap (java.util.HashMap)3 Bindings (javax.script.Bindings)3 Subject (javax.security.auth.Subject)3 ResourceException (org.forgerock.json.resource.ResourceException)3 SSOException (com.iplanet.sso.SSOException)2 SSOToken (com.iplanet.sso.SSOToken)2 AuthSPrincipal (com.sun.identity.rest.AuthSPrincipal)2 Set (java.util.Set)2 SimpleBindings (javax.script.SimpleBindings)2 JsonValue (org.forgerock.json.JsonValue)2 NotSupportedException (org.forgerock.json.resource.NotSupportedException)2 HttpContext (org.forgerock.json.resource.http.HttpContext)2 ScriptErrorCode (org.forgerock.openam.scripting.ScriptConstants.ScriptErrorCode)2 Context (org.forgerock.services.context.Context)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ConditionDecision (com.sun.identity.entitlement.ConditionDecision)1