Project

General

Profile

« Previous | Next » 

Revision 6141

Added by Chris Jones over 13 years ago

Updated tests to use ObjectFormatCache.getInstance(), and added tests for CNCoreImpl.listFormats and getFormat().

View differences:

test/edu/ucsb/nceas/metacat/dataone/MetadataTypeRegisterTest.java
81 81
    {
82 82
      ObjectFormat of = null;
83 83
      try {
84
	        of = ObjectFormatCache.getFormat("XXXX");
84
	        of = ObjectFormatCache.getInstance().getFormat("XXXX");
85 85
              
86 86
      } catch (NotFound e) {
87 87
        assertFalse(MetadataTypeRegister.isMetadataType(of));
......
90 90
      
91 91
      try {
92 92
	      
93
      	of = ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.0.0");
93
      	of = ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.0.0");
94 94
	      assertTrue(MetadataTypeRegister.isMetadataType(of));
95 95
	      
96
	      of = ObjectFormatCache.getFormat("http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2");
96
	      of = ObjectFormatCache.getInstance().getFormat("http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2");
97 97
	      assertTrue(MetadataTypeRegister.isMetadataType(of));
98 98
	      
99
	      of = ObjectFormatCache.getFormat("CF-1.3");
99
	      of = ObjectFormatCache.getInstance().getFormat("CF-1.3");
100 100
	      assertTrue(MetadataTypeRegister.isMetadataType(of));
101 101
	      
102
	      of = ObjectFormatCache.getFormat("http://www.loc.gov/METS/");
102
	      of = ObjectFormatCache.getInstance().getFormat("http://www.loc.gov/METS/");
103 103
	      assertTrue(MetadataTypeRegister.isMetadataType(of));
104 104
	      
105
	      of = ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-3.0.0");
105
	      of = ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-3.0.0");
106 106
	      assertFalse(MetadataTypeRegister.isMetadataType(of));
107 107
      
108 108
      } catch (NotFound e) {
test/edu/ucsb/nceas/metacat/dataone/CNCoreTest.java
25 25

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

  
28
import static org.junit.Assert.assertTrue;
29
import static org.junit.Assert.fail;
30

  
28 31
import java.util.Date;
29 32

  
30 33
import junit.framework.Test;
31 34
import junit.framework.TestSuite;
32 35

  
33 36
import org.dataone.client.ObjectFormatCache;
37
import org.dataone.service.exceptions.InsufficientResources;
38
import org.dataone.service.exceptions.InvalidRequest;
39
import org.dataone.service.exceptions.NotFound;
40
import org.dataone.service.exceptions.NotImplemented;
41
import org.dataone.service.exceptions.ServiceFailure;
34 42
import org.dataone.service.types.Checksum;
35 43
import org.dataone.service.types.ChecksumAlgorithm;
36 44
import org.dataone.service.types.Identifier;
37 45
import org.dataone.service.types.NodeReference;
46
import org.dataone.service.types.ObjectFormat;
47
import org.dataone.service.types.ObjectFormatIdentifier;
48
import org.dataone.service.types.ObjectFormatList;
38 49
import org.dataone.service.types.Session;
39 50
import org.dataone.service.types.Subject;
40 51
import org.dataone.service.types.SystemMetadata;
......
77 88
		TestSuite suite = new TestSuite();
78 89
		suite.addTest(new CNCoreTest("initialize"));
79 90
		
91
		suite.addTest(new CNCoreTest("testListFormats"));
92
		suite.addTest(new CNCoreTest("testGetFormat"));
80 93
		suite.addTest(new CNCoreTest("testRegisterSystemMetadata"));
81 94
	
82 95
		return suite;
......
97 110
			Identifier guid = new Identifier();
98 111
			guid.setValue("testRegisterSystemMetadata." + System.currentTimeMillis());
99 112
			SystemMetadata sysmeta = createSystemMetadata(guid, subject);
100
			CNCoreImpl.getInstance().registerSystemMetaData(session, guid, sysmeta);
113
			CNCoreImpl.getInstance().registerSystemMetadata(session, guid, sysmeta);
101 114
        }
102 115
        catch(Exception e)
103 116
        {
......
114 127
	    printTestHeader("initialize");
115 128
		assertTrue(1 == 1);
116 129
	}
117
	
118
	
130
		
119 131
	/**
120 132
	 * create system metadata with a specified id
121 133
	 */
......
125 137
	    SystemMetadata sm = new SystemMetadata();
126 138
        //set the id
127 139
        sm.setIdentifier(id);
128
        sm.setObjectFormat(ObjectFormatCache.getFormat("application/octet-stream"));
140
        sm.setObjectFormat(ObjectFormatCache.getInstance().getFormat("application/octet-stream"));
129 141
        //create the checksum
130 142
        String checksumS = "test";
131 143
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
......
145 157
        sm.setAuthoritativeMemberNode(nr);
146 158
        return sm;
147 159
	}
160

  
161
	/**
162
	 * test to list the object formats registered in metacat
163
	 */
164
	public void testListFormats() {
165
		
166
    printTestHeader("testListFormats");
167
    
168
    // there should be at least 59 formats in the list
169
  	int formatsCount = 59;
170
  	ObjectFormatList objectFormatList;
171
  	
172
  	try {
173
	    objectFormatList = CNCoreImpl.getInstance().listFormats();
174
	  	assertTrue(objectFormatList.getTotal() >= formatsCount);
175
  	
176
  	} catch (InvalidRequest e) {
177
  		fail("Could not get the object format list: " + e.getMessage());
178
    
179
  	} catch (ServiceFailure e) {
180
  		fail("Could not get the object format list: " + e.getMessage());
181

  
182
    } catch (NotFound e) {
183
  		fail("Could not get the object format list: " + e.getMessage());
184

  
185
    } catch (InsufficientResources e) {
186
  		fail("Could not get the object format list: " + e.getMessage());
187

  
188
    } catch (NotImplemented e) {
189
  		fail("Could not get the object format list: " + e.getMessage());
190

  
191
    }
192
    
193
	}
148 194
	
195
  /**
196
   * Test getting a single object format from the registered list
197
   */
198
  public void testGetFormat() {
199
  	
200
    printTestHeader("testGetFormat");
201

  
202
    String knownFormat = "text/plain";
203
    ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
204
    fmtid.setValue(knownFormat);
205
  	
206
    try {
207
	    
208
			String result = 
209
				CNCoreImpl.getInstance().getFormat(fmtid).getFmtid().getValue();
210
	  	System.out.println("Expected result: " + knownFormat);
211
	  	System.out.println("Found    result: " + result);
212
	  	assertTrue(result.equals(knownFormat));
213
  
214
    } catch (NullPointerException npe) {	  
215
	    fail("The returned format was null: " + npe.getMessage());
216
    
217
    } catch (NotFound nfe) {     
218
    	fail("The format " + knownFormat + " was not found: " + nfe.getMessage());
219
    	
220
    } catch (InvalidRequest ire) {
221
    	fail("The format " + knownFormat + " was not found: " + ire.getMessage());
222

  
223
    } catch (ServiceFailure sfe) {
224
    	fail("The format " + knownFormat + " was not found: " + sfe.getMessage());
225

  
226
    } catch (InsufficientResources ise) {
227
    	fail("The format " + knownFormat + " was not found: " + ise.getMessage());
228
 
229
    } catch (NotImplemented nie) {
230
    	fail("The getFormat() method has not been implemented: " + nie.getMessage());
231

  
232
    }
233
  	
234
  }
235
	
236
  /**
237
   * Test getting a non-existent object format, returning NotFound
238
   */
239
  public void testObjectFormatNotFoundException() {
240
  
241
    printTestHeader("testObjectFormatNotFoundException");
242

  
243
    ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
244
  	String badFormat = "text/bad-format";
245
  	fmtid.setValue(badFormat);
246
  	
247
  	try {
248
  		
249
	    ObjectFormat objectFormat = 
250
	    	CNCoreImpl.getInstance().getFormat(fmtid);
251
      
252
  	} catch (Exception e) {
253
	    
254
  		assertTrue(e instanceof NotFound);
255
  	}
256
  	
257
  }
258
	
149 259
	/**
150 260
	 * print a header to start each test
151 261
	 */
test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
132 132
            //create the system metadata then run the create method
133 133
            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
134 134
            SystemMetadata sm = createSystemMetadata(id, testDoc, 
135
                    ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.1.0"));
135
                    ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0"));
136 136
            //create the doc
137 137
            Identifier rGuid = cs.create(token, id, sbis, sm);
138 138
            
......
292 292
	            id.setValue(docid);
293 293
	            String testDoc = getTestDoc();
294 294
	            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
295
	            ObjectFormat of1 = ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.1.0");
295
	            ObjectFormat of1 = ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0");
296 296
	            SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
297 297
	            assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
298 298
	            //create the doc
......
311 311
            id.setValue(docid);
312 312
            String testDoc = getTestDoc();
313 313
            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
314
            ObjectFormat of1 = ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.1.0");
314
            ObjectFormat of1 = ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0");
315 315
            SystemMetadata sm = createSystemMetadata(id, testDoc, of1);
316 316
            assertFalse(sm.getChecksum().getValue().startsWith("MD5"));
317 317
            //create the doc
......
359 359
	        Date d1 = new Date(new Date().getTime() - 5000);
360 360
	        CrudService cs = CrudService.getInstance();
361 361
	        AuthToken token = getToken();
362
	        ObjectFormat of1 = ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.1.0");
362
	        ObjectFormat of1 = ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0");
363 363
	        //create docs at different times
364 364

  
365 365
	        Identifier id = new Identifier();
......
480 480
	    {
481 481
	        CrudService cs = CrudService.getInstance();
482 482
	        AuthToken token = getToken();
483
	        ObjectFormat of1 = ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.1.0");
484
	        ObjectFormat of2 = ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.0.0");
483
	        ObjectFormat of1 = ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0");
484
	        ObjectFormat of2 = ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.0.0");
485 485
	        //create docs at different times
486 486
	        Date d1 = new Date();
487 487
	        Identifier id1 = createDoc(token, getTestDoc(EML2_1_0), of1);
......
821 821
	 */
822 822
	public Identifier createDoc(AuthToken token, String testDoc) throws Exception
823 823
	{
824
	    return createDoc(token, testDoc, ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.1.0"));
824
	    return createDoc(token, testDoc, ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0"));
825 825
	}
826 826
	
827 827
	/**
......
852 852
	  throws Exception
853 853
	{
854 854
	    return createSystemMetadata(id, testDoc, 
855
	            ObjectFormatCache.getFormat("eml://ecoinformatics.org/eml-2.1.0"));
855
	            ObjectFormatCache.getInstance().getFormat("eml://ecoinformatics.org/eml-2.1.0"));
856 856
	}
857 857
	
858 858
	/**

Also available in: Unified diff