Search in sources :

Example 1 with MockServiceReference

use of org.apache.sling.commons.testing.osgi.MockServiceReference in project sling by apache.

the class ServletResourceProviderCreateTest method testCreateSelectorsExtensions.

public void testCreateSelectorsExtensions() {
    MockServiceReference msr = new MockServiceReference(null);
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, RES_TYPE);
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, new String[] { HttpConstants.METHOD_GET });
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_SELECTORS, new String[] { "ext" });
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_EXTENSIONS, new String[] { "json" });
    ServletResourceProvider srp = factory.create(msr, TEST_SERVLET);
    assertNotNull(srp);
    String[] paths = srp.getServletPaths();
    assertNotNull(paths);
    assertEquals(1, paths.length);
    Set<String> checkerSet = new HashSet<>();
    checkerSet.add(ROOT + RES_TYPE_PATH + "/ext.json." + HttpConstants.METHOD_GET + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION);
    for (String path : paths) {
        assertTrue(path + " not expected", checkerSet.remove(path));
    }
    assertTrue(checkerSet.isEmpty());
}
Also used : MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) HashSet(java.util.HashSet)

Example 2 with MockServiceReference

use of org.apache.sling.commons.testing.osgi.MockServiceReference in project sling by apache.

the class ServletResourceProviderCreateTest method testCreateMethodsExtensions.

public void testCreateMethodsExtensions() {
    MockServiceReference msr = new MockServiceReference(null);
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, RES_TYPE);
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, new String[] { HttpConstants.METHOD_GET, HttpConstants.METHOD_POST });
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_EXTENSIONS, new String[] { "json", "html" });
    ServletResourceProvider srp = factory.create(msr, TEST_SERVLET);
    assertNotNull(srp);
    String[] paths = srp.getServletPaths();
    assertNotNull(paths);
    assertEquals(4, paths.length);
    Set<String> checkerSet = new HashSet<>();
    checkerSet.add(ROOT + RES_TYPE_PATH + "/json." + HttpConstants.METHOD_GET + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION);
    checkerSet.add(ROOT + RES_TYPE_PATH + "/html." + HttpConstants.METHOD_GET + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION);
    checkerSet.add(ROOT + RES_TYPE_PATH + "/json." + HttpConstants.METHOD_POST + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION);
    checkerSet.add(ROOT + RES_TYPE_PATH + "/html." + HttpConstants.METHOD_POST + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION);
    for (String path : paths) {
        assertTrue(path + " not expected", checkerSet.remove(path));
    }
    assertTrue(checkerSet.isEmpty());
}
Also used : MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) HashSet(java.util.HashSet)

Example 3 with MockServiceReference

use of org.apache.sling.commons.testing.osgi.MockServiceReference in project sling by apache.

the class SlingServletResolverTest method setUp.

@Before
public void setUp() throws Exception {
    mockResourceResolver = new MockResourceResolver() {

        @Override
        public void close() {
        // nothing to do;
        }

        @Override
        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
            return null;
        }

        @Override
        public ResourceResolver clone(Map<String, Object> authenticationInfo) throws LoginException {
            throw new LoginException("MockResourceResolver can't be cloned - excepted for this test!");
        }

        @Override
        public void refresh() {
        // nothing to do
        }
    };
    mockResourceResolver.setSearchPath("/");
    final ResourceResolverFactory factory = new ResourceResolverFactory() {

        @Override
        public ResourceResolver getAdministrativeResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getServiceResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
            return mockResourceResolver;
        }

        @Override
        public ResourceResolver getThreadResourceResolver() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    servlet = new MockSlingRequestHandlerServlet();
    servletResolver = new SlingServletResolver();
    Class<?> resolverClass = servletResolver.getClass();
    // set resource resolver factory
    final Field resolverField = resolverClass.getDeclaredField("resourceResolverFactory");
    resolverField.setAccessible(true);
    resolverField.set(servletResolver, factory);
    MockBundle bundle = new MockBundle(1L);
    MockBundleContext bundleContext = new MockBundleContext(bundle) {

        @Override
        public ServiceRegistration registerService(String s, Object o, Dictionary dictionary) {
            return null;
        }

        @Override
        public ServiceRegistration registerService(String[] strings, Object o, Dictionary dictionary) {
            return null;
        }
    };
    MockServiceReference serviceReference = new MockServiceReference(bundle);
    serviceReference.setProperty(Constants.SERVICE_ID, 1L);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_NAME, SERVLET_NAME);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_PATHS, SERVLET_PATH);
    serviceReference.setProperty(ServletResolverConstants.SLING_SERVLET_EXTENSIONS, SERVLET_EXTENSION);
    servletResolver.bindServlet(SlingServletResolverTest.this.servlet, serviceReference);
    servletResolver.activate(bundleContext, new SlingServletResolver.Config() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return SlingServletResolver.Config.class;
        }

        @Override
        public String servletresolver_servletRoot() {
            return "0";
        }

        @Override
        public String[] servletresolver_paths() {
            return new String[] { "/" };
        }

        @Override
        public String[] servletresolver_defaultExtensions() {
            // TODO Auto-generated method stub
            return new String[] { "html" };
        }

        @Override
        public int servletresolver_cacheSize() {
            return 200;
        }
    });
    String path = "/" + MockSlingHttpServletRequest.RESOURCE_TYPE + "/" + ResourceUtil.getName(MockSlingHttpServletRequest.RESOURCE_TYPE) + ".servlet";
    MockServletResource res = new MockServletResource(mockResourceResolver, servlet, path);
    mockResourceResolver.addResource(res);
    MockResource parent = new MockResource(mockResourceResolver, ResourceUtil.getParent(res.getPath()), "nt:folder");
    mockResourceResolver.addResource(parent);
    List<Resource> childRes = new ArrayList<>();
    childRes.add(res);
    mockResourceResolver.addChildren(parent, childRes);
}
Also used : Dictionary(java.util.Dictionary) MockResource(org.apache.sling.commons.testing.sling.MockResource) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) MockBundle(org.apache.sling.commons.testing.osgi.MockBundle) MockServletResource(org.apache.sling.servlets.resolver.internal.resource.MockServletResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.commons.testing.sling.MockResource) MockServletResource(org.apache.sling.servlets.resolver.internal.resource.MockServletResource) Annotation(java.lang.annotation.Annotation) MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) MockBundleContext(org.apache.sling.commons.testing.osgi.MockBundleContext) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) Map(java.util.Map) Before(org.junit.Before)

