Search in sources :

Example 1 with PathNotFoundException

use of org.craftercms.core.exception.PathNotFoundException in project core by craftercms.

the class ContentStoreRestControllerTest method testHandlePathNotFoundException.

@Test
public void testHandlePathNotFoundException() throws Exception {
    PathNotFoundException ex = new PathNotFoundException("This is a test");
    Map<String, Object> model = storeRestController.handlePathNotFoundException(request, ex);
    assertEquals(ex.getMessage(), model.get(MESSAGE_MODEL_ATTRIBUTE_NAME));
}
Also used : CachingAwareObject(org.craftercms.core.util.cache.CachingAwareObject) PathNotFoundException(org.craftercms.core.exception.PathNotFoundException) Test(org.junit.Test)

Example 2 with PathNotFoundException

use of org.craftercms.core.exception.PathNotFoundException in project core by craftercms.

the class ContentStoreServiceImplTest method testGetDescriptorItem.

@Test
public void testGetDescriptorItem() throws Exception {
    Item item = contentStoreService.findItem(context, CONTENT_FR_ES_DESCRIPTOR_PATH);
    assertContentFrEsDescriptorItem(item, false);
    // Sleep so that we get a different caching time in the next call if the caching is being done wrong.
    Thread.sleep(100);
    Item cachedItem = contentStoreService.getItem(context, CONTENT_FR_ES_DESCRIPTOR_PATH);
    assertEquals(item, cachedItem);
    assertCaching(item, cachedItem);
    item = contentStoreService.findItem(context, INVALID_PATH);
    assertNull(item);
    try {
        contentStoreService.getItem(context, INVALID_PATH);
        fail("Expected " + PathNotFoundException.class.getName());
    } catch (PathNotFoundException e) {
    }
    TextMetaDataExtractingProcessor extractor = new TextMetaDataExtractingProcessor("//first-quote", "//second-quote", "//third-quote");
    item = contentStoreService.findItem(context, DEFAULT_CACHING_OPTIONS, CONTENT_FR_ES_DESCRIPTOR_PATH, extractor);
    assertContentFrEsDescriptorItem(item, true);
    // Sleep so that we get a different caching time in the next call if the caching is being done wrong.
    Thread.sleep(100);
    cachedItem = contentStoreService.getItem(context, DEFAULT_CACHING_OPTIONS, CONTENT_FR_ES_DESCRIPTOR_PATH, extractor);
    assertEquals(item, cachedItem);
    assertCaching(item, cachedItem);
    item = contentStoreService.findItem(context, DEFAULT_CACHING_OPTIONS, INVALID_PATH, extractor);
    assertNull(item);
    try {
        contentStoreService.getItem(context, DEFAULT_CACHING_OPTIONS, INVALID_PATH, extractor);
        fail("Expected " + PathNotFoundException.class.getName());
    } catch (PathNotFoundException e) {
    }
}
Also used : Item(org.craftercms.core.service.Item) TextMetaDataExtractingProcessor(org.craftercms.core.processors.impl.TextMetaDataExtractingProcessor) PathNotFoundException(org.craftercms.core.exception.PathNotFoundException) Test(org.junit.Test) InheritLevelsMergeStrategyTest(org.craftercms.core.xml.mergers.impl.strategies.InheritLevelsMergeStrategyTest)

Example 3 with PathNotFoundException

use of org.craftercms.core.exception.PathNotFoundException in project engine by craftercms.

the class ContentStoreServiceMockUtils method setUpGetContentFromClassPath.

public static ContentStoreService setUpGetContentFromClassPath(ContentStoreService mock) {
    Answer<Content> getContentAnswer = new Answer<Content>() {

        @Override
        public Content answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            String url;
            if (args.length == 2) {
                url = (String) args[1];
            } else {
                url = (String) args[2];
            }
            Content content = getContentFromClassPath(url);
            if (content == null) {
                throw new PathNotFoundException();
            }
            return content;
        }
    };
    Answer<Boolean> existsAnswer = new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            String url = (String) args[1];
            return new ClassPathResource(url).exists();
        }
    };
    when(mock.getContent(any(Context.class), anyString())).then(getContentAnswer);
    when(mock.getContent(any(Context.class), any(CachingOptions.class), anyString())).then(getContentAnswer);
    when(mock.exists(any(Context.class), anyString())).then(existsAnswer);
    when(mock.findChildren(any(Context.class), anyString())).then(new Answer<List<Item>>() {

        @Override
        public List<Item> answer(InvocationOnMock invocation) throws Throwable {
            String folderUrl = (String) invocation.getArguments()[1];
            Resource folderRes = new ClassPathResource(folderUrl);
            File folder = folderRes.getFile();
            String[] childNames = folder.list();
            List<Item> children = new ArrayList<>(childNames.length);
            for (String childName : childNames) {
                Item child = new Item();
                child.setUrl(folderUrl + "/" + childName);
                children.add(child);
            }
            return children;
        }
    });
    return mock;
}
Also used : Context(org.craftercms.core.service.Context) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Mockito.anyString(org.mockito.Mockito.anyString) ClassPathResource(org.springframework.core.io.ClassPathResource) Answer(org.mockito.stubbing.Answer) Item(org.craftercms.core.service.Item) CachingOptions(org.craftercms.core.service.CachingOptions) Content(org.craftercms.core.service.Content) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) List(java.util.List) PathNotFoundException(org.craftercms.core.exception.PathNotFoundException) File(java.io.File)

