Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2003 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the MetaCatURL class by JUnit
6
 *
7
 *   '$Author: jones $'
8
 *     '$Date: 2003-08-12 12:35:53 -0700 (Tue, 12 Aug 2003) $'
9
 * '$Revision: 1795 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest.client;
27

    
28
import edu.ucsb.nceas.metacat.client.*;
29
import edu.ucsb.nceas.utilities.IOUtil;
30

    
31
import java.io.FileReader;
32
import java.io.InputStreamReader;
33
import java.io.IOException;
34
import java.io.Reader;
35
import java.io.StringReader;
36
import java.io.StringWriter;
37
import java.text.DateFormat;
38
import java.util.Calendar;
39
import java.util.GregorianCalendar;
40
import java.util.Date;
41
import java.util.SimpleTimeZone;
42
import java.util.TimeZone;
43

    
44
import junit.framework.Test;
45
import junit.framework.TestCase;
46
import junit.framework.TestResult;
47
import junit.framework.TestSuite;
48

    
49
/**
50
 * A JUnit test for testing Step class processing
51
 */
52
public class MetacatClientTest extends TestCase
53
{
54
    private String metacatUrl = 
55
        "http://dev.nceas.ucsb.edu:8091/tao/servlet/metacat";
56
        //"http://knb.ecoinformatics.org/knb/servlet/metacat";
57
    private String username = "@mcuser@";
58
    private String password = "@mcpassword@";
59
    private String failpass = "uidfnkj43987yfdn";
60
    private String prefix = "test";
61
    private String newdocid = null;
62
    private String testfile = "test/jones.204.22.xml";
63
    private String queryFile = "test/query.xml";
64
    private String testdocument = "";
65
    
66
    private Metacat m;
67

    
68
    /**
69
     * Constructor to build the test
70
     *
71
     * @param name the name of the test method
72
     */
73
    public MetacatClientTest(String name)
74
    {
75
        super(name);
76
        newdocid = generateDocid();
77
    }
78

    
79
    /**
80
     * Establish a testing framework by initializing appropriate objects
81
     */
82
    public void setUp()
83
    {
84
        try {
85
            FileReader fr = new FileReader(testfile);
86
            testdocument = IOUtil.getAsString(fr, true);
87
        } catch (IOException ioe) {
88
            fail("Can't read test data to run the test: " + testfile);
89
        }
90

    
91
        try {
92
            m = MetacatFactory.createMetacatConnection(metacatUrl);
93
        } catch (MetacatInaccessibleException mie) {
94
            fail("Metacat connection failed." + mie.getMessage());
95
        }
96
    }
97
  
98
    /**
99
     * Release any objects after tests are complete
100
     */
101
    public void tearDown()
102
    {
103
    }
104
  
105
    /**
106
     * Create a suite of tests to be run together
107
     */
108
    public static Test suite()
109
    {
110
        TestSuite suite = new TestSuite();
111
        suite.addTest(new MetacatClientTest("initialize"));
112
        suite.addTest(new MetacatClientTest("invalidLogin"));
113
        suite.addTest(new MetacatClientTest("login"));
114
        suite.addTest(new MetacatClientTest("insert"));
115
        suite.addTest(new MetacatClientTest("read"));
116
        suite.addTest(new MetacatClientTest("query"));
117
        suite.addTest(new MetacatClientTest("update"));
118
        suite.addTest(new MetacatClientTest("delete"));
119
        return suite;
120
    }
121
  
122
    /**
123
     * Run an initial test that always passes to check that the test
124
     * harness is working.
125
     */
126
    public void initialize()
127
    {
128
        assertTrue(1 == 1);
129
    }
130
  
131
    /**
132
     * Test the login() function with valid credentials
133
     */
134
    public void login()
135
    {
136
        // Try a valid login
137
        try {
138
            m.login(username, password);
139
        } catch (MetacatAuthException mae) {
140
            fail("Authorization failed:\n" + mae.getMessage());
141
        } catch (MetacatInaccessibleException mie) {
142
            fail("Metacat Inaccessible:\n" + mie.getMessage());
143
        }
144
    }
145

    
146
    /**
147
     * Test the login() function with INVALID credentials
148
     */
149
    public void invalidLogin()
150
    {
151
        // Try an invalid login
152
        try {
153
            m.login(username, failpass);
154
            fail("Authorization should have failed.");
155
        } catch (MetacatAuthException mae) {
156
            assertTrue(1 == 1);
157
        } catch (MetacatInaccessibleException mie) {
158
            fail("Metacat Inaccessible:\n" + mie.getMessage());
159
        }
160
    }
161

    
162
    /**
163
     * Test the read() function with a known document
164
     */
165
    public void read()
166
    {
167
        try {
168
            m.login(username, password);
169
            Reader r = m.read(newdocid+".1");
170
            String doc = IOUtil.getAsString(r, true);
171
            System.err.println(doc);
172
            assertTrue(doc.equals(testdocument));
173
        } catch (MetacatAuthException mae) {
174
            fail("Authorization failed:\n" + mae.getMessage());
175
        } catch (MetacatInaccessibleException mie) {
176
            fail("Metacat Inaccessible:\n" + mie.getMessage());
177
        } catch (Exception e) {
178
            fail("General exception:\n" + e.getMessage());
179
        }
180
    }
181
    
182
    /**
183
     * Test the query() function with a known document
184
     */
185
    public void query()
186
    {
187
        try {
188
            FileReader fr = new FileReader(queryFile);
189
            Reader r = m.query(fr);
190
            String result = IOUtil.getAsString(r, true);
191
            assertTrue(result.indexOf(newdocid+".1")!=-1);
192
        } catch (MetacatInaccessibleException mie) {
193
            fail("Metacat Inaccessible:\n" + mie.getMessage());
194
        } catch (Exception e) {
195
            fail("General exception:\n" + e.getMessage());
196
        }
197
    }
198

    
199
    /**
200
     * Test the insert() function with a known document
201
     */
202
    public void insert()
203
    {
204
        try {
205
            String identifier = newdocid + ".1";
206
            m.login(username, password);
207
            String response = m.insert(identifier, 
208
                    new StringReader(testdocument), null);
209
            assertTrue(response.indexOf("<success>") != -1);
210
            assertTrue(response.indexOf(identifier) != -1);
211
            System.err.println(response);
212

    
213
        } catch (MetacatAuthException mae) {
214
            fail("Authorization failed:\n" + mae.getMessage());
215
        } catch (MetacatInaccessibleException mie) {
216
            fail("Metacat Inaccessible:\n" + mie.getMessage());
217
        } catch (InsufficientKarmaException ike) {
218
            fail("Insufficient karma:\n" + ike.getMessage());
219
        } catch (MetacatException me) {
220
            fail("Metacat Error:\n" + me.getMessage());
221
        } catch (Exception e) {
222
            fail("General exception:\n" + e.getMessage());
223
        }
224
    }
225

    
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
    /**
279
     * Create a hopefully unique docid for testing insert and update. Does
280
     * not include the 'revision' part of the id.
281
     *
282
     * @return a String docid based on the current date and time
283
     */
284
    private String generateDocid()
285
    {
286
        StringBuffer docid = new StringBuffer(prefix);
287
        docid.append(".");
288
        
289
        // Create a calendar to get the date formatted properly
290
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
291
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
292
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
293
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
294
        Calendar calendar = new GregorianCalendar(pdt);
295
        Date trialTime = new Date();
296
        calendar.setTime(trialTime);
297
        docid.append(calendar.get(Calendar.YEAR));
298
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
299
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
300
        docid.append(calendar.get(Calendar.MINUTE));
301
        docid.append(calendar.get(Calendar.SECOND));
302

    
303
        return docid.toString();
304
    }
305
}
    (1-1/1)