Search in sources :

Example 16 with Resource

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

the class TaskDescriptorLoaderTest method testLoad.

@Test
public void testLoad() {
    final DescriptorKey descriptorKey = DescriptorKey.from("myapp1:task1");
    final ResourceKey resourceKey = this.loader.toResource(descriptorKey);
    assertEquals("myapp1:/tasks/task1/task1.xml", resourceKey.toString());
    final Resource resource = this.resourceService.getResource(resourceKey);
    final TaskDescriptor descriptor = this.loader.load(descriptorKey, resource);
    assertEquals("MyTask", descriptor.getDescription());
    FormItem formItem = descriptor.getConfig().getFormItem("param1");
    assertEquals(" something ", formItem.toInput().getDefaultValue().getRootValue());
}
Also used : TaskDescriptor(com.enonic.xp.task.TaskDescriptor) FormItem(com.enonic.xp.form.FormItem) Resource(com.enonic.xp.resource.Resource) DescriptorKey(com.enonic.xp.page.DescriptorKey) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 17 with Resource

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

the class WebAppHandlerTest method handle_serveAsset.

@Test
public void handle_serveAsset() throws Exception {
    final Resource resource = mockResource("myapp:/assets/a/b.txt", "hello".getBytes());
    this.request.setRawPath("/webapp/myapp/a/b.txt");
    this.request.setMethod(HttpMethod.GET);
    final WebResponse response = this.handler.doHandle(this.request, null, this.chain);
    assertEquals(HttpStatus.OK, response.getStatus());
    assertSame(resource, response.getBody());
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) MockResource(com.enonic.xp.resource.MockResource) Resource(com.enonic.xp.resource.Resource) Test(org.junit.jupiter.api.Test)

Example 18 with Resource

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

the class ScriptExportsCache method getOrCompute.

public Object getOrCompute(final ResourceKey key, final Function<Resource, Object> requireFunction) throws InterruptedException, TimeoutException {
    ScriptExportEntry cached = cache.get(key);
    if (cached != null) {
        return cached.value;
    }
    if (lock.tryLock(5, TimeUnit.MINUTES)) {
        try {
            cached = cache.get(key);
            if (cached != null) {
                return cached.value;
            }
            final Resource resource = resourceLookup.apply(key);
            final Object value = requireFunction.apply(resource);
            cache.put(key, new ScriptExportEntry(resource, value));
            return value;
        } finally {
            lock.unlock();
        }
    } else {
        throw new TimeoutException();
    }
}
Also used : Resource(com.enonic.xp.resource.Resource) TimeoutException(java.util.concurrent.TimeoutException)

Example 19 with Resource

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

the class ScriptExportsCacheTest method getOrCompute_calls_require_only_once.

@Test
void getOrCompute_calls_require_only_once() throws Exception {
    final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("some.app"), "main.js");
    final Resource resource = mock(Resource.class);
    final Object value = new Object();
    when(requireFunction.apply(resource)).thenReturn(value);
    when(resourceLookup.apply(resourceKey)).thenReturn(resource);
    final ScriptExportsCache scriptExportsCache = new ScriptExportsCache(RunMode.PROD, resourceLookup, expiredCallback);
    final Object getOrCompute1 = scriptExportsCache.getOrCompute(resourceKey, requireFunction);
    final Object getOrCompute2 = scriptExportsCache.getOrCompute(resourceKey, requireFunction);
    assertAll(() -> assertSame(value, getOrCompute1), () -> assertSame(value, getOrCompute2));
    verify(resourceLookup, times(1)).apply(resourceKey);
    verifyNoMoreInteractions(resourceLookup);
    verify(requireFunction, times(1)).apply(resource);
    verifyNoMoreInteractions(requireFunction);
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 20 with Resource

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

the class ScriptExportsCacheTest method expireCacheIfNeeded.

@Test
void expireCacheIfNeeded() throws Exception {
    final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("some.app"), "main.js");
    final Resource resource = mock(Resource.class);
    when(resource.getTimestamp()).thenReturn(1L, 2L);
    final Object value = new Object();
    when(requireFunction.apply(resource)).thenReturn(value);
    when(resourceLookup.apply(resourceKey)).thenReturn(resource);
    final ScriptExportsCache scriptExportsCache = new ScriptExportsCache(RunMode.DEV, resourceLookup, expiredCallback);
    final Object getOrCompute1 = scriptExportsCache.getOrCompute(resourceKey, requireFunction);
    scriptExportsCache.expireCacheIfNeeded();
    final Object getOrCompute2 = scriptExportsCache.getOrCompute(resourceKey, requireFunction);
    assertAll(() -> assertSame(value, getOrCompute1), () -> assertSame(value, getOrCompute2));
    verify(resourceLookup, times(2)).apply(resourceKey);
    verifyNoMoreInteractions(resourceLookup);
    verify(requireFunction, times(2)).apply(resource);
    verifyNoMoreInteractions(requireFunction);
}
Also used : Resource(com.enonic.xp.resource.Resource) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (com.enonic.xp.resource.Resource)28 ResourceKey (com.enonic.xp.resource.ResourceKey)21 Test (org.junit.jupiter.api.Test)14 PortalResponse (com.enonic.xp.portal.PortalResponse)7 ContentService (com.enonic.xp.content.ContentService)6 PortalRequest (com.enonic.xp.portal.PortalRequest)6 ResourceService (com.enonic.xp.resource.ResourceService)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ApplicationKey (com.enonic.xp.app.ApplicationKey)5 Branch (com.enonic.xp.branch.Branch)4 ContentId (com.enonic.xp.content.ContentId)4 ContentPath (com.enonic.xp.content.ContentPath)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 DescriptorKey (com.enonic.xp.page.DescriptorKey)4 RenderMode (com.enonic.xp.portal.RenderMode)4 ErrorHandlerScript (com.enonic.xp.portal.impl.error.ErrorHandlerScript)4 ErrorHandlerScriptFactory (com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory)4 MockResource (com.enonic.xp.resource.MockResource)4 RunMode (com.enonic.xp.server.RunMode)4 Site (com.enonic.xp.site.Site)4