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
|
|
29
|
import edu.ucsb.nceas.MCTestCase;
|
30
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
31
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
32
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
33
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
34
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
35
|
|
36
|
public class IdentifierManagerTest extends MCTestCase {
|
37
|
private String badGuid = "test:testIdThatDoesNotExist";
|
38
|
|
39
|
/**
|
40
|
* Initialize the connection to metacat, and insert a document to be
|
41
|
* used for testing with a known docid.
|
42
|
*/
|
43
|
public void setUp() {
|
44
|
|
45
|
metacatConnectionNeeded = true;
|
46
|
try {
|
47
|
DBConnectionPool pool = DBConnectionPool.getInstance();
|
48
|
} catch (SQLException e2) {
|
49
|
fail(e2.getMessage());
|
50
|
}
|
51
|
|
52
|
try {
|
53
|
super.setUp();
|
54
|
} catch (Exception e1) {
|
55
|
fail(e1.getMessage());
|
56
|
}
|
57
|
}
|
58
|
|
59
|
/** Test that IM instances can be created. */
|
60
|
public void testGetInstance() {
|
61
|
IdentifierManager im = IdentifierManager.getInstance();
|
62
|
assertNotNull(im);
|
63
|
}
|
64
|
|
65
|
/** Test that known LocalId's can be looked up from GUIDs. */
|
66
|
public void testGetLocalId() {
|
67
|
IdentifierManager im = IdentifierManager.getInstance();
|
68
|
String docid = insertTestDocument();
|
69
|
String goodGuid = "test:" + docid;
|
70
|
im.createMapping(goodGuid, docid);
|
71
|
String idReturned;
|
72
|
try {
|
73
|
idReturned = im.getLocalId(goodGuid);
|
74
|
assertEquals(docid, idReturned);
|
75
|
} catch (McdbDocNotFoundException e) {
|
76
|
fail(e.getMessage());
|
77
|
}
|
78
|
}
|
79
|
|
80
|
/** Test that unknown LocalId's return the proper exception. */
|
81
|
public void testGetLocalIdNotFound() {
|
82
|
IdentifierManager im = IdentifierManager.getInstance();
|
83
|
String idReturned;
|
84
|
try {
|
85
|
idReturned = im.getLocalId(badGuid);
|
86
|
fail("Failed: getLocalID() should have returned an document not " +
|
87
|
"found exception but did not.");
|
88
|
} catch (McdbDocNotFoundException e) {
|
89
|
assertNotNull(e);
|
90
|
}
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Test that an identifier is present in the system when it should
|
95
|
* be, and that it is not present when it shouldn't be.
|
96
|
*/
|
97
|
public void testIdentifierExists() {
|
98
|
IdentifierManager im = IdentifierManager.getInstance();
|
99
|
String docid = insertTestDocument();
|
100
|
im.createMapping("test:"+docid, docid);
|
101
|
String goodGuid = "test:" + docid;
|
102
|
assertTrue(im.identifierExists(goodGuid));
|
103
|
assertFalse(im.identifierExists(badGuid));
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Test that we are able to create mappings from guids to localIds, and that
|
108
|
* improperly formatted docids generate the proper exceptions. This also tests
|
109
|
* getLocalId() and getGUID()
|
110
|
*/
|
111
|
public void testCreateMapping() {
|
112
|
try
|
113
|
{
|
114
|
IdentifierManager im = IdentifierManager.getInstance();
|
115
|
String docid = insertTestDocument();
|
116
|
String guid = "test:" + docid;
|
117
|
im.createMapping(guid, docid);
|
118
|
String guiddocid = docid.substring(0, docid.length() - 2);
|
119
|
System.out.println("guiddocid: " + guiddocid);
|
120
|
String guid2 = im.getGUID(guiddocid, 1);
|
121
|
assertTrue(guid2.equals(guid));
|
122
|
String docid2 = im.getLocalId(guid);
|
123
|
assertTrue(docid.equals(docid2));
|
124
|
} catch (McdbDocNotFoundException e) {
|
125
|
e.printStackTrace();
|
126
|
fail("createMapping failed to create proper localId from guid: " + e.getMessage());
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* test createSystemMetadataMapping and getSystemMetadataId
|
132
|
*/
|
133
|
public void testCreateSystemMetadataMapping()
|
134
|
{
|
135
|
try
|
136
|
{
|
137
|
IdentifierManager im = IdentifierManager.getInstance();
|
138
|
String docid = insertTestDocument();
|
139
|
String guid = "test:" + docid;
|
140
|
im.createSystemMetadataMapping(guid, docid);
|
141
|
String docid2 = im.getSystemMetadataId(guid);
|
142
|
assertTrue(docid2.equals(docid));
|
143
|
} catch (McdbDocNotFoundException e) {
|
144
|
e.printStackTrace();
|
145
|
fail("createSystemMetadataMapping failed to create proper localId from guid: " + e.getMessage());
|
146
|
}
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* test the local id creation
|
151
|
*/
|
152
|
public void testGenerateLocalId() {
|
153
|
IdentifierManager im = IdentifierManager.getInstance();
|
154
|
String localid = im.generateLocalId("mynewid.1.3.3.2", 1);
|
155
|
System.out.println("localid: " + localid);
|
156
|
assertTrue(localid != null);
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Insert a test document, returning the docid that was used.
|
161
|
*/
|
162
|
private String insertTestDocument() {
|
163
|
String accessBlock = getAccessBlock("public", true, true,
|
164
|
false, false, false);
|
165
|
String emldoc = getTestEmlDoc("Test identifier manager", EML2_1_0, null,
|
166
|
null, "http://fake.example.com/somedata", null,
|
167
|
accessBlock, null, null,
|
168
|
null, null);
|
169
|
String docid = generateDocumentId() + ".1";
|
170
|
try {
|
171
|
m.login(username, password);
|
172
|
String response = insertDocumentId(docid, emldoc, true, false);
|
173
|
} catch (MetacatAuthException e) {
|
174
|
fail(e.getMessage());
|
175
|
} catch (MetacatInaccessibleException e) {
|
176
|
fail(e.getMessage());
|
177
|
}
|
178
|
return docid;
|
179
|
}
|
180
|
}
|