Search in sources :

Example 26 with ResourceKey

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

the class MappingHandlerTest method executeNothing.

@Test
public void executeNothing() throws Exception {
    final ResourceKey controller = ResourceKey.from("demo:/services/test");
    final ControllerMappingDescriptor mapping = ControllerMappingDescriptor.create().controller(controller).pattern("/nomatch").build();
    setupContentAndSite(mapping);
    final WebHandlerChain chain = mock(WebHandlerChain.class);
    final PortalResponse response = PortalResponse.create().build();
    this.request.setBaseUri("/site");
    this.request.setContentPath(ContentPath.from("/site/somesite/content"));
    this.handler.handle(this.request, response, chain);
    verify(chain).handle(this.request, response);
    verifyNoInteractions(rendererDelegate);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) WebHandlerChain(com.enonic.xp.web.handler.WebHandlerChain) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 27 with ResourceKey

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

the class MacroProcessorScriptTest method setup.

@BeforeEach
public void setup() throws Exception {
    this.macroContext = MacroContext.create().name("macroName").body("body").param("firstParam", "firstParamValue").param("secondParam", "secondParamValue").document("<h1>document</h1>").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.getConfig()).thenReturn(ConfigBuilder.create().build());
    Mockito.when(application.isStarted()).thenReturn(true);
    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 = MacroProcessorScriptTest.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 MacroProcessorFactoryImpl();
    this.factory.setScriptService(scriptService);
    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) 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) Application(com.enonic.xp.app.Application) BundleContext(org.osgi.framework.BundleContext) ApplicationService(com.enonic.xp.app.ApplicationService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 28 with ResourceKey

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

the class PortalUrlServiceImpl_assetUrlTest method createUrl_withVirtualHost.

@Test
public void createUrl_withVirtualHost() {
    final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("myapplication"), "META-INF/MANIFEST.MF");
    when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 1));
    final AssetUrlParams params = new AssetUrlParams().portalRequest(this.portalRequest).path("css/my.css");
    // Mocks a virtual host and the HTTP request
    final VirtualHost virtualHost = mock(VirtualHost.class);
    when(req.getAttribute(VirtualHost.class.getName())).thenReturn(virtualHost);
    // Calls the method with a virtual mapping /main -> /
    when(virtualHost.getSource()).thenReturn("/main");
    when(virtualHost.getTarget()).thenReturn("/");
    String url = this.service.assetUrl(params);
    assertEquals("/main/site/default/draft/_/asset/myapplication:0000000000000001/css/my.css", url);
    // Calls the method with a virtual mapping /main -> /site/default/draft/context
    when(virtualHost.getSource()).thenReturn("/main");
    when(virtualHost.getTarget()).thenReturn("/site");
    url = this.service.assetUrl(params);
    assertEquals("/main/default/draft/_/asset/myapplication:0000000000000001/css/my.css", url);
    // Calls the method with a virtual mapping /main -> /site/default/draft/context
    when(virtualHost.getSource()).thenReturn("/main");
    when(virtualHost.getTarget()).thenReturn("/site/default/draft");
    url = this.service.assetUrl(params);
    assertEquals("/main/_/asset/myapplication:0000000000000001/css/my.css", url);
    // Calls the method with a virtual mapping / -> /site/default/draft/context
    when(virtualHost.getSource()).thenReturn("/");
    when(virtualHost.getTarget()).thenReturn("/site/default/draft/context");
    url = this.service.assetUrl(params);
    assertEquals("/_/asset/myapplication:0000000000000001/css/my.css", url);
    // Calls the method with a virtual mapping /main/path -> /site/default/draft/context/path
    when(virtualHost.getSource()).thenReturn("/main/path");
    when(virtualHost.getTarget()).thenReturn("/site/default/draft/context/path");
    url = this.service.assetUrl(params);
    assertEquals("/main/path/_/asset/myapplication:0000000000000001/css/my.css", url);
    // Calls the method with a virtual mapping /site/default/draft/context/path -> /site/default/draft/context/path
    when(virtualHost.getSource()).thenReturn("/site/default/draft/context/path");
    when(virtualHost.getTarget()).thenReturn("/site/default/draft/context/path");
    url = this.service.assetUrl(params);
    assertEquals("/site/default/draft/context/path/_/asset/myapplication:0000000000000001/css/my.css", url);
}
Also used : VirtualHost(com.enonic.xp.web.vhost.VirtualHost) ResourceKey(com.enonic.xp.resource.ResourceKey) AssetUrlParams(com.enonic.xp.portal.url.AssetUrlParams) Test(org.junit.jupiter.api.Test)

Example 29 with ResourceKey

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

the class PortalUrlServiceImpl_assetUrlTest method createUrl.

@Test
public void createUrl() {
    final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("myapplication"), "META-INF/MANIFEST.MF");
    when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 1));
    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 30 with ResourceKey

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

the class PortalUrlServiceImpl_assetUrlTest method createUrl_withApplication.

@Test
public void createUrl_withApplication() {
    final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("otherapplication"), "META-INF/MANIFEST.MF");
    when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 2));
    final AssetUrlParams params = new AssetUrlParams().portalRequest(this.portalRequest).application("otherapplication").path("css/my.css");
    final String url = this.service.assetUrl(params);
    assertEquals("/site/default/draft/_/asset/otherapplication:0000000000000002/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)

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