AnsweredAssumed Answered

How do I write tests for contentWriter?

Question asked by piousbox on Oct 14, 2010
Latest reply on Oct 14, 2010 by mrogers
I have the following code and it doesn't work.


import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;
import java.util.*;

import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.namespace.QName;

import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.model.FileInfo;

import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
//import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.ServiceRegistry;

public class TransformXML implements ActionHandler {

    public static NodeService nodeService;
    public static FileFolderService fileFolderService;

    // Set method for spring injecting
    public void setNodeService(NodeService nodeService) {
        this.nodeService = nodeService;
    }

    public NodeService getNodeService() {
        return this.nodeService;
    }

    public void setFileFolderService(FileFolderService fileFolderService) {
        this.fileFolderService = fileFolderService;
    }

    public FileFolderService getFileFolderService() {
        return this.fileFolderService;
    }

    // The execution method of workflow Action
    public void execute(ExecutionContext ctx) throws Exception {
        try {
            //fileFolderService = serviceRegistry.getFileFolderService();
            NodeRef docsroot = new NodeRef((String) ctx.getContextInstance().getVariable("packageref"));
//            String authorname = (String) ctx.getContextInstance().getVariable("authorname");

            //get the attached docs from the jBPM package nodeRef
            List<ChildAssociationRef> docs = this.nodeService.getChildAssocs(docsroot);
            for (int i = 0; i < docs.size(); i++) {
                //Only the doc with mediatecdoc aspect applied will generate the xml file
                if (this.nodeService.hasAspect(docs.get(i).getChildRef(), QName.createQName("{mediatec.document.model}mediatecdoc"))) {
                    this.generateXMLContent(docs.get(i).getChildRef(), this.nodeService, this.fileFolderService);
                }
            }
        } catch (Throwable e) {
            System.out.println(e.toString());
        }
    }

    // generate corresponding XML Content based on one of attached documents
    public void generateXMLContent(NodeRef doc, NodeService nodeService, FileFolderService fileFolderService) {

        String fileName = nodeService.getProperty(doc, QName.createQName("{http://www.alfresco.org/model/content/1.0}name")) + ".xml";

        NodeRef folder = nodeService.getPrimaryParent(doc).getParentRef();

        FileInfo xmlFileInfo = fileFolderService.create(folder, fileName, ContentModel.TYPE_CONTENT);

        ContentWriter writer = fileFolderService.getWriter(xmlFileInfo.getNodeRef());

        String str = "<?xml version='1.0' encoding='UTF-8' ?><Article>\n";

        if (nodeService.getProperty(doc, QName.createQName("{mediatec.document.model}article_id")) != null) {
            int article_id = (Integer) nodeService.getProperty(doc, QName.createQName("{mediatec.document.model}article_id"));
            str += "<Article_id><![CDATA[" + article_id + "]]></Article_id>\n";

        }
        str+="</Article>";
        writer.putContent(str);
    }
}

how would I test it? Also, how would I log events in out/catalina.out ?

Thanks in advance.

Outcomes