From: SVN c. m. f. t. SWORD-A. p. <swo...@li...> - 2012-03-25 16:18:09
|
Revision: 488 http://sword-app.svn.sourceforge.net/sword-app/?rev=488&view=rev Author: richard-jones Date: 2012-03-25 16:17:58 +0000 (Sun, 25 Mar 2012) Log Message: ----------- add error handling tests where possible Modified Paths: -------------- JavaClient2.0/src/test/java/org/swordapp/client/test/SpecTests.java Modified: JavaClient2.0/src/test/java/org/swordapp/client/test/SpecTests.java =================================================================== --- JavaClient2.0/src/test/java/org/swordapp/client/test/SpecTests.java 2012-03-25 15:57:48 UTC (rev 487) +++ JavaClient2.0/src/test/java/org/swordapp/client/test/SpecTests.java 2012-03-25 16:17:58 UTC (rev 488) @@ -1017,79 +1017,201 @@ assertEquals(newReceipt.getStatusCode(), 200); } - /* Python tests to mimic + @Test + public void errorChecksumMismatch() + throws Exception + { + SWORDClient client = new SWORDClient(new ClientConfiguration()); + ServiceDocument sd = client.getServiceDocument(this.sdIRI, new AuthCredentials(this.user, this.pass, this.obo)); + SWORDCollection col = sd.getWorkspaces().get(0).getCollections().get(0); - def test_35_error_checksum_mismatch(self): - conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, - error_response_raises_exceptions=False) - conn.get_service_document() - col = conn.sd.workspaces[0][1][0] - with open(PACKAGE) as pkg: - receipt = conn.create(col_iri = col.href, - payload=pkg, - mimetype=PACKAGE_MIME, - filename="example.zip", - packaging = 'http://purl.org/net/sword/package/SimpleZip', - in_progress = True, - suggested_identifier = "zyxwvutsrq", - md5sum="123456789") + Deposit deposit = new Deposit(); + deposit.setFile(new FileInputStream(this.file)); + deposit.setMimeType("application/zip"); + deposit.setFilename("example.zip"); + deposit.setPackaging(UriRegistry.PACKAGE_SIMPLE_ZIP); + deposit.setMd5("123456789"); // this is not correct + deposit.setInProgress(true); - assert receipt.code == 412 - assert isinstance(receipt, Error_Document) - assert receipt.error_href == "http://purl.org/net/sword/error/ErrorChecksumMismatch" + try + { + DepositReceipt receipt = client.deposit(col, deposit, new AuthCredentials(this.user, this.pass, this.obo)); + assertTrue(false); + } + catch (SWORDError e) + { + assertEquals(e.getStatus(), 412); + assertEquals(e.getErrorURI(), "http://purl.org/net/sword/error/ErrorChecksumMismatch"); + } + } - def test_36_error_bad_request(self): - conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, - error_response_raises_exceptions=False) - conn.get_service_document() - col = conn.sd.workspaces[0][1][0] - with open(PACKAGE) as pkg: - receipt = conn.create(col_iri = col.href, - payload=pkg, - mimetype=PACKAGE_MIME, - filename="example.zip", - packaging = 'http://purl.org/net/sword/package/SimpleZip', - in_progress = "Invalid", # the API seems to allow this! - suggested_identifier = "zyxwvutsrq") + @Test + public void errorBadRequest() + throws Exception + { + // FIXME: it is highly non-trivial to make a bad request with the client library + // How can we do this? - assert receipt.code == 400 - assert isinstance(receipt, Error_Document) - assert receipt.error_href == "http://purl.org/net/sword/error/ErrorBadRequest" + /* + SWORDClient client = new SWORDClient(new ClientConfiguration()); + ServiceDocument sd = client.getServiceDocument(this.sdIRI, new AuthCredentials(this.user, this.pass, this.obo)); + SWORDCollection col = sd.getWorkspaces().get(0).getCollections().get(0); - def test_37_error_target_owner_unknown(self): - conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, - error_response_raises_exceptions=False) - conn.get_service_document() - col = conn.sd.workspaces[0][1][0] - with open(PACKAGE) as pkg: - receipt = conn.create(col_iri = col.href, - payload=pkg, - mimetype=PACKAGE_MIME, - filename="example.zip", - packaging = 'http://purl.org/net/sword/package/SimpleZip', - in_progress = True, - suggested_identifier = "zyxwvutsrq", - on_behalf_of="richard") # we expressly set the wrong obo on the request rather than the connection + Deposit deposit = new Deposit(); + deposit.setFile(new FileInputStream(this.file)); + deposit.setMimeType("application/zip"); + deposit.setFilename("example.zip"); + deposit.setPackaging(UriRegistry.PACKAGE_SIMPLE_ZIP); + deposit.setMd5(this.fileMd5); + deposit.setInProgress(true); - assert receipt.code == 403 - assert isinstance(receipt, Error_Document) - assert receipt.error_href == "http://purl.org/net/sword/error/TargetOwnerUnknown" + try + { + DepositReceipt receipt = client.deposit(col, deposit, new AuthCredentials(this.user, this.pass, this.obo)); + assertTrue(false); + } + catch (SWORDError e) + { + assertEquals(e.getStatus(), 400); + assertEquals(e.getErrorURI(), "http://purl.org/net/sword/error/ErrorBadRequest"); + } + */ + } - def test_38_error_mediation_not_allowed(self): - # this is a placeholder; it's not possible to reliably test for this - pass + @Test + public void errorTargetOwnerUnknown() + throws Exception + { + SWORDClient client = new SWORDClient(new ClientConfiguration()); + ServiceDocument sd = client.getServiceDocument(this.sdIRI, new AuthCredentials(this.user, this.pass, this.obo)); + SWORDCollection col = sd.getWorkspaces().get(0).getCollections().get(0); - def test_39_error_method_not_allowed(self): - # this is a placeholder; it's not possible to reliably test for this - pass + Deposit deposit = new Deposit(); + deposit.setFile(new FileInputStream(this.file)); + deposit.setMimeType("application/zip"); + deposit.setFilename("example.zip"); + deposit.setPackaging(UriRegistry.PACKAGE_SIMPLE_ZIP); + deposit.setMd5(this.fileMd5); + deposit.setInProgress(true); - def test_40_error_max_upload_size_exceeded(self): - # this is a placeholder; it's not possible to reliably test for this - pass + try + { + // using an invalid OBO user + DepositReceipt receipt = client.deposit(col, deposit, new AuthCredentials(this.user, this.pass, "richard")); + assertTrue(false); + } + catch (SWORDError e) + { + assertEquals(e.getStatus(), 403); + assertEquals(e.getErrorURI(), "http://purl.org/net/sword/error/TargetOwnerUnknown"); + } + } - def test_41_get_service_document_unauthorised(self): - conn = Connection(SSS_URL, user_name="alsdkfjsdz", user_pass="ZAKJKLASJDF") - conn.get_service_document() - assert conn.sd is None - */ + @Test + public void errorMediatonNotAllowed() + throws Exception + { + // FIXME: not possible to reliably test for this + + /* + SWORDClient client = new SWORDClient(new ClientConfiguration()); + ServiceDocument sd = client.getServiceDocument(this.sdIRI, new AuthCredentials(this.user, this.pass, this.obo)); + SWORDCollection col = sd.getWorkspaces().get(0).getCollections().get(0); + + Deposit deposit = new Deposit(); + deposit.setFile(new FileInputStream(this.file)); + deposit.setMimeType("application/zip"); + deposit.setFilename("example.zip"); + deposit.setPackaging(UriRegistry.PACKAGE_SIMPLE_ZIP); + deposit.setMd5(this.fileMd5); + deposit.setInProgress(true); + + try + { + // using an invalid OBO user + DepositReceipt receipt = client.deposit(col, deposit, new AuthCredentials(this.user, this.pass, "richard")); + assertTrue(false); + } + catch (SWORDError e) + { + assertEquals(e.getStatus(), 403); + assertEquals(e.getErrorURI(), "http://purl.org/net/sword/error/TargetOwnerUnknown"); + } + */ + } + + @Test + public void errorMethodNotAllowed() + throws Exception + { + // FIXME: not possible to reliably test for this + + /* + SWORDClient client = new SWORDClient(new ClientConfiguration()); + ServiceDocument sd = client.getServiceDocument(this.sdIRI, new AuthCredentials(this.user, this.pass, this.obo)); + SWORDCollection col = sd.getWorkspaces().get(0).getCollections().get(0); + + Deposit deposit = new Deposit(); + deposit.setFile(new FileInputStream(this.file)); + deposit.setMimeType("application/zip"); + deposit.setFilename("example.zip"); + deposit.setPackaging(UriRegistry.PACKAGE_SIMPLE_ZIP); + deposit.setMd5(this.fileMd5); + deposit.setInProgress(true); + + try + { + // using an invalid OBO user + DepositReceipt receipt = client.deposit(col, deposit, new AuthCredentials(this.user, this.pass, "richard")); + assertTrue(false); + } + catch (SWORDError e) + { + assertEquals(e.getStatus(), 403); + assertEquals(e.getErrorURI(), "http://purl.org/net/sword/error/TargetOwnerUnknown"); + } + */ + } + + @Test + public void errorMaxUploadSizeExceeded() + throws Exception + { + // FIXME: not possible to reliably test for this + + /* + SWORDClient client = new SWORDClient(new ClientConfiguration()); + ServiceDocument sd = client.getServiceDocument(this.sdIRI, new AuthCredentials(this.user, this.pass, this.obo)); + SWORDCollection col = sd.getWorkspaces().get(0).getCollections().get(0); + + Deposit deposit = new Deposit(); + deposit.setFile(new FileInputStream(this.file)); + deposit.setMimeType("application/zip"); + deposit.setFilename("example.zip"); + deposit.setPackaging(UriRegistry.PACKAGE_SIMPLE_ZIP); + deposit.setMd5(this.fileMd5); + deposit.setInProgress(true); + + try + { + // using an invalid OBO user + DepositReceipt receipt = client.deposit(col, deposit, new AuthCredentials(this.user, this.pass, "richard")); + assertTrue(false); + } + catch (SWORDError e) + { + assertEquals(e.getStatus(), 403); + assertEquals(e.getErrorURI(), "http://purl.org/net/sword/error/TargetOwnerUnknown"); + } + */ + } + + @Test + public void getServiceDocumentUnauthorised() + throws Exception + { + SWORDClient client = new SWORDClient(new ClientConfiguration()); + ServiceDocument sd = client.getServiceDocument(this.sdIRI, new AuthCredentials("adfasf", "awerwevwe")); + assertNull(sd); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |