Project

General

Profile

« Previous | Next » 

Revision 1795

Added by Matt Jones over 21 years ago

Implemented the update and delete methods in the interface, along with tests.

View differences:

test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
52 52
public class MetacatClientTest extends TestCase
53 53
{
54 54
    private String metacatUrl = 
55
        "http://dev.nceas.ucsb.edu/tao/servlet/metacat";
56
        //"http://dev.nceas.ucsb.edu/tao/servlet/metacat";
55
        "http://dev.nceas.ucsb.edu:8091/tao/servlet/metacat";
56
        //"http://knb.ecoinformatics.org/knb/servlet/metacat";
57 57
    private String username = "@mcuser@";
58 58
    private String password = "@mcpassword@";
59 59
    private String failpass = "uidfnkj43987yfdn";
60
    private String docid = "jones.204.22";
61 60
    private String prefix = "test";
62 61
    private String newdocid = null;
63 62
    private String testfile = "test/jones.204.22.xml";
64 63
    private String queryFile = "test/query.xml";
65
    //private String docid = "Gramling.61.26";
66 64
    private String testdocument = "";
67 65
    
68 66
    private Metacat m;
......
116 114
        suite.addTest(new MetacatClientTest("insert"));
117 115
        suite.addTest(new MetacatClientTest("read"));
118 116
        suite.addTest(new MetacatClientTest("query"));
117
        suite.addTest(new MetacatClientTest("update"));
118
        suite.addTest(new MetacatClientTest("delete"));
119 119
        return suite;
120 120
    }
121 121
  
......
206 206
            m.login(username, password);
207 207
            String response = m.insert(identifier, 
208 208
                    new StringReader(testdocument), null);
209
            System.err.println(
210
                DateFormat.getDateTimeInstance().format(new Date()));
211
            //Thread.sleep(20000);
212
            System.err.println(
213
                DateFormat.getDateTimeInstance().format(new Date()));
214 209
            assertTrue(response.indexOf("<success>") != -1);
215 210
            assertTrue(response.indexOf(identifier) != -1);
216 211
            System.err.println(response);
......
229 224
    }
230 225

  
231 226
    /**
227
     * Test the update() function with a known document
228
     */
229
    public void update()
230
    {
231
        try {
232
            String identifier = newdocid + ".2";
233
            m.login(username, password);
234
            String response = m.update(identifier, 
235
                    new StringReader(testdocument), null);
236
            assertTrue(response.indexOf("<success>") != -1);
237
            assertTrue(response.indexOf(identifier) != -1);
238
            System.err.println(response);
239

  
240
        } catch (MetacatAuthException mae) {
241
            fail("Authorization failed:\n" + mae.getMessage());
242
        } catch (MetacatInaccessibleException mie) {
243
            fail("Metacat Inaccessible:\n" + mie.getMessage());
244
        } catch (InsufficientKarmaException ike) {
245
            fail("Insufficient karma:\n" + ike.getMessage());
246
        } catch (MetacatException me) {
247
            fail("Metacat Error:\n" + me.getMessage());
248
        } catch (Exception e) {
249
            fail("General exception:\n" + e.getMessage());
250
        }
251
    }
252

  
253
    /**
254
     * Test the delete() function with a known document
255
     */
256
    public void delete()
257
    {
258
        try {
259
            String identifier = newdocid + ".2";
260
            m.login(username, password);
261
            String response = m.delete(identifier);
262
            assertTrue(response.indexOf("<success>") != -1);
263
            System.err.println(response);
264

  
265
        } catch (MetacatAuthException mae) {
266
            fail("Authorization failed:\n" + mae.getMessage());
267
        } catch (MetacatInaccessibleException mie) {
268
            fail("Metacat Inaccessible:\n" + mie.getMessage());
269
        } catch (InsufficientKarmaException ike) {
270
            fail("Insufficient karma:\n" + ike.getMessage());
271
        } catch (MetacatException me) {
272
            fail("Metacat Error:\n" + me.getMessage());
273
        } catch (Exception e) {
274
            fail("General exception:\n" + e.getMessage());
275
        }
276
    }
277

  
278
    /**
232 279
     * Create a hopefully unique docid for testing insert and update. Does
233 280
     * not include the 'revision' part of the id.
234 281
     *
src/edu/ucsb/nceas/metacat/client/MetacatClient.java
244 244
     * @param xmlDocument a Reader for accessing the XML text to be updated
245 245
     * @param schema a Reader for accessing the DTD or XML Schema for 
246 246
     *               the document
247
     * @return the metacat response message
247 248
     * @throws InsufficientKarmaException when the user has insufficent rights 
248 249
     *                                    for the operation
250
     * @throws MetacatInaccessibleException when the metacat server can not be
251
     *                                    reached or does not respond
252
     * @throws MetacatException when the metacat server generates another error
253
     * @throws IOException when there is an error reading the xml document
249 254
     */
