Search in sources :

Example 6 with PortalRequest

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());
}
Also used : Site(com.enonic.xp.site.Site) WebException(com.enonic.xp.web.WebException) ContentNotFoundException(com.enonic.xp.content.ContentNotFoundException) ContentPath(com.enonic.xp.content.ContentPath) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 7 with PortalRequest

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);
}
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 8 with PortalRequest

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());
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 9 with PortalRequest

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);
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 10 with 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"));
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Aggregations

PortalRequest (com.enonic.xp.portal.PortalRequest)103 Test (org.junit.jupiter.api.Test)47 BeforeEach (org.junit.jupiter.api.BeforeEach)35 PortalResponse (com.enonic.xp.portal.PortalResponse)25 Site (com.enonic.xp.site.Site)19 Content (com.enonic.xp.content.Content)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ApplicationKey (com.enonic.xp.app.ApplicationKey)10 ContentPath (com.enonic.xp.content.ContentPath)10 ContentService (com.enonic.xp.content.ContentService)10 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)10 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ResourceKey (com.enonic.xp.resource.ResourceKey)9 ResourceService (com.enonic.xp.resource.ResourceService)9 ContentId (com.enonic.xp.content.ContentId)7 Matcher (java.util.regex.Matcher)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)7 Assertions.fail (org.junit.jupiter.api.Assertions.fail)7 Mockito.when (org.mockito.Mockito.when)7