()
+ {
+ @Override
+ public NodeRef execute() throws Throwable
+ {
+ // find "Company Home"
+ ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home\"");
+ NodeRef result = resultSet.getNodeRef(0);
+ resultSet.close();
+
+ return result;
+ }
+ });
+
+ InputStream testDataIS = getClass().getClassLoader().getResourceAsStream(TEST_DATA_FILE_NAME);
+ InputStream davLockInfoIS = getClass().getClassLoader().getResourceAsStream(DAV_LOCK_INFO_XML);
+ testDataFile = IOUtils.toByteArray(testDataIS);
+ davLockInfoFile = IOUtils.toByteArray(davLockInfoIS);
+ testDataIS.close();
+ davLockInfoIS.close();
+
+ txn = transactionService.getUserTransaction();
+ txn.begin();
+ }
+
+ @After
+ public void tearDown() throws Exception
+ {
+ method = null;
+ request = null;
+ response = null;
+ testDataFile = null;
+ davLockInfoFile = null;
+
+ AuthenticationUtil.clearCurrentSecurityContext();
+ }
+
+ public void doOnContentUpdate(NodeRef nodeRef, boolean newContent)
+ {
+ flag = true;
+ ++counter;
+ }
+
+ /**
+ * Put a content file and check that onContentUpdate fired
+ *
+ * Lock the file
+ *
+ * Put the contents
+ *
+ * Unlock the node
+ */
+ @Test
+ public void testUploadNewContentFiresContentUpdatePolicies() throws Exception
+ {
+ flag = false;
+ counter = 0;
+ policyComponent.bindClassBehaviour(OnContentUpdatePolicy.QNAME, ContentModel.TYPE_CONTENT, new JavaBehaviour(this, "doOnContentUpdate"));
+
+ String fileName = "file-" + GUID.generate();
+ NodeRef fileNoderef = null;
+
+ try
+ {
+ executeMethod(WebDAV.METHOD_LOCK, fileName, davLockInfoFile, null);
+
+ ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home//cm:" + fileName + "\"");
+ fileNoderef = resultSet.getNodeRef(0);
+ resultSet.close();
+
+ assertEquals("File should be locked", LockStatus.LOCK_OWNER, lockService.getLockStatus(fileNoderef));
+ }
+ catch (Exception e)
+ {
+ fail("Failed to lock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
+ }
+
+ txn.commit();
+ txn = transactionService.getUserTransaction();
+ txn.begin();
+
+ // Construct IF HEADER
+ String lockToken = fileNoderef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + AuthenticationUtil.getAdminUserName();
+ String lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
+ HashMap headers = new HashMap();
+ 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)
+ {
+ fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
+ }
+
+ txn.commit();
+ txn = transactionService.getUserTransaction();
+ txn.begin();
+
+ headers = new HashMap();
+ 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)
+ {
+ fail("Failed to unlock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
+ }
+
+ assertTrue("onContentUpdate policies were not triggered", flag);
+ assertEquals("onContentUpdate policies should be triggered only once", counter, 1);
+
+ if (fileNoderef != null)
+ {
+ nodeService.deleteNode(fileNoderef);
+ }
+ }
+
+ /**
+ * Executes WebDAV method for testing
+ *
+ * Sets content to request from a test file
+ *
+ * @param method Method to prepare, should be initialized (PUT, LOCK, UNLOCK are supported)
+ * @param fileName the name of the file set to the context, can be used with path, i.e. "path/to/file/fileName.txt"
+ * @param content If not null adds test content to the request
+ * @param headers to set to request, can be null
+ * @throws Exception
+ */
+ private void executeMethod(String methodName, String fileName, byte[] content, Map headers) throws Exception
+ {
+ if (methodName == WebDAV.METHOD_PUT)
+ method = new PutMethod();
+ else if (methodName == WebDAV.METHOD_LOCK)
+ method = new LockMethod();
+ else if (methodName == WebDAV.METHOD_UNLOCK)
+ method = new UnlockMethod();
+ if (method != null)
+ {
+ request = new MockHttpServletRequest(methodName, "/alfresco/webdav/" + fileName);
+ response = new MockHttpServletResponse();
+ request.setServerPort(8080);
+ request.setServletPath("/webdav");
+ if (content != null)
+ {
+ request.setContent(content);
+ }
+
+ if (headers != null && !headers.isEmpty())
+ {
+ for (String key : headers.keySet())
+ {
+ request.addHeader(key, headers.get(key));
+ }
+ }
+
+ method.setDetails(request, response, webDAVHelper, companyHomeNodeRef);
+
+ method.execute();
+ }
+ }
+}