use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class PutMethodTest method testPutContentBadPath.
/**
* Updating a file in an non-existent path
*/
@Test
public void testPutContentBadPath() throws Exception {
String fileName = "file-" + GUID.generate();
NodeRef fileNoderef = null;
try {
// Add non-existent path
executeMethod(WebDAV.METHOD_PUT, "non/existent/path" + fileName, testDataFile, null);
fail("The PUT execution should fail with a 400 error");
} catch (WebDAVServerException wse) {
// The execution failed and it is expected
assertTrue(wse.getHttpStatusCode() == HttpServletResponse.SC_CONFLICT);
} catch (Exception e) {
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
} finally {
if (fileNoderef != null) {
nodeService.deleteNode(fileNoderef);
}
}
}
use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class PutMethodTest method testPutNoContentFileAndUpdate.
/**
* Putting a zero content file and update it
* <p>
* Put an empty file
* <p>
* Lock the file
* <p>
* Put the contents
* <p>
* Unlock the node
*/
@Test
public void testPutNoContentFileAndUpdate() throws Exception {
String fileName = "file-" + GUID.generate();
NodeRef fileNoderef = null;
try {
executeMethod(WebDAV.METHOD_PUT, fileName, new byte[0], null);
List<NodeRef> refs = searchService.selectNodes(nodeService.getRootNode(storeRef), "/app:company_home/cm:" + fileName, null, namespaceService, false);
fileNoderef = refs.get(0);
assertTrue("File should exist.", nodeService.exists(fileNoderef));
assertEquals("Filename is not correct", fileName, nodeService.getProperty(fileNoderef, ContentModel.PROP_NAME));
assertTrue("Expected return status is " + HttpServletResponse.SC_CREATED + ", but returned is " + response.getStatus(), HttpServletResponse.SC_CREATED == response.getStatus());
byte[] updatedFile = IOUtils.toByteArray(fileFolderService.getReader(fileNoderef).getContentInputStream());
assertTrue("The content should be empty", updatedFile.length == 0);
} catch (Exception e) {
throw new RuntimeException("Failed to upload a file", e);
}
try {
executeMethod(WebDAV.METHOD_LOCK, fileName, davLockInfoAdminFile, null);
assertEquals("File should be locked", LockStatus.LOCK_OWNER, lockService.getLockStatus(fileNoderef));
} catch (Exception e) {
throw new RuntimeException("Failed to lock a file", e);
}
// Construct IF HEADER
String lockToken = fileNoderef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + AuthenticationUtil.getAdminUserName();
String lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
HashMap<String, String> headers = new HashMap<String, String>();
headers.put(WebDAV.HEADER_IF, lockHeaderValue);
try {
executeMethod(WebDAV.METHOD_PUT, fileName, testDataFile, headers);
assertTrue("File does not exist.", nodeService.exists(fileNoderef));
assertEquals("Filename is not correct", fileName, nodeService.getProperty(fileNoderef, ContentModel.PROP_NAME));
assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(), HttpServletResponse.SC_NO_CONTENT == response.getStatus());
assertTrue("File should have NO_CONTENT aspect", nodeService.hasAspect(fileNoderef, ContentModel.ASPECT_NO_CONTENT));
InputStream updatedFileIS = fileFolderService.getReader(fileNoderef).getContentInputStream();
byte[] updatedFile = IOUtils.toByteArray(updatedFileIS);
updatedFileIS.close();
assertTrue("The content has to be equal", ArrayUtils.isEquals(testDataFile, updatedFile));
} catch (Exception e) {
throw new RuntimeException("Failed to upload a file", e);
}
headers = new HashMap<String, String>();
headers.put(WebDAV.HEADER_LOCK_TOKEN, "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">");
try {
executeMethod(WebDAV.METHOD_UNLOCK, fileName, null, headers);
assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(), HttpServletResponse.SC_NO_CONTENT == response.getStatus());
assertFalse("File should not have NO_CONTENT aspect", nodeService.hasAspect(fileNoderef, ContentModel.ASPECT_NO_CONTENT));
assertEquals("File should be unlocked", LockStatus.NO_LOCK, lockService.getLockStatus(fileNoderef));
} catch (Exception e) {
throw new RuntimeException("Failed to unlock a file", e);
}
if (fileNoderef != null) {
nodeService.deleteNode(fileNoderef);
}
}
use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class FormRestApiJsonPost_Test method testAddNewAssociationsToNode.
/**
* This test method attempts to add new associations between existing nodes.
*/
public void testAddNewAssociationsToNode() throws Exception {
List<NodeRef> associatedNodes;
checkOriginalAssocsBeforeChanges();
// Add three additional associations
JSONObject jsonPostData = new JSONObject();
String assocsToAdd = associatedDoc_C + "," + associatedDoc_D + "," + associatedDoc_E;
jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated associations via the node service
List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
assertEquals(5, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (AssociationRef assocRef : modifiedAssocs) {
associatedNodes.add(assocRef.getTargetRef());
}
assertTrue(associatedNodes.contains(associatedDoc_A));
assertTrue(associatedNodes.contains(associatedDoc_B));
assertTrue(associatedNodes.contains(associatedDoc_C));
assertTrue(associatedNodes.contains(associatedDoc_D));
assertTrue(associatedNodes.contains(associatedDoc_E));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(referencingNodeUpdateUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
// We expect exactly 5 assocs on the test node
assertEquals(5, jsonAssocs.split(",").length);
for (AssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
}*/
}
use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class FormRestApiJsonPost_Test method testRemoveChildAssociationsFromNode.
/**
* This test method attempts to remove an existing child association between two
* existing nodes.
*/
public void testRemoveChildAssociationsFromNode() throws Exception {
List<NodeRef> associatedNodes;
checkOriginalChildAssocsBeforeChanges();
// Remove an association
JSONObject jsonPostData = new JSONObject();
String assocsToRemove = childDoc_B.toString();
jsonPostData.put(ASSOC_SYS_CHILDREN_REMOVED, assocsToRemove);
String jsonPostString = jsonPostData.toString();
sendRequest(new PostRequest(containingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
// Check the now updated child associations via the node service
List<ChildAssociationRef> modifiedAssocs = nodeService.getChildAssocs(containerNodeRef);
assertEquals(1, modifiedAssocs.size());
// Extract the target nodeRefs to make them easier to examine
associatedNodes = new ArrayList<NodeRef>(5);
for (ChildAssociationRef assocRef : modifiedAssocs) {
associatedNodes.add(assocRef.getChildRef());
}
assertTrue(associatedNodes.contains(childDoc_A));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(containingNodeUpdateUrl), 200);
String jsonRspString = response.getContentAsString();
JSONObject jsonGetResponse = new JSONObject(jsonRspString);
JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
assertNotNull(jsonData);
JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
assertNotNull(jsonFormData);
String jsonAssocs = (String)jsonFormData.get(ASSOC_SYS_CHILDREN);
// We expect exactly 1 assoc on the test node
assertEquals(1, jsonAssocs.split(",").length);
for (ChildAssociationRef assocRef : modifiedAssocs)
{
assertTrue(jsonAssocs.contains(assocRef.getChildRef().toString()));
}*/
}
use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class InvitationWebScriptTest method makeAvatar.
public static String makeAvatar(final NodeService nodeService, final NodeRef person) {
nodeService.addAspect(person, ContentModel.ASPECT_PREFERENCES, null);
ChildAssociationRef assoc = nodeService.createNode(person, ContentModel.ASSOC_PREFERENCE_IMAGE, avatarQName, ContentModel.TYPE_CONTENT);
NodeRef avatar = assoc.getChildRef();
nodeService.createAssociation(person, avatar, ContentModel.ASSOC_AVATAR);
return "api/node/" + avatar + "/content/thumbnails/avatar";
}
Aggregations