Project

General

Profile

« Previous | Next » 

Revision 7692

Added by Jing Tao over 11 years ago

Rewrite some methods, so the query result can be processed many times.

View differences:

test/edu/ucsb/nceas/metacat/dataone/SolrQueryAccessFilterTest.java
60 60
    private static final String EMLFILE = "test/restfiles/knb-lter-gce.109.6.xml";
61 61
    private static final String INTRUSTCERTFILE = "test/test-credentials/test-user.pem";
62 62
    private static final String IDXPATH = "//response/result/doc/str[@name='id']/text()";
63
    private static final String TITLEPATH = "//response/result/doc/str[@name='title']/text()";
63 64
    
64 65
    /**
65 66
     * Build the test suite
......
114 115
        createObject(session, id, object, sysmeta);
115 116
        Thread.sleep(10000);
116 117
        Session querySession = getSession(Constants.SUBJECT_PUBLIC, null);
117
        String resultId = query(querySession, id);
118
        InputStream input = query(querySession, id);
119
        Document doc = generateDoc(input);
120
        String resultId  = extractElementValue(doc, IDXPATH);
118 121
        assertTrue("In the testPublicReadable method, the query result should have the id "+id.getValue()+ " rather than "+resultId, resultId.equals(id.getValue()));
119 122
        Session querySession2 = getSession(QUERYUSER, null);
120
        String resultId2 = query(querySession2, id);
123
        input = query(querySession2, id);
124
        doc = generateDoc(input);
125
        String resultId2 = extractElementValue(doc, IDXPATH);
121 126
        assertTrue("In the testPublicReadable method, the query result should have the id "+id.getValue()+ " rather than "+resultId2, resultId2.equals(id.getValue()));
122 127
        archive(session, id);
123
        String resultId3 = query(querySession2, id);
128
        
129
        input = query(querySession2, id);
130
        doc = generateDoc(input);
131
        String resultId3 = extractElementValue(doc, IDXPATH);
124 132
        assertTrue("In the testPublicReadable method, the query result should be null since the document was archived. ", resultId3 == null);
125 133
    }
126 134
    
......
139 147
        
140 148
        Thread.sleep(10000);
141 149
        Session querySession = getSession(Constants.SUBJECT_PUBLIC, null);
142
        String resultId = query(querySession, id);
150
        InputStream input = query(querySession, id);
151
        Document doc = generateDoc(input);
152
        String resultId = extractElementValue(doc, IDXPATH);
143 153
        assertTrue("In the testOnlyUserReadable method, the query result id should be null for the public rather than "+resultId, resultId == null);
144 154
        Session querySession2 = getSession(QUERYUSER, null);
145
        resultId = query(querySession2, id);
155
        input = query(querySession2, id);
156
        doc = generateDoc(input);
157
        resultId = extractElementValue(doc, IDXPATH);
146 158
        assertTrue("In the testOnlyUserReadable method, the query result for the user "+QUERYUSER+" should have the id "+id.getValue()+" rather than "+resultId, resultId.equals(id.getValue()));
147 159
        archive(session, id);
148 160
    }
......
160 172
        createObject(session, id, object, sysmeta);
161 173
        Thread.sleep(10000);
162 174
        Session querySession = getSession(Constants.SUBJECT_PUBLIC, null);
163
        String resultId = query(querySession, id);
175
        InputStream input = query(querySession, id);
176
        Document doc = generateDoc(input);
177
        String resultId = extractElementValue(doc, IDXPATH);
164 178
        assertTrue("In the testGroupReadable method, the query result id should be null for the public ", resultId == null);
165 179
        Session querySession2 = getSession(QUERYUSER, null);
166
        resultId = query(querySession2, id);
180
        input = query(querySession2, id);
181
        doc = generateDoc(input);
182
        resultId = extractElementValue(doc, IDXPATH);
167 183
        assertTrue("In the testGroupReadable method, the query result for the user "+QUERYUSER+" which doesn't belong to the group should be null ", resultId == null);
168 184
        String[]groups = {GROUP1};
169 185
        Session querySession3 = getSession(QUERYUSER, groups);
170
        resultId = query(querySession3, id);
186
        input = query(querySession3, id);
187
        doc = generateDoc(input);
188
        resultId = extractElementValue(doc, IDXPATH);
171 189
        assertTrue("In the testGroupReadable method, the query result for the user "+QUERYUSER+" which belong to the group should have the id "+id.getValue(), resultId.equals(id.getValue()));
172 190
        archive(session, id);
173 191
    }
......
186 204
        createObject(session, id, object, sysmeta);
187 205
        Thread.sleep(10000);
188 206
        Session querySession = getSession(Constants.SUBJECT_PUBLIC, null);
189
        String resultId = query(querySession, id);
207
        InputStream input = query(querySession, id);
208
        Document doc = generateDoc(input);
209
        String resultId = extractElementValue(doc, IDXPATH);
190 210
        assertTrue("In the testOnlyRightHolderReadable method, the query result id should be null for the public ", resultId == null);
191 211
        Session querySession2 = getSession(QUERYUSER, null);
192
        resultId = query(querySession2, id);
212
        input = query(querySession2, id);
213
        doc = generateDoc(input);
214
        resultId = extractElementValue(doc, IDXPATH);
193 215
        assertTrue("In the testOnlyRightHolderReadable method, the query result for the user "+QUERYUSER+" which doesn't belong to the group should be null.", resultId == null);
194 216
        String[]groups = {GROUP1};
195 217
        Session querySession3 = getSession(QUERYUSER, groups);
196
        resultId = query(querySession3, id);
218
        input = query(querySession3, id);
219
        doc = generateDoc(input);
220
        resultId = extractElementValue(doc, IDXPATH);
197 221
        assertTrue("In the testOnlyRightHolderReadable method, the query result for the user "+QUERYUSER+" which belong to the group should be null.", resultId == null);
198 222
        Session querySession4 = getSession(CREATEUSER, groups);
199
        resultId = query(querySession4, id);
223
        input = query(querySession4, id);
224
        doc = generateDoc(input);
225
        resultId = extractElementValue(doc, IDXPATH);
200 226
        assertTrue("In the testOnlyRightHolderReadable method, the query result for the creator "+CREATEUSER+" should be "+id.getValue(), id.getValue().equals(resultId));
201 227
        archive(session, id);
202 228
    }
......
217 243
        
218 244
        //use faking session, the user can query the document
219 245
        Session querySession = getSession(USERWITHCERT, null);
220
        String resultId = query(querySession, id);
246
        
247
        InputStream input = query(querySession, id);
248
        Document doc = generateDoc(input);
249
        String resultId = extractElementValue(doc, IDXPATH);
221 250
        //assertTrue("In the testDistrustCertificate method, the query result id should be "+id.getValue(), id.getValue().equals(resultId));
222 251
        
223 252
        //Use the libclient without the session, the user shouldn't query the document since its certificate is distrusted and it will be considered as the public.
......
226 255
        String baseURL = node.getBaseURL();
227 256
        //System.out.println("================The base url is "+baseURL);
228 257
        MNode mnNode = D1Client.getMN(baseURL);
229
        InputStream input = mnNode.query(SOLR, generateQuery(id.getValue()));
230
        String resultId2 = extractId(input);
258
        input = mnNode.query(SOLR, generateQuery(id.getValue()));
259
        doc = generateDoc(input);
260
        String resultId2 = extractElementValue(doc, IDXPATH);
231 261
        assertTrue("In the testDistrustCertificate method, the query result id should be null", resultId2==null);
232 262
        archive(session, id);
233 263
        
......
311 341
    /*
312 342
     * Query the server to find the doc which matches the specified id
313 343
     */
