Search in sources :

Example 6 with ResourceKey

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

the class PortalUrlServiceImpl_assetUrlTest method createUrl_withContentPath.

@Test
public void createUrl_withContentPath() {
    final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("myapplication"), "META-INF/MANIFEST.MF");
    when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 1));
    final AssetUrlParams params = new AssetUrlParams().portalRequest(this.portalRequest).contextPathType(ContextPathType.RELATIVE.getValue()).path("css/my.css");
    final String url = this.service.assetUrl(params);
    assertEquals("/site/default/draft/context/path/_/asset/myapplication:0000000000000001/css/my.css", url);
}
Also used : ResourceKey(com.enonic.xp.resource.ResourceKey) AssetUrlParams(com.enonic.xp.portal.url.AssetUrlParams) Test(org.junit.jupiter.api.Test)

Example 7 with ResourceKey

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

the class AbstractScriptTest method createScriptRuntimeFactory.

private ScriptRuntimeFactory createScriptRuntimeFactory() {
    final BundleContext bundleContext = Mockito.mock(BundleContext.class);
    final Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(bundle.getBundleContext()).thenReturn(bundleContext);
    final Application application = Mockito.mock(Application.class);
    Mockito.when(application.getBundle()).thenReturn(bundle);
    Mockito.when(application.getKey()).thenReturn(APPLICATION_KEY);
    Mockito.when(application.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
    Mockito.when(application.getClassLoader()).thenReturn(getClass().getClassLoader());
    Mockito.when(application.isStarted()).thenReturn(true);
    Mockito.when(application.getConfig()).thenReturn(ConfigBuilder.create().build());
    final ApplicationService applicationService = Mockito.mock(ApplicationService.class);
    Mockito.when(applicationService.getInstalledApplication(APPLICATION_KEY)).thenReturn(application);
    final ResourceService resourceService = Mockito.mock(ResourceService.class);
    Mockito.when(resourceService.getResource(Mockito.any())).thenAnswer(invocation -> {
        final ResourceKey resourceKey = (ResourceKey) invocation.getArguments()[0];
        final URL resourceUrl = AbstractScriptTest.class.getResource("/" + resourceKey.getApplicationKey() + resourceKey.getPath());
        return new UrlResource(resourceKey, resourceUrl);
    });
    final ScriptAsyncService scriptAsyncService = Mockito.mock(ScriptAsyncService.class);
    return new ScriptRuntimeFactoryImpl(applicationService, resourceService, scriptAsyncService);
}
Also used : ScriptRuntimeFactoryImpl(com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl) UrlResource(com.enonic.xp.resource.UrlResource) Bundle(org.osgi.framework.Bundle) ResourceService(com.enonic.xp.resource.ResourceService) ScriptAsyncService(com.enonic.xp.script.impl.async.ScriptAsyncService) Application(com.enonic.xp.app.Application) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext) ApplicationService(com.enonic.xp.app.ApplicationService) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 8 with ResourceKey

use of com.enonic.xp.resource.ResourceKey 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 9 with ResourceKey

use of com.enonic.xp.resource.ResourceKey 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 10 with ResourceKey

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

the class ScriptRuntimeTest method testExecuteExported.

@Test
public void testExecuteExported() {
    final ResourceKey script = ResourceKey.from("myapplication:/export-test.js");
    final ScriptExports exports = runTestScript(script);
    assertNotNull(exports);
    assertSame(script, exports.getScript());
    assertTrue(exports.hasMethod("hello"));
    assertEquals("Hello World!", exports.executeMethod("hello", "World").getValue());
}
Also used : ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceKey (com.enonic.xp.resource.ResourceKey)76 Test (org.junit.jupiter.api.Test)49 Resource (com.enonic.xp.resource.Resource)21 ApplicationKey (com.enonic.xp.app.ApplicationKey)12 ResourceService (com.enonic.xp.resource.ResourceService)12 ScriptExports (com.enonic.xp.script.ScriptExports)11 Application (com.enonic.xp.app.Application)9 UrlResource (com.enonic.xp.resource.UrlResource)9 URL (java.net.URL)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 PortalRequest (com.enonic.xp.portal.PortalRequest)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 ApplicationService (com.enonic.xp.app.ApplicationService)7 AssetUrlParams (com.enonic.xp.portal.url.AssetUrlParams)7 ScriptAsyncService (com.enonic.xp.script.impl.async.ScriptAsyncService)7 ScriptRuntimeFactoryImpl (com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl)7 PortalResponse (com.enonic.xp.portal.PortalResponse)6 PortalScriptServiceImpl (com.enonic.xp.portal.impl.script.PortalScriptServiceImpl)6 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)6 PropertyTree (com.enonic.xp.data.PropertyTree)5