Search in sources :

Example 66 with ResourceKey

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

the class PortalUrlServiceImpl_assetUrlTest method createUrlWithLongContentPath.

@Test
public void createUrlWithLongContentPath() {
    final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("myapplication"), "META-INF/MANIFEST.MF");
    when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 1));
    final StringBuilder longContentPath = new StringBuilder();
    longContentPath.append("/a".repeat(10000));
    this.portalRequest.setContentPath(ContentPath.from(longContentPath.toString()));
    final AssetUrlParams params = new AssetUrlParams().portalRequest(this.portalRequest).path("css/my.css");
    final String url = this.service.assetUrl(params);
    assertEquals("/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)

Example 67 with ResourceKey

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

the class WebAppHandler method getScript.

private ControllerScript getScript(final ApplicationKey applicationKey) {
    final ResourceKey script = ResourceKey.from(applicationKey, "/webapp/webapp.js");
    final Trace trace = Tracer.current();
    if (trace != null) {
        trace.put("script", script.getPath());
    }
    return this.controllerScriptFactory.fromScript(script);
}
Also used : Trace(com.enonic.xp.trace.Trace) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 68 with ResourceKey

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

the class AbstractErrorHandlerTest method setup.

@BeforeEach
public void setup() throws Exception {
    this.portalRequest = new PortalRequest();
    this.portalRequest.setMethod(HttpMethod.GET);
    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 = AbstractErrorHandlerTest.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 ErrorHandlerScriptFactoryImpl();
    this.factory.setScriptService(scriptService);
    this.postProcessor = new PostProcessorImpl();
    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) PostProcessorImpl(com.enonic.xp.portal.impl.postprocess.PostProcessorImpl) Application(com.enonic.xp.app.Application) BundleContext(org.osgi.framework.BundleContext) ApplicationService(com.enonic.xp.app.ApplicationService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 69 with ResourceKey

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

the class ExceptionRendererImplTest method render_default_error_page_when_error_in_custom_handler.

@Test
void render_default_error_page_when_error_in_custom_handler() {
    this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
    final Site site = newSite();
    this.request.setSite(site);
    final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
    final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> {
        throw new RuntimeException("Something went wrong in the handler script");
    };
    when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
    final Resource resource = mock(Resource.class);
    when(resource.exists()).thenReturn(true);
    when(this.resourceService.getResource(errorResource)).thenReturn(resource);
    final RuntimeException cause = new RuntimeException("Custom message");
    final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
    final String body = res.getBody().toString();
    assertTrue(body.contains("400 Bad Request"));
    assertTrue(body.contains("Custom message"));
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 70 with ResourceKey

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

the class ExceptionRendererImplTest method render_custom_error_for_404_in_site_path.

@Test
void render_custom_error_for_404_in_site_path() {
    this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
    this.request.setContentPath(ContentPath.from("/mysite/some/long/path"));
    final Site site = newSite();
    when(contentService.findNearestSiteByPath(eq(ContentPath.from("/mysite/some/long/path")))).thenReturn(site);
    final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
    final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> PortalResponse.create().body("Custom message page").status(HttpStatus.NOT_FOUND).postProcess(false).build();
    when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
    final Resource resource = mock(Resource.class);
    when(resource.exists()).thenReturn(true);
    when(this.resourceService.getResource(errorResource)).thenReturn(resource);
    final RuntimeException cause = new RuntimeException("Custom message");
    final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.NOT_FOUND, cause));
    assertEquals(HttpStatus.NOT_FOUND, res.getStatus());
    assertEquals("Custom message page", res.getBody().toString());
    assertFalse(postProcessor.isExecuted());
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) 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