Search in sources :

Example 66 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntriesTest method test_doRemoveAlias2.

@Test
public void test_doRemoveAlias2() throws Exception {
    final Method addResource = MapEntries.class.getDeclaredMethod("addResource", String.class, AtomicBoolean.class);
    addResource.setAccessible(true);
    final Method removeAlias = MapEntries.class.getDeclaredMethod("removeAlias", String.class, String.class, AtomicBoolean.class);
    removeAlias.setAccessible(true);
    assertEquals(0, aliasMap.size());
    Resource parent = mock(Resource.class);
    when(parent.getPath()).thenReturn("/parent");
    final Resource result = mock(Resource.class);
    when(resourceResolver.getResource("/parent/child")).thenReturn(result);
    when(result.getParent()).thenReturn(parent);
    when(result.getPath()).thenReturn("/parent/child");
    when(result.getName()).thenReturn("child");
    when(result.getValueMap()).thenReturn(buildValueMap());
    //testing jcr:content node removal
    final Resource jcrContentResult = mock(Resource.class);
    when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult);
    when(jcrContentResult.getParent()).thenReturn(result);
    when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content");
    when(jcrContentResult.getName()).thenReturn("jcr:content");
    when(jcrContentResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "aliasJcrContent"));
    when(result.getChild("jcr:content")).thenReturn(jcrContentResult);
    addResource.invoke(mapEntries, "/parent/child/jcr:content", new AtomicBoolean());
    Map<String, String> aliasMapEntry = mapEntries.getAliasMap("/parent");
    assertNotNull(aliasMapEntry);
    assertTrue(aliasMapEntry.containsKey("aliasJcrContent"));
    assertEquals("child", aliasMapEntry.get("aliasJcrContent"));
    assertEquals(1, aliasMap.size());
    removeAlias.invoke(mapEntries, "/parent", "/parent/child/jcr:content", new AtomicBoolean());
    aliasMapEntry = mapEntries.getAliasMap("/parent");
    assertNull(aliasMapEntry);
    assertEquals(0, aliasMap.size());
    //re-add node and test nodeDeletion true
    addResource.invoke(mapEntries, "/parent/child/jcr:content", new AtomicBoolean());
    aliasMapEntry = mapEntries.getAliasMap("/parent");
    assertNotNull(aliasMapEntry);
    assertTrue(aliasMapEntry.containsKey("aliasJcrContent"));
    assertEquals("child", aliasMapEntry.get("aliasJcrContent"));
    assertEquals(1, aliasMap.size());
    when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(null);
    when(result.getChild("jcr:content")).thenReturn(null);
    removeAlias.invoke(mapEntries, "/parent", "/parent/child/jcr:content", new AtomicBoolean());
    aliasMapEntry = mapEntries.getAliasMap("/parent");
    assertNull(aliasMapEntry);
    assertEquals(0, aliasMap.size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Resource(org.apache.sling.api.resource.Resource) Method(java.lang.reflect.Method) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 67 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntriesTest method setup.

@SuppressWarnings({ "unchecked" })
@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    final List<VanityPathConfig> configs = new ArrayList<>();
    configs.add(new VanityPathConfig("/libs/", false));
    configs.add(new VanityPathConfig("/libs/denied", true));
    configs.add(new VanityPathConfig("/foo/", false));
    configs.add(new VanityPathConfig("/baa/", false));
    configs.add(new VanityPathConfig("/justVanityPath", false));
    configs.add(new VanityPathConfig("/justVanityPath2", false));
    configs.add(new VanityPathConfig("/badVanityPath", false));
    configs.add(new VanityPathConfig("/redirectingVanityPath", false));
    configs.add(new VanityPathConfig("/redirectingVanityPath301", false));
    configs.add(new VanityPathConfig("/vanityPathOnJcrContent", false));
    Collections.sort(configs);
    vanityBloomFilterFile = new File("src/main/resourcesvanityBloomFilter.txt");
    when(bundle.getSymbolicName()).thenReturn("TESTBUNDLE");
    when(bundleContext.getBundle()).thenReturn(bundle);
    when(bundleContext.getDataFile("vanityBloomFilter.txt")).thenReturn(vanityBloomFilterFile);
    when(resourceResolverFactory.getServiceResourceResolver(any(Map.class))).thenReturn(resourceResolver);
    when(resourceResolverFactory.isVanityPathEnabled()).thenReturn(true);
    when(resourceResolverFactory.getVanityPathConfig()).thenReturn(configs);
    when(resourceResolverFactory.isOptimizeAliasResolutionEnabled()).thenReturn(true);
    when(resourceResolverFactory.getObservationPaths()).thenReturn(new Path[] { new Path("/") });
    when(resourceResolverFactory.getMapRoot()).thenReturn(MapEntries.DEFAULT_MAP_ROOT);
    when(resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(-1L);
    when(resourceResolverFactory.isMaxCachedVanityPathEntriesStartup()).thenReturn(true);
    when(resourceResolver.findResources(anyString(), eq("sql"))).thenReturn(Collections.<Resource>emptySet().iterator());
    mapEntries = new MapEntries(resourceResolverFactory, bundleContext, eventAdmin);
    final Field aliasMapField = MapEntries.class.getDeclaredField("aliasMap");
    aliasMapField.setAccessible(true);
    this.aliasMap = (Map<String, Map<String, String>>) aliasMapField.get(mapEntries);
}
Also used : Path(org.apache.sling.api.resource.path.Path) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) VanityPathConfig(org.apache.sling.resourceresolver.impl.mapping.MapConfigurationProvider.VanityPathConfig) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map) HashMap(java.util.HashMap) Before(org.junit.Before)

