package org.mitre.rhex;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.mitre.test.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
/**
* Test for section document deletion
*
* <pre>
* 6.5 baseURL/sectionpath/documentname
*
* 6.5.4 DELETE
*
* This operation MAY be implemented. If a DELETE is sent to the document URL,
* the document is completely deleted. If DELETE is implemented, special
* precautions should be taken to assure against accidental or malicious
* deletion.
*
* Future requests to the section URL MAY return a status code of 410,
* unless the record is restored.
*
* Status Code: <B>204</B>, 410, [405]
*
* 6.1.2 General Conventions
*
* Any HTTP GET, PUT, POST, DELETE, or OPTIONS operation on a given resource
* that are not implemented MUST return an HTTP response with a status code
* of <B>405<B> that includes an Allow header that specifies the allowed methods.
* </pre>
*
* @author Jason Mathews, MITRE Corp.
* Date: 2/20/12 10:45 AM
*/
public class DocumentDelete extends BaseTest {
@NonNull
@Override
public String getId() {
return "6.5.4.1";
}
@Override
public boolean isRequired() {
// This operation MAY be implemented. If a DELETE is sent to the document URL,
// the document is completely deleted.
return false;
}
@NonNull
public String getName() {
return "DELETE sent to document URL if successful MUST return 204";
}
@NonNull
public List<Class<? extends TestUnit>> getDependencyClasses() {
return Collections.<Class<? extends TestUnit>> singletonList(DocumentCreate.class); // 6.4.2.2
}
public void execute() throws TestException {
// pre-conditions: for this test to be executed the prerequisite test BaseUrlRootXml must have passed
// with 200 HTTP response and valid root.xml content.
TestUnit baseTest = getDependency(DocumentCreate.class);
if (baseTest == null) {
// assertion failed: this should never be null
log.error("Failed to retrieve prerequisite test: BaseUrlRootXml");
setStatus(StatusEnumType.SKIPPED, "Failed to retrieve prerequisite test: 6.4.2.2");
return;
}
final DocumentCreate documentCreate = (DocumentCreate) baseTest;
URI documentURL = documentCreate.getDocumentURL();
if (documentURL == null) {
log.error("Failed to retrieve prerequisite test results: DocumentCreate");
setStatus(StatusEnumType.SKIPPED, "Failed to retrieve prerequisite test results: 6.4.2.2");
return;
}
final Context context = Loader.getInstance().getContext();
HttpClient client = context.getHttpClient();
try {
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
System.out.println("\nURL=" + documentURL);
}
HttpDelete req = new HttpDelete(documentURL);
req.setHeader("Accept", "application/atom+xml, application/xml, text/html, */*");
HttpResponse response = context.executeRequest(client, req);
int code = response.getStatusLine().getStatusCode();
if (code != 204 || debugEnabled) {
if (!debugEnabled) {
System.out.println("\nURL=" + documentURL);
}
dumpResponse(req, response, true);
}
if (code == 204) {
setStatus(StatusEnumType.SUCCESS);
} else {
// check 410 status code in dependent test
if (code == 405) {
// not implemented case can also be seen as a success with warning
// but for sake of dependent tests the status is FAILED
setStatus(StatusEnumType.FAILED, "Operation not implemented");
} else
setStatus(StatusEnumType.FAILED, "Expected 204 HTTP status code but was: " + code);
}
} catch (IOException e) {
throw new TestException(e);
} finally {
client.getConnectionManager().shutdown();
}
}
}