Project

General

Profile

« Previous | Next » 

Revision 5341

Added by berkley over 14 years ago

adding test for getSystemMetadata. a bit of refactoring.

View differences:

test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
97 97
		suite.addTest(new CrudServiceTest("initialize"));
98 98
		suite.addTest(new CrudServiceTest("testSingletonAccessor"));
99 99
		suite.addTest(new CrudServiceTest("testCreateAndGet"));
100
		suite.addTest(new CrudServiceTest("testGetSystemMetadata"));
100 101
		//suite.addTest(new CrudServiceTest(""));
101 102
		//suite.addTest(new CrudServiceTest(""));
102 103
		//suite.addTest(new CrudServiceTest(""));
103
		//suite.addTest(new CrudServiceTest(""));
104 104
		return suite;
105 105
	}
106 106
	
107 107
	/**
108
	 * public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
109
     *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
110
     *       InvalidRequest, NotImplemented
111
	 */
112
	public void testGetSystemMetadata()
113
	{
114
	    
115
	}
116
	
117
	/**
108 118
	 * create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta) 
109 119
     *   throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
110 120
     *       InsufficientResources, InvalidSystemMetadata, NotImplemented
......
117 127
	{
118 128
	    try
119 129
	    {
120
	        Identifier id;
121
            String testDoc;
122
            AuthToken token;
123
	        testDoc = "<?xml version=\"1.0\"?><test></test>\n";
130
	        String testDoc = getTestDoc();
131
	        CrudService cs = CrudService.getInstance();
132
	        AuthToken token = getToken();
133
	        //run create
134
	        Identifier id = createDoc(token);
124 135
            
125
            CrudService cs = CrudService.getInstance();
126
            //login and get a sessionid
127
            System.out.println("creating MetacatRestClient with url " + cs.getContextUrl());
128
            MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
129
            String username = PropertyService.getProperty("test.mcUser");
130
            String password = PropertyService.getProperty("test.mcPassword");
131
            System.out.println("logging in with username: " + username + " and password " + password + " to context " + cs.getContextUrl());
132
            String response = restClient.login(username, password);
133
            //System.out.println("response to login: " + response);
134
            String sessionid = restClient.getSessionId();
135
            SessionService sessionService = SessionService.getInstance();
136
            sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
137
            System.out.println("sessionid: " + sessionid);
138
            token = new AuthToken(sessionid);
139
            
140
            id = new Identifier();
141
            String docid = generateDocumentId();
142
            id.setValue(docid);
143
	                    
144
            //create the system metadata then run the create method
145
            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
146
            SystemMetadata sm = new SystemMetadata();
147
            //set the id
148
            sm.setIdentifier(id);
149
            System.out.println("sm id is " + id.getValue());
150
            sm.setObjectFormat(ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
151
            System.out.println("sm objectformat: " + sm.getObjectFormat());
152
            //create the checksum
153
            String checksumS = checksum(testDoc);
154
            ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
155
            Checksum checksum = new Checksum();
156
            checksum.setValue(checksumS);
157
            checksum.setAlgorithm(ca);
158
            sm.setChecksum(checksum);
159
            System.out.println("sm checksum is " + checksumS);
160
            //set the size
161
            sm.setSize(testDoc.getBytes().length);
162
            System.out.println("sm size: " + testDoc.getBytes().length);
163
            //submitter
164
            Principal p = new Principal();
165
            p.setValue("joe");
166
            sm.setSubmitter(p);
167
            sm.setRightsHolder(p);
168
            sm.setDateUploaded(new Date());
169
            sm.setDateSysMetadataModified(new Date());
170
            NodeReference nr = new NodeReference();
171
            nr.setValue("metacat");
172
            sm.setOriginMemberNode(nr);
173
            sm.setAuthoritativeMemberNode(nr);
174
            //create the doc
175
            cs.create(token, id, sbis, sm);
176
            
177 136
            //try to get the same doc then compare them
178 137
            InputStream gotDocStream = cs.get(token, id);
179 138
            StringBuffer sb = new StringBuffer();
......
213 172
	{
214 173
		assertTrue(1 == 1);
215 174
	}
175
	
176
	private String getTestDoc()
177
	{
178
	    return "<?xml version=\"1.0\"?><test></test>\n";
179
	}
180
	
181
	/**
182
	 * authenticate and return a token
183
	 */
184
	private AuthToken getToken()
185
	  throws Exception
186
	{
187
	    CrudService cs = CrudService.getInstance();
188
        //login and get a sessionid
189
        System.out.println("creating MetacatRestClient with url " + cs.getContextUrl());
190
        MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
191
        String username = PropertyService.getProperty("test.mcUser");
192
        String password = PropertyService.getProperty("test.mcPassword");
193
        System.out.println("logging in with username: " + username + " and password " + password + " to context " + cs.getContextUrl());
194
        String response = restClient.login(username, password);
195
        //System.out.println("response to login: " + response);
196
        String sessionid = restClient.getSessionId();
197
        SessionService sessionService = SessionService.getInstance();
198
        sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
199
        System.out.println("sessionid: " + sessionid);
200
        AuthToken token = new AuthToken(sessionid);
201
        return token;
202
	}
203
	
204
	/**
205
	 * create a doc and return its id
206
	 */
207
	private Identifier createDoc(AuthToken token) throws Exception
208
	{
209
	    Identifier id;
210
        CrudService cs = CrudService.getInstance();
211
        String testDoc = getTestDoc();
212
        
213
        id = new Identifier();
214
        String docid = generateDocumentId();
215
        id.setValue(docid);
216
                    
217
        //create the system metadata then run the create method
218
        StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
219
        SystemMetadata sm = new SystemMetadata();
220
        //set the id
221
        sm.setIdentifier(id);
222
        System.out.println("sm id is " + id.getValue());
223
        sm.setObjectFormat(ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
224
        System.out.println("sm objectformat: " + sm.getObjectFormat());
225
        //create the checksum
226
        String checksumS = checksum(testDoc);
227
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
228
        Checksum checksum = new Checksum();
229
        checksum.setValue(checksumS);
230
        checksum.setAlgorithm(ca);
231
        sm.setChecksum(checksum);
232
        System.out.println("sm checksum is " + checksumS);
233
        //set the size
234
        sm.setSize(testDoc.getBytes().length);
235
        System.out.println("sm size: " + testDoc.getBytes().length);
236
        //submitter
237
        Principal p = new Principal();
238
        p.setValue("joe");
239
        sm.setSubmitter(p);
240
        sm.setRightsHolder(p);
241
        sm.setDateUploaded(new Date());
242
        sm.setDateSysMetadataModified(new Date());
243
        NodeReference nr = new NodeReference();
244
        nr.setValue("metacat");
245
        sm.setOriginMemberNode(nr);
246
        sm.setAuthoritativeMemberNode(nr);
247
        //create the doc
248
        cs.create(token, id, sbis, sm);
249
        return id;
250
	}
216 251
  
217 252
	/**
218 253
	 * produce an md5 checksum for item

Also available in: Unified diff