Project

General

Profile

« Previous | Next » 

Revision 5340

Added by berkley almost 14 years ago

refactored XMLSchemaService to not have static methods. made the CrudServiceTest more robust.

View differences:

CrudServiceTest.java
50 50
 * A JUnit test for testing the dataone CrudService class
51 51
 */
52 52
public class CrudServiceTest extends MCTestCase 
53
{
54
  /**
55
   * consstructor for the test
56
   */
57
  public CrudServiceTest(String name)
58
  {
59
    super(name);
60
  }
53
{   
54
    /**
55
    * consstructor for the test
56
    */
57
    public CrudServiceTest(String name)
58
    {
59
        super(name);
60
        init();
61
    }
61 62
  
62
  /**
63
    /**
63 64
	 * Establish a testing framework by initializing appropriate objects
64 65
	 */
65 66
	public void setUp() throws Exception 
......
73 74
	public void tearDown() 
74 75
	{
75 76
	}
77
	
78
	public void init()
79
	{
80
	    try
81
	    {
82
            
83
        }
84
        catch(Exception e)
85
        {
86
            e.printStackTrace();
87
            fail("Could not initialize CrudServiceTest: " + e.getMessage());
88
        }
89
	}
76 90

  
77 91
	/**
78 92
	 * Create a suite of tests to be run together
......
82 96
		TestSuite suite = new TestSuite();
83 97
		suite.addTest(new CrudServiceTest("initialize"));
84 98
		suite.addTest(new CrudServiceTest("testSingletonAccessor"));
85
		suite.addTest(new CrudServiceTest("testCreate"));
99
		suite.addTest(new CrudServiceTest("testCreateAndGet"));
86 100
		//suite.addTest(new CrudServiceTest(""));
87 101
		//suite.addTest(new CrudServiceTest(""));
88 102
		//suite.addTest(new CrudServiceTest(""));
89 103
		//suite.addTest(new CrudServiceTest(""));
90
		//suite.addTest(new CrudServiceTest(""));
91 104
		return suite;
92 105
	}
93 106
	
......
95 108
	 * create(AuthToken token, Identifier guid, InputStream object, SystemMetadata sysmeta) 
96 109
     *   throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
97 110
     *       InsufficientResources, InvalidSystemMetadata, NotImplemented
111
     *
112
     * public InputStream get(AuthToken token, Identifier guid)
113
     *       throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
114
     *       NotImplemented
98 115
	 */
99
	public void testCreate()
116
	public void testCreateAndGet()
100 117
	{
101 118
	    try
102 119
	    {
120
	        Identifier id;
121
            String testDoc;
122
            AuthToken token;
123
	        testDoc = "<?xml version=\"1.0\"?><test></test>\n";
124
            
103 125
            CrudService cs = CrudService.getInstance();
104
            
105 126
            //login and get a sessionid
106 127
            System.out.println("creating MetacatRestClient with url " + cs.getContextUrl());
107 128
            MetacatRestClient restClient = new MetacatRestClient(cs.getContextUrl());
108 129
            String username = PropertyService.getProperty("test.mcUser");
109
			String password = PropertyService.getProperty("test.mcPassword");
110
			System.out.println("logging in with username: " + username + " and password " + password + " to context " + cs.getContextUrl());
111
			String response = restClient.login(username, password);
112
			//System.out.println("response to login: " + response);
113
			String sessionid = restClient.getSessionId();
114
			SessionService sessionService = SessionService.getInstance();
115
			sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
116
			System.out.println("sessionid: " + sessionid);
117
			AuthToken token = new AuthToken(sessionid);
118
			
119
            String s = "<?xml version=\"1.0\"?><test></test>\n";
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);
120 139
            
121
            //create the system metadata then run the create method
122
            StringBufferInputStream sbis = new StringBufferInputStream(s);
140
            id = new Identifier();
123 141
            String docid = generateDocumentId();
124
            String smdocid = generateDocumentId();
125
            Identifier id = new Identifier();
126 142
            id.setValue(docid);
143
	                    
144
            //create the system metadata then run the create method
145
            StringBufferInputStream sbis = new StringBufferInputStream(testDoc);
127 146
            SystemMetadata sm = new SystemMetadata();
128 147
            //set the id
129 148
            sm.setIdentifier(id);
130
            System.out.println("sm id is " + id);
149
            System.out.println("sm id is " + id.getValue());
131 150
            sm.setObjectFormat(ObjectFormat.convert("eml://ecoinformatics.org/eml-2.1.0"));
132 151
            System.out.println("sm objectformat: " + sm.getObjectFormat());
133 152
            //create the checksum
134
            String checksumS = checksum(s);
153
            String checksumS = checksum(testDoc);
135 154
            ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
136 155
            Checksum checksum = new Checksum();
137 156
            checksum.setValue(checksumS);
......
139 158
            sm.setChecksum(checksum);
140 159
            System.out.println("sm checksum is " + checksumS);
141 160
            //set the size
142
            sm.setSize(s.getBytes().length);
143
            System.out.println("sm size: " + s.getBytes().length);
161
            sm.setSize(testDoc.getBytes().length);
162
            System.out.println("sm size: " + testDoc.getBytes().length);
144 163
            //submitter
145 164
            Principal p = new Principal();
146 165
            p.setValue("joe");
......
152 171
            nr.setValue("metacat");
153 172
            sm.setOriginMemberNode(nr);
154 173
            sm.setAuthoritativeMemberNode(nr);
174
            //create the doc
175
            cs.create(token, id, sbis, sm);
155 176
            
177
            //try to get the same doc then compare them
178
            InputStream gotDocStream = cs.get(token, id);
179
            StringBuffer sb = new StringBuffer();
180
            byte[] b = new byte[1024];
181
            int numread = gotDocStream.read(b, 0, 1024);
182
            while(numread != -1)
183
            {
184
                sb.append(new String(b, 0, numread));
185
                numread = gotDocStream.read(b, 0, 1024);
186
            }
156 187
            
157
            cs.create(token, id, sbis, sm);
188
            String gotDoc = sb.toString();
189
            System.out.println("got doc: " + gotDoc);
190
            assertTrue(gotDoc.trim().equals(testDoc.trim()));
158 191
        }
159 192
        catch(Exception e)
160 193
        {

Also available in: Unified diff