Example 68 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntriesTest method test_getVanityPaths_5.

@Test
public //SLING-4891
void test_getVanityPaths_5() throws Exception {
    final Resource justVanityPath = mock(Resource.class, "justVanityPath");
    when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath);
    when(justVanityPath.getPath()).thenReturn("/justVanityPath");
    when(justVanityPath.getName()).thenReturn("justVanityPath");
    when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath"));
    when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {

        @Override
        public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments()[0].toString().contains("sling:vanityPath")) {
                return Collections.singleton(justVanityPath).iterator();
            } else {
                return Collections.<Resource>emptySet().iterator();
            }
        }
    });
    when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(2L);
    when(this.resourceResolverFactory.isMaxCachedVanityPathEntriesStartup()).thenReturn(false);
    Method method = MapEntries.class.getDeclaredMethod("getVanityPaths", String.class);
    method.setAccessible(true);
    method.invoke(mapEntries, "/target/justVanityPath");
    Field vanityCounter = MapEntries.class.getDeclaredField("vanityCounter");
    vanityCounter.setAccessible(true);
    AtomicLong counter = (AtomicLong) vanityCounter.get(mapEntries);
    assertEquals(2, counter.longValue());
    final Resource justVanityPath2 = mock(Resource.class, "justVanityPath2");
    when(resourceResolver.getResource("/justVanityPath2")).thenReturn(justVanityPath2);
    when(justVanityPath2.getPath()).thenReturn("/justVanityPath2");
    when(justVanityPath2.getName()).thenReturn("justVanityPath2");
    when(justVanityPath2.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath", "sling:vanityOrder", 100));
    when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {

        @Override
        public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments()[0].toString().contains("sling:vanityPath")) {
                return Collections.singleton(justVanityPath).iterator();
            } else {
                return Collections.<Resource>emptySet().iterator();
            }
        }
    });
    method.invoke(mapEntries, "/target/justVanityPath");
    counter = (AtomicLong) vanityCounter.get(mapEntries);
    assertEquals(2, counter.longValue());
}
Also used : Field(java.lang.reflect.Field) AtomicLong(java.util.concurrent.atomic.AtomicLong) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Resource(org.apache.sling.api.resource.Resource) Iterator(java.util.Iterator) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 69 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntriesTest method test_vanity_path_registration.

