1 package org.kuali.ole.docstore.discovery.service;
2
3 import org.apache.solr.client.solrj.SolrQuery;
4 import org.apache.solr.client.solrj.SolrServer;
5 import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
6 import org.apache.solr.client.solrj.response.FacetField;
7 import org.apache.solr.client.solrj.response.QueryResponse;
8 import org.apache.solr.common.SolrDocument;
9 import org.apache.solr.common.SolrDocumentList;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Ignore;
13 import org.junit.Test;
14 import org.kuali.ole.docstore.discovery.BaseTestCase;
15 import org.kuali.ole.docstore.indexer.solr.DocumentIndexerManagerFactory;
16 import org.kuali.ole.docstore.indexer.solr.IndexerService;
17 import org.kuali.ole.docstore.model.enums.DocCategory;
18 import org.kuali.ole.docstore.model.enums.DocFormat;
19 import org.kuali.ole.docstore.model.enums.DocType;
20 import org.kuali.ole.docstore.discovery.solr.work.bib.marc.WorkBibMarcFields;
21 import org.kuali.rice.core.api.config.property.ConfigContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import java.io.File;
26 import java.net.URL;
27 import java.util.*;
28
29 import static org.junit.Assert.*;
30
31
32
33
34
35
36
37 public class DiscoveryIndexer_AT extends BaseTestCase implements WorkBibMarcFields {
38
39 private static final Logger LOG = LoggerFactory.getLogger(DiscoveryIndexer_AT.class);
40
41 public Map<String, Object> fieldToExpectedValueMap = new HashMap<String, Object>();
42 private List<Map<String, Object>> expRecs = null;
43 private Map<String, List<List<String>>> expectedFacetsDetailsMap = new HashMap<String, List<List<String>>>();
44 private List<List<String>> facetValuesList = null;
45
46 @Before
47 public void setUp() throws Exception {
48 super.setUp();
49
50 }
51
52 @After
53 public void tearDown() throws Exception {
54
55 }
56 @Ignore
57 @Test
58 public void testDiscoveryOutcomeforMarc() throws Throwable {
59 LOG.info("Test: Marc Verifying.....");
60 String inputRecordsFile = "/bib/bib/marc/marc_test.xml";
61 String expectedResultsFile = "/bib/bib/marc/marc_test.properties";
62 setEnvironment(expectedResultsFile);
63 indexFile(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.MARC.getCode(), inputRecordsFile);
64 boolean succeeded1 = testSolrOutputDocsForInputXML();
65 boolean succeeded2 = testFacetsForInputXML();
66 assertTrue(succeeded1 && succeeded2);
67 }
68 @Ignore
69 @Test
70 public void testDiscoveryOutcomeforDublin() throws Throwable {
71 LOG.info("Test: Dublin Core Verifying.....");
72 String inputRecordsFile = "/bib/bib/dublin/Bib-Bib-DublinQ-Test1.xml";
73 String expectedResultsFile = "/bib/bib/dublin/Bib-Bib-DublinQ-Test1.properties";
74 setEnvironment(expectedResultsFile);
75 indexFile(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.DUBLIN_CORE.getCode(), inputRecordsFile);
76 boolean succeeded1 = testSolrOutputDocsForInputXML();
77 boolean succeeded2 = testFacetsForInputXML();
78 assertTrue(succeeded1 && succeeded2);
79 }
80 @Ignore
81 @Test
82 public void testDiscoveryOutcomeforDublinUnQ() throws Throwable {
83 LOG.info("Test: Dublin Core Unqualified Verifying.....");
84 String inputRecordsFile = "/bib/bib/dublin/unqualified/Bib-Bib-DublinUnQ-Test1.xml";
85 String expectedResultsFile = "/bib/bib/dublin/unqualified/Bib-Bib-DublinUnQ-Test1.properties";
86 setEnvironment(expectedResultsFile);
87 indexFile(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.DUBLIN_UNQUALIFIED.getCode(), inputRecordsFile);
88 boolean succeeded1 = testSolrOutputDocsForInputXML();
89 boolean succeeded2 = testFacetsForInputXML();
90 assertTrue(succeeded1 && succeeded2);
91 }
92
93 public void setEnvironment(String expectedResultsFile) throws Exception {
94 SolrFieldsFileReader expectedValues = new SolrFieldsFileReader(expectedResultsFile);
95 expRecs = expectedValues.getRecords();
96 getExpectedFacetsDetailsMap(expRecs);
97 cleanUpData();
98 }
99
100 private void getExpectedFacetsDetailsMap(List<Map<String, Object>> expRecs) {
101 Map<String, Object> map = null;
102 Set<String> set = null;
103 Iterator<String> ite = null;
104 String next = null;
105 String temp = "";
106 List<String> facetValueList = null;
107 for (int k = 0; k < expRecs.size(); k++) {
108 map = expRecs.get(k);
109 set = map.keySet();
110 ite = set.iterator();
111 while (ite.hasNext()) {
112 next = ite.next();
113 temp = next.substring(next.indexOf("_"));
114 if (temp.equalsIgnoreCase("_facet")) {
115 facetValueList = (List<String>) map.get(next);
116 if (expectedFacetsDetailsMap.containsKey(next)) {
117 expectedFacetsDetailsMap.get(next).add(facetValueList);
118 } else {
119 facetValuesList = new ArrayList<List<String>>();
120 facetValuesList.add(facetValueList);
121 expectedFacetsDetailsMap.put(next, facetValuesList);
122 }
123 }
124
125 }
126 }
127 }
128
129 public boolean testFacetsForInputXML() throws Throwable {
130
131 Map<String, Integer> facetCount = new HashMap<String, Integer>();
132 String facetFieldName = "";
133 boolean hasNoErrors = true;
134 FacetField facetField = null;
135 Set<String> facetsFieldsSet = expectedFacetsDetailsMap.keySet();
136 Iterator<String> facetsFieldsIterator = facetsFieldsSet.iterator();
137 QueryResponse response = executeFacetQuery("*:*", facetsFieldsSet);
138 while (facetsFieldsIterator.hasNext()) {
139 facetFieldName = facetsFieldsIterator.next();
140 LOG.info("Verifying Facet: " + facetFieldName);
141 facetField = response.getFacetField(facetFieldName);
142 List<List<String>> expectedFacetValuesList = expectedFacetsDetailsMap.get(facetFieldName);
143 List<String> convertedFacetsList = convertMultipleFacetsListToSingleFacetList(expectedFacetValuesList);
144 Map<String, Integer> facetValueCountMap = getFacetCount(convertedFacetsList);
145 try {
146 verifyFacetFieldValues(facetValueCountMap, facetFieldName, facetField);
147 } catch (AssertionError e) {
148 hasNoErrors = false;
149 LOG.error(e.getMessage());
150 }
151 }
152 return hasNoErrors;
153 }
154
155 private void verifyFacetFieldValues(Map<String, Integer> facetValueCountMap, String facetFieldName,
156 FacetField facetField) {
157 FacetField.Count fc = null;
158 for (int i = 0; i < facetField.getValues().size(); i++) {
159 fc = facetField.getValues().get(i);
160 if (facetValueCountMap.containsKey(fc.getName())) {
161
162 assertEquals(fc.getCount(), facetValueCountMap.get(fc.getName()).intValue());
163 } else {
164 String msg = "could not test facetFieldName " + facetFieldName + " for " + fc.getName()
165 + " because actual value " + fc.getName() + " is not defined in the properties file";
166 assertNotNull(msg, facetValueCountMap.get(fc.getName()));
167 }
168 }
169 }
170
171 private void verifyFacetFieldValue(FacetField facetField, String facetFieldValue, int facetFieldCount) {
172 FacetField.Count fc = null;
173 int count = 0;
174 boolean matched = false;
175 LOG.info("Facet field name=" + facetField.getName() + "; Expected facetFieldValue=" + facetFieldValue
176 + "; facetFieldCount=" + facetFieldCount);
177
178 List<FacetField.Count> facetFieldValues = facetField.getValues();
179 if (null != facetFieldValues) {
180 for (int i = 0; i < facetFieldValues.size(); i++) {
181 fc = facetFieldValues.get(i);
182
183 if (fc.getName().equals(facetFieldValue)) {
184 LOG.info("Expected facetFieldValue found.");
185 assertEquals(facetFieldCount, fc.getCount());
186 matched = true;
187 break;
188 }
189 }
190 }
191 if (!matched) {
192 LOG.info("Expected facetFieldValue not found.");
193 }
194 assertEquals(matched, true);
195 }
196
197 private List<String> convertMultipleFacetsListToSingleFacetList(List<List<String>> list) {
198 List<String> convertedList = new ArrayList<String>();
199 for (int i = 0; i < list.size(); i++) {
200 for (int k = 0; k < list.get(i).size(); k++) {
201 convertedList.add(list.get(i).get(k));
202 }
203 }
204 return convertedList;
205
206 }
207
208 private QueryResponse executeFacetQuery(String args, Set<String> facetFields) {
209 QueryResponse response = null;
210 try {
211 String docSearchUrl="";
212 if(ConfigContext.getCurrentContextConfig()!=null){
213 docSearchUrl=ConfigContext.getCurrentContextConfig().getProperty("docSearchURL");
214 }
215 else{
216 docSearchUrl="http://localhost:8080/oledocstore";
217 }
218
219 SolrServer solr = new CommonsHttpSolrServer(docSearchUrl + "bib");
220 SolrQuery query = new SolrQuery();
221 query.setQuery(args);
222 Iterator<String> facetFieldsIterator = facetFields.iterator();
223 while (facetFieldsIterator.hasNext()) {
224 query.addFacetField(facetFieldsIterator.next());
225
226 }
227
228 query.setFacet(true);
229 response = solr.query(query);
230 } catch (Exception e) {
231 LOG.error(e.getMessage() , e);
232 }
233 return response;
234 }
235
236 private Map<String, Integer> getFacetCount(List<String> list) {
237 Map<String, Integer> facetCount = new HashMap<String, Integer>();
238 for (String word : list) {
239 Integer count = facetCount.get(word);
240 facetCount.put(word, (count == null) ? 1 : count + 1);
241 }
242 return facetCount;
243 }
244
245 public boolean testSolrOutputDocsForInputXML() throws Throwable {
246
247 List<Object> actRecs = getSolrRecordsForQueryField("*", "*");
248 String fieldNameTemp = "";
249 boolean hasNoErrors = true;
250
251 assertEquals(expRecs.size(), actRecs.size());
252
253
254 for (int i = 0; i < expRecs.size(); i++) {
255 for (String fieldName : expRecs.get(i).keySet()) {
256 fieldNameTemp = fieldName.substring(fieldName.indexOf("_"));
257 Object exceptedValue = expRecs.get(i).get(fieldName);
258 Object actualValue = ((SolrDocument) actRecs.get(i)).getFieldValue(fieldName);
259 if (!fieldNameTemp.equalsIgnoreCase("_facet")) {
260 LOG.info("Verifying Field: " + fieldName + "\t\t:\t\t" + actualValue);
261 try {
262 assertEquals(exceptedValue == null ? "" : exceptedValue, actualValue == null ? "" : actualValue);
263 } catch (AssertionError e) {
264 hasNoErrors = false;
265 LOG.error(e.getMessage());
266 }
267 }
268 }
269 }
270
271 return hasNoErrors;
272 }
273
274 protected void indexFile(String docCategory, String docType, String docFormat, String inputRecordsFile) throws Exception {
275 URL resource = getClass().getResource(inputRecordsFile);
276 File file = new File(resource.toURI());
277 String docContent = readFile(file);
278 String result = null;
279
280 result = getIndexerService(docCategory, docType, docFormat).indexDocumentsFromStringBySolrDoc(docCategory, docType, docFormat, docContent);
281 assertNotNull(result);
282 assertTrue(result.contains(IndexerService.SUCCESS));
283 String countStr = result.substring(result.indexOf("-") + 1);
284 int count = Integer.parseInt(countStr);
285 assertTrue(count > 0);
286 }
287
288 private List<Object> getSolrRecordsForQueryField(String fieldName, String fieldValue) throws Exception {
289 SolrDocument doc = null;
290 List<Object> records = new ArrayList<Object>();
291 String args = fieldName + ":" + fieldValue;
292 QueryResponse response = executeQuery(args);
293 SolrDocumentList solrDocumentList = response.getResults();
294 for (int i = 0; i < solrDocumentList.size(); i++) {
295 doc = solrDocumentList.get(i);
296 records.add(doc);
297 }
298 return records;
299 }
300
301 private IndexerService getIndexerService(String category, String type, String format) {
302 IndexerService indexerService = DocumentIndexerManagerFactory.getInstance().getDocumentIndexManager(category, type, format);
303 return indexerService;
304 }
305
306 }