1 |
5282
|
jones
|
/**
|
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 |
5377
|
berkley
|
import java.util.*;
|
29 |
5282
|
jones
|
|
30 |
|
|
import edu.ucsb.nceas.MCTestCase;
|
31 |
6005
|
cjones
|
import edu.ucsb.nceas.metacat.AccessionNumber;
|
32 |
|
|
import edu.ucsb.nceas.metacat.AccessionNumberException;
|
33 |
5282
|
jones
|
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 |
5377
|
berkley
|
|
62 |
|
|
/**
|
63 |
5452
|
berkley
|
* test getting a guid from the systemmetadata table
|
64 |
|
|
*/
|
65 |
|
|
public void testGetGUID()
|
66 |
|
|
{
|
67 |
5887
|
berkley
|
ph("testGetGUID");
|
68 |
5452
|
berkley
|
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 |
6005
|
cjones
|
assertNotNull(gotGuid);
|
77 |
5452
|
berkley
|
}
|
78 |
|
|
catch(Exception e)
|
79 |
|
|
{
|
80 |
|
|
fail("Unexpected exception in testGetGUID: " + e.getMessage());
|
81 |
|
|
}
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
5377
|
berkley
|
* test getting a list of all local ids
|
86 |
|
|
*/
|
87 |
|
|
public void testGetAllLocalIds()
|
88 |
|
|
{
|
89 |
5887
|
berkley
|
ph("testGetAllLocalIds");
|
90 |
5377
|
berkley
|
try
|
91 |
|
|
{
|
92 |
|
|
List l = IdentifierManager.getInstance().getAllLocalIds();
|
93 |
|
|
for(int i=0; i<l.size(); i++)
|
94 |
|
|
{
|
95 |
5887
|
berkley
|
System.out.println(l.get(i));
|
96 |
5377
|
berkley
|
}
|
97 |
|
|
assertTrue(l.size() > 0);
|
98 |
|
|
}
|
99 |
|
|
catch(Exception e)
|
100 |
|
|
{
|
101 |
|
|
fail("Error in testGetAllLocalIds: " + e.getMessage());
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
5282
|
jones
|
|
105 |
|
|
/** Test that IM instances can be created. */
|
106 |
|
|
public void testGetInstance() {
|
107 |
5887
|
berkley
|
ph("testGetInstance");
|
108 |
5282
|
jones
|
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 |
5887
|
berkley
|
ph("testGetLocalId");
|
115 |
5282
|
jones
|
IdentifierManager im = IdentifierManager.getInstance();
|
116 |
6005
|
cjones
|
String docidWithRev = insertTestDocument();
|
117 |
|
|
String docid = docidWithRev.substring(0, docidWithRev.lastIndexOf("."));
|
118 |
|
|
String guid;
|
119 |
5282
|
jones
|
String idReturned;
|
120 |
|
|
try {
|
121 |
6005
|
cjones
|
guid = im.getGUID(docid, 1);
|
122 |
|
|
idReturned = im.getLocalId(guid);
|
123 |
|
|
assertEquals(docidWithRev, idReturned);
|
124 |
|
|
|
125 |
5282
|
jones
|
} catch (McdbDocNotFoundException e) {
|
126 |
|
|
fail(e.getMessage());
|
127 |
|
|
}
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
/** Test that unknown LocalId's return the proper exception. */
|
131 |
|
|
public void testGetLocalIdNotFound() {
|
132 |
5887
|
berkley
|
ph("testGetLocalIdNotFound");
|
133 |
5282
|
jones
|
IdentifierManager im = IdentifierManager.getInstance();
|
134 |
|
|
String idReturned;
|
135 |
|
|
try {
|
136 |
|
|
idReturned = im.getLocalId(badGuid);
|
137 |
|
|
fail("Failed: getLocalID() should have returned an document not " +
|
138 |
|
|
"found exception but did not.");
|
139 |
|
|
} catch (McdbDocNotFoundException e) {
|
140 |
|
|
assertNotNull(e);
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* Test that an identifier is present in the system when it should
|
146 |
|
|
* be, and that it is not present when it shouldn't be.
|
147 |
|
|
*/
|
148 |
|
|
public void testIdentifierExists() {
|
149 |
5887
|
berkley
|
ph("testIdentifierExists");
|
150 |
6005
|
cjones
|
|
151 |
|
|
String goodGuid = "";
|
152 |
|
|
String accString = "";
|
153 |
|
|
String docid = "";
|
154 |
|
|
int rev = 0;
|
155 |
|
|
|
156 |
|
|
try {
|
157 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
158 |
|
|
accString = insertTestDocument();
|
159 |
|
|
AccessionNumber accNumber = new AccessionNumber(accString, "NONE");
|
160 |
|
|
docid = accNumber.getDocid();
|
161 |
|
|
rev = new Integer(accNumber.getRev());
|
162 |
|
|
goodGuid = im.getGUID(docid, rev);
|
163 |
|
|
assertTrue(im.identifierExists(goodGuid));
|
164 |
|
|
assertFalse(im.identifierExists(badGuid));
|
165 |
|
|
|
166 |
|
|
} catch ( McdbDocNotFoundException dnfe ) {
|
167 |
|
|
fail("The document " + docid + "couldn't be found. The error was: " +
|
168 |
|
|
dnfe.getMessage());
|
169 |
|
|
|
170 |
|
|
} catch ( AccessionNumberException ane ) {
|
171 |
|
|
fail("The accession number could not be created for docid" +
|
172 |
|
|
accString + ". The error was: " + ane.getMessage());
|
173 |
|
|
|
174 |
|
|
} catch ( NumberFormatException nfe ) {
|
175 |
|
|
fail("The revision number could not be created for docid" +
|
176 |
|
|
accString + ". The error was: " + nfe.getMessage());
|
177 |
|
|
|
178 |
|
|
} catch ( SQLException sqle ) {
|
179 |
|
|
fail("The accession number could not be created for docid" +
|
180 |
|
|
accString + ". The error was: " + sqle.getMessage());
|
181 |
|
|
|
182 |
|
|
}
|
183 |
5282
|
jones
|
}
|
184 |
|
|
|
185 |
|
|
/**
|
186 |
|
|
* Test that we are able to create mappings from guids to localIds, and that
|
187 |
5335
|
berkley
|
* improperly formatted docids generate the proper exceptions. This also tests
|
188 |
|
|
* getLocalId() and getGUID()
|
189 |
5282
|
jones
|
*/
|
190 |
|
|
public void testCreateMapping() {
|
191 |
5887
|
berkley
|
ph("testCreateMapping");
|
192 |
5335
|
berkley
|
try
|
193 |
|
|
{
|
194 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
195 |
6005
|
cjones
|
String docid = "test." + new Date().getTime() + ".1";
|
196 |
|
|
String guid = "guid:" + docid;
|
197 |
5335
|
berkley
|
im.createMapping(guid, docid);
|
198 |
|
|
String guiddocid = docid.substring(0, docid.length() - 2);
|
199 |
|
|
System.out.println("guiddocid: " + guiddocid);
|
200 |
|
|
String guid2 = im.getGUID(guiddocid, 1);
|
201 |
|
|
assertTrue(guid2.equals(guid));
|
202 |
|
|
String docid2 = im.getLocalId(guid);
|
203 |
|
|
assertTrue(docid.equals(docid2));
|
204 |
5282
|
jones
|
} catch (McdbDocNotFoundException e) {
|
205 |
5335
|
berkley
|
e.printStackTrace();
|
206 |
|
|
fail("createMapping failed to create proper localId from guid: " + e.getMessage());
|
207 |
5282
|
jones
|
}
|
208 |
|
|
}
|
209 |
5335
|
berkley
|
|
210 |
|
|
|
211 |
|
|
/**
|
212 |
|
|
* test the local id creation
|
213 |
|
|
*/
|
214 |
|
|
public void testGenerateLocalId() {
|
215 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
216 |
6005
|
cjones
|
String localid = im.generateLocalId("mynewid." + new Date().getTime(), 1);
|
217 |
5335
|
berkley
|
System.out.println("localid: " + localid);
|
218 |
|
|
assertTrue(localid != null);
|
219 |
|
|
}
|
220 |
5282
|
jones
|
|
221 |
5335
|
berkley
|
/**
|
222 |
|
|
* Insert a test document, returning the docid that was used.
|
223 |
|
|
*/
|
224 |
5282
|
jones
|
private String insertTestDocument() {
|
225 |
|
|
String accessBlock = getAccessBlock("public", true, true,
|
226 |
|
|
false, false, false);
|
227 |
|
|
String emldoc = getTestEmlDoc("Test identifier manager", EML2_1_0, null,
|
228 |
|
|
null, "http://fake.example.com/somedata", null,
|
229 |
|
|
accessBlock, null, null,
|
230 |
|
|
null, null);
|
231 |
5887
|
berkley
|
System.out.println("inserting doc: " + emldoc);
|
232 |
5282
|
jones
|
String docid = generateDocumentId() + ".1";
|
233 |
|
|
try {
|
234 |
|
|
m.login(username, password);
|
235 |
|
|
String response = insertDocumentId(docid, emldoc, true, false);
|
236 |
|
|
} catch (MetacatAuthException e) {
|
237 |
|
|
fail(e.getMessage());
|
238 |
|
|
} catch (MetacatInaccessibleException e) {
|
239 |
|
|
fail(e.getMessage());
|
240 |
|
|
}
|
241 |
|
|
return docid;
|
242 |
|
|
}
|
243 |
5887
|
berkley
|
|
244 |
|
|
private void ph(String s)
|
245 |
|
|
{
|
246 |
|
|
System.out.println("*********************** " + s + " ****************************");
|
247 |
|
|
}
|
248 |
5282
|
jones
|
}
|