Project

General

Profile

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

    
25
package edu.ucsb.nceas.metacattest;
26

    
27
import java.io.ByteArrayInputStream;
28
import java.io.InputStream;
29
import java.sql.SQLException;
30
import java.util.*;
31

    
32
import org.dataone.service.types.v1.Identifier;
33
import org.dataone.service.types.v1.Session;
34
import org.dataone.service.types.v2.SystemMetadata;
35

    
36
import edu.ucsb.nceas.MCTestCase;
37
import edu.ucsb.nceas.metacat.AccessionNumber;
38
import edu.ucsb.nceas.metacat.AccessionNumberException;
39
import edu.ucsb.nceas.metacat.IdentifierManager;
40
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
41
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
42
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
43
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
44
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
45
import edu.ucsb.nceas.metacat.dataone.MNodeService;
46

    
47
public class IdentifierManagerTest extends D1NodeServiceTest {
48
    private String badGuid = "test:testIdThatDoesNotExist";
49
    
50
    public IdentifierManagerTest(String name) {
51
        super(name);
52
    }
53
    /**
54
     * Initialize the connection to metacat, and insert a document to be 
55
     * used for testing with a known docid.
56
     */
57
    public void setUp() {
58
        
59
        metacatConnectionNeeded = true;
60
        try {
61
            DBConnectionPool pool = DBConnectionPool.getInstance();
62
        } catch (SQLException e2) {
63
            fail(e2.getMessage());
64
        }
65
        
66
        try {
67
            super.setUp();
68
        } catch (Exception e1) {
69
            fail(e1.getMessage());
70
        }
71
    }
72
    
73
    /**
74
     * test getting a guid from the systemmetadata table
75
     */
76
    public void testGetGUID()
77
    {
78
        ph("testGetGUID");
79
        try
80
        {
81
            IdentifierManager im = IdentifierManager.getInstance();
82

    
83
            String docid = insertTestDocument();
84
            docid = docid.substring(0, docid.lastIndexOf("."));
85
            
86
            String gotGuid = im.getGUID(docid, 1);
87
            assertNotNull(gotGuid);
88
        }
89
        catch(Exception e)
90
        {
91
            fail("Unexpected exception in testGetGUID: " + e.getMessage());
92
        }
93
    }
94
    
95
    /**
96
     * test getting a list of all local ids
97
     */
98
    public void testGetAllLocalIds()
99
    {
100
        ph("testGetAllLocalIds");
101
        try
102
        {
103
            List l = IdentifierManager.getInstance().getAllLocalIds();
104
            for(int i=0; i<l.size(); i++)
105
            {
106
                System.out.println(l.get(i));
107
            }
108
            assertTrue(l.size() > 0);
109
        }
110
        catch(Exception e)
111
        {
112
            fail("Error in testGetAllLocalIds: " + e.getMessage());
113
        }
114
    }
115

    
116
    /** Test that IM instances can be created. */
117
    public void testGetInstance() {
118
        ph("testGetInstance");
119
        IdentifierManager im = IdentifierManager.getInstance();
120
        assertNotNull(im);
121
    }
122

    
123
    /** Test that known LocalId's can be looked up from GUIDs. */
124
    public void testGetLocalId() {
125
        ph("testGetLocalId");
126
        IdentifierManager im = IdentifierManager.getInstance();
127
        String docidWithRev = insertTestDocument();
128
        String docid = docidWithRev.substring(0, docidWithRev.lastIndexOf("."));
129
        String guid;
130
        String idReturned;
131
        try {
132
            guid = im.getGUID(docid, 1);
133
            idReturned = im.getLocalId(guid);
134
            assertEquals(docidWithRev, idReturned);
135
            
136
        } catch (McdbDocNotFoundException e) {
137
            fail(e.getMessage());
138
        } catch (SQLException e) {
139
            fail(e.getMessage());
140
        }
141
    }
142
    
143
    /** Test that unknown LocalId's return the proper exception. */
144
    public void testGetLocalIdNotFound() {
145
        ph("testGetLocalIdNotFound");
146
        IdentifierManager im = IdentifierManager.getInstance();
147
        String idReturned;
148
        try {
149
            idReturned = im.getLocalId(badGuid);
150
            fail("Failed: getLocalID() should have returned an document not " + 
151
                 "found exception but did not.");
152
        } catch (McdbDocNotFoundException e) {
153
            assertNotNull(e);
154
        } catch (SQLException e) {
155
            fail(e.getMessage());
156
        }
157
    }
158
    
159
    /** 
160
     * Test that an identifier is present in the system when it should
161
     *  be, and that it is not present when it shouldn't be. 
162
     */
163
    public void testIdentifierExists() {
164
        ph("testIdentifierExists");
165
        
166
        String goodGuid  = "";
167
        String accString = "";
168
        String docid     = "";
169
        int rev          = 0;
170
        
171
        try {
172
          IdentifierManager im = IdentifierManager.getInstance();
173
          accString = insertTestDocument();
174
          AccessionNumber accNumber = new AccessionNumber(accString, "NONE");
175
          docid = accNumber.getDocid();
176
          rev = new Integer(accNumber.getRev());
177
          goodGuid = im.getGUID(docid, rev);
178
          assertTrue(im.identifierExists(goodGuid));
179
          assertFalse(im.identifierExists(badGuid));
180
          
181
        } catch ( McdbDocNotFoundException dnfe ) {
182
          fail("The document " + docid + "couldn't be found. The error was: " +
183
               dnfe.getMessage());
184
          
185
        } catch ( AccessionNumberException ane ) {
186
          fail("The accession number could not be created for docid" +
187
               accString + ". The error was: " + ane.getMessage());
188
        
189
        } catch ( NumberFormatException nfe ) {
190
          fail("The revision number could not be created for docid" + 
191
               accString + ". The error was: " + nfe.getMessage());
192
          
193
           } catch ( SQLException sqle ) {
194
             fail("The accession number could not be created for docid" + 
195
                  accString + ". The error was: " + sqle.getMessage());
196

    
197
        }
198
    }
199
    
200
    /**
201
     * Test that we are able to create mappings from guids to localIds, and that
202
     * improperly formatted docids generate the proper exceptions.  This also tests
203
     * getLocalId() and getGUID()
204
     */
205
    public void testCreateMapping() {
206
       ph("testCreateMapping");
207
       try
208
       {
209
            IdentifierManager im = IdentifierManager.getInstance();
210
            String docid = "test." + new Date().getTime() + ".1";
211
            String guid = "guid:" + docid;
212
            im.createMapping(guid, docid);
213
            String guiddocid = docid.substring(0, docid.length() - 2);
214
            System.out.println("guiddocid: " + guiddocid);
215
            String guid2 = im.getGUID(guiddocid, 1);
216
            assertTrue(guid2.equals(guid));
217
            String docid2 = im.getLocalId(guid);
218
            assertTrue(docid.equals(docid2));
219
        } catch (McdbDocNotFoundException e) {
220
            e.printStackTrace();
221
            fail("createMapping failed to create proper localId from guid: " + e.getMessage());
222
        } catch (SQLException e) {
223
            fail(e.getMessage());
224
        }
225
    }
226
    
227
    
228
    /**
229
     * test the local id creation
230
     */
231
    public void testGenerateLocalId() {
232
      IdentifierManager im = IdentifierManager.getInstance();
233
      String localid = im.generateLocalId("mynewid." + new Date().getTime(), 1);
234
      System.out.println("localid: " + localid);
235
      assertTrue(localid != null);
236
    }
237
    
238
    /**
239
     * Test the method - getHeadPID for a speicified SID
240
     */
241
    public void testGetHeadPID() {
242
        
243
        try {
244
            //insert test documents with a series id
245
            Session session = getTestSession();
246
            Identifier guid = new Identifier();
247
            guid.setValue(generateDocumentId());
248
            InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8"));
249
            SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object);
250
            String sid1= "sid."+System.nanoTime();
251
            Identifier seriesId = new Identifier();
252
            seriesId.setValue(sid1);
253
            System.out.println("the first sid is "+seriesId.getValue());
254
            sysmeta.setSeriesId(seriesId);
255
            MNodeService.getInstance(request).create(session, guid, object, sysmeta);
256
            System.out.println("the first pid is "+guid.getValue());
257
            Identifier head = IdentifierManager.getInstance().getHeadPID(seriesId);
258
            System.out.println("the head 1 is "+head.getValue());
259
            assertTrue(head.getValue().equals(guid.getValue()));
260
            assertTrue(IdentifierManager.getInstance().systemMetadataSIDExists(seriesId.getValue()));
261
            assertTrue(!IdentifierManager.getInstance().systemMetadataGUIDExists(seriesId.getValue()));
262
            assertTrue(IdentifierManager.getInstance().systemMetadataGUIDExists(guid.getValue()));
263
            assertTrue(!IdentifierManager.getInstance().systemMetadataSIDExists(guid.getValue()));
264
            
265
            //do a update with the same series id
266
            Thread.sleep(1000);
267
            Identifier newPid = new Identifier();
268
            newPid.setValue(generateDocumentId()+"1");
269
            System.out.println("the second pid is "+newPid.getValue());
270
            SystemMetadata newSysMeta = createSystemMetadata(newPid, session.getSubject(), object);
271
            newSysMeta.setObsoletes(guid);
272
            newSysMeta.setSeriesId(seriesId);
273
            MNodeService.getInstance(request).update(session, guid, object, newPid, newSysMeta);
274
            // the pid should be the newPid when we try to get the sid1
275
            head = IdentifierManager.getInstance().getHeadPID(seriesId);
276
            System.out.println("the head 2 is "+head.getValue());
277
            assertTrue(head.getValue().equals(newPid.getValue()));
278
            
279
            //do another update with different series id
280
            Thread.sleep(1000);
281
            String sid2 = "sid."+System.nanoTime();
282
            Identifier seriesId2= new Identifier();
283
            seriesId2.setValue(sid2);
284
            System.out.println("the second sid is "+seriesId2.getValue());
285
            Identifier newPid2 = new Identifier();
286
            newPid2.setValue(generateDocumentId()+"2");
287
            System.out.println("the third pid is "+newPid2.getValue());
288
            SystemMetadata sysmeta3 = createSystemMetadata(newPid2, session.getSubject(), object);
289
            sysmeta3.setObsoletes(newPid);
290
            sysmeta3.setSeriesId(seriesId2);
291
            MNodeService.getInstance(request).update(session, newPid, object, newPid2, sysmeta3);
292
            
293
            // the pid should be the newPid when we try to get the sid1
294
            head =IdentifierManager.getInstance().getHeadPID(seriesId);
295
            System.out.println("the head 3 is "+head.getValue());
296
            assertTrue(head.getValue().equals(newPid.getValue()));
297
            
298
            // the pid should be the newPid2 when we try to get the sid2
299
            head = IdentifierManager.getInstance().getHeadPID(seriesId2);
300
            System.out.println("the head 4 is "+head.getValue());
301
            assertTrue(head.getValue().equals(newPid2.getValue()));
302
            
303
            // the pid should be null when we try to get a no-exist sid
304
            Identifier non_exist_sid = new Identifier();
305
            non_exist_sid.setValue("no-sid-exist-123qwe");
306
            assertTrue(IdentifierManager.getInstance().getHeadPID(non_exist_sid) == null);
307
            
308
        } catch (Exception e) {
309
            fail(e.getMessage());
310
        }
311
        
312
    }
313

    
314
    /** 
315
     * Insert a test document, returning the docid that was used. 
316
     */
317
    private String insertTestDocument() {
318
        String accessBlock = getAccessBlock("public", true, true,
319
                false, false, false);
320
        String emldoc = getTestEmlDoc("Test identifier manager", EML2_1_0, null,
321
                null, "http://fake.example.com/somedata", null,
322
                accessBlock, null, null,
323
                null, null);
324
        System.out.println("inserting doc: " + emldoc);
325
        String docid = generateDocumentId() + ".1";
326
        try {
327
            m.login(username, password);
328
            String response = insertDocumentId(docid, emldoc, true, false);
329
        } catch (MetacatAuthException e) {
330
            fail(e.getMessage());
331
        } catch (MetacatInaccessibleException e) {
332
            fail(e.getMessage());
333
        }
334
        return docid;
335
    }
336
    
337
    private void ph(String s)
338
    {
339
        System.out.println("*********************** " + s + " ****************************");
340
    }
341
}
(7-7/26)