def test_42_dip_deposit_sss(self): # deposit as single operation dipdir = self.create_populate_tst_dip("testdip") self.assertTrue(os.path.isdir(dipdir)) # Configure endpoint argvconfig = ( [ "dip", "config" , "--collection_uri=%s"%(SSS.collection_uri) , "--servicedoc_uri=%s"%(SSS.servicedoc_uri) , "--username=%s"%(SSS.username) , "--password=%s"%(SSS.password) ]) outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvconfig) self.assertEqual(status, diperrors.DIP_SUCCESS) # Create package and deposit argvdeposit = ( [ "dip", "deposit", "--dip", "testdip" , "--collection_uri=%s"%(SSS.collection_uri) ]) outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvdeposit) result = outstr.getvalue() log.info(result) self.assertEqual(status, diperrors.DIP_SUCCESS) self.assertRegexpMatches(result, r'^token=.*$') return
def test_20_dip_add_file_single(self): # create dipdir = self.create_tst_dip("testdip") self.assertTrue(os.path.isdir(dipdir)) # add file1path = self.fpath("files/file1.txt") argv = ["dip", "add-files", file1path] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() # print("***** add-files *****\n%s*****"%result) self.assertIn("Adding files to deposit information package at %s ..."%(dipdir), result) self.assertIn(" %s"%file1path, result) # self.assertIn("Done.", result) # show argv = ["dip", "show"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() # print("***** show *****\n%s*****"%result) self.assertIn("Files:", result) self.assertIn(" %s"%file1path, result) return
def test_30_dip_add_show_attributes(self): # create dipdir = self.create_tst_dip("testdip") self.assertTrue(os.path.isdir(dipdir)) # Test attributes argvshow = ( [ "dip", "show-attributes" , "dc:creator" , "dc:created" , "dc:title" , "dc:identifier" ]) outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvshow) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() self.assertNotIn("dc:creator", result) self.assertNotIn("dc:created", result) self.assertNotIn("dc:title", result) self.assertNotIn("dc:identifier", result) # Add attributes argvset = ( [ "dip", "add-attributes" , "dc:creator=John Smith", "dc:creator=Tom Jones" , "dc:created=2014-01-01" , '''dc:title="Smith and Jones' package"''' , "dc:identifier=testdip/123456789" ]) outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvset) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() self.assertIn("Adding attributes to deposit information package at %s"%dipdir, result) self.assertIn(''' dc:creator="John Smith"''', result) self.assertIn(''' dc:creator="Tom Jones"''', result) self.assertIn(''' dc:created="2014-01-01"''', result) self.assertIn(''' dc:title="Smith and Jones' package"''', result) self.assertIn(''' dc:identifier="testdip/123456789"''', result) # Test attributes again outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvshow) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() self.assertIn('''dc:creator="John Smith"''', result) self.assertIn('''dc:creator="Tom Jones"''', result) self.assertIn('''dc:created="2014-01-01"''', result) self.assertIn('''dc:title="Smith and Jones' package"''', result) self.assertIn('''dc:identifier="testdip/123456789"''', result) return
def test_44_dip_deposit_sss_status(self): # deposit as single operation dipdir = self.create_populate_tst_dip("testdip") self.assertTrue(os.path.isdir(dipdir)) # Configure endpoint argvconfig = ( [ "dip", "config" , "--collection_uri=%s"%(SSS.collection_uri) , "--servicedoc_uri=%s"%(SSS.servicedoc_uri) , "--username=%s"%(SSS.username) , "--password=%s"%(SSS.password) ]) outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvconfig) self.assertEqual(status, diperrors.DIP_SUCCESS) # Create package and deposit argvdeposit = ( [ "dip", "deposit", "--dip", "testdip" , "--collection_uri=%s"%(SSS.collection_uri) ]) outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvdeposit) result = outstr.getvalue() log.info(result) self.assertEqual(status, diperrors.DIP_SUCCESS) # Get status of deposit result = result.splitlines()[0] matchresult = re.match(r'token=(.*)$', result) self.assertIsNotNone(matchresult) token = matchresult.group(1) argvstatus = ( [ "dip", "status", "--dip", "testdip" , "--collection_uri=%s"%(SSS.collection_uri) , "--token=%s"%(token) ]) outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvstatus) result = outstr.getvalue() result = result.splitlines()[0] log.info(result) self.assertEqual(status, diperrors.DIP_SUCCESS) pkguri = "http://localhost:8080/cont-uri/%s/%s"%(SSS_COLL_ID, token) self.assertEqual(result, pkguri) return
def test_24_dip_remove_file_multiple(self): # create dipdir = self.create_populate_tst_dip("testdip") self.assertTrue(os.path.isdir(dipdir)) # Check files present self.assertFilesInDip( filespresent=[ "files/file1.txt", "files/file2.txt", "files/sub1/sub11.txt", "files/sub1/sub12.txt" ] ) # Remove file argv = ["dip", "remove-files", "files/file1.txt", "files/file2.txt", "files/sub1"] outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() self.assertIn("Removing files from deposit information package at %s"%dipdir, result) self.assertIn(" %s"%self.fpath("files/file1.txt"), result) self.assertIn(" %s"%self.fpath("files/file2.txt"), result) self.assertIn(" %s"%self.fpath("files/sub1/sub11.txt"), result) self.assertIn(" %s"%self.fpath("files/sub1/sub12.txt"), result) # Check files present again self.assertFilesInDip( filesabsent=[ "files/file1.txt", "files/file2.txt", "files/sub1/sub11.txt", "files/sub1/sub12.txt" ] ) return
def create_populate_tst_dip(self, dipref="testdip"): dipdir = self.create_tst_dip(dipref=dipref) filespath = self.fpath("files") argv = ["dip", "--recursive", "add-files", filespath] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) return dipdir
def create_tst_dip(self, dipref="testdip"): dipdir = self.dpath(dipref) # create argv = ["dip", "create", "--dip", dipref] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) return dipdir
def test_12_dip_use(self): dipdir = os.path.join(self._dipdir, "testdip") argv = ["dip", "use", "--dip", "testdip"] outstr = StringIO.StringIO() with SwitchStdout(outstr): with SwitchStderr(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_NOTEXISTS) self.assertEqual( outstr.getvalue(), "Specified directory does not exist: %s\n"%(dipdir) ) # create directory but no content os.makedirs(dipdir) outstr = StringIO.StringIO() with SwitchStdout(outstr): with SwitchStderr(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_NODIPHERE) self.assertEqual( outstr.getvalue(), "Specified directory does not contain a deposit information package: %s\n"%(dipdir) ) shutil.rmtree(dipdir) # create newdir = self.create_tst_dip("testdip") self.assertEqual(newdir, dipdir) # again... argv = ["dip", "use", "--dip", "testdip"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) self.assertEqual(outstr.getvalue(), "%s\n"%(dipdir)) # again... argv = ["dip", "use"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) self.assertEqual(outstr.getvalue(), "%s\n"%(dipdir)) return
def test_01_dip_config_dipdir(self): dipdir = os.path.join(self._dipdir, "testdip") argv = ["dip", "config", "--dip", "testdip"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) self.assertEqual(dip_get_default_dir(self._cnfdir), self._dipdir+"/testdip") result = outstr.getvalue() self.assertIn('''dipdir="%s"'''%(dipdir), result) self.assertIn('''dipbase="%s"'''%(self._dipdir), result) # Show config argv = ["dip", "config"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) self.assertEqual(dip_get_default_dir(self._cnfdir), self._dipdir+"/testdip") result = outstr.getvalue() self.assertIn('''dipdir="%s"'''%(dipdir), result) self.assertIn('''dipbase="%s"'''%(self._dipdir), result) return
def test_14_dip_remove(self): # create dipdir = self.create_tst_dip("testdip") self.assertTrue(os.path.isdir(dipdir)) # show argv = ["dip", "remove", "--dip", "testdip"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) # confirm removed self.assertEqual(status, diperrors.DIP_SUCCESS) self.assertFalse(os.path.isdir(dipdir)) return
def test_02_dip_config_sword_collection(self): dipdir = os.path.join(self._dipdir, "testdip") argv = ( [ "dip", "config" , '''--collection_uri="%s"'''%DSS.collection_uri , '''--servicedoc_uri="%s"'''%DSS.servicedoc_uri , '''--username="******"'''%DSS.username , '''--password="******"'''%DSS.password ]) outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() # print("========\n%s\n========"%result) self.assertIn('''collection_uri="%s"'''%(DSS.collection_uri), result) self.assertIn('''servicedoc_uri="%s"'''%(DSS.servicedoc_uri), result) self.assertIn('''username="******"'''%(DSS.username), result) self.assertIn('''password="******"'''%("*"*len(DSS.password)), result) self.assertNotIn('''dipdir="%s"'''%(dipdir), result) self.assertNotIn('''dipbase="%s"'''%(self._dipdir), result) # Show config argv = ["dip", "config"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) self.assertEqual(dip_get_default_dir(self._cnfdir), self._dipdir+"/testdip") result = outstr.getvalue() # print("========\n%s\n========"%result) self.assertIn('''collection_uri="%s"'''%(DSS.collection_uri), result) self.assertIn('''servicedoc_uri="%s"'''%(DSS.servicedoc_uri), result) self.assertIn('''username="******"'''%(DSS.username), result) self.assertIn('''password="******"'''%("*"*len(DSS.password)), result) # self.assertNotIn('''dipdir="%s"'''%(dipdir), result) # self.assertNotIn('''dipbase="%s"'''%(self._dipdir), result) return
def test_11_dip_create(self): dipdir = os.path.join(self._dipdir, "testdip") argv = ["dip", "create", "--dip", "testdip"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) self.assertTrue(os.path.isdir(dipdir)) self.assertEqual(dip_get_default_dir(self._cnfdir), self._dipdir+"/testdip") self.assertEqual( outstr.getvalue(), "Created deposit information package at %s\n"%(dipdir) ) # again... outstr = StringIO.StringIO() with SwitchStdout(outstr): with SwitchStderr(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_EXISTS) self.assertEqual( outstr.getvalue(), "Specified directory already exists: %s\n"%(dipdir) ) return
def assertFilesInDip(self, filespresent=[], filesabsent=[]): argv = ["dip", "show"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() # print("***** files in DIP *****\n%s*****"%result) for f in filespresent: p = self.fpath(f) self.assertIn(" %s"%p, result) for f in filesabsent: p = self.fpath(f) self.assertNotIn(" %s"%p, result) return
def test_13_dip_show(self): # create dipdir = self.create_tst_dip("testdip") # show argv = ["dip", "show"] outstr = StringIO.StringIO() with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argv) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() self.assertIn("Deposit information package at %s"%dipdir, result) self.assertIn("Files:", result) self.assertIn("Metadata files:", result) self.assertIn("%s/metadata/dcterms.xml (dcterms)"%dipdir, result) self.assertIn("Dublin Core:", result) self.assertIn("Endpoints:", result) return
def test_40_dip_package(self): # create dipdir = self.create_populate_tst_dip("testdip") self.assertTrue(os.path.isdir(dipdir)) # Create package argvpackage = ( [ "dip", "package", "--dip", "testdip" ]) outstr = StringIO.StringIO() with ChangeCurrentDir(BASE_DIR): with SwitchStdout(outstr): status = runCommand(self._cnfdir, self._dipdir, argvpackage) self.assertEqual(status, diperrors.DIP_SUCCESS) result = outstr.getvalue() # Check filename response retext = "("+self._dipdir+r'/testdip/packages/.*/SimpleZip.zip'+")" pathre = re.compile(retext) log.info("Path result string: %s"%(result)) log.info("Path search pattern: %s"%(retext)) pathmatch = pathre.search(result) self.assertIsNotNone(pathmatch) pathtext = pathmatch.group(1) # Check zip package contents z = zipfile.ZipFile(pathtext) ziplist = z.namelist() self.assertEqual(len(ziplist), 9) self.assertIn("dcterms.xml", ziplist) self.assertIn("files/file1.txt", ziplist) self.assertIn("files/file2.txt", ziplist) self.assertIn("files/sub1/sub11.txt", ziplist) self.assertIn("files/sub1/sub12.txt", ziplist) self.assertIn("files/sub2/sub21.txt", ziplist) self.assertIn("files/sub2/sub22.txt", ziplist) self.assertIn("files/sub3/sub31/sub311.txt", ziplist) self.assertIn("files/sub3/sub33/sub331.txt", ziplist) # self.assertIn("file1.txt", ziplist) # self.assertIn("file2.txt", ziplist) # self.assertIn("sub11.txt", ziplist) # self.assertIn("sub12.txt", ziplist) # self.assertIn("sub21.txt", ziplist) # self.assertIn("sub22.txt", ziplist) # self.assertIn("sub311.txt", ziplist) # self.assertIn("sub331.txt", ziplist) return