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.IdentifierManager;
32
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
33
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
34
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
35
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
36

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

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

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

    
113
    /** Test that known LocalId's can be looked up from GUIDs. */
114
    public void testGetLocalId() {
115
        ph("testGetLocalId");
116
        IdentifierManager im = IdentifierManager.getInstance();
117
        String docid = insertTestDocument();
118
        String goodGuid = "test:" + docid;
119
        im.createMapping(goodGuid, docid);
120
        String idReturned;
121
        try {
122
            idReturned = im.getLocalId(goodGuid);
123
            assertEquals(docid, idReturned);
124
        } catch (McdbDocNotFoundException e) {
125
            fail(e.getMessage());
126
        }
127
    }
128
    
129
    /** Test that unknown LocalId's return the proper exception. */
130
    public void testGetLocalIdNotFound() {
131
        ph("testGetLocalIdNotFound");
132
        IdentifierManager im = IdentifierManager.getInstance();
133
        String idReturned;
134
        try {
135
            idReturned = im.getLocalId(badGuid);
136
            fail("Failed: getLocalID() should have returned an document not " + 
137
                 "found exception but did not.");
138
        } catch (McdbDocNotFoundException e) {
139
            assertNotNull(e);
140
        }
141
    }
142
    
143
    /** 
144
     * Test that an identifier is present in the system when it should
145
     *  be, and that it is not present when it shouldn't be. 
146
     */
147
    public void testIdentifierExists() {
148
        ph("testIdentifierExists");
149
        IdentifierManager im = IdentifierManager.getInstance();
150
        String docid = insertTestDocument();
151
        im.createMapping("test:"+docid, docid);
152
        String goodGuid = "test:" + docid;
153
        assertTrue(im.identifierExists(goodGuid));
154
        assertFalse(im.identifierExists(badGuid));
155
    }
156
    
157
    /**
158
     * Test that we are able to create mappings from guids to localIds, and that
159
     * improperly formatted docids generate the proper exceptions.  This also tests
160
     * getLocalId() and getGUID()
161
     */
162
    public void testCreateMapping() {
163
       ph("testCreateMapping");
164
       try
165
       {
166
            IdentifierManager im = IdentifierManager.getInstance();
167
            String docid = insertTestDocument();
168
            String guid = "test:" + docid;
169
            im.createMapping(guid, docid);
170
            String guiddocid = docid.substring(0, docid.length() - 2);
171
            System.out.println("guiddocid: " + guiddocid);
172
            String guid2 = im.getGUID(guiddocid, 1);
173
            assertTrue(guid2.equals(guid));
174
            String docid2 = im.getLocalId(guid);
175
            assertTrue(docid.equals(docid2));
176
        } catch (McdbDocNotFoundException e) {
177
            e.printStackTrace();
178
            fail("createMapping failed to create proper localId from guid: " + e.getMessage());
179
        }
180
    }
181
    
182
    /**
183
     * test createSystemMetadataMapping and getSystemMetadataId
184
     */
185
    public void testCreateSystemMetadataMapping()
186
    {
187
        ph("testCreateSystemMetadataMapping");
188
        try
189
        {
190
            IdentifierManager im = IdentifierManager.getInstance();
191
            String docid = insertTestDocument();
192
            String guid = "test:" + docid;
193
            im.createSystemMetadataMapping(guid, docid);
194
            String docid2 = im.getSystemMetadataLocalId(guid);
195
            assertTrue(docid2.equals(docid));
196
        } catch (McdbDocNotFoundException e) {
197
          e.printStackTrace();
198
          fail("createSystemMetadataMapping failed to create proper localId from guid: " + e.getMessage());
199
        }
200
    }
201
    
202
    /**
203
     * test the local id creation
204
     */
205
    public void testGenerateLocalId() {
206
      IdentifierManager im = IdentifierManager.getInstance();
207
      String localid = im.generateLocalId("mynewid.1.3.3.2", 1);
208
      System.out.println("localid: " + localid);
209
      assertTrue(localid != null);
210
    }
211

    
212
    /** 
213
     * Insert a test document, returning the docid that was used. 
214
     */
215
    private String insertTestDocument() {
216
        String accessBlock = getAccessBlock("public", true, true,
217
                false, false, false);
218
        String emldoc = getTestEmlDoc("Test identifier manager", EML2_1_0, null,
219
                null, "http://fake.example.com/somedata", null,
220
                accessBlock, null, null,
221
                null, null);
222
        System.out.println("inserting doc: " + emldoc);
223
        String docid = generateDocumentId() + ".1";
224
        try {
225
            m.login(username, password);
226
            String response = insertDocumentId(docid, emldoc, true, false);
227
        } catch (MetacatAuthException e) {
228
            fail(e.getMessage());
229
        } catch (MetacatInaccessibleException e) {
230
            fail(e.getMessage());
231
        }
232
        return docid;
233
    }
234
    
235
    private void ph(String s)
236
    {
237
        System.out.println("*********************** " + s + " ****************************");
238
    }
239
}
(6-6/24)