314
    private String query(Session session, Identifier id) throws Exception{
344
    private InputStream query(Session session, Identifier id) throws Exception{
315 345
        String query = generateQuery(id.getValue());
316 346
        MNodeService service = MNodeService.getInstance(request);
317 347
        service.setSession(session);
318 348
        InputStream input = service.query(SOLR, query);
319
        return extractId(input);
349
        return input;
320 350
    }
321 351
    
322 352
    /*
323
     * Extract the return id from the query result input stream
353
     * 
324 354
     */
325
    private String extractId(InputStream input ) throws Exception {
326
        String id = null;
355
    private Document generateDoc(InputStream input) throws Exception {
327 356
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
328 357
        DocumentBuilder builder = factory.newDocumentBuilder();
329 358
        Document doc = builder.parse(new InputSource(input));
330
       
359
        return doc;
360
    }
361
    
362
    /*
363
     * Extract the return id from the query result input stream
364
     */
365
    private String extractElementValue(Document doc, String path) throws Exception {
366
        String id = null;
331 367
        XPathFactory xPathfactory = XPathFactory.newInstance();
332 368
        XPath xpath = xPathfactory.newXPath();
333
        XPathExpression expr = xpath.compile(IDXPATH);
369
        XPathExpression expr = xpath.compile(path);
334 370
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
335 371
        System.out.println("================ result is "+result);
336 372
        if(result != null) {
......
346 382
            
347 383
        }
348 384
       
349
        System.out.println("the id is ====== "+id);
385
        System.out.println("the value of the element " + path+ " is ====== "+id);
350 386
        return id;
351 387

  
352 388
    

Also available in: Unified diff