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);
}
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;
}
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());
}
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());
}
}
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);
}
Aggregations