Search in sources :

Example 41 with ResourceKey

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

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

the class ProcessingCache method doProcess.

private ProcessingEntry doProcess(final ResourceProcessor<?, ?> processor) {
    final ResourceKey resourceKey = processor.toResourceKey();
    final Resource resource = this.loader.apply(resourceKey);
    final Object value = processor.process(resource);
    if (value == null) {
        return null;
    }
    return new ProcessingEntry(resourceKey, value, resource.getTimestamp());
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 43 with ResourceKey

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

the class LocaleServiceImplTest method loadResource.

private Resource loadResource(final InvocationOnMock invocation) {
    final ResourceKey resourceKey = (ResourceKey) invocation.getArguments()[0];
    final String path = resourceKey.getName();
    final URL url = getClass().getResource(path.substring(path.lastIndexOf('/') + 1));
    return new UrlResource(resourceKey, url);
}
Also used : UrlResource(com.enonic.xp.resource.UrlResource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URL(java.net.URL) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 44 with ResourceKey

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

the class LocaleServiceImpl method getAppLocales.

private Set<Locale> getAppLocales(final ApplicationKey applicationKey, final String... bundleNames) {
    LOG.debug("Create app locales for {}", applicationKey);
    final Set<Locale> locales = new LinkedHashSet<>();
    for (final String bundleName : bundleNames) {
        final String bundlePattern = "^" + Pattern.quote(bundleName.startsWith("/") ? bundleName : "/" + bundleName) + ".*\\.properties$";
        final ResourceKeys resourceKeys = resourceService.findFiles(applicationKey, bundlePattern);
        for (ResourceKey resourceKey : resourceKeys) {
            final Locale locale = localeFromResource(resourceKey.getName());
            locales.add(locale);
            if (locale.equals(LOCALE_NO)) {
                locales.add(LOCALE_NB);
                locales.add(LOCALE_NN);
            }
            if (locale.getLanguage().equals(LOCALE_NB.getLanguage()) || locale.getLanguage().equals(LOCALE_NN.getLanguage())) {
                locales.add(LOCALE_NO);
            }
        }
    }
    return locales;
}
Also used : Locale(java.util.Locale) LinkedHashSet(java.util.LinkedHashSet) ResourceKeys(com.enonic.xp.resource.ResourceKeys) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 45 with ResourceKey

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

the class IconLoader method loadIcon.

private static Icon loadIcon(final MacroKey macroKey, final String mimeType, final String ext, final ResourceService resourceService, final String path) {
    final ResourceKey resourceKey = toResourceKey(macroKey, ext, path);
    final Resource resource = resourceService.getResource(resourceKey);
    return doLoadIcon(resource, mimeType);
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey)

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