Project

General

Profile

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

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

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

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

    
35
public class SolrIndexIT  {
36
    
37
    private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
38
    private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
39
    private static final String SYSTEMMETAUPDATEFILEPATH = "src/test/resources/eml-updating-system-meta-example.xml";
40
    private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
41
    private static final String SYSTEMMETAARCHIVEFILEPATH = "src/test/resources/eml-archive-system-meta-example.xml";
42
    private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
43
    private static final String newId = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39099";
44
    
45
	private String annotation_id = "http://doi.org/annotation.1.1";
46
	private static final String ANNOTATION_SYSTEM_META_FILE_PATH = "src/test/resources/annotation-system-meta-example.xml";
47
	private static final String ANNOTATION_FILE_PATH = "src/test/resources/annotation-example.rdf";;
48
    
49
	private SolrIndex solrIndex = null;
50
    
51
    @Before
52
    public void setUp() throws Exception {
53
            solrIndex  = generateSolrIndex();
54
    }
55
    
56
    public static SolrIndex generateSolrIndex() throws Exception {
57
        String springConfigFile = "/index-processor-context.xml";
58
        String metacatPropertyFile = null; //in this test, we use the test.properties file rather than metacat.properties file. so set it to be null.
59
        ApplicationController controller = new ApplicationController(springConfigFile, metacatPropertyFile);
60
        controller.initialize();
61
        List<SolrIndex> list = controller.getSolrIndexes();
62
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
63
        SolrIndex index = solrIndexesarray[0];
64
        //SolrServer solrServer = SolrServerFactory.createSolrServer();
65
        //index.setSolrServer(solrServer);
66
        return index;
67
    }
68
    
69
    /**
70
     * Test building index for an insert.
71
     */
72
//    @Test
73
    public void testInsert() throws Exception {
74
    	
75
    	
76
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
77
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
78
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH)); 
79
       //List<String> chain = null;
80
       Identifier pid = new Identifier();
81
       pid.setValue(id);
82
       solrIndex.update(pid, systemMetadata, emlInputStream);
83
       String result = doQuery(solrIndex.getSolrServer());
84
       List<String> ids = solrIndex.getSolrIds();
85
       //assertTrue(ids.size() == 1);
86
       boolean foundId = false;
87
       for(String identifiers :ids) {
88
           if(id.equals(identifiers)) {
89
               foundId = true;
90
           }
91
       }
92
       assertTrue(foundId);
93
       assertTrue(result.contains("version1"));
94
    	
95
    }
96
    
97
    /**
98
     * Test building index for an insert.
99
     */
100
//    @Test
101
    public void testUpdate() throws Exception {
102
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
103
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
104
       InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));  
105
       /*obsoletes.add(id);
106
       obsoletes.add("tao");*/
107
       Identifier pid = new Identifier();
108
       pid.setValue(newId);
109
       solrIndex.update(pid, systemMetadata, emlInputStream);
110
       String result = doQuery(solrIndex.getSolrServer());
111
       assertTrue(result.contains("version1"));
112
       assertTrue(result.contains("version2"));
113
    }
114
    
115
    /**
116
     * Test building index for an insert.
117
     */
118
//    @Test
119
    public void testArchive() throws Exception {
120
       SolrIndex solrIndex = generateSolrIndex();
121
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
122
       //System metadata's archive is true.
123
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAARCHIVEFILEPATH);
124
       InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));    
125
       /*ArrayList<String> obsoletes = new ArrayList<String>();
126
       obsoletes.add(id);
127
       obsoletes.add("tao");*/
128
       Identifier pid = new Identifier();
129
       pid.setValue(newId);
130
       solrIndex.update(pid, systemMetadata, emlInputStream);
131
       String result = doQuery(solrIndex.getSolrServer());
132
       assertTrue(result.contains("version1"));
133
       assertTrue(!result.contains("version2"));
134
    }
135
    
136
    
137
    /**
138
     * Test building index for dynamic fields.
139
     */
140
    @Test
141
    public void testDynamicFields() throws Exception {
142
    	
143
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
144
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH)); 
145
       Identifier pid = new Identifier();
146
       pid.setValue(id);
147
       solrIndex.update(pid, systemMetadata, emlInputStream);
148
       String result = doQuery(solrIndex.getSolrServer());
149
       List<String> ids = solrIndex.getSolrIds();
150
       boolean foundId = false;
