Project

General

Profile

« Previous | Next » 

Revision 1789

Added by Matt Jones over 21 years ago

Implemented the "insert()" method and wrote a test to test it. The new test
depends on the property "newdocid" be set to something unique in the build.xml
file so that the insert will work properly. Probably need to fix this to be
some kind of autoincrement counter or random number so that it can be run
several times in succession. Right now, if you run the test without resetting
the newdocid property (and touch the test file), it will fail on the second
and subsequent passes.

View differences:

test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
32 32
import java.io.InputStreamReader;
33 33
import java.io.IOException;
34 34
import java.io.Reader;
35
import java.io.StringReader;
35 36
import java.io.StringWriter;
36 37

  
37 38
import junit.framework.Test;
......
50 51
    private String password = "@mcpassword@";
51 52
    private String failpass = "uidfnkj43987yfdn";
52 53
    private String docid = "jones.204.22";
54
    private String newdocid = "@newdocid@";
53 55
    private String testfile = "test/jones.204.22.xml";
54 56
    private String queryFile = "test/query.xml";
55 57
    //private String docid = "Gramling.61.26";
......
104 106
        suite.addTest(new MetacatClientTest("initialize"));
105 107
        suite.addTest(new MetacatClientTest("invalidLogin"));
106 108
        suite.addTest(new MetacatClientTest("login"));
109
        suite.addTest(new MetacatClientTest("insert"));
107 110
        suite.addTest(new MetacatClientTest("read"));
108 111
        suite.addTest(new MetacatClientTest("query"));
109 112
        return suite;
......
186 189
            fail("General exception:\n" + e.getMessage());
187 190
        }
188 191
    }
192

  
193
    /**
194
     * Test the insert() function with a known document
195
     */
196
    public void insert()
197
    {
198
        try {
199
            m.login(username, password);
200
            String response = m.insert(newdocid, 
201
                    new StringReader(testdocument), null);
202
            assertTrue(response.indexOf("<success>") != -1);
203
            assertTrue(response.indexOf(newdocid) != -1);
204

  
205
        } catch (MetacatAuthException mae) {
206
            fail("Authorization failed:\n" + mae.getMessage());
207
        } catch (MetacatInaccessibleException mie) {
208
            fail("Metacat Inaccessible:\n" + mie.getMessage());
209
        } catch (InsufficientKarmaException ike) {
210
            fail("Insufficient karma:\n" + ike.getMessage());
211
        } catch (MetacatException me) {
212
            fail("Metacat Error:\n" + me.getMessage());
213
        } catch (Exception e) {
214
            fail("General exception:\n" + e.getMessage());
215
        }
216
    }
189 217
}
src/edu/ucsb/nceas/metacat/client/MetacatClient.java
185 185
     * @param xmlDocument a Reader for accessing the XML document to be inserted
186 186
     * @param schema a Reader for accessing the DTD or XML Schema for 
187 187
     *               the document
188
     * @return the metacat response message
188 189
     * @throws InsufficientKarmaException when the user has insufficent rights 
189 190
     *                                    for the operation
191
     * @throws MetacatInaccessibleException when the metacat server can not be
192
     *                                    reached or does not respond
193
     * @throws MetacatException when the metacat server generates another error
194
     * @throws IOException when there is an error reading the xml document
190 195
     */
191
    public void insert(String docid, Reader xmlDocument, Reader schema)
192
        throws InsufficientKarmaException
196
    public String insert(String docid, Reader xmlDocument, Reader schema)
197
        throws InsufficientKarmaException, MetacatException, IOException,
198
        MetacatInaccessibleException
193 199
    {
200
        Reader reader = null;
201
        String doctext = null;
202
        String schematext = null;
203
        try {
204
          doctext = IOUtil.getAsString(xmlDocument, true);
205
          if (schema != null) {
206
              schematext = IOUtil.getAsString(schema, true);
207
          }
208
        } catch (IOException ioE) {
209
          throw ioE;
210
        }
211

  
212
        //set up properties
213
        Properties prop = new Properties();
214
        prop.put("action", "insert");
215
        prop.put("docid", docid);
216
        prop.put("doctext", doctext);
217
        if (schematext != null) {
218
            prop.put("dtdtext", schematext);
219
        }
220
        
221
        String response = null;
222
        try {
223
            response = sendDataForString(prop);
224
        } catch (Exception e) {
225
            throw new MetacatInaccessibleException(e.getMessage());
226
        }
227

  
228
        // Check for an error condition
229
        if (response.indexOf("<error>") != -1) {
230
            if (response.indexOf("does not have permission") != -1) {
231
                throw new InsufficientKarmaException(response);
232
            } else {
233
                throw new MetacatException(response);
234
            }
235
        }
236

  
237
        return response;
194 238
    }
195 239

  
196 240
    /**
src/edu/ucsb/nceas/metacat/client/Metacat.java
80 80
     * @param xmlDocument a Reader for accessing the XML document to be inserted
81 81
     * @param schema a Reader for accessing the DTD or XML Schema for 
82 82
     *               the document
83
     * @return the metacat response message
83 84
     * @throws InsufficientKarmaException when the user has insufficent rights 
84 85
     *                                    for the operation
86
     * @throws MetacatInaccessibleException when the metacat server can not be
87
     *                                    reached or does not respond
88
     * @throws MetacatException when the metacat server generates another error
89
     * @throws IOException when there is an error reading the xml document
85 90
     */
86
    public void insert(String docid, Reader xmlDocument, Reader schema)
87
        throws InsufficientKarmaException;
91
    public String insert(String docid, Reader xmlDocument, Reader schema)
92
        throws InsufficientKarmaException, MetacatException, IOException,
93
        MetacatInaccessibleException;
88 94

  
89 95
    /**
90 96
     * Update an XML document in the repository.
build.xml
195 195
      <property name="srcdir" value="./src" />
196 196
      <property name="testdir" value="./test" />
197 197
      <property name="testtorun" value="MetacatClientTest" />
198
      <property name="newdocid" value="foombj.102.1"/>
199
      <filter token="newdocid" value="${newdocid}"/>
198 200
      <property name="junittestsdir" value="./test/edu/ucsb/nceas/metacattest" />
199 201
      <property name="junitnettestsdir" value="./test/edu/ucsb/nceas/metacatnettest" />
200 202
      <property name="build.dir" value="./build"/>

Also available in: Unified diff