Example 4 with PathNotFoundException

use of org.craftercms.core.exception.PathNotFoundException in project core by craftercms.

the class ContentStoreServiceImplTest method testGetChildren.

@Test
public void testGetChildren() throws Exception {
    CachingAwareList<Item> children = (CachingAwareList<Item>) contentStoreService.findChildren(context, ROOT_FOLDER_PATH);
    assertRootFolderChildren(children, false, false);
    // Sleep so that we get a different caching time in the next call if the caching is being done wrong.
    Thread.sleep(100);
    CachingAwareList<Item> cachedChildren = (CachingAwareList<Item>) contentStoreService.getChildren(context, ROOT_FOLDER_PATH);
    assertEquals(children, cachedChildren);
    assertListCaching(children, cachedChildren);
    children = (CachingAwareList<Item>) contentStoreService.findChildren(context, INVALID_PATH);
    assertNull(children);
    try {
        contentStoreService.getChildren(context, INVALID_PATH);
        fail("Expected " + PathNotFoundException.class.getName());
    } catch (PathNotFoundException e) {
    }
    ItemProcessorPipeline extractorPipeline = new ItemProcessorPipeline();
    extractorPipeline.addProcessor(new TextMetaDataCollectionExtractingProcessor("//extension"));
    extractorPipeline.addProcessor(new TextMetaDataExtractingProcessor("//size"));
    children = (CachingAwareList<Item>) contentStoreService.findChildren(context, DEFAULT_CACHING_OPTIONS, ROOT_FOLDER_PATH, OnlyNonDescriptorsFilter.instance, extractorPipeline);
    assertRootFolderChildren(children, true, true);
    // Sleep so that we get a different caching time in the next call if the caching is being done wrong.
    Thread.sleep(100);
    cachedChildren = (CachingAwareList<Item>) contentStoreService.getChildren(context, DEFAULT_CACHING_OPTIONS, ROOT_FOLDER_PATH, OnlyNonDescriptorsFilter.instance, extractorPipeline);
    assertEquals(children, cachedChildren);
    assertListCaching(children, cachedChildren);
    children = (CachingAwareList<Item>) contentStoreService.findChildren(context, DEFAULT_CACHING_OPTIONS, INVALID_PATH, OnlyNonDescriptorsFilter.instance, extractorPipeline);
    assertNull(children);
    try {
        contentStoreService.getChildren(context, DEFAULT_CACHING_OPTIONS, INVALID_PATH, OnlyNonDescriptorsFilter.instance, extractorPipeline);
        assertNull(children);
        fail("Expected " + PathNotFoundException.class.getName());
    } catch (PathNotFoundException e) {
    }
}
Also used : Item(org.craftercms.core.service.Item) ItemProcessorPipeline(org.craftercms.core.processors.impl.ItemProcessorPipeline) TextMetaDataCollectionExtractingProcessor(org.craftercms.core.processors.impl.TextMetaDataCollectionExtractingProcessor) TextMetaDataExtractingProcessor(org.craftercms.core.processors.impl.TextMetaDataExtractingProcessor) PathNotFoundException(org.craftercms.core.exception.PathNotFoundException) CachingAwareList(org.craftercms.core.util.cache.impl.CachingAwareList) Test(org.junit.Test) InheritLevelsMergeStrategyTest(org.craftercms.core.xml.mergers.impl.strategies.InheritLevelsMergeStrategyTest)

Example 5 with PathNotFoundException

use of org.craftercms.core.exception.PathNotFoundException in project core by craftercms.

the class ContentStoreServiceImplTest method testGetTree.

