Search in sources :

Example 1 with NameValuePairList

use of org.apache.sling.commons.testing.integration.NameValuePairList in project sling by apache.

the class PropertyRenderingTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // set test values
    testText = "This is a test " + System.currentTimeMillis();
    testMultiText1 = "This is a multivalued test " + System.currentTimeMillis();
    testMultiText2 = "This is another multivalued test " + System.currentTimeMillis();
    slingResourceType = getClass().getName();
    // create the test node, under a path that's specific to this class to allow collisions
    final String url = HTTP_BASE_URL + "/" + getClass().getSimpleName() + "/" + System.currentTimeMillis() + SlingPostConstants.DEFAULT_CREATE_SUFFIX;
    NameValuePairList list = new NameValuePairList();
    list.add("sling:resourceType", slingResourceType);
    list.add("text", testText);
    list.add("multiText", testMultiText1);
    list.add("multiText", testMultiText2);
    displayUrl = testClient.createNode(url, list, null, true);
}
Also used : NameValuePairList(org.apache.sling.commons.testing.integration.NameValuePairList)

Example 2 with NameValuePairList

use of org.apache.sling.commons.testing.integration.NameValuePairList in project sling by apache.

the class PostServletPatchTest method testInvalidPatch.

public void testInvalidPatch() throws Exception {
    final NameValuePairList props = new NameValuePairList();
    // 1. create multi-value property
    props.add("prop@TypeHint", "String[]");
    props.add("prop", "alpha");
    props.add("prop", "beta");
    props.add("prop", "gamma");
    props.add("prop", "delta");
    String location = testClient.createNode(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, props, null, false);
    props.clear();
    // 2. update mv prop through Patch method
    // but use only invalid values
    props.add("prop@Patch", "true");
    props.add("prop", "wrong");
    props.add("prop", "#noop");
    testClient.createNode(location, props, null, false);
    String content = getContent(location + ".json", CONTENT_TYPE_JSON);
    assertJavascript("true", content, "out.println(data.prop.length == 4)");
    assertJavascript("alpha", content, "out.println(data.prop[0])");
    assertJavascript("beta", content, "out.println(data.prop[1])");
    assertJavascript("gamma", content, "out.println(data.prop[2])");
    assertJavascript("delta", content, "out.println(data.prop[3])");
}
Also used : NameValuePairList(org.apache.sling.commons.testing.integration.NameValuePairList)

Example 3 with NameValuePairList

use of org.apache.sling.commons.testing.integration.NameValuePairList in project sling by apache.

the class PostServletPrivilegesUpdateTest method testUpdatePropertyPrivilegesAndEvents.

/**
     * Test for SLING-897 fix: 
     * 1. Updating a property requires jcr:modifyProperties privilege on node.
     * 2. When changing an existing property observers should receive a PROPERTY_CHANGED event instead 
     *     of a PROPERTY_REMOVED event and a PROPERTY_ADDED event
     */
