Search in sources :

Example 61 with ResourceKey

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

the class FilterScriptImplTest method setup.

@BeforeEach
public void setup() throws Exception {
    this.portalRequest = new PortalRequest();
    this.portalResponse = PortalResponse.create().build();
    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.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(ApplicationKey.from("myapplication"))).thenReturn(application);
    this.resourceService = Mockito.mock(ResourceService.class);
    Mockito.when(resourceService.getResource(Mockito.any())).thenAnswer(invocation -> {
        final ResourceKey resourceKey = (ResourceKey) invocation.getArguments()[0];
        final URL resourceUrl = FilterScriptImplTest.class.getResource("/" + resourceKey.getApplicationKey() + resourceKey.getPath());
        return new UrlResource(resourceKey, resourceUrl);
    });
    final ScriptAsyncService scriptAsyncService = Mockito.mock(ScriptAsyncService.class);
    final ScriptRuntimeFactoryImpl runtimeFactory = new ScriptRuntimeFactoryImpl(applicationService, this.resourceService, scriptAsyncService);
    final PortalScriptServiceImpl scriptService = new PortalScriptServiceImpl(runtimeFactory);
    scriptService.initialize();
    this.factory = new FilterScriptFactoryImpl();
    this.factory.setScriptService(scriptService);
    final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
    ServletRequestHolder.setRequest(req);
}
Also used : Bundle(org.osgi.framework.Bundle) ResourceService(com.enonic.xp.resource.ResourceService) PortalScriptServiceImpl(com.enonic.xp.portal.impl.script.PortalScriptServiceImpl) URL(java.net.URL) PortalRequest(com.enonic.xp.portal.PortalRequest) ResourceKey(com.enonic.xp.resource.ResourceKey) ScriptRuntimeFactoryImpl(com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl) HttpServletRequest(javax.servlet.http.HttpServletRequest) UrlResource(com.enonic.xp.resource.UrlResource) ScriptAsyncService(com.enonic.xp.script.impl.async.ScriptAsyncService) Application(com.enonic.xp.app.Application) BundleContext(org.osgi.framework.BundleContext) ApplicationService(com.enonic.xp.app.ApplicationService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 62 with ResourceKey

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

the class MappingHandlerTest method executeFilter.

@Test
public void executeFilter() throws Exception {
    final ResourceKey filter = ResourceKey.from("demo:/services/test");
    final ControllerMappingDescriptor mapping = ControllerMappingDescriptor.create().filter(filter).pattern(".*/content").build();
    setupContentAndSite(mapping);
    this.request.setBaseUri("/site");
    this.request.setContentPath(ContentPath.from("/site/somesite/content"));
    final WebResponse response = this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertEquals(HttpStatus.OK, response.getStatus());
    assertNotNull(this.request.getApplicationKey());
    assertNotNull(this.request.getSite());
    assertNotNull(this.request.getContent());
    assertEquals("/site/draft/site", this.request.getContextPath());
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 63 with ResourceKey

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

the class MainExecutorTest method mainJsError.

@Test
public void mainJsError() {
    final ResourceKey key = ResourceKey.from("foo.bar:/main.js");
    when(this.scriptService.hasScript(key)).thenReturn(true);
    when(this.scriptService.executeAsync(key)).thenReturn(CompletableFuture.failedFuture(new RuntimeException()));
    final Application app = mock(Application.class);
    when(app.getKey()).thenReturn(ApplicationKey.from("foo.bar"));
    this.executor.activated(app);
}
Also used : Application(com.enonic.xp.app.Application) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 64 with ResourceKey

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

the class PortalUrlServiceImpl_assetUrlTest method createUrl_encodeChars.

@Test
public void createUrl_encodeChars() {
    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).path("css/my other & strange.css");
    final String url = this.service.assetUrl(params);
    assertEquals("/site/default/draft/_/asset/myapplication:0000000000000001/css/my%20other%20&%20strange.css", url);
}
Also used : ResourceKey(com.enonic.xp.resource.ResourceKey) AssetUrlParams(com.enonic.xp.portal.url.AssetUrlParams) Test(org.junit.jupiter.api.Test)

Example 65 with ResourceKey

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

the class PortalUrlServiceImpl_assetUrlTest method createUrl_absolute.

@Test
public void createUrl_absolute() {
    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().type(UrlTypeConstants.ABSOLUTE).portalRequest(this.portalRequest).path("css/my.css");
    when(req.getServerName()).thenReturn("localhost");
    when(req.getScheme()).thenReturn("http");
    when(req.getServerPort()).thenReturn(80);
    final String url = this.service.assetUrl(params);
    assertEquals("http://localhost/site/default/draft/_/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)

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