@Test
public void test_vanity_path_registration() throws Exception {
    // specifically making this a weird value because we want to verify that
    // the configuration value is being used
    int DEFAULT_VANITY_STATUS = 333333;
    when(resourceResolverFactory.getDefaultVanityPathRedirectStatus()).thenReturn(DEFAULT_VANITY_STATUS);
    final List<Resource> resources = new ArrayList<>();
    Resource justVanityPath = mock(Resource.class, "justVanityPath");
    when(justVanityPath.getPath()).thenReturn("/justVanityPath");
    when(justVanityPath.getName()).thenReturn("justVanityPath");
    when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath"));
    resources.add(justVanityPath);
    Resource badVanityPath = mock(Resource.class, "badVanityPath");
    when(badVanityPath.getPath()).thenReturn("/badVanityPath");
    when(badVanityPath.getName()).thenReturn("badVanityPath");
    when(badVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/content/mypage/en-us-{132"));
    resources.add(badVanityPath);
    Resource redirectingVanityPath = mock(Resource.class, "redirectingVanityPath");
    when(redirectingVanityPath.getPath()).thenReturn("/redirectingVanityPath");
    when(redirectingVanityPath.getName()).thenReturn("redirectingVanityPath");
    when(redirectingVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/redirectingVanityPath", "sling:redirect", true));
    resources.add(redirectingVanityPath);
    Resource redirectingVanityPath301 = mock(Resource.class, "redirectingVanityPath301");
    when(redirectingVanityPath301.getPath()).thenReturn("/redirectingVanityPath301");
    when(redirectingVanityPath301.getName()).thenReturn("redirectingVanityPath301");
    when(redirectingVanityPath301.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/redirectingVanityPath301", "sling:redirect", true, "sling:redirectStatus", 301));
    resources.add(redirectingVanityPath301);
    Resource vanityPathOnJcrContentParent = mock(Resource.class, "vanityPathOnJcrContentParent");
    when(vanityPathOnJcrContentParent.getPath()).thenReturn("/vanityPathOnJcrContent");
    when(vanityPathOnJcrContentParent.getName()).thenReturn("vanityPathOnJcrContent");
    Resource vanityPathOnJcrContent = mock(Resource.class, "vanityPathOnJcrContent");
    when(vanityPathOnJcrContent.getPath()).thenReturn("/vanityPathOnJcrContent/jcr:content");
    when(vanityPathOnJcrContent.getName()).thenReturn("jcr:content");
    when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent);
    when(vanityPathOnJcrContent.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/vanityPathOnJcrContent"));
    resources.add(vanityPathOnJcrContent);
    when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {

        @Override
        public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments()[0].toString().contains("sling:vanityPath")) {
                return resources.iterator();
            } else {
                return Collections.<Resource>emptySet().iterator();
            }
        }
    });
    mapEntries.doInit();
    mapEntries.initializeVanityPaths();
    List<MapEntry> entries = mapEntries.getResolveMaps();
    assertEquals(8, entries.size());
    for (MapEntry entry : entries) {
        if (entry.getPattern().contains("/target/redirectingVanityPath301")) {
            assertEquals(301, entry.getStatus());
            assertFalse(entry.isInternal());
        } else if (entry.getPattern().contains("/target/redirectingVanityPath")) {
            assertEquals(DEFAULT_VANITY_STATUS, entry.getStatus());
            assertFalse(entry.isInternal());
        } else if (entry.getPattern().contains("/target/justVanityPath")) {
            assertTrue(entry.isInternal());
        } else if (entry.getPattern().contains("/target/vanityPathOnJcrContent")) {
            for (String redirect : entry.getRedirect()) {
                assertFalse(redirect.contains("jcr:content"));
            }
        }
    }
    Field field = MapEntries.class.getDeclaredField("vanityTargets");
    field.setAccessible(true);
    @SuppressWarnings("unchecked") Map<String, List<String>> vanityTargets = (Map<String, List<String>>) field.get(mapEntries);
    assertEquals(4, vanityTargets.size());
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) Matchers.anyString(org.mockito.Matchers.anyString) Field(java.lang.reflect.Field) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 70 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class MapEntriesTest method test_doRemoveAlias5.

@Test
public void test_doRemoveAlias5() throws Exception {
    final Method addResource = MapEntries.class.getDeclaredMethod("addResource", String.class, AtomicBoolean.class);
    addResource.setAccessible(true);
    final Method removeAlias = MapEntries.class.getDeclaredMethod("removeAlias", String.class, String.class, AtomicBoolean.class);
    removeAlias.setAccessible(true);
    assertEquals(0, aliasMap.size());
    Resource parent = mock(Resource.class);
    when(parent.getPath()).thenReturn("/");
    final Resource result = mock(Resource.class);
    when(resourceResolver.getResource("/parent")).thenReturn(result);
    when(result.getParent()).thenReturn(parent);
    when(result.getPath()).thenReturn("/parent");
    when(result.getName()).thenReturn("parent");
    when(result.getValueMap()).thenReturn(buildValueMap());
    //testing jcr:content node removal
    final Resource jcrContentResult = mock(Resource.class);
    when(resourceResolver.getResource("/parent/jcr:content")).thenReturn(jcrContentResult);
    when(jcrContentResult.getParent()).thenReturn(result);
    when(jcrContentResult.getPath()).thenReturn("/parent/jcr:content");
    when(jcrContentResult.getName()).thenReturn("jcr:content");
    when(jcrContentResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "aliasJcrContent"));
    when(result.getChild("jcr:content")).thenReturn(jcrContentResult);
    addResource.invoke(mapEntries, "/parent/jcr:content", new AtomicBoolean());
    Map<String, String> aliasMapEntry = mapEntries.getAliasMap("/");
    assertNotNull(aliasMapEntry);
    assertTrue(aliasMapEntry.containsKey("aliasJcrContent"));
    assertEquals("parent", aliasMapEntry.get("aliasJcrContent"));
    assertEquals(1, aliasMap.size());
    removeAlias.invoke(mapEntries, "/", "/parent/jcr:content", new AtomicBoolean());
    aliasMapEntry = mapEntries.getAliasMap("/");
    assertNull(aliasMapEntry);
    assertEquals(0, aliasMap.size());
    //re-add node and test nodeDeletion true
    addResource.invoke(mapEntries, "/parent/jcr:content", new AtomicBoolean());
    aliasMapEntry = mapEntries.getAliasMap("/");
    assertNotNull(aliasMapEntry);
    assertTrue(aliasMapEntry.containsKey("aliasJcrContent"));
    assertEquals("parent", aliasMapEntry.get("aliasJcrContent"));
    assertEquals(1, aliasMap.size());
    when(resourceResolver.getResource("/parent/jcr:content")).thenReturn(null);
    when(result.getChild("jcr:content")).thenReturn(null);
    removeAlias.invoke(mapEntries, "/", "/parent/jcr:content", new AtomicBoolean());
    aliasMapEntry = mapEntries.getAliasMap("/");
    assertNull(aliasMapEntry);
    assertEquals(0, aliasMap.size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Resource(org.apache.sling.api.resource.Resource) Method(java.lang.reflect.Method) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1151 Test (org.junit.Test)633 ValueMap (org.apache.sling.api.resource.ValueMap)263 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)164 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)159 Node (javax.jcr.Node)116 HashMap (java.util.HashMap)104 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)99 PersistenceException (org.apache.sling.api.resource.PersistenceException)94 ArrayList (java.util.ArrayList)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)55 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)43 Map (java.util.Map)41 InputStream (java.io.InputStream)38 LoginException (org.apache.sling.api.resource.LoginException)37 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)36 Iterator (java.util.Iterator)33