package xyz.its_me.alfresco;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.transaction.TransactionService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public class FileFolderServiceSpringTest extends AbstractSpringTest {
    @Autowired
    @Qualifier("fileFolderService")
    private FileFolderService fileFolderService;

    @Autowired
    @Qualifier("nodeService")
    private NodeService nodeService;

    @Autowired
    @Qualifier("transactionService")
    private TransactionService transactionService;

    @Autowired
    private Repository repository;

    private NodeRef testFolder;

    @Before
    public void before() {
        final NodeRef companyHome = repository.getCompanyHome();
        assertThat(companyHome, notNullValue());
        final FileInfo fileInfo = runTransactional(
                () -> fileFolderService.create(companyHome, "testFolder", ContentModel.TYPE_FOLDER));
        this.testFolder = fileInfo.getNodeRef();
    }

    @After
    public void after() {
        if (testFolder != null) {
            runTransactional(() -> fileFolderService.delete(testFolder));
        }
    }

    @Test
    public void testCreateFolder() throws Exception {
        assertThat(testFolder, notNullValue());

    }

    /**
     * Helper for running code in a retrying transaction.
     *
     * @param <R> return type for callback
     */
    private <R> R runTransactional(RetryingTransactionHelper.RetryingTransactionCallback<R> callback) {
        return transactionService.getRetryingTransactionHelper().doInTransaction(callback);
    }

    /**
     * Helper for running code in a retrying transaction with no return value.
     */
    private void runTransactional(Runnable callback) {
        transactionService.getRetryingTransactionHelper().doInTransaction(() -> {
            callback.run();
            return null;
        });
    }
}

@@ -27,48 +27,8 @@
 o;%0A%0A
-import org.alfresco.model.ContentModel;%0A
 impo
@@ -407,32 +407,8 @@
 ce;%0A
-import org.junit.After;%0A
 impo
@@ -578,16 +578,148 @@
 ifier;%0A%0A
+import static java.lang.String.format;%0Aimport static org.alfresco.model.ContentModel.*;%0Aimport static org.hamcrest.CoreMatchers.is;%0A
 import s
@@ -1225,24 +1225,31 @@
     private 
+static 
 NodeRef test
@@ -1292,24 +1292,88 @@
  before() %7B%0A
+        if (testFolder != null) %7B%0A            return;%0A        %7D%0A
         fina
@@ -1470,24 +1470,168 @@
 llValue());%0A
+        testFolder = fileFolderService.searchSimple(companyHome, %22testFolder%22);%0A        if (testFolder != null) %7B%0A            return;%0A        %7D%0A
         fina
@@ -1673,25 +1673,8 @@
 nal(
-%0A                
 () -
@@ -1727,29 +1727,16 @@
 older%22, 
-ContentModel.
 TYPE_FOL
@@ -1754,13 +1754,8 @@
     
-this.
 test
@@ -1794,21 +1794,172 @@
     
-%7D%0A%0A    @After
+    for (int i = 0; i %3C 10000; i++) %7B%0A            final NodeRef nodeRef = createFile(i);%0A            assertThat(nodeRef, notNullValue());%0A        %7D%0A    %7D%0A%0A    @Test
 %0A   
@@ -1975,105 +1975,126 @@
 oid 
+testFolderCre
 a
-f
 te
-r() %7B%0A        if (testFolder != null) %7B%0A            runTransactional(() -%3E fileFol
+d() throws Exception %7B%0A        assertThat(testFolder, notNullValue());%0A        assertThat(no
 de
-r
 Service.
 dele
@@ -2093,15 +2093,48 @@
 ice.
-delete(
+getProperty(testFolder, PROP_NAME), is(%22
 test
@@ -2139,30 +2139,21 @@
 stFolder
+%22
 ));%0A
-        %7D%0A
     %7D%0A%0A 
@@ -2185,20 +2185,20 @@
 test
+Files
 Create
-Folder
+d
 () t
@@ -2238,37 +2238,349 @@
 hat(
-testFolder, notNullValue());%0A
+fileFolderService.searchSimple(testFolder, %22test09999.txt%22), notNullValue());%0A    %7D%0A%0A    private NodeRef createFile(int index) %7B%0A        final String name = format(%22test%2505d.txt%22, index);%0A        final FileInfo fileInfo = runTransactional(() -%3E fileFolderService.create(testFolder, name, TYPE_CONTENT));%0A        return fileInfo.getNodeRef();
 %0A   

create 10000 test files