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

    
158

    
159
    
160
    
161
}
162

    
(4-4/4)