@Test
public void testGetTree() throws Exception {
    Tree tree = contentStoreService.findTree(context, ROOT_FOLDER_PATH);
    assertRootFolderTree(tree, UNLIMITED_TREE_DEPTH, false, false);
    // Sleep so that we get a different caching time in the next call if the caching is being done wrong.
    Thread.sleep(100);
    Tree cachedTree = contentStoreService.getTree(context, ROOT_FOLDER_PATH);
    assertEquals(tree, cachedTree);
    assertTreeCaching(tree, cachedTree);
    tree = contentStoreService.findTree(context, INVALID_PATH);
    assertNull(tree);
    try {
        contentStoreService.getTree(context, INVALID_PATH);
        fail("Expected " + PathNotFoundException.class.getName());
    } catch (PathNotFoundException e) {
    }
    tree = contentStoreService.findTree(context, ROOT_FOLDER_PATH, 1);
    assertRootFolderTree(tree, 1, false, false);
    // Sleep so that we get a different caching time in the next call if the caching is being done wrong.
    Thread.sleep(100);
    cachedTree = contentStoreService.getTree(context, ROOT_FOLDER_PATH, 1);
    assertEquals(tree, cachedTree);
    assertTreeCaching(tree, cachedTree);
    tree = contentStoreService.findTree(context, INVALID_PATH, 1);
    assertNull(tree);
    try {
        contentStoreService.getTree(context, INVALID_PATH, 1);
        fail("Expected " + PathNotFoundException.class.getName());
    } catch (PathNotFoundException e) {
    }
    ItemProcessorPipeline extractorPipeline = new ItemProcessorPipeline();
    extractorPipeline.addProcessor(new TextMetaDataCollectionExtractingProcessor("//extension"));
    extractorPipeline.addProcessor(new TextMetaDataExtractingProcessor("//first-quote", "//second-quote", "//third-quote", "//size"));
    tree = contentStoreService.findTree(context, DEFAULT_CACHING_OPTIONS, ROOT_FOLDER_PATH, 1, OnlyNonDescriptorsFilter.instance, extractorPipeline);
    assertRootFolderTree(tree, 1, true, true);
    // Sleep so that we get a different caching time in the next call if the caching is being done wrong.
    Thread.sleep(100);
    cachedTree = contentStoreService.getTree(context, DEFAULT_CACHING_OPTIONS, ROOT_FOLDER_PATH, 1, OnlyNonDescriptorsFilter.instance, extractorPipeline);
    assertEquals(tree, cachedTree);
    assertTreeCaching(tree, cachedTree);
    tree = contentStoreService.findTree(context, DEFAULT_CACHING_OPTIONS, INVALID_PATH, 1, OnlyNonDescriptorsFilter.instance, extractorPipeline);
    assertNull(tree);
    try {
        contentStoreService.getTree(context, DEFAULT_CACHING_OPTIONS, INVALID_PATH, 1, OnlyNonDescriptorsFilter.instance, extractorPipeline);
        fail("Expected " + PathNotFoundException.class.getName());
    } catch (PathNotFoundException e) {
    }
}
Also used : ItemProcessorPipeline(org.craftercms.core.processors.impl.ItemProcessorPipeline) TextMetaDataCollectionExtractingProcessor(org.craftercms.core.processors.impl.TextMetaDataCollectionExtractingProcessor) TextMetaDataExtractingProcessor(org.craftercms.core.processors.impl.TextMetaDataExtractingProcessor) Tree(org.craftercms.core.service.Tree) PathNotFoundException(org.craftercms.core.exception.PathNotFoundException) Test(org.junit.Test) InheritLevelsMergeStrategyTest(org.craftercms.core.xml.mergers.impl.strategies.InheritLevelsMergeStrategyTest)

Aggregations

PathNotFoundException (org.craftercms.core.exception.PathNotFoundException)5 Test (org.junit.Test)4 TextMetaDataExtractingProcessor (org.craftercms.core.processors.impl.TextMetaDataExtractingProcessor)3 Item (org.craftercms.core.service.Item)3 InheritLevelsMergeStrategyTest (org.craftercms.core.xml.mergers.impl.strategies.InheritLevelsMergeStrategyTest)3 ItemProcessorPipeline (org.craftercms.core.processors.impl.ItemProcessorPipeline)2 TextMetaDataCollectionExtractingProcessor (org.craftercms.core.processors.impl.TextMetaDataCollectionExtractingProcessor)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CachingOptions (org.craftercms.core.service.CachingOptions)1 Content (org.craftercms.core.service.Content)1 Context (org.craftercms.core.service.Context)1 Tree (org.craftercms.core.service.Tree)1 CachingAwareObject (org.craftercms.core.util.cache.CachingAwareObject)1 CachingAwareList (org.craftercms.core.util.cache.impl.CachingAwareList)1 Mockito.anyString (org.mockito.Mockito.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1