use of com.enonic.xp.site.Site in project xp by enonic.
the class ContentResolverTest method resolve_existing_but_needs_authentication_in_live_mode.
@Test
void resolve_existing_but_needs_authentication_in_live_mode() {
final Site site = newSite();
final PortalRequest request = new PortalRequest();
final ContentPath contentPath = ContentPath.from("/mysite/landing-page/non-existing");
request.setContentPath(contentPath);
when(this.contentService.getByPath(contentPath)).thenThrow(new ContentNotFoundException(contentPath, null));
when(this.contentService.contentExists(contentPath)).thenReturn(true);
when(this.contentService.findNearestSiteByPath(contentPath)).thenReturn(site);
final ContentResolverResult result = new ContentResolver(contentService).resolve(request);
assertNull(result.getContent());
assertSame(site, result.getNearestSite());
assertEquals("/landing-page/non-existing", result.getSiteRelativePath());
final WebException e = assertThrows(WebException.class, result::getContentOrElseThrow);
assertEquals(HttpStatus.UNAUTHORIZED, e.getStatus());
}
use of com.enonic.xp.site.Site in project xp by enonic.
the class ContentResolverTest method resolve_in_edit_mode.
@Test
void resolve_in_edit_mode() {
final Content content = newContent();
final Site site = newSite();
final PortalRequest request = new PortalRequest();
request.setMode(RenderMode.EDIT);
request.setContentPath(ContentPath.from("/c8da0c10-0002-4b68-b407-87412f3e45c8"));
when(this.contentService.getById(ContentId.from("c8da0c10-0002-4b68-b407-87412f3e45c8"))).thenReturn(content);
when(this.contentService.getNearestSite(ContentId.from("c8da0c10-0002-4b68-b407-87412f3e45c8"))).thenReturn(site);
final ContentResolverResult result = new ContentResolver(contentService).resolve(request);
assertSame(content, result.getContent());
assertSame(site, result.getNearestSite());
assertEquals("/landing-page", result.getSiteRelativePath());
}
use of com.enonic.xp.site.Site 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());
}
use of com.enonic.xp.site.Site 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());
}
use of com.enonic.xp.site.Site in project xp by enonic.
the class RenderBaseHandlerTest method setupSite.
protected void setupSite() {
final Site site = createSite("id", "site");
when(this.contentService.getNearestSite(isA(ContentId.class))).thenReturn(site);
when(this.contentService.findNearestSiteByPath(isA(ContentPath.class))).thenReturn(site);
}
Aggregations