151
       for(String identifiers :ids) {
152
           if (id.equals(identifiers)) {
153
               foundId = true;
154
           }
155
       }
156
       assertTrue(foundId);
157
       assertTrue(result.contains("version1"));
158
       
159
       // augment with the dynamic field
160
       String fieldName = "test_count_i";
161
       Map<String, List<Object>> fields = new HashMap<String, List<Object>>();
162
       List<Object> values = new ArrayList<Object>();
163
       values.add(6);
164
       fields.put(fieldName, values);
165
       solrIndex.insertFields(pid, fields);
166
       result = doQuery(solrIndex.getSolrServer(), "&fq=" + fieldName + ":[0 TO 5]");
167
       assertFalse(result.contains(id));
168
       result = doQuery(solrIndex.getSolrServer(), "&fq=" + fieldName + ":[6 TO 6]");
169
       assertTrue(result.contains(id));
170
       
171
       // now update the value
172
       values.clear();
173
       values.add(7);
174
       fields.put(fieldName, values);
175
       solrIndex.insertFields(pid, fields);
176
       result = doQuery(solrIndex.getSolrServer(), "&fq=" + fieldName + ":[7 TO 7]");
177
       assertTrue(result.contains(id));
178
       
179
    }
180
    
181
    /**
182
     * Test building index for annotation.
183
     */
184
    @Test
185
    public void testAnnotation() throws Exception {
186
    	
187
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
188
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH)); 
189
       Identifier pid = new Identifier();
190
       pid.setValue(id);
191
       solrIndex.update(pid, systemMetadata, emlInputStream);
192
       String result = doQuery(solrIndex.getSolrServer());
193
       List<String> ids = solrIndex.getSolrIds();
194
       boolean foundId = false;
195
       for(String identifiers :ids) {
196
           if (id.equals(identifiers)) {
197
               foundId = true;
198
           }
199
       }
200
       assertTrue(foundId);
201
       assertTrue(result.contains("version1"));
202
       
203
       // augment with the dynamic field
204
       SystemMetadata annotationSystemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, ANNOTATION_SYSTEM_META_FILE_PATH);
205
       InputStream annotationInputStream = new FileInputStream(new File(ANNOTATION_FILE_PATH)); 
206
       Identifier annotationPid = new Identifier();
207
       annotationPid.setValue(annotation_id);
208
       solrIndex.update(annotationPid, annotationSystemMetadata, annotationInputStream);
209
       String annotationResult = doQuery(solrIndex.getSolrServer());
210
       assertTrue(annotationResult.contains("measurement_sm"));
211
       
212
    }
213
    
214
    /**
215
     * Do query - with no additional params
216
     */
217
    public static String doQuery(SolrServer server)
218
                    throws SolrServerException {
219
    	return doQuery(server, null);
220
    }
221
    
222
    /**
223
     * Do query, allowing additional parameters
224
     */
225
    public static String doQuery(SolrServer server, String moreParams)
226
                    throws SolrServerException {
227
                StringBuffer request = new StringBuffer();
228
                request.append("q=" + "*:*");
229
                if (moreParams != null) {
230
                    request.append(moreParams);
231
                }
232
                SolrParams solrParams = SolrRequestParsers.parseQueryString(request
233
                        .toString());
234
                QueryResponse reponse = server.query(solrParams);
235
                String result = toXML(solrParams, reponse);
236
                System.out.println("**************************************************************************");
237
                System.out.println("The query result:\n");
238
                System.out.println(result);
239
                System.out.println("**************************************************************************");
240
                return result;
241
    }
242
    
243
    /**
244
     * Transform the query response to the xml format.
245
     */
246
    private static String toXML(SolrParams request, QueryResponse response) {
247
        XMLResponseWriter xmlWriter = new XMLResponseWriter();
248
        Writer w = new StringWriter();
249
        SolrQueryResponse sResponse = new SolrQueryResponse();
250
        sResponse.setAllValues(response.getResponse());
251
        try {
252
            SolrCore core = null;
253
            CoreContainer container = SolrServerFactory.getCoreContainer();
254
            core = container.getCore("collection1");
255
            xmlWriter.write(w, new LocalSolrQueryRequest(core, request), sResponse);
256
        } catch (Exception e) {
257
            throw new RuntimeException("Unable to convert Solr response into XML", e);
258
        }
259
        return w.toString();
260
    }
261

    
262

    
263
    
264
    
265
}
266

    
(4-4/5)