@Test
// TODO fails on jackrabbit 2.6.5 and on Oak
@Ignore
public void testUpdatePropertyPrivilegesAndEvents() throws IOException, JsonException, RepositoryException, InterruptedException {
    //1. Create user as admin (OK)
    // curl -F:name=myuser -Fpwd=password -FpwdConfirm=password http://admin:admin@localhost:8080/system/userManager/user.create.html
    testUserId = H.createTestUser();
    //2. Create node as admin (OK)
    // curl -F:nameHint=node -FpropOne=propOneValue1 -FpropOne=propOneValue2 -FpropTwo=propTwoValue http://admin:admin@localhost:8080/test/
    final String createTestNodeUrl = postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX;
    NameValuePairList clientNodeProperties = new NameValuePairList();
    clientNodeProperties.add(SlingPostConstants.RP_NODE_NAME_HINT, testName.getMethodName());
    clientNodeProperties.add("propOne", "propOneValue1");
    clientNodeProperties.add("propOne", "propOneValue2");
    clientNodeProperties.add("propTwo", "propTwoValue");
    String testNodeUrl = H.getTestClient().createNode(createTestNodeUrl, clientNodeProperties, null, false);
    String content = H.getContent(testNodeUrl + ".json", HttpTest.CONTENT_TYPE_JSON);
    JsonObject json = JsonUtil.parseObject(content);
    Object propOneObj = json.get("propOne");
    assertTrue(propOneObj instanceof JsonArray);
    assertEquals(2, ((JsonArray) propOneObj).size());
    assertEquals("propOneValue1", ((JsonArray) propOneObj).getString(0));
    assertEquals("propOneValue2", ((JsonArray) propOneObj).getString(1));
    Object propTwoObj = json.get("propTwo");
    assertTrue(propTwoObj instanceof JsonString);
    assertEquals("propTwoValue", ((JsonString) propTwoObj).getString());
    //3. Attempt to update property of node as testUser (500: javax.jcr.AccessDeniedException: /test/node/propOne: not allowed to add or modify item)
    // curl -FpropOne=propOneValueChanged -FpropTwo=propTwoValueChanged1 -FpropTwo=propTwoValueChanged2 http://myuser:password@localhost:8080/test/node
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("propOne", "propOneValueChanged"));
    postParams.add(new NameValuePair("propTwo", "propTwoValueChanged1"));
    postParams.add(new NameValuePair("propTwo", "propTwoValueChanged2"));
    Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");
    String expectedMessage = "Expected javax.jcr.AccessDeniedException";
    H.assertAuthenticatedPostStatus(testUserCreds, testNodeUrl, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, expectedMessage);
    //4. Grant jcr:modifyProperties rights to testUser as admin (OK)
    // curl -FprincipalId=myuser -Fprivilege@jcr:modifyProperties=granted http://admin:admin@localhost:8080/test/node.modifyAce.html
    Map<String, String> nodeAceProperties = new HashMap<String, String>();
    nodeAceProperties.put("principalId", testUserId);
    nodeAceProperties.put("privilege@jcr:modifyProperties", "granted");
    H.getTestClient().createNode(testNodeUrl + ".modifyAce.html", nodeAceProperties);
    //use a davex session to verify the correct JCR events are delivered
    Repository repository = JcrUtils.getRepository(HttpTest.HTTP_BASE_URL + "/server/");
    Session jcrSession = null;
    TestEventListener listener = new TestEventListener();
    ObservationManager observationManager = null;
    try {
        jcrSession = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
        observationManager = jcrSession.getWorkspace().getObservationManager();
        String testNodePath = testNodeUrl.substring(HttpTest.HTTP_BASE_URL.length());
        observationManager.addEventListener(listener, //event types
        Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, //absPath
        testNodePath, //isDeep 
        true, //uuid
        null, //nodeTypeName
        null, //noLocal
        false);
        //5. Attempt to update properties of node (OK)
        // curl -FpropOne=propOneValueChanged -FpropTwo=propTwoValueChanged1 -FpropTwo=propTwoValueChanged2 http://myuser:password@localhost:8080/test/node
        H.assertAuthenticatedPostStatus(testUserCreds, testNodeUrl, HttpServletResponse.SC_OK, postParams, expectedMessage);
        //verify the change happened
        String afterUpdateContent = H.getContent(testNodeUrl + ".json", HttpTest.CONTENT_TYPE_JSON);
        JsonObject afterUpdateJson = JsonUtil.parseObject(afterUpdateContent);
        Object afterUpdatePropOneObj = afterUpdateJson.get("propOne");
        assertTrue(afterUpdatePropOneObj instanceof JsonArray);
        assertEquals(1, ((JsonArray) afterUpdatePropOneObj).size());
        assertEquals("propOneValueChanged", ((JsonArray) afterUpdatePropOneObj).getString(0));
        Object afterUpdatePropTwoObj = afterUpdateJson.get("propTwo");
        assertTrue(afterUpdatePropTwoObj instanceof JsonArray);
        assertEquals(2, ((JsonArray) afterUpdatePropTwoObj).size());
        assertEquals("propTwoValueChanged1", ((JsonArray) afterUpdatePropTwoObj).getString(0));
        assertEquals("propTwoValueChanged2", ((JsonArray) afterUpdatePropTwoObj).getString(1));
        //wait for the expected JCR events to be delivered
        for (int second = 0; second < 15; second++) {
            if (listener.getEventBundlesProcessed() > 0) {
                break;
            }
            Thread.sleep(1000);
        }
        assertEquals("One property added event was expected: " + listener.toString(), 1, listener.addedProperties.size());
        assertEquals(testNodePath + "/propTwo", listener.addedProperties.get(0));
        assertEquals("One property removed event was expected: " + listener.toString(), 1, listener.removedProperties.size());
        assertEquals(testNodePath + "/propTwo", listener.removedProperties.get(0));
        assertEquals("One property changed event was expected: " + listener.toString(), 1, listener.changedProperties.size());
        assertEquals(testNodePath + "/propOne", listener.changedProperties.get(0));
    } finally {
        //cleanup
        if (observationManager != null) {
            observationManager.removeEventListener(listener);
        }
        jcrSession.logout();
        repository = null;
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) ObservationManager(javax.jcr.observation.ObservationManager) JsonString(javax.json.JsonString) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) JsonArray(javax.json.JsonArray) SimpleCredentials(javax.jcr.SimpleCredentials) Repository(javax.jcr.Repository) JsonObject(javax.json.JsonObject) NameValuePairList(org.apache.sling.commons.testing.integration.NameValuePairList) JsonString(javax.json.JsonString) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) SimpleCredentials(javax.jcr.SimpleCredentials) Session(javax.jcr.Session) Ignore(org.junit.Ignore) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 4 with NameValuePairList

