Search in sources :

Example 1 with PortalScriptService

use of com.enonic.xp.portal.script.PortalScriptService in project xp by enonic.

the class NamedTaskFactoryImplTest method setUp.

@BeforeEach
void setUp() {
    final PortalScriptService portalScriptService = setupPortalScriptService();
    namedTaskScriptFactory = new NamedTaskScriptFactoryImpl(portalScriptService, taskDescriptorService);
}
Also used : PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with PortalScriptService

use of com.enonic.xp.portal.script.PortalScriptService in project xp by enonic.

the class NamedTaskFactoryImplTest method setupPortalScriptService.

private PortalScriptService setupPortalScriptService() {
    final BundleContext bundleContext = mock(BundleContext.class);
    final Bundle bundle = mock(Bundle.class);
    when(bundle.getBundleContext()).thenReturn(bundleContext);
    final Application application = mock(Application.class);
    when(application.getBundle()).thenReturn(bundle);
    when(application.getClassLoader()).thenReturn(getClass().getClassLoader());
    when(application.isStarted()).thenReturn(true);
    when(application.getConfig()).thenReturn(ConfigBuilder.create().build());
    final ApplicationService applicationService = mock(ApplicationService.class);
    when(applicationService.getInstalledApplication(ApplicationKey.from("myapplication"))).thenReturn(application);
    ResourceService resourceService = mock(ResourceService.class);
    final Answer<Object> getResource = invocation -> {
        final ResourceKey resourceKey = (ResourceKey) invocation.getArguments()[0];
        final URL resourceUrl = NamedTaskFactoryImplTest.class.getResource("/" + resourceKey.getApplicationKey() + resourceKey.getPath());
        return new UrlResource(resourceKey, resourceUrl);
    };
    when(resourceService.getResource(any())).thenAnswer(getResource);
    final ScriptAsyncService scriptAsyncService = mock(ScriptAsyncService.class);
    final ScriptRuntimeFactoryImpl runtimeFactory = new ScriptRuntimeFactoryImpl(applicationService, resourceService, scriptAsyncService);
    final PortalScriptServiceImpl scriptService = new PortalScriptServiceImpl(runtimeFactory);
    scriptService.initialize();
    return scriptService;
}
Also used : Strictness(org.mockito.quality.Strictness) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) TaskDescriptorService(com.enonic.xp.task.TaskDescriptorService) URL(java.net.URL) Mock(org.mockito.Mock) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalScriptServiceImpl(com.enonic.xp.portal.impl.script.PortalScriptServiceImpl) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ScriptRuntimeFactoryImpl(com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl) Answer(org.mockito.stubbing.Answer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RunnableTask(com.enonic.xp.task.RunnableTask) Application(com.enonic.xp.app.Application) ScriptAsyncService(com.enonic.xp.script.impl.async.ScriptAsyncService) ConfigBuilder(com.enonic.xp.config.ConfigBuilder) Bundle(org.osgi.framework.Bundle) PropertyTree(com.enonic.xp.data.PropertyTree) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ResourceService(com.enonic.xp.resource.ResourceService) Mockito.when(org.mockito.Mockito.when) UrlResource(com.enonic.xp.resource.UrlResource) BundleContext(org.osgi.framework.BundleContext) ApplicationKey(com.enonic.xp.app.ApplicationKey) TaskId(com.enonic.xp.task.TaskId) Test(org.junit.jupiter.api.Test) TaskNotFoundException(com.enonic.xp.task.TaskNotFoundException) DescriptorKey(com.enonic.xp.page.DescriptorKey) TaskDescriptor(com.enonic.xp.task.TaskDescriptor) ApplicationService(com.enonic.xp.app.ApplicationService) Mockito.mock(org.mockito.Mockito.mock) 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) 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)

Example 3 with PortalScriptService

use of com.enonic.xp.portal.script.PortalScriptService in project xp by enonic.

the class ResponseProcessorExecutorTest method testExecuteResponseProcessorWithByteSourceBody.