Example 4 with MockServiceReference

use of org.apache.sling.commons.testing.osgi.MockServiceReference in project sling by apache.

the class SlingServletResolverTest method testCreateServiceRegistrationProperties.

@Test
public void testCreateServiceRegistrationProperties() throws Throwable {
    MockServiceReference msr = new MockServiceReference(null);
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, "sample");
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, "GET");
    Field srpf = SlingServletResolver.class.getDeclaredField("servletResourceProviderFactory");
    srpf.setAccessible(true);
    ServletResourceProviderFactory factory = (ServletResourceProviderFactory) srpf.get(servletResolver);
    ServletResourceProvider servlet = factory.create(msr, null);
    Method createServiceProperties = SlingServletResolver.class.getDeclaredMethod("createServiceProperties", ServiceReference.class, ServletResourceProvider.class, String.class);
    createServiceProperties.setAccessible(true);
    // no ranking
    assertNull(msr.getProperty(Constants.SERVICE_RANKING));
    @SuppressWarnings("unchecked") final Dictionary<String, Object> p1 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
    assertNull(p1.get(Constants.SERVICE_RANKING));
    // illegal type of ranking
    Object nonIntValue = "Some Non Integer Value";
    msr.setProperty(Constants.SERVICE_RANKING, nonIntValue);
    assertEquals(nonIntValue, msr.getProperty(Constants.SERVICE_RANKING));
    @SuppressWarnings("unchecked") final Dictionary<String, Object> p2 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
    assertNull(p2.get(Constants.SERVICE_RANKING));
    // illegal type of ranking
    Object intValue = Integer.valueOf(123);
    msr.setProperty(Constants.SERVICE_RANKING, intValue);
    assertEquals(intValue, msr.getProperty(Constants.SERVICE_RANKING));
    @SuppressWarnings("unchecked") final Dictionary<String, Object> p3 = (Dictionary<String, Object>) createServiceProperties.invoke(servletResolver, msr, servlet, "/a");
    assertEquals(intValue, p3.get(Constants.SERVICE_RANKING));
}
Also used : MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) Field(java.lang.reflect.Field) Dictionary(java.util.Dictionary) ServletResourceProviderFactory(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 5 with MockServiceReference

use of org.apache.sling.commons.testing.osgi.MockServiceReference in project sling by apache.

the class ServletResourceProviderCreateTest method testCreateMethodsMultiple.

public void testCreateMethodsMultiple() {
    MockServiceReference msr = new MockServiceReference(null);
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, RES_TYPE);
    msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, new String[] { "GET", "POST", "PUT" });
    ServletResourceProvider srp = factory.create(msr, TEST_SERVLET);
    assertNotNull(srp);
    String[] paths = srp.getServletPaths();
    assertNotNull(paths);
    assertEquals(3, paths.length);
    Set<String> checkerSet = new HashSet<>();
    checkerSet.add(ROOT + RES_TYPE_PATH + "/" + HttpConstants.METHOD_GET + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION);
    checkerSet.add(ROOT + RES_TYPE_PATH + "/" + HttpConstants.METHOD_POST + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION);
    checkerSet.add(ROOT + RES_TYPE_PATH + "/" + HttpConstants.METHOD_PUT + ServletResourceProviderFactory.SERVLET_PATH_EXTENSION);
    for (String path : paths) {
        assertTrue(path + " not expected", checkerSet.remove(path));
    }
    assertTrue(checkerSet.isEmpty());
}
Also used : MockServiceReference(org.apache.sling.commons.testing.osgi.MockServiceReference) HashSet(java.util.HashSet)

Aggregations

MockServiceReference (org.apache.sling.commons.testing.osgi.MockServiceReference)8 HashSet (java.util.HashSet)6 Field (java.lang.reflect.Field)2 Dictionary (java.util.Dictionary)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 LoginException (org.apache.sling.api.resource.LoginException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)1 MockBundle (org.apache.sling.commons.testing.osgi.MockBundle)1 MockBundleContext (org.apache.sling.commons.testing.osgi.MockBundleContext)1 MockResource (org.apache.sling.commons.testing.sling.MockResource)1 MockResourceResolver (org.apache.sling.commons.testing.sling.MockResourceResolver)1 MockServletResource (org.apache.sling.servlets.resolver.internal.resource.MockServletResource)1 ServletResourceProvider (org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider)1 ServletResourceProviderFactory (org.apache.sling.servlets.resolver.internal.resource.ServletResourceProviderFactory)1 Before (org.junit.Before)1