Skip to content

Commit

Permalink
SLING-4825:Add ability to handle webdav based DELETE requests
Browse files Browse the repository at this point in the history
* Added tests
  • Loading branch information
Satya Deep Maheshwari committed Jul 7, 2015
1 parent 81fc98d commit 918466c
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.launchpad.webapp.integrationtest;

import junitx.framework.Assert;
import org.apache.sling.commons.testing.integration.HttpTestBase;
import org.junit.Test;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WebdavDeleteTest extends HttpTestBase{
private final String testDir = "/sling-test/" + getClass().getSimpleName() + System.currentTimeMillis();
private final String testDirUrl = HTTP_BASE_URL + "/dav/" + testDir;

private final String DEFAULT_HANDLER = "default-delete-handler";
private final String HANDLER_1 = "test-delete-handler-1";
private final String HANDLER_2 = "test-delete-handler-2";

private final String HANDLER_1_BKP = "backed-up-by-" + HANDLER_1;
private final String HANDLER_2_BKP = "backed-up-by-" + HANDLER_2;

@Override
protected void setUp() throws Exception {
super.setUp();
testClient.mkdirs(HTTP_BASE_URL, testDir);

Map<String, String> map = new HashMap<String, String>();
map.put("jcr:primaryType", "nt:unstructured");
testClient.createNode(HTTP_BASE_URL + testDir + "/" + HANDLER_1, map);
testClient.createNode(HTTP_BASE_URL + testDir + "/" + HANDLER_2, map);
testClient.createNode(HTTP_BASE_URL + testDir + "/" + DEFAULT_HANDLER, map);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
testClient.delete(testDirUrl);
}

@Test
public void testDelete() {
try {
testClient.delete(HTTP_BASE_URL + testDir + "/" + HANDLER_1);
testClient.delete(HTTP_BASE_URL + testDir + "/" + HANDLER_2);
testClient.delete(HTTP_BASE_URL + testDir + "/" + DEFAULT_HANDLER);

Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, testClient.get(HTTP_BASE_URL + testDir + "/" + HANDLER_1));
Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, testClient.get(HTTP_BASE_URL + testDir + "/" + HANDLER_2));
Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, testClient.get(HTTP_BASE_URL + testDir + "/" + DEFAULT_HANDLER));

Assert.assertEquals(HttpServletResponse.SC_OK, testClient.get(HTTP_BASE_URL + testDir + "/" + HANDLER_1 + HANDLER_1_BKP + ".json"));
Assert.assertEquals(HttpServletResponse.SC_OK, testClient.get(HTTP_BASE_URL + testDir + "/" + HANDLER_2 + HANDLER_2_BKP + ".json"));

} catch (IOException e) {
Assert.fail(e);
}
}

}
2 changes: 1 addition & 1 deletion launchpad/test-services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-jcr-server</artifactId>
<version>2.2.8</version>
<version>2.10.2-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.sling.launchpad.testservices.handlers;

import org.apache.jackrabbit.server.io.DeleteContext;
import org.apache.jackrabbit.server.io.DeleteHandler;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavResource;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

public abstract class AbstractDeleteHandler implements DeleteHandler {

protected String HANDLER_NAME = "";
protected String HANDLER_BKP;

@Override
public boolean delete(DeleteContext deleteContext,
DavResource resource) throws DavException {
try {
deleteContext.getSession().getWorkspace().move(resource.getResourcePath(), resource.getResourcePath() + HANDLER_BKP);
return true;
} catch (RepositoryException e) {
return false;
}
}

@Override
public boolean canDelete(DeleteContext deleteContext,
DavResource resource) {
try {
Node nodeToDelete = deleteContext.getSession().getNode(resource.getResourcePath());
return HANDLER_NAME.equals(nodeToDelete.getName());
} catch (RepositoryException e) {
return false;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.launchpad.testservices.handlers;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.server.io.DeleteContext;
import org.apache.jackrabbit.server.io.DeleteHandler;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavResource;
import org.osgi.framework.Constants;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

@Component(metatype = true)
@Property(name = Constants.SERVICE_RANKING, intValue = 2, propertyPrivate = false)
@Service(value = { DeleteHandler.class })
public class TestDeleteHandler1 extends AbstractDeleteHandler{

public TestDeleteHandler1() {
this.HANDLER_NAME = "test-delete-handler-1";
this.HANDLER_BKP = "backed-up-by-" + HANDLER_NAME;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.launchpad.testservices.handlers;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.server.io.DeleteContext;
import org.apache.jackrabbit.server.io.DeleteHandler;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavResource;
import org.osgi.framework.Constants;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

@Component(metatype = true)
@Property(name = Constants.SERVICE_RANKING, intValue = 2, propertyPrivate = false)
@Service(value = { DeleteHandler.class })
public class TestDeleteHandler2 extends AbstractDeleteHandler {

public TestDeleteHandler2() {
this.HANDLER_NAME = "test-delete-handler-2";
this.HANDLER_BKP = "backed-up-by-" + HANDLER_NAME;
}

}

0 comments on commit 918466c

Please sign in to comment.