Skip to content

Commit

Permalink
MODE-1583 - Added unit tests for the new methods provided by the Mode…
Browse files Browse the repository at this point in the history
…ShapeRestService class.
  • Loading branch information
Horia Chiorean authored and rhauch committed Aug 16, 2012
1 parent 7b30453 commit 52efc35
Show file tree
Hide file tree
Showing 21 changed files with 751 additions and 83 deletions.
Expand Up @@ -41,12 +41,13 @@
*/
public final class SharedLockingInputStream extends InputStream {

protected final BinaryKey key;
protected final File file;
protected final NamedLocks lockManager;
protected InputStream stream;
protected Lock processLock;
protected FileLocks.WrappedLock fileLock;
private final BinaryKey key;
private final File file;
private final NamedLocks lockManager;
private InputStream stream;
private Lock processLock;
private FileLocks.WrappedLock fileLock;
private boolean eofReached;

/**
* Create a self-closing, (shared) locking {@link InputStream} to read the content of the supplied {@link File file}.
Expand Down Expand Up @@ -83,6 +84,7 @@ public Object call() throws Exception {
SharedLockingInputStream.this.stream = new BufferedInputStream(
new FileInputStream(file),
AbstractBinaryStore.bestBufferSize(file.length()));
SharedLockingInputStream.this.eofReached = false;
}
return null;
}
Expand All @@ -94,6 +96,9 @@ public int available() throws IOException {
return doOperation(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
if (eofReached) {
return 0;
}
open();
return stream.available();
}
Expand Down Expand Up @@ -179,8 +184,16 @@ public int read( final byte[] b,
return doOperation(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
if (eofReached) {
return -1;
}
open();
return stream.read(b, off, len);
int result = stream.read(b, off, len);
if (result == -1) {
eofReached = true;
close();
}
return result;
}
});
}
Expand All @@ -190,8 +203,16 @@ public int read( final byte[] b ) throws IOException {
return doOperation(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
if (eofReached) {
return -1;
}
open();
return stream.read(b);
int result = stream.read(b);
if (result == -1) {
eofReached = true;
close();
}
return result;
}
});
}
Expand All @@ -201,8 +222,16 @@ public int read() throws IOException {
return doOperation(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
if (eofReached) {
return -1;
}
open();
return stream.read();
int result = stream.read();
if (result == -1) {
eofReached = true;
close(); //without this, there might be locks
}
return result;
}
});
}
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -24,7 +24,16 @@

package org.modeshape.web.jcr.rest;

import javax.ws.rs.core.MediaType;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Ignore;
import org.junit.Test;
import org.modeshape.common.util.IoUtil;
import org.modeshape.web.jcr.rest.handler.RestBinaryHandler;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
* Unit test for the v2 version of the rest service: {@link ModeShapeRestService}
Expand Down Expand Up @@ -158,6 +167,11 @@ protected String nodeWithBinaryProperty() {
return "v2/put/node_with_binary_property.json";
}

@Override
protected String binaryPropertyName() {
return "testProperty";
}

@Override
protected String nodeBinaryPropertyAfterEdit() {
return "v2/put/node_with_binary_property_after_edit.json";
Expand Down Expand Up @@ -219,7 +233,177 @@ protected String jcrSQL2Result() {
}

@Override
@Ignore("Ignored because the deprecated parameter isn't supported anymore")
@Ignore( "Ignored because the deprecated parameter isn't supported anymore" )
public void shouldRetrieveRootNodeWhenDeprecatedDepthSet() throws Exception {
}

@Test
public void shouldRetrieveBinaryPropertyValue() throws Exception {
doPost(nodeWithBinaryProperty(), itemsUrl(TEST_NODE)).isCreated();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
doGet(binaryUrl(TEST_NODE, binaryPropertyName()))
.isOk()
.hasMimeType(MediaType.TEXT_PLAIN)
.hasContentDisposition(RestBinaryHandler.DEFAULT_CONTENT_DISPOSITION_PREFIX + TEST_NODE)
.copyInputStream(bos);
assertTrue(bos.size() > 0);
assertEquals("testValue", new String(bos.toByteArray()));
}

private String newBinaryProperty() {
return "v2/post/new_binary_property_response.json";
}

@Test
public void shouldRetrieveBinaryPropertyValueWithMimeTypeAndContentDisposition() throws Exception {
doPost(nodeWithBinaryProperty(), itemsUrl(TEST_NODE)).isCreated();
String mimeType = MediaType.TEXT_XML;
String contentDisposition = "inline;filename=TestFile.txt";
String urlParams = "?mimeType=" + RestHelper.URL_ENCODER.encode(mimeType) + "&contentDisposition=" + RestHelper
.URL_ENCODER.encode(contentDisposition);
doGet(binaryUrl(TEST_NODE, binaryPropertyName()) + urlParams)
.isOk()
.hasMimeType(mimeType)
.hasContentDisposition(contentDisposition);
}

@Test
public void shouldReturnNotFoundForInvalidBinaryProperty() throws Exception {
doPost(nodeWithBinaryProperty(), itemsUrl(TEST_NODE)).isCreated();
doGet(binaryUrl(TEST_NODE, "invalid")).isNotFound();
}

@Test
public void shouldReturnNotFoundForNonBinaryProperty() throws Exception {
doPost(nodeWithBinaryProperty(), itemsUrl(TEST_NODE)).isCreated();
doGet(binaryUrl(TEST_NODE, "jcr:primaryType")).isNotFound();
}

@Test
public void shouldCreateBinaryProperty() throws Exception {
doPost((String)null, itemsUrl(TEST_NODE)).isCreated();
doPost(fileStream("v2/post/binary.pdf"), binaryUrl(TEST_NODE, binaryPropertyName()))
.isCreated()
.isJSONObjectLikeFile(newBinaryProperty());
ByteArrayOutputStream actualBinaryContent = new ByteArrayOutputStream();
doGet(binaryUrl(TEST_NODE, binaryPropertyName()))
.isOk()
.copyInputStream(actualBinaryContent);

byte[] expectedBinaryContent = IoUtil.readBytes(fileStream("v2/post/binary.pdf"));
assertArrayEquals(expectedBinaryContent, actualBinaryContent.toByteArray());
}

@Test
public void shouldUpdateBinaryPropertyViaPost() throws Exception {
doPost(nodeWithBinaryProperty(), itemsUrl(TEST_NODE)).isCreated();

String otherBinaryValue = String.valueOf(System.currentTimeMillis());
doPost(new ByteArrayInputStream(otherBinaryValue.getBytes()), binaryUrl(TEST_NODE, binaryPropertyName())).isOk();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
doGet(binaryUrl(TEST_NODE, binaryPropertyName())).isOk().copyInputStream(bos);
assertEquals(otherBinaryValue, new String(bos.toByteArray()));
}

@Test
public void shouldUpdateBinaryPropertyViaPut() throws Exception {
doPost(nodeWithBinaryProperty(), itemsUrl(TEST_NODE)).isCreated();

String otherBinaryValue = String.valueOf(System.currentTimeMillis());
doPut(new ByteArrayInputStream(otherBinaryValue.getBytes()), binaryUrl(TEST_NODE, binaryPropertyName())).isOk();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
doGet(binaryUrl(TEST_NODE, binaryPropertyName())).isOk().copyInputStream(bos);
assertEquals(otherBinaryValue, new String(bos.toByteArray()));
}

@Test
public void shouldNotUpdateNonExistingBinaryPropertyViaPut() throws Exception {
doPost(nodeWithBinaryProperty(), itemsUrl(TEST_NODE)).isCreated();

String otherBinaryValue = String.valueOf(System.currentTimeMillis());
doPut(new ByteArrayInputStream(otherBinaryValue.getBytes()), binaryUrl(TEST_NODE, "invalidProp")).isNotFound();
}

@Test
public void shouldCreateBinaryValueViaMultiPartRequest() throws Exception {
doPost((String)null, itemsUrl(TEST_NODE)).isCreated();
doPostMultiPart(fileStream("v2/post/binary.pdf"), "file", binaryUrl(TEST_NODE, binaryPropertyName()),
MediaType.APPLICATION_OCTET_STREAM)
.isCreated()
.isJSONObjectLikeFile(newBinaryProperty());
}

@Test
public void shouldRetrieveNtBaseNodeType() throws Exception {
doGet(nodeTypesUrl("nt:base")).isOk().isJSONObjectLikeFile("v2/get/nt_base_nodeType_response.json");
}

@Test
public void shouldReturnNotFoundIfNodeTypeMissing() throws Exception {
doGet(nodeTypesUrl("invalidNodeType")).isNotFound();
}

@Test
public void shouldImportCNDFile() throws Exception {
doPost(fileStream(nodeTypesCND()), nodeTypesUrl())
.isOk()
.isJSONArrayLikeFile(cndImportResponse());
}

private String cndImportResponse() {
return "v2/post/cnd_import_response.json";
}

private String nodeTypesCND() {
return "v2/post/node_types.cnd";
}

@Test
public void shouldImportCNDFileViaMultiPartRequest() throws Exception {
doPostMultiPart(fileStream(nodeTypesCND()), "file", nodeTypesUrl(), MediaType.TEXT_PLAIN)
.isOk()
.isJSONArrayLikeFile(cndImportResponse());
}

@Test
public void importingCNDViaWrongHTMLElementNameShouldFail() throws Exception {
doPostMultiPart(fileStream(nodeTypesCND()), "invalidHTML", nodeTypesUrl(), MediaType.TEXT_PLAIN)
.isBadRequest();
}

@Test
public void shouldCreateMultipleNodes() throws Exception {
doPost((String)null, itemsUrl(TEST_NODE)).isCreated();
doPost("v2/post/multiple_nodes_request.json", itemsUrl())
.isOk()
.isJSONArrayLikeFile("v2/post/multiple_nodes_response.json");
}

@Test
public void shouldEditMultipleNodes() throws Exception {
doPost((String)null, itemsUrl(TEST_NODE)).isCreated();
doPost("v2/post/multiple_nodes_request.json", itemsUrl()).isOk();
doPut("v2/put/multiple_nodes_edit_request.json", itemsUrl())
.isOk()
.isJSONArrayLikeFile("v2/put/multiple_nodes_edit_response.json");
}

@Test
@Ignore("A limitation of HTTPUrlConnection prevents this test from running. Consider enabling it if/when switching to Http Client")
public void shouldDeleteMultipleNodes() throws Exception {
doPost((String)null, itemsUrl(TEST_NODE)).isCreated();
doPost("v2/post/multiple_nodes_request.json", itemsUrl()).isOk();
doDelete("v2/delete/multiple_nodes_delete.json", itemsUrl()).isOk();
doGet(itemsUrl(TEST_NODE)).isJSONObjectLikeFile(nodeWithoutPrimaryTypeRequest());
}

private String binaryUrl( String... additionalPathSegments ) {
return RestHelper.urlFrom(REPOSITORY_NAME + "/default/" + RestHelper.BINARY_METHOD_NAME, additionalPathSegments);
}

private String nodeTypesUrl( String... additionalPathSegments ) {
return RestHelper.urlFrom(REPOSITORY_NAME + "/default/" + RestHelper.NODE_TYPES_METHOD_NAME, additionalPathSegments);
}
}
@@ -0,0 +1 @@
["testNode/otherChild", "testNode/child", "testNode/child/subChild"]
@@ -0,0 +1,63 @@
{
"nt:base":{
"mixin":false,
"abstract":true,
"queryable":true,
"hasOrderableChildNodes":false,
"propertyDefinitions":[
{
"jcr:primaryType":{
"requiredType":"Name",
"declaringNodeTypeName":"nt:base",
"mandatory":true,
"multiple":false,
"autocreated":true,
"protected":true,
"fullTextSearchable":true,
"onParentVersion":"COMPUTE"
}
},
{
"jcr:mixinTypes":{
"requiredType":"Name",
"declaringNodeTypeName":"nt:base",
"mandatory":false,
"multiple":true,
"autocreated":false,
"protected":true,
"fullTextSearchable":true,
"onParentVersion":"COMPUTE"
}
}
],
"subTypes":[
"http://localhost:8090/resources/repo/default/nodetypes/mode:lock",
"http://localhost:8090/resources/repo/default/nodetypes/mode:locks",
"http://localhost:8090/resources/repo/default/nodetypes/mode:namespace",
"http://localhost:8090/resources/repo/default/nodetypes/mode:namespaces",
"http://localhost:8090/resources/repo/default/nodetypes/mode:nodeTypes",
"http://localhost:8090/resources/repo/default/nodetypes/mode:resource",
"http://localhost:8090/resources/repo/default/nodetypes/mode:root",
"http://localhost:8090/resources/repo/default/nodetypes/mode:share",
"http://localhost:8090/resources/repo/default/nodetypes/mode:system",
"http://localhost:8090/resources/repo/default/nodetypes/mode:versionHistoryFolder",
"http://localhost:8090/resources/repo/default/nodetypes/nt:activity",
"http://localhost:8090/resources/repo/default/nodetypes/nt:address",
"http://localhost:8090/resources/repo/default/nodetypes/nt:childNodeDefinition",
"http://localhost:8090/resources/repo/default/nodetypes/nt:configuration",
"http://localhost:8090/resources/repo/default/nodetypes/nt:frozenNode",
"http://localhost:8090/resources/repo/default/nodetypes/nt:hierarchyNode",
"http://localhost:8090/resources/repo/default/nodetypes/nt:naturalText",
"http://localhost:8090/resources/repo/default/nodetypes/nt:nodeType",
"http://localhost:8090/resources/repo/default/nodetypes/nt:propertyDefinition",
"http://localhost:8090/resources/repo/default/nodetypes/nt:query",
"http://localhost:8090/resources/repo/default/nodetypes/nt:resource",
"http://localhost:8090/resources/repo/default/nodetypes/nt:unstructured",
"http://localhost:8090/resources/repo/default/nodetypes/nt:version",
"http://localhost:8090/resources/repo/default/nodetypes/nt:versionHistory",
"http://localhost:8090/resources/repo/default/nodetypes/nt:versionLabels",
"http://localhost:8090/resources/repo/default/nodetypes/nt:versionedChild"
]
}
}

Binary file not shown.

0 comments on commit 52efc35

Please sign in to comment.