source: SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestUtils.java @ 5799

Last change on this file since 5799 was 5799, checked in by Oliver Schonefeld, 10 years ago
  • Property svn:eol-style set to native
File size: 12.8 KB
Line 
1/**
2 * This software is copyright (c) 2012-2014 by
3 *  - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
4 * This is free software. You can redistribute it
5 * and/or modify it under the terms described in
6 * the GNU General Public License v3 of which you
7 * should have received a copy. Otherwise you can download
8 * it from
9 *
10 *   http://www.gnu.org/licenses/gpl-3.0.txt
11 *
12 * @copyright Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
13 *
14 * @license http://www.gnu.org/licenses/gpl-3.0.txt
15 *  GNU General Public License v3
16 */
17package eu.clarin.sru.client;
18
19import java.net.URI;
20import java.util.List;
21
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24import org.w3c.dom.Node;
25
26import eu.clarin.sru.client.SRUExplainRecordData.ConfigInfo;
27import eu.clarin.sru.client.SRUExplainRecordData.Schema;
28import eu.clarin.sru.client.fcs.ClarinFCSEndpointDescription;
29import eu.clarin.sru.client.fcs.ClarinFCSEndpointDescription.ResourceInfo;
30import eu.clarin.sru.client.fcs.ClarinFCSRecordData;
31import eu.clarin.sru.client.fcs.DataView;
32import eu.clarin.sru.client.fcs.DataViewGenericDOM;
33import eu.clarin.sru.client.fcs.DataViewGenericString;
34import eu.clarin.sru.client.fcs.DataViewHits;
35import eu.clarin.sru.client.fcs.Resource;
36
37class TestUtils {
38    private static final Logger logger =
39            LoggerFactory.getLogger(TestUtils.class);
40
41    public static SRUExplainRequest makeExplainRequest(String baseURI) {
42        SRUExplainRequest request = new SRUExplainRequest(baseURI);
43        request.setExtraRequestData("x-indent-response", "4");
44        request.setExtraRequestData("x-fcs-endpoint-description", "true");
45        request.setParseRecordDataEnabled(true);
46        return request;
47    }
48
49
50    public static SRUScanRequest makeScanRequest(String baseURI) {
51        SRUScanRequest request = new SRUScanRequest(baseURI);
52        request.setScanClause("fcs.resource = root");
53        request.setExtraRequestData("x-clarin-resource-info", "true");
54        request.setExtraRequestData("x-indent-response", "4");
55        return request;
56    }
57
58
59    public static SRUSearchRetrieveRequest makeSearchRequest(String baseURI, String query) {
60        if ((query == null) || query.isEmpty()) {
61            query = "Faustus";
62        }
63        SRUSearchRetrieveRequest request = new SRUSearchRetrieveRequest(baseURI);
64        request.setQuery(query);
65//        request.setRecordSchema(ClarinFCSRecordData.LEGACY_RECORD_SCHEMA);
66        request.setMaximumRecords(5);
67        request.setRecordPacking(SRURecordPacking.XML);
68        request.setExtraRequestData("x-indent-response", "4");
69        return request;
70    }
71
72
73    public static void printExplainResponse(SRUExplainResponse response) {
74        logger.info("displaying results of 'explain' request ...");
75        if (response.hasDiagnostics()) {
76            for (SRUDiagnostic diagnostic : response.getDiagnostics()) {
77                logger.info("uri={}, message={}, detail={}",
78                        diagnostic.getURI(),
79                        diagnostic.getMessage(),
80                        diagnostic.getDetails());
81            }
82        }
83        if (response.hasRecord()) {
84            SRURecord record = response.getRecord();
85            logger.info("schema = {}", record.getRecordSchema());
86            if (record.isRecordSchema(SRUExplainRecordData.RECORD_SCHEMA)) {
87                dumpExplainRecordData(record.getRecordData());
88            }
89            if (record.hasExtraRecordData()) {
90                logger.info("extraRecordInfo = {}",
91                        record.getExtraRecordData());
92            }
93        }
94        if (response.hasExtraResponseData()) {
95            for (SRUExtraResponseData data : response.getExtraResponseData()) {
96                if (data instanceof ClarinFCSEndpointDescription) {
97                    dumpEndpointDescription(
98                            (ClarinFCSEndpointDescription) data);
99                } else {
100                    logger.info("extraResponseData = {} (class={})",
101                            data.getRootElement(), data.getClass().getName());
102                }
103
104            }
105        }
106    }
107
108
109    public static void printScanResponse(SRUScanResponse response) {
110        logger.info("displaying results of 'scan' request ...");
111        if (response.hasDiagnostics()) {
112            for (SRUDiagnostic diagnostic : response.getDiagnostics()) {
113                logger.info("uri={}, message={}, detail={}",
114                        diagnostic.getURI(),
115                        diagnostic.getMessage(),
116                        diagnostic.getDetails());
117            }
118        }
119        if (response.hasTerms()) {
120            for (SRUTerm term : response.getTerms()) {
121                logger.info("value={}, numberOfRecords={}, displayTerm={}",
122                            term.getValue(),
123                            term.getNumberOfRecords(),
124                            term.getDisplayTerm());
125            }
126        } else {
127            logger.info("no terms");
128        }
129    }
130
131
132    public static void printSearchResponse(SRUSearchRetrieveResponse response) {
133        logger.info("displaying results of 'searchRetrieve' request ...");
134        logger.info("numberOfRecords = {}, nextResultPosition = {}",
135                response.getNumberOfRecords(),
136                response.getNextRecordPosition());
137        if (response.hasDiagnostics()) {
138            for (SRUDiagnostic diagnostic : response.getDiagnostics()) {
139                logger.info("uri={}, message={}, detail={}",
140                        diagnostic.getURI(),
141                        diagnostic.getMessage(),
142                        diagnostic.getDetails());
143            }
144        }
145        if (response.hasRecords()) {
146            for (SRURecord record : response.getRecords()) {
147                logger.info("schema = {}, identifier = {}, position = {}",
148                        record.getRecordSchema(),
149                        record.getRecordIdentifier(),
150                        record.getRecordPosition());
151                if (record.isRecordSchema(ClarinFCSRecordData.RECORD_SCHEMA)) {
152                    ClarinFCSRecordData rd =
153                            (ClarinFCSRecordData) record.getRecordData();
154                    dumpResource(rd.getResource());
155                } else if (record.isRecordSchema(SRUSurrogateRecordData.RECORD_SCHEMA)) {
156                    SRUSurrogateRecordData r =
157                            (SRUSurrogateRecordData) record.getRecordData();
158                    logger.info("SURROGATE DIAGNOSTIC: uri={}, message={}, detail={}",
159                                r.getURI(), r.getMessage(), r.getDetails());
160                } else {
161                    logger.info("UNSUPPORTED SCHEMA: {}",
162                            record.getRecordSchema());
163                }
164            }
165        } else {
166            logger.info("no results");
167        }
168    }
169
170
171    public static void dumpExplainRecordData(SRURecordData recordData) {
172        if (SRUExplainRecordData.RECORD_SCHEMA.equals(recordData.getRecordSchema())) {
173            SRUExplainRecordData data = (SRUExplainRecordData) recordData;
174            logger.info("host={}, port={}, database={}",
175                    data.getServerInfo().getHost(),
176                    data.getServerInfo().getPort(),
177                    data.getServerInfo().getDatabase());
178            List<Schema> schemaInfo = data.getSchemaInfo();
179            if (schemaInfo != null) {
180                for (Schema schema : schemaInfo) {
181                    logger.debug("schema: identifier={}, name={}, " +
182                            "location={}, sort={}, retrieve={}",
183                            schema.getIdentifier(),
184                            schema.getName(),
185                            schema.getLocation(),
186                            schema.getSort(),
187                            schema.getRetrieve());
188                }
189            }
190            ConfigInfo configInfo = data.getConfigInfo();
191            if (configInfo != null) {
192                if (configInfo.getDefaults() != null) {
193                    logger.debug("configInfo/default = {}",
194                            configInfo.getDefaults());
195                }
196                if (configInfo.getSettings() != null) {
197                    logger.debug("configInfo/setting = {}",
198                            configInfo.getSettings());
199                }
200                if (configInfo.getSupports() != null) {
201                    logger.debug("configInfo/supports = {}",
202                            configInfo.getSupports());
203                }
204            }
205        }
206    }
207
208
209    public static void dumpResource(Resource resource) {
210        logger.info("CLARIN-FCS: pid={}, ref={}",
211                resource.getPid(), resource.getRef());
212        if (resource.hasDataViews()) {
213            dumpDataView("CLARIN-FCS: ", resource.getDataViews());
214        }
215        if (resource.hasResourceFragments()) {
216            for (Resource.ResourceFragment fragment : resource.getResourceFragments()) {
217                logger.debug("CLARIN-FCS: ResourceFragment: pid={}, ref={}",
218                        fragment.getPid(), fragment.getRef());
219                if (fragment.hasDataViews()) {
220                    dumpDataView("CLARIN-FCS: ResourceFragment/", fragment.getDataViews());
221                }
222            }
223        }
224    }
225
226
227    private static void dumpEndpointDescription(ClarinFCSEndpointDescription ed) {
228        logger.info("dumping <EndpointDescription> (version={})",
229                ed.getVersion());
230        for (URI capability : ed.getCapabilities()) {
231            logger.info("  capability: {}", capability);
232        } // for
233        for (ClarinFCSEndpointDescription.DataView dataView :
234            ed.getSupportedDataViews()) {
235            logger.info("  supportedDataView: id={}, type={}, policy={}",
236                    dataView.getIdentifier(),
237                    dataView.getMimeType(),
238                    dataView.getDeliveryPolicy());
239        } // for
240        dumpResourceInfo(ed.getResources(), 1, "  ");
241    }
242
243
244    private static void dumpResourceInfo(List<ResourceInfo> ris, int depth,
245            String indent) {
246        for (ResourceInfo ri : ris) {
247            logger.info("{}[depth={}] <ResourceInfo>", indent, depth);
248            logger.info("{}    pid={}", indent, ri.getPid());
249            logger.info("{}    title: {}", indent, ri.getTitle());
250            if (ri.getDescription() != null) {
251                logger.info("{}    description: {}",
252                        indent, ri.getDescription());
253            }
254            if (ri.getLandingPageURI() != null) {
255                logger.info("{}    landingPageURI: {}",
256                        indent, ri.getLandingPageURI());
257            }
258            for (ClarinFCSEndpointDescription.DataView dv :
259                ri.getAvailableDataViews()) {
260                logger.info("{}    available dataviews: type={}, policy={}",
261                        indent, dv.getMimeType(), dv.getDeliveryPolicy());
262            }
263            if (ri.hasSubResources()) {
264                dumpResourceInfo(ri.getSubResources(),
265                        depth + 1, indent + "  ");
266            }
267        }
268    }
269
270
271    private static void dumpDataView(String s, List<DataView> dataviews) {
272        for (DataView dataview : dataviews) {
273            logger.info("{}DataView: type={}, pid={}, ref={}",
274                    s, dataview.getMimeType(), dataview.getPid(),
275                    dataview.getRef());
276            if (dataview instanceof DataViewGenericDOM) {
277                final DataViewGenericDOM view = (DataViewGenericDOM) dataview;
278                final Node root = view.getDocument().getFirstChild();
279                logger.info("{}DataView (generic dom): root element <{}> / {}",
280                        s, root.getNodeName(),
281                        root.getOwnerDocument().hashCode());
282            } else if (dataview instanceof DataViewGenericString) {
283                final DataViewGenericString view =
284                        (DataViewGenericString) dataview;
285                logger.info("{}DataView (generic string): data = {}",
286                        s, view.getContent());
287            } else if (dataview instanceof DataViewHits) {
288                final DataViewHits hits = (DataViewHits) dataview;
289                logger.info("{}DataView: {}",
290                        s, addHitHighlights(hits));
291            } else {
292                logger.info("{}DataView: cannot display " +
293                        "contents of unexpected class '{}'",
294                        s, dataview.getClass().getName());
295            }
296        }
297    }
298
299
300    private static String addHitHighlights(DataViewHits hits) {
301        StringBuilder sb = new StringBuilder(hits.getText());
302        int corr = 0;
303        for (int i = 0; i < hits.getHitCount(); i++) {
304            int[] offsets = hits.getHitOffsets(i);
305            sb.insert(offsets[0] + corr, "[");
306            corr += 1;
307            sb.insert(offsets[1] + corr, "]");
308            corr += 1;
309        }
310        return sb.toString();
311    }
312
313} // class TestUtils
Note: See TracBrowser for help on using the repository browser.