Search in sources :

Example 1 with NonExistingResource

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

the class AuthUtilTest method test_isRedirectValid_resource_resolver_root_context.

@Test
public void test_isRedirectValid_resource_resolver_root_context() {
    context.checking(new Expectations() {

        {
            allowing(resolver).resolve(with(any(HttpServletRequest.class)), with(equal("/absolute/path")));
            will(returnValue(new SyntheticResource(resolver, "/absolute/path", "test")));
            allowing(resolver).resolve(with(any(HttpServletRequest.class)), with(equal("relative/path")));
            will(returnValue(new NonExistingResource(resolver, "relative/path")));
            allowing(resolver).resolve(with(any(HttpServletRequest.class)), with(any(String.class)));
            will(returnValue(new NonExistingResource(resolver, "/absolute/missing")));
            allowing(request).getAttribute(with(AuthenticationSupport.REQUEST_ATTRIBUTE_RESOLVER));
            will(returnValue(resolver));
            allowing(request).getAttribute(with(any(String.class)));
            will(returnValue(null));
            allowing(request).getContextPath();
            will(returnValue(""));
        }
    });
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "relative/path"));
    TestCase.assertTrue(AuthUtil.isRedirectValid(request, "/absolute/path"));
    TestCase.assertTrue(AuthUtil.isRedirectValid(request, "/absolute/missing"));
    TestCase.assertTrue(AuthUtil.isRedirectValid(request, "/absolute/missing/valid"));
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "/absolute/missing/invalid/<"));
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "/absolute/missing/invalid/>"));
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "/absolute/missing/invalid/'"));
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "/absolute/missing/invalid/\""));
}
Also used : Expectations(org.jmock.Expectations) HttpServletRequest(javax.servlet.http.HttpServletRequest) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 2 with NonExistingResource

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

the class AuthUtilTest method test_isRedirectValid_resource_resolver_non_root_context.

@Test
public void test_isRedirectValid_resource_resolver_non_root_context() {
    context.checking(new Expectations() {

        {
            allowing(resolver).resolve(with(any(HttpServletRequest.class)), with(equal("/absolute/path")));
            will(returnValue(new SyntheticResource(resolver, "/absolute/path", "test")));
            allowing(resolver).resolve(with(any(HttpServletRequest.class)), with(equal("relative/path")));
            will(returnValue(new NonExistingResource(resolver, "relative/path")));
            allowing(request).getAttribute(with(AuthenticationSupport.REQUEST_ATTRIBUTE_RESOLVER));
            will(returnValue(resolver));
            allowing(request).getAttribute(with(any(String.class)));
            will(returnValue(null));
            allowing(request).getContextPath();
            will(returnValue("/ctx"));
        }
    });
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "relative/path"));
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "/absolute/path"));
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "ctx/relative/path"));
    TestCase.assertTrue(AuthUtil.isRedirectValid(request, "/ctx/absolute/path"));
    TestCase.assertFalse(AuthUtil.isRedirectValid(request, "/ctxrelative/path"));
}
Also used : Expectations(org.jmock.Expectations) HttpServletRequest(javax.servlet.http.HttpServletRequest) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 3 with NonExistingResource

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

the class ValidationServiceImplTest method testSyntheticResource.

// see https://issues.apache.org/jira/browse/SLING-5749
@Test
public void testSyntheticResource() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    ResourceProperty property = propertyBuilder.build("field1");
    modelBuilder.resourceProperty(property);
    ChildResource modelChild = new ChildResourceImpl("child", null, true, Collections.singletonList(property), Collections.emptyList());
    modelBuilder.childResource(modelChild);
    modelChild = new ChildResourceImpl("optionalChild", null, false, Collections.singletonList(property), Collections.emptyList());
    modelBuilder.childResource(modelChild);
    ValidationModel vm = modelBuilder.build("sometype", "some source");
    ResourceResolver rr = context.resourceResolver();
    Resource nonExistingResource = new SyntheticResource(rr, "someresource", "resourceType");
    ValidationResult vr = validationService.validate(nonExistingResource, vm);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>containsInAnyOrder(new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"), new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_WITH_NAME, "child")));
}
Also used : ResourceProperty(org.apache.sling.validation.model.ResourceProperty) ValidationModel(org.apache.sling.validation.model.ValidationModel) ChildResourceImpl(org.apache.sling.validation.impl.model.ChildResourceImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ChildResource(org.apache.sling.validation.model.ChildResource) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 4 with NonExistingResource

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

the class ValidationServiceImplTest method testValidateResourceRecursivelyOnNonExistingResource.

// see https://issues.apache.org/jira/browse/SLING-5674
@Test
public void testValidateResourceRecursivelyOnNonExistingResource() throws Exception {
    ResourceResolver rr = context.resourceResolver();
    Resource nonExistingResource = new NonExistingResource(rr, "non-existing-resource");
    ValidationResult vr = validationService.validateResourceRecursively(nonExistingResource, true, null, true);
    Assert.assertTrue("resource should have been considered valid", vr.isValid());
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 5 with NonExistingResource

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

the class ValidationServiceImplTest method testNonExistingResource.

// see https://issues.apache.org/jira/browse/SLING-5674
@Test
public void testNonExistingResource() throws Exception {
    // accept any digits
    propertyBuilder.validator(REGEX_VALIDATOR_ID, 0, RegexValidator.REGEX_PARAM, "\\d");
    ResourceProperty property = propertyBuilder.build("field1");
    modelBuilder.resourceProperty(property);
    ChildResource modelChild = new ChildResourceImpl("child", null, true, Collections.singletonList(property), Collections.emptyList());
    modelBuilder.childResource(modelChild);
    modelChild = new ChildResourceImpl("optionalChild", null, false, Collections.singletonList(property), Collections.emptyList());
    modelBuilder.childResource(modelChild);
    ValidationModel vm = modelBuilder.build("sometype", "some source");
    ResourceResolver rr = context.resourceResolver();
    Resource nonExistingResource = new NonExistingResource(rr, "non-existing-resource");
    ValidationResult vr = validationService.validate(nonExistingResource, vm);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>containsInAnyOrder(new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"), new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_CHILD_RESOURCE_WITH_NAME, "child")));
}
Also used : ResourceProperty(org.apache.sling.validation.model.ResourceProperty) ValidationModel(org.apache.sling.validation.model.ValidationModel) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) ChildResourceImpl(org.apache.sling.validation.impl.model.ChildResourceImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ChildResource(org.apache.sling.validation.model.ChildResource) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Aggregations

NonExistingResource (org.apache.sling.api.resource.NonExistingResource)24 Resource (org.apache.sling.api.resource.Resource)22 Test (org.junit.Test)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Node (javax.jcr.Node)10 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)6 ValidationResult (org.apache.sling.validation.ValidationResult)3 ChildResource (org.apache.sling.validation.model.ChildResource)3 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)3 MergedResource (org.apache.sling.resourcemerger.impl.MergedResource)2 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)2 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)2 ValidationModel (org.apache.sling.validation.model.ValidationModel)2 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)2 Expectations (org.jmock.Expectations)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1