Search in sources :

Example 1 with ResourceProblemException

use of com.enonic.xp.resource.ResourceProblemException in project xp by enonic.

the class SendMailScriptTest method testFailMissingTo.

@Test
public void testFailMissingTo() throws Exception {
    final MailService mailService = message -> {
        throw new RuntimeException("Error sending mail");
    };
    addService(MailService.class, mailService);
    try {
        runFunction("/test/send-test.js", "sendWithoutRequiredTo");
        fail("Expected exception");
    } catch (ResourceProblemException e) {
        assertEquals("Parameter 'to' is required", e.getMessage());
    }
    assertNull(this.actualMessage);
}
Also used : Message(javax.mail.Message) Assertions.fail(org.junit.jupiter.api.Assertions.fail) MimeMultipart(javax.mail.internet.MimeMultipart) AddressException(javax.mail.internet.AddressException) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) MailService(com.enonic.xp.mail.MailService) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MimeMessage(javax.mail.internet.MimeMessage) InputStreamReader(java.io.InputStreamReader) MailMessage(com.enonic.xp.mail.MailMessage) StandardCharsets(java.nio.charset.StandardCharsets) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) Stream(java.util.stream.Stream) ScriptTestSupport(com.enonic.xp.testing.ScriptTestSupport) CharStreams(com.google.common.io.CharStreams) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Session(javax.mail.Session) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) InputStream(java.io.InputStream) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) MailService(com.enonic.xp.mail.MailService) Test(org.junit.jupiter.api.Test)

Example 2 with ResourceProblemException

use of com.enonic.xp.resource.ResourceProblemException in project xp by enonic.

the class SendMailScriptTest method testFailMissingFrom.

@Test
public void testFailMissingFrom() throws Exception {
    final MailService mailService = message -> {
        throw new RuntimeException("Error sending mail");
    };
    addService(MailService.class, mailService);
    try {
        runFunction("/test/send-test.js", "sendWithoutRequiredFrom");
        fail("Expected exception");
    } catch (ResourceProblemException e) {
        assertEquals("Parameter 'from' is required", e.getMessage());
    }
    assertNull(this.actualMessage);
}
Also used : Message(javax.mail.Message) Assertions.fail(org.junit.jupiter.api.Assertions.fail) MimeMultipart(javax.mail.internet.MimeMultipart) AddressException(javax.mail.internet.AddressException) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) MailService(com.enonic.xp.mail.MailService) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) MimeMessage(javax.mail.internet.MimeMessage) InputStreamReader(java.io.InputStreamReader) MailMessage(com.enonic.xp.mail.MailMessage) StandardCharsets(java.nio.charset.StandardCharsets) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) Stream(java.util.stream.Stream) ScriptTestSupport(com.enonic.xp.testing.ScriptTestSupport) CharStreams(com.google.common.io.CharStreams) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Session(javax.mail.Session) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) InputStream(java.io.InputStream) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) MailService(com.enonic.xp.mail.MailService) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceProblemException

use of com.enonic.xp.resource.ResourceProblemException in project xp by enonic.

the class ScriptRuntimeTest method testCompileError.

@Test
public void testCompileError() {
    final ResourceKey script = ResourceKey.from("myapplication:/error/error-test.js");
    try {
        runTestScript(script);
        fail("Should throw ResourceProblemException");
    } catch (final ResourceProblemException e) {
        assertEquals(1, e.getLineNumber());
        assertEquals(script, e.getResource());
    }
}
Also used : ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceProblemException

use of com.enonic.xp.resource.ResourceProblemException in project xp by enonic.

the class ScriptRuntimeTest method testRuntimeError.

@Test
public void testRuntimeError() {
    final ResourceKey script = ResourceKey.from("myapplication:/error/error-in-export-test.js");
    final ScriptExports exports = runTestScript(script);
    assertNotNull(exports);
    try {
        exports.executeMethod("hello");
        fail("Should throw ResourceProblemException");
    } catch (final ResourceProblemException e) {
        assertEquals(1, e.getLineNumber());
        assertEquals(ResourceKey.from("myapplication:/error/error-test.js"), e.getResource());
    }
}
Also used : ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 5 with ResourceProblemException

use of com.enonic.xp.resource.ResourceProblemException in project xp by enonic.

the class FilterNextFunctionWrapper method apply.

@Override
public Object apply(final Object scriptRequestObject) {
    if (functionWasCalled) {
        throw scriptError("Filter 'next' function was called multiple times", null);
    }
    functionWasCalled = true;
    ScriptValue scriptRequestParam = scriptService.toScriptValue(this.script, scriptRequestObject);
    try {
        final PortalRequest portalRequest = new PortalRequestSerializer(request, scriptRequestParam).serialize();
        final WebResponse newResponse = webHandlerChain.handle(portalRequest, response);
        final PortalResponseMapper response = new PortalResponseMapper((PortalResponse) newResponse);
        return scriptService.toNativeObject(this.script, response);
    } catch (ResourceProblemException | WebException e) {
        throw e;
    } catch (Exception e) {
        throw scriptError("Error executing filter script: " + script, e);
    }
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) WebResponse(com.enonic.xp.web.WebResponse) PortalResponseMapper(com.enonic.xp.portal.impl.mapper.PortalResponseMapper) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) WebException(com.enonic.xp.web.WebException) WebException(com.enonic.xp.web.WebException) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) PortalRequest(com.enonic.xp.portal.PortalRequest)

Aggregations

ResourceProblemException (com.enonic.xp.resource.ResourceProblemException)10 Test (org.junit.jupiter.api.Test)8 WebHandlerChain (com.enonic.xp.web.handler.WebHandlerChain)3 MailMessage (com.enonic.xp.mail.MailMessage)2 MailService (com.enonic.xp.mail.MailService)2 ResourceKey (com.enonic.xp.resource.ResourceKey)2 ScriptValue (com.enonic.xp.script.ScriptValue)2 ScriptTestSupport (com.enonic.xp.testing.ScriptTestSupport)2 ByteSource (com.google.common.io.ByteSource)2 CharStreams (com.google.common.io.CharStreams)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Stream (java.util.stream.Stream)2 BodyPart (javax.mail.BodyPart)2 Message (javax.mail.Message)2 Session (javax.mail.Session)2 AddressException (javax.mail.internet.AddressException)2 InternetAddress (javax.mail.internet.InternetAddress)2 MimeMessage (javax.mail.internet.MimeMessage)2