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.sql.SQLException;
28
import java.util.*;
29

    
30
import edu.ucsb.nceas.MCTestCase;
31
import edu.ucsb.nceas.metacat.AccessionNumber;
32
import edu.ucsb.nceas.metacat.AccessionNumberException;
33
import edu.ucsb.nceas.metacat.IdentifierManager;
34
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
35
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
36
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
37
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
38

    
39
public class IdentifierManagerTest extends MCTestCase {
40
    private String badGuid = "test:testIdThatDoesNotExist";
41
    
42
    /**
43
     * Initialize the connection to metacat, and insert a document to be 
44
     * used for testing with a known docid.
45
     */
46
    public void setUp() {
47
        
48
        metacatConnectionNeeded = true;
49
        try {
50
            DBConnectionPool pool = DBConnectionPool.getInstance();
51
        } catch (SQLException e2) {
52
            fail(e2.getMessage());
53
        }
54
        
55
        try {
56
            super.setUp();
57
        } catch (Exception e1) {
58
            fail(e1.getMessage());
59
        }
60
    }
61
    
62
    /**
63
     * test getting a guid from the systemmetadata table
64
     */
65
    public void testGetGUID()
66
    {
67
        ph("testGetGUID");
68
        try
69
        {
70
            IdentifierManager im = IdentifierManager.getInstance();
71

    
72
            String docid = insertTestDocument();
73
            docid = docid.substring(0, docid.lastIndexOf("."));
74
            
75
            String gotGuid = im.getGUID(docid, 1);
76
            assertNotNull(gotGuid);
77
        }
78
        catch(Exception e)
79
        {
80
            fail("Unexpected exception in testGetGUID: " + e.getMessage());
81
        }
82
    }
83
    
84
    /**
85
     * test getting a list of all local ids
86
     */
87
    public void testGetAllLocalIds()
88
    {
89
        ph("testGetAllLocalIds");
90
        try
91
        {
92
            List l = IdentifierManager.getInstance().getAllLocalIds();
93
            for(int i=0; i<l.size(); i++)
94
            {
95
                System.out.println(l.get(i));
96
            }
97
            assertTrue(l.size() > 0);
98
        }
99
        catch(Exception e)
100
        {
101
            fail("Error in testGetAllLocalIds: " + e.getMessage());
102
        }
103
    }
104

    
105
    /** Test that IM instances can be created. */
106
    public void testGetInstance() {
107
        ph("testGetInstance");
108
        IdentifierManager im = IdentifierManager.getInstance();
109
        assertNotNull(im);
110
    }
111

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

    
186
        }
187
    }
188
    
189
    /**
190
     * Test that we are able to create mappings from guids to localIds, and that
191
     * improperly formatted docids generate the proper exceptions.  This also tests
192
     * getLocalId() and getGUID()
193
     */
194
    public void testCreateMapping() {
195
       ph("testCreateMapping");
196
       try
197
       {
198
            IdentifierManager im = IdentifierManager.getInstance();
199
            String docid = "test." + new Date().getTime() + ".1";
200
            String guid = "guid:" + docid;
201
            im.createMapping(guid, docid);
202
            String guiddocid = docid.substring(0, docid.length() - 2);
203
            System.out.println("guiddocid: " + guiddocid);
204
            String guid2 = im.getGUID(guiddocid, 1);
205
            assertTrue(guid2.equals(guid));
206
            String docid2 = im.getLocalId(guid);
207
            assertTrue(docid.equals(docid2));
208
        } catch (McdbDocNotFoundException e) {
209
            e.printStackTrace();
210
            fail("createMapping failed to create proper localId from guid: " + e.getMessage());
211
        } catch (SQLException e) {
212
            fail(e.getMessage());
213
        }
214
    }
215
    
216
    
217
    /**
218
     * test the local id creation
219
     */
220
    public void testGenerateLocalId() {
221
      IdentifierManager im = IdentifierManager.getInstance();
222
      String localid = im.generateLocalId("mynewid." + new Date().getTime(), 1);
223
      System.out.println("localid: " + localid);
224
      assertTrue(localid != null);
225
    }
226

    
227
    /** 
228
     * Insert a test document, returning the docid that was used. 
229
     */
230
    private String insertTestDocument() {
231
        String accessBlock = getAccessBlock("public", true, true,
232
                false, false, false);
233
        String emldoc = getTestEmlDoc("Test identifier manager", EML2_1_0, null,
234
                null, "http://fake.example.com/somedata", null,
235
                accessBlock, null, null,
236
                null, null);
237
        System.out.println("inserting doc: " + emldoc);
238
        String docid = generateDocumentId() + ".1";
239
        try {
240
            m.login(username, password);
241
            String response = insertDocumentId(docid, emldoc, true, false);
242
        } catch (MetacatAuthException e) {
243
            fail(e.getMessage());
244
        } catch (MetacatInaccessibleException e) {
245
            fail(e.getMessage());
246
        }
247
        return docid;
248
    }
249
    
250
    private void ph(String s)
251
    {
252
        System.out.println("*********************** " + s + " ****************************");
253
    }
254
}
(7-7/26)