source: FCSEndpointTester/trunk/src/main/java/eu/clarin/fcs/tester/tests/TestSearch12.java @ 7172

Last change on this file since 7172 was 7172, checked in by Oliver Schonefeld, 6 years ago
  • add Test for advanced search
  • add escapeCQL and escapeFCS methods
  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
1/**
2 * This software is copyright (c) 2013 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.fcs.tester.tests;
18
19import eu.clarin.fcs.tester.FCSTest;
20import eu.clarin.fcs.tester.FCSTestCase;
21import eu.clarin.fcs.tester.FCSTestContext;
22import eu.clarin.fcs.tester.FCSTestProfile;
23import eu.clarin.fcs.tester.FCSTestHandler;
24import eu.clarin.fcs.tester.FCSTestHandler.SurrogateDiagnostic;
25import eu.clarin.fcs.tester.FCSTestResult;
26import eu.clarin.sru.client.SRUClientConstants;
27import eu.clarin.sru.client.SRUClientException;
28import eu.clarin.sru.client.SRUDiagnostic;
29import eu.clarin.sru.client.SRUSearchRetrieveRequest;
30import eu.clarin.sru.client.SRUSimpleClient;
31import eu.clarin.sru.client.SRUSurrogateRecordData;
32import eu.clarin.sru.client.fcs.DataView;
33import eu.clarin.sru.client.fcs.LegacyClarinFCSRecordData;
34import eu.clarin.sru.client.fcs.Resource;
35import eu.clarin.sru.client.fcs.Resource.ResourceFragment;
36
37@FCSTestCase(priority=3600, profiles = {
38        FCSTestProfile.CLARIN_FCS_LEGACY
39})
40@SuppressWarnings("deprecation")
41public class TestSearch12 extends FCSTest {
42    private static final String FCS_RECORD_SCHEMA = "http://clarin.eu/fcs/1.0";
43    private static final String MIME_TYPE_KWIC =
44            "application/x-clarin-fcs-kwic+xml";
45
46
47    @Override
48    public String getName() {
49        return "SearchRetrieve";
50    }
51
52
53    @Override
54    public String getDescription() {
55        return "Search for user specified search term and " +
56                "requesting endpoint to return results in CLARIN-FCS " +
57                "record schema '" + FCS_RECORD_SCHEMA + "'";
58    }
59
60
61    @Override
62    public String getExpected() {
63        return "Expecting at least one record in CLARIN-FCS legacy record " +
64                "schema (without any surrogate diagnostics)";
65    }
66
67
68    @Override
69    public FCSTestResult perform(FCSTestContext context, SRUSimpleClient client,
70            FCSTestHandler handler) throws SRUClientException {
71        SRUSearchRetrieveRequest req = context.createSearchRetrieveRequest();
72        req.setQuery(SRUClientConstants.QUERY_TYPE_CQL,
73                escapeCQL(context.getUserSearchTerm()));
74        req.setRecordSchema(FCS_RECORD_SCHEMA);
75        req.setMaximumRecords(5);
76        client.searchRetrieve(req, handler);
77        if (handler.findDiagnostic("info:srw/diagnostic/1/66")) {
78            return makeError("Endpoint claims to not " +
79                    "support FCS record schema (" + FCS_RECORD_SCHEMA + ")");
80        } else if (handler.getRecordCount() == 0) {
81            return makeWarning("Endpoint has no results " +"" +
82                    "for search term \"" + context.getUserSearchTerm() +
83                    "\". Please supply a different search term.");
84        } else {
85            for (FCSTestHandler.Record record : handler.getRecords()) {
86                String recordSchema = record.getData().getRecordSchema();
87                if (SRUSurrogateRecordData.RECORD_SCHEMA.equals(recordSchema)) {
88                    final SurrogateDiagnostic data =
89                            (SurrogateDiagnostic) record.getData();
90                    final SRUDiagnostic d = data.getDiagnostic();
91
92                    if (data.isDiagnostic("info:srw/diagnostic/1/67")) {
93                        return makeError("Endpoint cannot render record in " +
94                                "CLARIN-FCS record format and returned " +
95                                "surrogate diagnostic \"info:srw/diagnostic/1/67\" " +
96                                "instead.");
97                    } else if (data.isDiagnostic("info:clarin/sru/diagnostic/2")) {
98                        return makeError("Endpoint sent one or more records with record " +
99                                        "schema of '" + d.getDetails() +
100                                        "' instead of '" + FCS_RECORD_SCHEMA +
101                                        "'.");
102                    } else {
103                        StringBuilder sb = new StringBuilder();
104                        sb.append("Endpoint returned unexpected surrgogate ")
105                                .append("diagnostic \"")
106                                .append(d.getURI()).append("\"");
107                        if (d.getDetails() != null) {
108                            sb.append("; details = \"")
109                                .append(d.getDetails()).append("\"");
110                        }
111                        if (d.getMessage() != null) {
112                            sb.append("; message = \"")
113                                .append(d.getMessage()).append("\"");
114                        }
115                        return makeError(sb.toString());
116                    }
117                } else if (FCS_RECORD_SCHEMA.equals(recordSchema)) {
118                    final LegacyClarinFCSRecordData data =
119                            (LegacyClarinFCSRecordData) record.getData();
120                    final Resource resource = data.getResource();
121                    boolean foundKwic = false;
122                    if (resource.hasDataViews()) {
123                        for (DataView dataView : resource.getDataViews()) {
124                            if (dataView.isMimeType(MIME_TYPE_KWIC)) {
125                                foundKwic = true;
126                            }
127                        }
128                    }
129
130                    if (resource.hasResourceFragments()) {
131                        for (ResourceFragment fragment : resource.getResourceFragments()) {
132                            for (DataView dataView : fragment.getDataViews()) {
133                                if (dataView.isMimeType(MIME_TYPE_KWIC)) {
134                                    foundKwic = true;
135                                }
136                            }
137                        }
138                    }
139                    if (!foundKwic) {
140                        return makeError("Endpoint did not provide mandatory KWIC dataview in results");
141                    }
142
143                } else {
144                    return makeError("Endpoint does not supply results in FCS record schema (" + FCS_RECORD_SCHEMA + "\"");
145                }
146            }
147            if (handler.getRecordCount() > req.getMaximumRecords()) {
148                return makeError("Endpoint did not honor upper requested limit for " +
149                                "\"maximumRecords\" parameter (up to " +
150                                req.getMaximumRecords() +
151                                " records where requested and endpoint delivered " +
152                                handler.getRecordCount() + " results)");
153            }
154            return makeSuccess();
155        }
156    }
157
158}
Note: See TracBrowser for help on using the repository browser.