Project

General

Profile

« Previous | Next » 

Revision 5373

Added by berkley over 14 years ago

new class to handle system metadata tasks in metacat

View differences:

test/edu/ucsb/nceas/metacat/dataone/SystemMetadataManagerTest.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2010 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: berkley $'
8
 *     '$Date: 2010-05-03 14:26:08 -0700 (Fri, 14 Aug 2009) $'
9
 * '$Revision: 5027 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

  
26
package edu.ucsb.nceas.metacat.dataone;
27

  
28
import java.util.*;
29
import java.io.*;
30

  
31
import java.security.MessageDigest;
32

  
33
import edu.ucsb.nceas.MCTestCase;
34
import edu.ucsb.nceas.metacat.IdentifierManager;
35
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
36
import edu.ucsb.nceas.metacat.client.MetacatException;
37
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
38
import edu.ucsb.nceas.metacat.dataone.CrudService;
39
import junit.framework.Test;
40
import junit.framework.TestSuite;
41

  
42
import org.dataone.service.exceptions.InvalidRequest;
43
import org.dataone.service.exceptions.InvalidToken;
44
import org.dataone.service.exceptions.NotAuthorized;
45
import org.dataone.service.exceptions.NotImplemented;
46
import org.dataone.service.exceptions.ServiceFailure;
47
import org.dataone.service.types.*;
48

  
49
import edu.ucsb.nceas.metacat.properties.PropertyService;
50
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
51

  
52
import edu.ucsb.nceas.metacat.service.SessionService;
53
import edu.ucsb.nceas.metacat.util.SessionData;
54

  
55
/**
56
 * A JUnit test for testing the dataone SystemMetadataManager class
57
 */
58
public class SystemMetadataManagerTest extends MCTestCase 
59
{   
60
    /**
61
    * consstructor for the test
62
    */
63
    public SystemMetadataManagerTest(String name)
64
    {
65
        super(name);
66
    }
67
  
68
    /**
69
	 * Establish a testing framework by initializing appropriate objects
70
	 */
71
	public void setUp() throws Exception 
72
	{
73
		super.setUp();
74
	}
75

  
76
	/**
77
	 * Release any objects after tests are complete
78
	 */
79
	public void tearDown() 
80
	{
81
	}
82

  
83
	/**
84
	 * Create a suite of tests to be run together
85
	 */
86
	public static Test suite() 
87
	{
88
		TestSuite suite = new TestSuite();
89
		suite.addTest(new SystemMetadataManagerTest("initialize"));
90
		suite.addTest(new SystemMetadataManagerTest("testSingletonAccessor"));
91

  
92
		//suite.addTest(new CrudServiceTest(""));
93
		return suite;
94
	}
95
	
96
	
97
	/**
98
	 * getInstance()
99
	 */
100
	public void testSingletonAccessor()
101
	{
102
	    printTestHeader("testSingletonAccessor");
103
	    SystemMetadataManager smm = SystemMetadataManager.getInstance();
104
	    assertNotNull(smm);
105
	}
106
	
107
	/**
108
	 * Run an initial test that always passes to check that the test harness is
109
	 * working.
110
	 */
111
	public void initialize() 
112
	{
113
	    printTestHeader("initialize");
114
		assertTrue(1 == 1);
115
	}
116
	
117
	/**
118
	 * print a header to start each test
119
	 */
120
	private void printTestHeader(String testName)
121
	{
122
	    System.out.println();
123
	    System.out.println("********************************** " + testName + " **********************************");
124
	}
125
  
126
	/**
127
	 * produce an md5 checksum for item
128
	 */
129
	private String checksum(String item)
130
	  throws Exception
131
	{
132
        StringBufferInputStream fis =  new StringBufferInputStream(item);
133
        
134
        byte[] buffer = new byte[1024];
135
        MessageDigest complete = MessageDigest.getInstance("MD5");
136
        int numRead;
137
        
138
        do 
139
        {
140
          numRead = fis.read(buffer);
141
          if (numRead > 0) 
142
          {
143
            complete.update(buffer, 0, numRead);
144
          }
145
        } while (numRead != -1);
146
        
147
        
148
        return getHex(complete.digest());
149
	}
150
	
151
	/**
152
	 * convert a byte array to a hex string
153
	 */
154
	private static String getHex( byte [] raw ) 
155
	{
156
	    final String HEXES = "0123456789ABCDEF";
157
        if ( raw == null ) {
158
          return null;
159
        }
160
        final StringBuilder hex = new StringBuilder( 2 * raw.length );
161
        for ( final byte b : raw ) {
162
          hex.append(HEXES.charAt((b & 0xF0) >> 4))
163
             .append(HEXES.charAt((b & 0x0F)));
164
        }
165
        return hex.toString();
166
    }
167
}
src/edu/ucsb/nceas/metacat/dataone/SystemMetadataManager.java
1
/**
2
 * 
3
 */
4
package edu.ucsb.nceas.metacat.dataone;
5

  
6
import java.util.*;
7
import java.io.*;
8

  
9
import edu.ucsb.nceas.metacat.*;
10

  
11
/**
12
 * @author berkley
13
 *
14
 * Class to handle SystemMetadata object in the metacat system
15
 */
16
public class SystemMetadataManager
17
{
18
    //singleton instance
19
    private static SystemMetadataManager manager = null;
20
    private IdentifierManager idManager = null;
21
    
22
    /**
23
     * private constructor.  Use the singleton getInstance() method to use
24
     * this class
25
     */
26
    private SystemMetadataManager()
27
    {
28
        idManager = IdentifierManager.getInstance();
29
    }
30
    
31
    /**
32
     * get a singleton instance of this class
33
     */
34
    public static SystemMetadataManager getInstance()
35
    {
36
        if(manager == null)
37
        {
38
            manager = new SystemMetadataManager();
39
        }
40
        return manager;
41
    }
42
    
43
    /**
44
     * return true if the document represented by guid has a system metadata
45
     * document registered in the systemmetadata table
46
     * @param guid
47
     * @return
48
     */
49
    public boolean documentHasSystemMetadata(String guid)
50
    {
51
        boolean hassm = false;
52
        try
53
        {
54
            String smid = idManager.getSystemMetadataId(guid);
55
            if(smid != null && !smid.trim().equals(""))
56
            {
57
                hassm = true;
58
            }
59
        }
60
        catch(McdbDocNotFoundException me)
61
        {
62
            //if this exception is thrown, the doc with guid does not have
63
            //sys metadata
64
        }
65
        
66
        return hassm;
67
    }
68
}

Also available in: Unified diff