250
    public void update(String docid, Reader xmlDocument, Reader schema)
251
        throws InsufficientKarmaException
255
    public String update(String docid, Reader xmlDocument, Reader schema)
256
        throws InsufficientKarmaException, MetacatException, IOException,
257
        MetacatInaccessibleException
252 258
    {
259
        Reader reader = null;
260
        String doctext = null;
261
        String schematext = null;
262
        try {
263
          doctext = IOUtil.getAsString(xmlDocument, true);
264
          if (schema != null) {
265
              schematext = IOUtil.getAsString(schema, true);
266
          }
267
        } catch (IOException ioE) {
268
          throw ioE;
269
        }
270

  
271
        //set up properties
272
        Properties prop = new Properties();
273
        prop.put("action", "update");
274
        prop.put("docid", docid);
275
        prop.put("doctext", doctext);
276
        if (schematext != null) {
277
            prop.put("dtdtext", schematext);
278
        }
279
        
280
        String response = null;
281
        try {
282
            response = sendDataForString(prop);
283
        } catch (Exception e) {
284
            throw new MetacatInaccessibleException(e.getMessage());
285
        }
286

  
287
        // Check for an error condition
288
        if (response.indexOf("<error>") != -1) {
289
            if (response.indexOf("does not have permission") != -1) {
290
                throw new InsufficientKarmaException(response);
291
            } else {
292
                throw new MetacatException(response);
293
            }
294
        }
295

  
296
        return response;
253 297
    }
254 298

  
255 299
    /**
256 300
     * Delete an XML document in the repository.
257 301
     *
258 302
     * @param docid the docid to delete
303
     * @return the metacat response message
259 304
     * @throws InsufficientKarmaException when the user has insufficent rights 
260 305
     *                                    for the operation
306
     * @throws MetacatInaccessibleException when the metacat server can not be
307
     *                                    reached or does not respond
308
     * @throws MetacatException when the metacat server generates another error
261 309
     */
262
    public void delete(String docid)
263
        throws InsufficientKarmaException
310
    public String delete(String docid)
311
        throws InsufficientKarmaException, MetacatException,
312
        MetacatInaccessibleException
264 313
    {
314
        //set up properties
315
        Properties prop = new Properties();
316
        prop.put("action", "delete");
317
        prop.put("docid", docid);
318
        
319
        String response = null;
320
        try {
321
            response = sendDataForString(prop);
322
        } catch (Exception e) {
323
            throw new MetacatInaccessibleException(e.getMessage());
324
        }
325

  
326
        // Check for an error condition
327
        if (response.indexOf("<error>") != -1) {
328
            if (response.indexOf("does not have permission") != -1) {
329
                throw new InsufficientKarmaException(response);
330
            } else {
331
                throw new MetacatException(response);
332
            }
333
        }
334

  
335
        return response;
265 336
    }
266 337

  
267 338
    /**
src/edu/ucsb/nceas/metacat/client/Metacat.java
99 99
     * @param xmlDocument a Reader for accessing the XML text to be updated
100 100
     * @param schema a Reader for accessing the DTD or XML Schema for 
101 101
     *               the document
102
     * @return the metacat response message
102 103
     * @throws InsufficientKarmaException when the user has insufficent rights 
103 104
     *                                    for the operation
105
     * @throws MetacatInaccessibleException when the metacat server can not be
106
     *                                    reached or does not respond
107
     * @throws MetacatException when the metacat server generates another error
108
     * @throws IOException when there is an error reading the xml document
104 109
     */
105
    public void update(String docid, Reader xmlDocument, Reader schema)
106
        throws InsufficientKarmaException;
110
    public String update(String docid, Reader xmlDocument, Reader schema)
111
        throws InsufficientKarmaException, MetacatException, IOException,
112
        MetacatInaccessibleException;
107 113

  
108 114
    /**
109 115
     * Delete an XML document in the repository.
110 116
     *
111 117
     * @param docid the docid to delete
118
     * @return the metacat response message
112 119
     * @throws InsufficientKarmaException when the user has insufficent rights 
113 120
     *                                    for the operation
121
     * @throws MetacatInaccessibleException when the metacat server can not be
122
     *                                    reached or does not respond
123
     * @throws MetacatException when the metacat server generates another error
114 124
     */
115
    public void delete(String docid)
116
        throws InsufficientKarmaException;
125
    public String delete(String docid)
126
        throws InsufficientKarmaException, MetacatException,
127
        MetacatInaccessibleException;
117 128

  
118 129
    /**
119 130
     * When the MetacatFactory creates an instance it needs to set the

Also available in: Unified diff