use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class ContentResolverTest method resolve_non_existing_in_live_mode.
@Test
void resolve_non_existing_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(false);
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.NOT_FOUND, e.getStatus());
}
use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class AbstractControllerTest 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 = AbstractControllerTest.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 ControllerScriptFactoryImpl();
this.factory.setScriptService(scriptService);
this.postProcessor = new PostProcessorImpl();
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
ServletRequestHolder.setRequest(req);
}
use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class PortalRequestSerializerTest method serializeNoHeadersNoParams.
@Test
public void serializeNoHeadersNoParams() throws Exception {
final PortalRequest sourceRequest = new PortalRequest();
final String jsonRequest = readResource("PortalRequestSerializer_request2.json");
final Object obj = execute("var result = " + jsonRequest + "; result;");
final ScriptValue value = this.factory.newValue(obj);
PortalRequestSerializer reqSerializer = new PortalRequestSerializer(sourceRequest, value);
final PortalRequest portalRequest = reqSerializer.serialize();
assertNotNull(portalRequest);
assertEquals(HttpMethod.POST, portalRequest.getMethod());
assertEquals("https", portalRequest.getScheme());
assertEquals("myhost", portalRequest.getHost());
assertEquals(1234, portalRequest.getPort());
assertEquals("/some/path", portalRequest.getPath());
assertEquals("https://myhost:1234/some/path/url", portalRequest.getUrl());
assertEquals("127.0.0.42", portalRequest.getRemoteAddress());
assertEquals(RenderMode.EDIT, portalRequest.getMode());
assertEquals(true, portalRequest.isValidTicket());
assertEquals(Branch.from("draft"), portalRequest.getBranch());
assertEquals("application/json", portalRequest.getContentType());
assertEquals(0, portalRequest.getHeaders().size());
assertEquals(0, portalRequest.getCookies().size());
assertEquals(0, portalRequest.getParams().size());
}
use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class PortalRequestSerializerTest method serializeNonObject.
@Test
public void serializeNonObject() throws Exception {
final PortalRequest sourceRequest = new PortalRequest();
final Object obj = execute("var result = 'response'; result;");
final ScriptValue value = this.factory.newValue(obj);
PortalRequestSerializer reqSerializer = new PortalRequestSerializer(sourceRequest, value);
final PortalRequest portalRequest = reqSerializer.serialize();
assertNotNull(portalRequest);
}
use of com.enonic.xp.portal.PortalRequest in project xp by enonic.
the class PortalRequestSerializerTest method serialize_no_params_duplication.
@Test
public void serialize_no_params_duplication() throws Exception {
final PortalRequest sourceRequest = new PortalRequest();
sourceRequest.getParams().put("a", "oldA");
sourceRequest.getParams().put("b", "oldB");
sourceRequest.getParams().put("b", "oldB");
sourceRequest.getParams().put("d", "oldD");
sourceRequest.getParams().put("e", "oldE");
sourceRequest.getParams().put("f", "oldF");
final String jsonRequest = readResource("PortalRequestSerializer_request4.json");
final Object obj = execute("var result = " + jsonRequest + "; result;");
final ScriptValue value = this.factory.newValue(obj);
PortalRequestSerializer reqSerializer = new PortalRequestSerializer(sourceRequest, value);
final PortalRequest portalRequest = reqSerializer.serialize();
assertThat(portalRequest.getParams().get("a")).containsExactly("newA");
assertThat(portalRequest.getParams().get("b")).containsExactly("newB", "1");
assertThat(portalRequest.getParams().get("c")).containsExactly("newC");
assertThat(portalRequest.getParams().get("d")).containsExactly("oldD");
assertFalse(portalRequest.getParams().containsKey("e"));
assertFalse(portalRequest.getParams().containsKey("f"));
}
Aggregations