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
        try
66
        {
67
            IdentifierManager im = IdentifierManager.getInstance();
68
            Date d1 = new Date();
69
            String guid1 = "guid:" + d1.getTime();
70

    
71
            String docid = insertTestDocument();
72
            im.createSystemMetadataMapping(guid1, docid);
73
            docid = docid.substring(0, docid.lastIndexOf("."));
74
            
75
            String gotGuid = im.getGUID(docid, 1);
76
            assertTrue(gotGuid.equals(guid1));
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
        try
90
        {
91
            List l = IdentifierManager.getInstance().getAllLocalIds();
92
            for(int i=0; i<l.size(); i++)
93
            {
94
                //System.out.println(l.get(i));
95
            }
96
            assertTrue(l.size() > 0);
97
        }
98
        catch(Exception e)
99
        {
100
            fail("Error in testGetAllLocalIds: " + e.getMessage());
101
        }
102
    }
103

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

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

    
204
    /** 
205
     * Insert a test document, returning the docid that was used. 
206
     */
207
    private String insertTestDocument() {
208
        String accessBlock = getAccessBlock("public", true, true,
209
                false, false, false);
210
        String emldoc = getTestEmlDoc("Test identifier manager", EML2_1_0, null,
211
                null, "http://fake.example.com/somedata", null,
212
                accessBlock, null, null,
213
                null, null);
214
        String docid = generateDocumentId() + ".1";
215
        try {
216
            m.login(username, password);
217
            String response = insertDocumentId(docid, emldoc, true, false);
218
        } catch (MetacatAuthException e) {
219
            fail(e.getMessage());
220
        } catch (MetacatInaccessibleException e) {
221
            fail(e.getMessage());
222
        }
223
        return docid;
224
    }
225
}
(6-6/22)