Project

General

Profile

1
package edu.ucsb.nceas.metacat.index.resourcemap;
2

    
3
import static org.junit.Assert.assertTrue;
4

    
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.util.ArrayList;
8
import java.util.HashMap;
9
import java.util.List;
10
import java.util.Map;
11

    
12
import javax.xml.parsers.DocumentBuilder;
13
import javax.xml.parsers.DocumentBuilderFactory;
14

    
15
import org.dataone.cn.indexer.parser.IDocumentSubprocessor;
16
import org.dataone.cn.indexer.solrhttp.SolrDoc;
17
import org.dataone.cn.indexer.solrhttp.SolrElementField;
18
import org.junit.Test;
19
import org.w3c.dom.Document;
20
import org.w3c.dom.Node;
21

    
22
import edu.ucsb.nceas.metacat.index.SolrIndex;
23
import edu.ucsb.nceas.metacat.index.SolrIndexIT;
24

    
25
public class ResourceMapSubprocessorIT {
26
    
27
    /**
28
     * Test the case that the resource map contains a component which is not int the solr index server.
29
     * @throws Exception
30
     */
31
    @Test
32
    public void testProcessDocument() throws Exception {
33
        //Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", "../lib/hazelcast.xml");
34
        String id = "resourceMap_urn:uuid:04780847-6082-455b-9831-22269c9ec0a5";
35
        Document doc = getResourceMapDoc();
36
        System.out.println("the lenght of child is "+doc.getChildNodes().getLength());
37
        System.out.println("the name of the childs is "+doc.getChildNodes().item(0).getLocalName());
38
        Node child = doc.getChildNodes().item(0);
39
        List<SolrElementField> sysSolrFields = new ArrayList<SolrElementField>();
40
        SolrDoc indexDocument = new SolrDoc(sysSolrFields);
41
        Map<String, SolrDoc> docs = new HashMap<String, SolrDoc>();
42
        docs.put(id, indexDocument);
43
        ResourceMapSubprocessor processor = getResourceMapSubprocessor();
44
        try {
45
            processor.processDocument(id, docs, doc);
46
            assertTrue("ResourceMapProcessor should throw an exception because the resource map has a component which doesn't exist.", false);
47
        } catch (Exception e) {
48
            assertTrue("ResourceMapProcessor should throw a ResourceMapException because the resource map has a component which doesn't exist.", e instanceof ResourceMapException);
49
            //System.out.println("the message is "+e.getMessage());
50
        }
51
    }
52
    
53
    /*
54
     * Get the document format of the test resource map file 
55
     */
56
    private Document getResourceMapDoc() throws Exception{
57
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
58
        documentBuilderFactory.setNamespaceAware(true);
59
        DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
60
        File file = new File("src/test/resources/resorcemap-example");
61
        Document doc = builder.parse(new FileInputStream(file));
62
        return doc;
63
    }
64
    
65
    /*
66
     * Get the ResourceMapSubprocessor
67
     */
68
    private ResourceMapSubprocessor getResourceMapSubprocessor() throws Exception {
69
        ResourceMapSubprocessor resorceMapprocessor = null;
70
        SolrIndex solrIndex = SolrIndexIT.generateSolrIndex();
71
        List<IDocumentSubprocessor> processors = solrIndex.getSubprocessors();
72
        for(IDocumentSubprocessor processor : processors) {
73
            if(processor instanceof ResourceMapSubprocessor) {
74
                resorceMapprocessor = (ResourceMapSubprocessor) processor;
75
            }
76
        }
77
        return resorceMapprocessor;
78
    }
79
}
    (1-1/1)