Project

General

Profile

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

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

    
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.util.List;
11

    
12
import javax.xml.parsers.ParserConfigurationException;
13

    
14

    
15

    
16
import org.apache.commons.io.FileUtils;
17
import org.apache.solr.client.solrj.SolrServer;
18
import org.apache.solr.client.solrj.SolrServerException;
19
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
20
import org.apache.solr.client.solrj.response.QueryResponse;
21
import org.apache.solr.common.SolrDocument;
22
import org.apache.solr.common.SolrDocumentList;
23
import org.apache.solr.common.params.SolrParams;
24
import org.apache.solr.core.CoreContainer;
25
import org.apache.solr.servlet.SolrRequestParsers;
26
import org.dataone.configuration.Settings;
27
import org.dataone.service.types.v1.SystemMetadata;
28
import org.dataone.service.util.TypeMarshaller;
29
import org.junit.Test;
30
import org.xml.sax.SAXException;
31

    
32
public class SolrIndexTest  {
33
    
34
    private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
35
    private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
36
    private static final String SYSTEMMETAUPDATEFILEPATH = "src/test/resources/eml-updating-system-meta-example.xml";
37
    private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
38
    private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
39
    private static final String newId = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39099";
40
    private static final String SOLRHOME = "solr-home";
41
    private static final String SOLRTESTHOMEPATH = "target/"+SOLRHOME;
42
    
43
    
44
    /**
45
     * Test building index for an insert.
46
     */
47
    @Test
48
    public void testInsert() throws Exception {
49
        SolrIndex solrIndex = generateSolrIndex();
50
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
51
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
52
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));    
53
       solrIndex.insert(id, systemMetadata, emlInputStream);
54
       String result = doQuery(solrIndex.getSolrServer());
55
       assertTrue(result.contains("version1"));
56
    }
57
    
58
    /**
59
     * Test building index for an insert.
60
     */
61
    @Test
62
    public void testUpdate() throws Exception {
63
        SolrIndex solrIndex = generateSolrIndex();
64
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
65
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
66
       InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));    
67
       solrIndex.update(newId, systemMetadata, emlInputStream);
68
       String result = doQuery(solrIndex.getSolrServer());
69
       assertTrue(result.contains("version2"));
70
    }
71
    
72
    /**
73
     * Test building index for an insert.
74
     */
75
    //@Test
76
    /*public void testDelete() throws Exception {
77
       SolrIndex solrIndex = generateSolrIndex();
78
       //solrIndex.remove(newId);
79
       //String result = doQuery(solrIndex.getSolrServer());
80
       //assertTrue(!result.contains("version1"));
81
       //assertTrue(!result.contains("version2"));
82
    }*/
83
    
84
   
85
    
86
    /*
87
     * Do query
88
     */
89
    private String doQuery(SolrServer server)
90
                    throws SolrServerException {
91
                StringBuffer request = new StringBuffer();
92
                request.append("q=" + "*:*");
93
                SolrParams solrParams = SolrRequestParsers.parseQueryString(request
94
                        .toString());
95
                QueryResponse reponse = server.query(solrParams);
96
                System.out.println("**************************************************************************");
97
                System.out.println("The query result:\n");
98
                System.out.println(reponse.toString());
99
                System.out.println("**************************************************************************");
100
                return reponse.toString();
101
    }
102

    
103
    
104
    /*
105
     * Print out the QueryResponse
106
     */
107
    /*private void print(QueryResponse response) {
108
        SolrDocumentList docs = response.getResults();
109
        if (docs != null) {
110
            System.out.println(docs.getNumFound() + " documents found, "
111
                    + docs.size() + " returned : ");
112
            for (int i = 0; i < docs.size(); i++) {
113
                SolrDocument doc = docs.get(i);
114
                System.out.println("\t" + doc.toString());
115
            }
116
        }
117
    }*/
118
    
119
    private SolrIndex generateSolrIndex() throws Exception {
120
        Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", ApplicationControllerTest.pathToHazelcastFile);
121
        ApplicationController controller = new ApplicationController();
122
        List<SolrIndex> list = controller.getSolrIndexes();
123
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
124
        SolrIndex index = solrIndexesarray[0];
125
        index.setSolrServer(generateSolrServer());
126
        return index;
127
    }
128
    
129
    /*
130
     * Generate a test solr server
131
     */
132
    private SolrServer generateSolrServer() throws IOException, ParserConfigurationException, SAXException {
133
        createSolrHome();
134
        System.setProperty(SolrIndex.SOLRHOME, SOLRTESTHOMEPATH);
135
        //System.out.println("====="+"/Users/tao/projects/metacat-index/"+SOLRTESTHOMEPATH);
136
        CoreContainer.Initializer init = new CoreContainer.Initializer();
137
        CoreContainer c = init.initialize();
138
        SolrServer solrServer = new EmbeddedSolrServer(c, "collection1");
139
        return solrServer;
140
    }
141
    
142
    /*
143
     * Create a solr home directory
144
     */
145
    private void createSolrHome() throws IOException {
146
        File solrHomeTestDir =  new File(SOLRTESTHOMEPATH);
147
        solrHomeTestDir.mkdir();
148
        File solrData = new File(SOLRTESTHOMEPATH+"/data");
149
        solrData.mkdir();
150
        File solr_home_source_dir = new File("src/main/resources/"+SOLRHOME);
151
        FileUtils.copyDirectory(solr_home_source_dir, solrHomeTestDir);
152
    }
153
}
154

    
(2-2/2)