use of org.apache.sling.commons.testing.integration.NameValuePairList in project sling by apache.

the class ReferenceTypeHintTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    postUrl = HTTP_BASE_URL + TEST_BASE_PATH + "/" + System.currentTimeMillis();
    Map<String, String> m = new HashMap<String, String>();
    m.put("sv", "http://www.jcp.org/jcr/sv/1.0");
    NamespaceContext ctx = new SimpleNamespaceContext(m);
    XMLUnit.setXpathNamespaceContext(ctx);
    final NameValuePairList props = new NameValuePairList();
    props.add("a", "");
    props.add("jcr:mixinTypes", "mix:referenceable");
    firstCreatedNodeUrl = testClient.createNode(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, props, null, false);
    firstUuid = getProperty(firstCreatedNodeUrl, "jcr:uuid");
    firstPath = getPath(firstCreatedNodeUrl);
    secondCreatedNodeUrl = testClient.createNode(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, props, null, false);
    secondUuid = getProperty(secondCreatedNodeUrl, "jcr:uuid");
    secondPath = getPath(secondCreatedNodeUrl);
}
Also used : HashMap(java.util.HashMap) NamespaceContext(org.custommonkey.xmlunit.NamespaceContext) SimpleNamespaceContext(org.custommonkey.xmlunit.SimpleNamespaceContext) SimpleNamespaceContext(org.custommonkey.xmlunit.SimpleNamespaceContext) NameValuePairList(org.apache.sling.commons.testing.integration.NameValuePairList)

Example 5 with NameValuePairList

use of org.apache.sling.commons.testing.integration.NameValuePairList in project sling by apache.

the class ReferenceTypeHintTest method testReferenceTypesCreatedFromPath.

public void testReferenceTypesCreatedFromPath() throws Exception {
    final NameValuePairList props = new NameValuePairList();
    props.add("r", firstPath);
    props.add("r@TypeHint", "Reference");
    props.add("w", firstPath);
    props.add("w@TypeHint", "WeakReference");
    props.add("rs", firstPath);
    props.add("rs", secondPath);
    props.add("rs@TypeHint", "Reference[]");
    props.add("ws", firstPath);
    props.add("ws", secondPath);
    props.add("ws@TypeHint", "WeakReference[]");
    final String referencingNodeUrl = testClient.createNode(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, props, null, false);
    verifyReferences(referencingNodeUrl);
}
Also used : NameValuePairList(org.apache.sling.commons.testing.integration.NameValuePairList)

Aggregations

NameValuePairList (org.apache.sling.commons.testing.integration.NameValuePairList)21 HashMap (java.util.HashMap)13 JsonObject (javax.json.JsonObject)11 ArrayList (java.util.ArrayList)2 JsonArray (javax.json.JsonArray)2 NameValuePair (org.apache.commons.httpclient.NameValuePair)2 Repository (javax.jcr.Repository)1 Session (javax.jcr.Session)1 SimpleCredentials (javax.jcr.SimpleCredentials)1 ObservationManager (javax.jcr.observation.ObservationManager)1 JsonString (javax.json.JsonString)1 Credentials (org.apache.commons.httpclient.Credentials)1 HttpMethod (org.apache.commons.httpclient.HttpMethod)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)1 NamespaceContext (org.custommonkey.xmlunit.NamespaceContext)1 SimpleNamespaceContext (org.custommonkey.xmlunit.SimpleNamespaceContext)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1