Search in sources :

Example 21 with ResourceKey

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

the class WebAppHandlerTest method mockResource.

private Resource mockResource(final String uri, final byte[] data) {
    final ResourceKey key = ResourceKey.from(uri);
    final Resource resource = new MockResource(key, data, System.currentTimeMillis());
    Mockito.when(this.resourceService.getResource(key)).thenReturn(resource);
    return resource;
}
Also used : MockResource(com.enonic.xp.resource.MockResource) MockResource(com.enonic.xp.resource.MockResource) Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 22 with ResourceKey

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

the class ExceptionRendererImplTest method render_custom_error_with_site_context.

@Test
void render_custom_error_with_site_context() {
    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, handlerMethod) -> PortalResponse.create().body("Custom message page").status(HttpStatus.BAD_REQUEST).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.BAD_REQUEST, cause));
    assertEquals(HttpStatus.BAD_REQUEST, 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)

Example 23 with ResourceKey

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

the class ExceptionRendererImplTest method customErrorWithPostProcessing.

@Test
void customErrorWithPostProcessing() {
    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, handlerMethod) -> PortalResponse.create().body("<h1>Custom message page</h1><!--#COMPONENT module:myPart -->").status(HttpStatus.BAD_REQUEST).postProcess(true).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.BAD_REQUEST, cause));
    assertEquals(HttpStatus.BAD_REQUEST, res.getStatus());
    assertEquals("<h1>Custom message page</h1><h3>My Part</h3>", res.getBody().toString());
    assertTrue(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)

Example 24 with ResourceKey

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

the class AssetHandlerTest method testCacheHeader.

@Test
public void testCacheHeader() throws Exception {
    addResource("demo:/assets/css/main.css");
    this.request.setEndpointPath("/_/asset/demo:0000000000000001/css/main.css");
    final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("demo"), "META-INF/MANIFEST.MF");
    when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 1));
    final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.OK, res.getStatus());
    assertEquals("public, max-age=31536000, immutable", res.getHeaders().get("Cache-Control"));
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) ResourceKey(com.enonic.xp.resource.ResourceKey) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 25 with ResourceKey

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

the class MappingHandlerTest method executeScript.

@Test
public void executeScript() throws Exception {
    final ResourceKey controller = ResourceKey.from("demo:/services/test");
    final ControllerMappingDescriptor mapping = ControllerMappingDescriptor.create().controller(controller).pattern(".*/content").build();
    setupContentAndSite(mapping);
    this.request.setBaseUri("/site");
    this.request.setContentPath(ContentPath.from("/site/somesite/content"));
    when(rendererDelegate.render(isA(ControllerMappingDescriptor.class), same(request))).thenReturn(PortalResponse.create().body("Ok").build());
    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());
}
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)

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