Project

General

Profile

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

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

    
5
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.io.StringWriter;
11
import java.io.Writer;
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import org.apache.solr.client.solrj.SolrServer;
16
import org.apache.solr.client.solrj.SolrServerException;
17
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
18
import org.apache.solr.client.solrj.response.QueryResponse;
19
import org.apache.solr.common.params.SolrParams;
20
import org.apache.solr.core.CoreContainer;
21
import org.apache.solr.core.SolrCore;
22
import org.apache.solr.request.LocalSolrQueryRequest;
23
import org.apache.solr.response.SolrQueryResponse;
24
import org.apache.solr.response.XMLResponseWriter;
25
import org.apache.solr.servlet.SolrRequestParsers;
26
import org.dataone.service.types.v1.SystemMetadata;
27
import org.dataone.service.util.TypeMarshaller;
28
import org.junit.Before;
29
import org.junit.Test;
30

    
31
public class SolrIndexIT  {
32
    
33
    private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
34
    private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
35
    private static final String SYSTEMMETAUPDATEFILEPATH = "src/test/resources/eml-updating-system-meta-example.xml";
36
    private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
37
    private static final String SYSTEMMETAARCHIVEFILEPATH = "src/test/resources/eml-archive-system-meta-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
    
41
	private SolrIndex solrIndex = null;
42
    
43
    
44
    @Before
45
    public void setUp() throws Exception {
46
            solrIndex  = generateSolrIndex();
47
    }
48
    
49
    private SolrIndex generateSolrIndex() throws Exception {
50
        String springConfigFile = "src/main/resources/index-processor-context.xml";
51
        String metacatPropertyFile = null; //in this test, we use the test.properties file rather than metacat.properties file. so set it to be null.
52
        ApplicationController controller = new ApplicationController(springConfigFile, metacatPropertyFile);
53
        controller.initialize();
54
        List<SolrIndex> list = controller.getSolrIndexes();
55
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
56
        SolrIndex index = solrIndexesarray[0];
57
        //SolrServer solrServer = SolrServerFactory.createSolrServer();
58
        //index.setSolrServer(solrServer);
59
        return index;
60
    }
61
    
62
    /**
63
     * Test building index for an insert.
64
     */
65
    @Test
66
    public void testInsert() throws Exception {
67
    	
68
    	
69
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
70
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
71
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH)); 
72
       List<String> chain = null;
73
       solrIndex.update(id, chain, systemMetadata, emlInputStream);
74
       String result = doQuery(solrIndex.getSolrServer());
75
       List<String> ids = solrIndex.getSolrIds();
76
       //assertTrue(ids.size() == 1);
77
       boolean foundId = false;
78
       for(String identifiers :ids) {
79
           if(id.equals(identifiers)) {
80
               foundId = true;
81
           }
82
       }
83
       assertTrue(foundId);
84
       assertTrue(result.contains("version1"));
85
    	
86
    }
87
    
88
    /**
89
     * Test building index for an insert.
90
     */
91
    @Test
92
    public void testUpdate() throws Exception {
93
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
94
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
95
       InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));  
96
       ArrayList<String> obsoletes = new ArrayList<String>();
97
       obsoletes.add(id);
98
       obsoletes.add("tao");
99
       solrIndex.update(newId, obsoletes, systemMetadata, emlInputStream);
100
       String result = doQuery(solrIndex.getSolrServer());
101
       assertTrue(result.contains("version2"));
102
    }
103
    
104
    /**
105
     * Test building index for an insert.
106
     */
107
    @Test
108
    public void testArchive() throws Exception {
109
       SolrIndex solrIndex = generateSolrIndex();
110
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
111
       //System metadata's archive is true.
112
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAARCHIVEFILEPATH);
113
       InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));    
114
       ArrayList<String> obsoletes = new ArrayList<String>();
115
       obsoletes.add(id);
116
       obsoletes.add("tao");
117
       solrIndex.update(newId, obsoletes, systemMetadata, emlInputStream);
118
       String result = doQuery(solrIndex.getSolrServer());
119
       assertTrue(!result.contains("version1"));
120
       assertTrue(!result.contains("version2"));
121
    }
122
    
123
    
124
    /*
125
     * Do query
126
     */
127
    public static String doQuery(SolrServer server)
128
                    throws SolrServerException {
129
                StringBuffer request = new StringBuffer();
130
                request.append("q=" + "*:*");
131
                SolrParams solrParams = SolrRequestParsers.parseQueryString(request
132
                        .toString());
133
                QueryResponse reponse = server.query(solrParams);
134
                String result = toXML(solrParams, reponse);
135
                System.out.println("**************************************************************************");
136
                System.out.println("The query result:\n");
137
                System.out.println(result);
138
                System.out.println("**************************************************************************");
139
                return result;
140
    }
141
    
142
    /*
143
     * Transform the query response to the xml format.
144
     */
145
    private static String toXML(SolrParams request, QueryResponse response) {
146
        XMLResponseWriter xmlWriter = new XMLResponseWriter();
147
        Writer w = new StringWriter();
148
        SolrQueryResponse sResponse = new SolrQueryResponse();
149
        sResponse.setAllValues(response.getResponse());
150
        try {
151
            SolrCore core = null;
152
            CoreContainer container = SolrServerFactory.getCoreContainer();
153
            core = container.getCore("collection1");
154
            xmlWriter.write(w, new LocalSolrQueryRequest(core, request), sResponse);
155
        } catch (Exception e) {
156
            throw new RuntimeException("Unable to convert Solr response into XML", e);
157
        }
158
        return w.toString();
159
    }
160

    
161

    
162
    
163
    
164
}
165

    
(4-4/4)