@Test
public void testExecuteResponseProcessorWithByteSourceBody() throws Exception {
    final ByteSource data = ByteSource.wrap("DATA".getBytes(StandardCharsets.UTF_8));
    final PortalScriptService scriptService = Mockito.mock(PortalScriptService.class);
    final ScriptExports scriptExports = Mockito.mock(ScriptExports.class);
    when(scriptExports.hasMethod("responseProcessor")).thenReturn(true);
    when(scriptService.execute(any(ResourceKey.class))).thenReturn(scriptExports);
    final ScriptValue result = Mockito.mock(ScriptValue.class);
    final ScriptValue body = Mockito.mock(ScriptValue.class);
    when(body.getValue()).thenReturn(data.toString());
    when(result.isObject()).thenReturn(true);
    when(result.getMember("body")).thenReturn(body);
    when(scriptExports.executeMethod(anyString(), any(PortalRequestMapper.class), any(PortalResponseMapper.class))).thenReturn(result);
    final ResponseProcessorExecutor filterExecutor = new ResponseProcessorExecutor(scriptService);
    final ResponseProcessorDescriptor filter = ResponseProcessorDescriptor.create().application(ApplicationKey.from("myApp")).name("filter1").build();
    final PortalRequest request = new PortalRequest();
    final PortalResponse response = PortalResponse.create().body(data).build();
    final PortalResponse filteredResponse = filterExecutor.execute(filter, request, response);
    assertNotNull(filteredResponse);
    assertTrue(filteredResponse.getBody() instanceof ByteSource);
    assertArrayEquals(data.read(), ((ByteSource) filteredResponse.getBody()).read());
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponse(com.enonic.xp.portal.PortalResponse) PortalResponseMapper(com.enonic.xp.portal.impl.mapper.PortalResponseMapper) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ByteSource(com.google.common.io.ByteSource) PortalRequestMapper(com.enonic.xp.portal.impl.mapper.PortalRequestMapper) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 4 with PortalScriptService

use of com.enonic.xp.portal.script.PortalScriptService in project xp by enonic.

the class ResponseProcessorExecutorTest method testExecuteResponseProcessorNotImplementingMethod.

@Test
public void testExecuteResponseProcessorNotImplementingMethod() throws Exception {
    final PortalScriptService scriptService = Mockito.mock(PortalScriptService.class);
    final ScriptExports scriptExports = Mockito.mock(ScriptExports.class);
    when(scriptService.execute(any(ResourceKey.class))).thenReturn(scriptExports);
    final ResponseProcessorExecutor filterExecutor = new ResponseProcessorExecutor(scriptService);
    final ResponseProcessorDescriptor filter = ResponseProcessorDescriptor.create().application(ApplicationKey.from("myApp")).name("filter1").build();
    final PortalRequest request = new PortalRequest();
    final PortalResponse response = PortalResponse.create().build();
    try {
        filterExecutor.execute(filter, request, response);
        fail("Expected exception");
    } catch (RenderException e) {
        assertEquals("Missing exported function [responseProcessor] in response filter [/site/processors/filter1.js]", e.getMessage());
    }
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 5 with PortalScriptService

use of com.enonic.xp.portal.script.PortalScriptService in project xp by enonic.

the class IdProviderControllerServiceImplTest method setup.

@BeforeEach
public void setup() throws Exception {
    // Mocks the IdProviderDescriptorService
    final IdProviderDescriptorService idProviderDescriptorService = Mockito.mock(IdProviderDescriptorService.class);
    Mockito.when(idProviderDescriptorService.getDescriptor(ApplicationKey.from("myapplication"))).thenReturn(IdProviderDescriptor.create().key(ApplicationKey.from("myapplication")).build());
    // Mocks the SecurityService
    final SecurityService securityService = Mockito.mock(SecurityService.class);
    final IdProvider emptyIdProvider = IdProvider.create().build();
    final IdProviderConfig idProviderConfig = IdProviderConfig.create().applicationKey(ApplicationKey.from("myapplication")).build();
    final IdProvider idProvider = IdProvider.create().idProviderConfig(idProviderConfig).build();
    Mockito.when(securityService.getIdProvider(IdProviderKey.from("myemptyidprovider"))).thenReturn(emptyIdProvider);
    Mockito.when(securityService.getIdProvider(IdProviderKey.from("myidprovider"))).thenReturn(idProvider);
    Mockito.when(securityService.getIdProvider(IdProviderKey.from("myemptyuserstore"))).thenReturn(emptyIdProvider);
    Mockito.when(securityService.getIdProvider(IdProviderKey.from("myuserstore"))).thenReturn(idProvider);
    // Mocks the PortalScriptService
    final PortalScriptService portalScriptService = setupPortalScriptService();
    // Creates IdProviderControllerScriptFactoryImpl
    final IdProviderControllerScriptFactoryImpl idProviderControllerScriptFactory = new IdProviderControllerScriptFactoryImpl();
    idProviderControllerScriptFactory.setScriptService(portalScriptService);
    // Creates IdProviderControllerServiceImpl
    idProviderControllerService = new IdProviderControllerServiceImpl();
    idProviderControllerService.setIdProviderControllerScriptFactory(idProviderControllerScriptFactory);
    idProviderControllerService.setIdProviderDescriptorService(idProviderDescriptorService);
    idProviderControllerService.setSecurityService(securityService);
}
Also used : IdProviderDescriptorService(com.enonic.xp.idprovider.IdProviderDescriptorService) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) SecurityService(com.enonic.xp.security.SecurityService) IdProvider(com.enonic.xp.security.IdProvider) IdProviderConfig(com.enonic.xp.security.IdProviderConfig) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

PortalScriptService (com.enonic.xp.portal.script.PortalScriptService)6 ResourceKey (com.enonic.xp.resource.ResourceKey)4 Test (org.junit.jupiter.api.Test)4 PortalRequest (com.enonic.xp.portal.PortalRequest)3 PortalResponse (com.enonic.xp.portal.PortalResponse)3 ScriptExports (com.enonic.xp.script.ScriptExports)3 ResponseProcessorDescriptor (com.enonic.xp.site.processor.ResponseProcessorDescriptor)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Application (com.enonic.xp.app.Application)1 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 ApplicationService (com.enonic.xp.app.ApplicationService)1 ConfigBuilder (com.enonic.xp.config.ConfigBuilder)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 IdProviderDescriptorService (com.enonic.xp.idprovider.IdProviderDescriptorService)1 DescriptorKey (com.enonic.xp.page.DescriptorKey)1 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)1 PortalResponseMapper (com.enonic.xp.portal.impl.mapper.PortalResponseMapper)1 RenderException (com.enonic.xp.portal.impl.rendering.RenderException)1 PortalScriptServiceImpl (com.enonic.xp.portal.impl.script.PortalScriptServiceImpl)1 ResourceService (com.enonic.xp.resource.ResourceService)1