Search in sources :

Example 1 with UrlResource

use of com.enonic.xp.resource.UrlResource 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 2 with UrlResource

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

the class IdProviderControllerServiceImplTest method setupPortalScriptService.

private PortalScriptService setupPortalScriptService() {
    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("defaultapplication"))).thenReturn(application);
    Mockito.when(applicationService.getInstalledApplication(ApplicationKey.from("myapplication"))).thenReturn(application);
    ResourceService 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, resourceService, scriptAsyncService);
    final PortalScriptServiceImpl scriptService = new PortalScriptServiceImpl(runtimeFactory);
    scriptService.initialize();
    return scriptService;
}
Also used : ScriptRuntimeFactoryImpl(com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl) UrlResource(com.enonic.xp.resource.UrlResource) Bundle(org.osgi.framework.Bundle) ResourceService(com.enonic.xp.resource.ResourceService) ScriptAsyncService(com.enonic.xp.script.impl.async.ScriptAsyncService) PortalScriptServiceImpl(com.enonic.xp.portal.impl.script.PortalScriptServiceImpl) Application(com.enonic.xp.app.Application) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext) ApplicationService(com.enonic.xp.app.ApplicationService) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 3 with UrlResource

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

the class AbstractScriptTest method createScriptRuntimeFactory.

private ScriptRuntimeFactory createScriptRuntimeFactory() {
    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.getKey()).thenReturn(APPLICATION_KEY);
    Mockito.when(application.getVersion()).thenReturn(Version.parseVersion("1.0.0"));
    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(APPLICATION_KEY)).thenReturn(application);
    final ResourceService resourceService = Mockito.mock(ResourceService.class);
    Mockito.when(resourceService.getResource(Mockito.any())).thenAnswer(invocation -> {
        final ResourceKey resourceKey = (ResourceKey) invocation.getArguments()[0];
        final URL resourceUrl = AbstractScriptTest.class.getResource("/" + resourceKey.getApplicationKey() + resourceKey.getPath());
        return new UrlResource(resourceKey, resourceUrl);
    });
    final ScriptAsyncService scriptAsyncService = Mockito.mock(ScriptAsyncService.class);
    return new ScriptRuntimeFactoryImpl(applicationService, resourceService, scriptAsyncService);
}
Also used : ScriptRuntimeFactoryImpl(com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl) UrlResource(com.enonic.xp.resource.UrlResource) Bundle(org.osgi.framework.Bundle) ResourceService(com.enonic.xp.resource.ResourceService) ScriptAsyncService(com.enonic.xp.script.impl.async.ScriptAsyncService) Application(com.enonic.xp.app.Application) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext) ApplicationService(com.enonic.xp.app.ApplicationService) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 4 with UrlResource

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

the class ResponseSerializerTest method serializeBodyResource.

@Test
public void serializeBodyResource() throws Exception {
    final ResourceKey resourceKey = ResourceKey.from("myapp:/site/test/view/body_file.txt");
    final URL resourceUrl = ResponseSerializerTest.class.getResource("body_file.txt");
    final WebRequest req = new WebRequest();
    req.setMethod(HttpMethod.GET);
    final WebResponse resp = WebResponse.create().status(HttpStatus.ACCEPTED).contentType(MediaType.PLAIN_TEXT_UTF_8).header("header-test", "header-value").cookie(new Cookie("cookie-name", "cookie-value")).body(new UrlResource(resourceKey, resourceUrl)).build();
    final ResponseSerializer serializer = new ResponseSerializer(req, resp);
    final HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    final ServletOutputStream servletOutputStream = mock(ServletOutputStream.class);
    when(httpResponse.getOutputStream()).thenReturn(servletOutputStream);
    serializer.serialize(httpResponse);
    verify(httpResponse).setHeader("header-test", "header-value");
    verify(httpResponse).setStatus(202);
    verify(httpResponse).setContentType("text/plain; charset=utf-8");
    verify(httpResponse).setContentLength(11);
    verify(servletOutputStream).write(any(byte[].class));
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) UrlResource(com.enonic.xp.resource.UrlResource) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) URL(java.net.URL) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 5 with UrlResource

use of com.enonic.xp.resource.UrlResource 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)

Aggregations

ResourceKey (com.enonic.xp.resource.ResourceKey)9 UrlResource (com.enonic.xp.resource.UrlResource)9 URL (java.net.URL)9 Application (com.enonic.xp.app.Application)7 ApplicationService (com.enonic.xp.app.ApplicationService)7 ResourceService (com.enonic.xp.resource.ResourceService)7 ScriptAsyncService (com.enonic.xp.script.impl.async.ScriptAsyncService)7 ScriptRuntimeFactoryImpl (com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl)7 Bundle (org.osgi.framework.Bundle)7 BundleContext (org.osgi.framework.BundleContext)7 PortalScriptServiceImpl (com.enonic.xp.portal.impl.script.PortalScriptServiceImpl)6 BeforeEach (org.junit.jupiter.api.BeforeEach)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 PortalRequest (com.enonic.xp.portal.PortalRequest)3 PostProcessorImpl (com.enonic.xp.portal.impl.postprocess.PostProcessorImpl)2 Test (org.junit.jupiter.api.Test)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 ConfigBuilder (com.enonic.xp.config.ConfigBuilder)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 DescriptorKey (com.enonic.xp.page.DescriptorKey)1