def test_versioning(self):
        # no contained resource
        self.assertEqual(self.resCollection.resources.count(), 0)

        # add 3 resources to collection
        self.resCollection.resources.add(self.resGen1)
        self.resCollection.resources.add(self.resGeoFeature)
        self.resCollection.resources.add(self.resCollection_with_missing_metadata)
        self.assertEqual(self.resCollection.resources.count(), 3)

        # make a new version of collection
        new_collection = create_empty_resource(self.resCollection.short_id, self.user1)

        new_collection = create_new_version_resource(self.resCollection, new_collection, self.user1)

        # test the new version is a collection
        self.assertTrue(isinstance(new_collection, CollectionResource))

        # new version collection should have same contained res as its original does
        self.assertEqual(new_collection.resources.count(), self.resCollection.resources.count())
        for contained_res in new_collection.resources.all():
            self.assertIn(contained_res, self.resCollection.resources.all())

        # changes to old version collection should not affect new version collection
        self.resCollection.resources.clear()
        self.assertEqual(self.resCollection.resources.count(), 0)
        self.assertEqual(new_collection.resources.count(), 3)
Esempio n. 2
0
    def setUp(self):
        super(TestHideOldVersions, self).setUp()
        self.group, _ = Group.objects.get_or_create(name='Hydroshare Author')

        # create a user who is the owner of the resource to be versioned
        self.owner = hydroshare.create_account('*****@*****.**',
                                               username='******',
                                               first_name='owner_firstname',
                                               last_name='owner_lastname',
                                               superuser=False,
                                               groups=[])
        # create a generic resource
        self.version0 = hydroshare.create_resource(
            resource_type='CompositeResource',
            owner=self.owner,
            title='Test Composite Resource')
        test_file1 = open('test1.txt', 'w')
        test_file1.write("Test text file in test1.txt")
        test_file1.close()
        test_file2 = open('test2.txt', 'w')
        test_file2.write("Test text file in test2.txt")
        test_file2.close()
        self.test_file1 = open('test1.txt', 'rb')
        self.test_file2 = open('test2.txt', 'rb')
        hydroshare.add_resource_files(self.version0.short_id, self.test_file1,
                                      self.test_file2)

        # make one version
        self.version1 = hydroshare.create_empty_resource(
            self.version0.short_id, self.owner)
        self.version1 = hydroshare.create_new_version_resource(
            self.version0, self.version1, self.owner)

        # and then make a version of that
        self.version2 = hydroshare.create_empty_resource(
            self.version1.short_id, self.owner)
        self.version2 = hydroshare.create_new_version_resource(
            self.version1, self.version2, self.owner)
Esempio n. 3
0
    def test_new_version_generic_resource(self):
        # test to make sure only owners can version a resource
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.CHANGE)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        # add key/value metadata to original resource
        self.res_generic.extra_metadata = {'variable': 'temp', 'units': 'deg F'}
        self.res_generic.save()

        new_res_generic = hydroshare.create_empty_resource(self.res_generic.short_id,
                                                           self.owner)
        # test to make sure the new versioned empty resource has no content files
        self.assertEqual(new_res_generic.files.all().count(), 0)

        new_res_generic = hydroshare.create_new_version_resource(self.res_generic, new_res_generic,
                                                                 self.owner)

        # test the new versioned resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_generic, GenericResource))

        # test the new versioned resource has the correct content file with correct path copied over
        self.assertEqual(new_res_generic.files.all().count(), 2)
        # add each file of resource to list
        new_res_file_list = []
        for f in new_res_generic.files.all():
            new_res_file_list.append(f.resource_file.name)
        for f in self.res_generic.files.all():
            ori_res_no_id_file_path = f.resource_file.name[len(self.res_generic.short_id):]
            new_res_file_path = new_res_generic.short_id + ori_res_no_id_file_path
            self.assertIn(new_res_file_path, new_res_file_list,
                          msg='resource content path is not created correctly '
                              'for new versioned resource')

        # test key/value metadata copied over
        self.assertEqual(new_res_generic.extra_metadata, self.res_generic.extra_metadata)
        # test science metadata elements are copied from the original resource to the new versioned
        # resource
        self.assertEqual(new_res_generic.metadata.title.value,
                         self.res_generic.metadata.title.value,
                         msg='metadata title is not copied over to the new versioned resource')
        self.assertEqual(new_res_generic.creator, self.owner,
                         msg='creator is not copied over to the new versioned resource')

        # test to make sure a new unique identifier has been created for the new versioned resource
        self.assertIsNotNone(
            new_res_generic.short_id,
            msg='Unique identifier has not been created for new versioned resource.')
        self.assertNotEqual(new_res_generic.short_id, self.res_generic.short_id)

        # test to make sure the new versioned resource has the correct identifier
        self.assertEqual(new_res_generic.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_generic.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new versioned resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_generic.short_id)
        self.assertIn(id_url, [id.url for id in new_res_generic.metadata.identifiers.all()],
                      msg="Identifier url was not found for new versioned resource.")

        # test to make sure the new versioned resource is linked with the original resource via
        # isReplacedBy and isVersionOf metadata elements
        self.assertGreater(new_res_generic.metadata.relations.all().count(), 0,
                           msg="New versioned resource does has relation element.")
        self.assertIn('isVersionOf', [rel.type for rel in new_res_generic.metadata.relations.all()],
                      msg="No relation element of type 'isVersionOf' for new versioned resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                self.res_generic.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in new_res_generic.metadata.relations.all()],
                      msg="The original resource identifier is not set as value for isVersionOf "
                          "for new versioned resource.")
        self.assertIn('isReplacedBy',
                      [rel.type for rel in self.res_generic.metadata.relations.all()],
                      msg="No relation element of type 'isReplacedBy' for the original resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                new_res_generic.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in self.res_generic.metadata.relations.all()],
                      msg="The new versioned resource identifier is not set as value for "
                          "isReplacedBy for original resource.")

        # test isReplacedBy is removed after the new versioned resource is deleted
        hydroshare.delete_resource(new_res_generic.short_id)
        self.assertNotIn('isReplacedBy',
                         [rel.type for rel in self.res_generic.metadata.relations.all()],
                         msg="isReplacedBy is not removed from the original resource after "
                             "its versioned resource is deleted")
        # delete the original resource to make sure iRODS files are cleaned up
        hydroshare.delete_resource(self.res_generic.short_id)
Esempio n. 4
0
    def test_new_version_raster_resource(self):
        # test to make sure only owners can version a resource
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.CHANGE)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        new_res_raster = hydroshare.create_empty_resource(self.res_raster.short_id,
                                                          self.owner)
        new_res_raster = hydroshare.create_new_version_resource(self.res_raster, new_res_raster,
                                                                self.owner)

        # test the new versioned resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_raster, RasterResource))

        # test science metadata elements are copied from the original resource to the new versioned
        # resource
        self.assertTrue(new_res_raster.metadata.title.value == self.res_raster.metadata.title.value)
        self.assertTrue(new_res_raster.creator == self.owner)

        # test extended metadata elements are copied from the original resource to the new
        # versioned resource
        self.assertTrue(OriginalCoverage.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        self.assertEqual(new_res_raster.metadata.originalCoverage.value,
                         self.res_raster.metadata.originalCoverage.value,
                         msg="OriginalCoverage of new versioned resource is not equal to "
                             "that of the original resource")

        self.assertTrue(CellInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newcell = new_res_raster.metadata.cellInformation
        oldcell = self.res_raster.metadata.cellInformation
        self.assertEqual(newcell.rows, oldcell.rows,
                         msg="Rows of new versioned resource is not equal to that of "
                             "the original resource")
        self.assertEqual(newcell.columns, oldcell.columns,
                         msg="Columns of new versioned resource is not equal to that of the "
                             "original resource")
        self.assertEqual(newcell.cellSizeXValue, oldcell.cellSizeXValue,
                         msg="CellSizeXValue of new versioned resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellSizeYValue, oldcell.cellSizeYValue,
                         msg="CellSizeYValue of new versioned resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellDataType, oldcell.cellDataType,
                         msg="CellDataType of new versioned resource is not equal to "
                             "that of the original resource")

        self.assertTrue(BandInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newband = new_res_raster.metadata.bandInformations.first()
        oldband = self.res_raster.metadata.bandInformations.first()
        self.assertEqual(newband.name, oldband.name,
                         msg="Band name of new versioned resource is not equal to that of "
                             "the original resource")

        # test to make sure a new unique identifier has been created for the new versioned resource
        self.assertIsNotNone(new_res_raster.short_id, msg='Unique identifier has not been '
                                                          'created for new versioned resource.')
        self.assertNotEqual(new_res_raster.short_id, self.res_raster.short_id)

        # test to make sure the new versioned resource has 2 content file
        # since an additional vrt file is created
        self.assertEqual(new_res_raster.files.all().count(), 2)

        # test to make sure the new versioned resource has the correct identifier
        self.assertEqual(new_res_raster.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_raster.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new versioned resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_raster.short_id)
        self.assertIn(id_url, [id.url for id in new_res_raster.metadata.identifiers.all()],
                      msg="Identifier url was not found for new versioned resource.")

        # test to make sure the new versioned resource is linked with the original resource via
        # isReplacedBy and isVersionOf metadata elements
        self.assertGreater(new_res_raster.metadata.relations.all().count(), 0,
                           msg="New versioned resource does has relation element.")
        self.assertIn('isVersionOf', [rel.type for rel in new_res_raster.metadata.relations.all()],
                      msg="No relation element of type 'isVersionOf' for new versioned resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                self.res_raster.short_id)
        self.assertIn(version_value, [rel.value for rel in new_res_raster.metadata.relations.all()],
                      msg="The original resource identifier is not set as value "
                          "for isVersionOf for new versioned resource.")
        self.assertIn('isReplacedBy',
                      [rel.type for rel in self.res_raster.metadata.relations.all()],
                      msg="No relation element of type 'isReplacedBy' for the original resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                new_res_raster.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in self.res_raster.metadata.relations.all()],
                      msg="The new versioned resource identifier is not set as value "
                          "for isReplacedBy for original resource.")

        # test isReplacedBy is removed after the new versioned resource is deleted
        hydroshare.delete_resource(new_res_raster.short_id)
        self.assertNotIn('isReplacedBy',
                         [rel.type for rel in self.res_raster.metadata.relations.all()],
                         msg="isReplacedBy is not removed from the original resource "
                             "after its versioned resource is deleted")
        # delete the original resource to make sure iRODS files are cleaned up
        hydroshare.delete_resource(self.res_raster.short_id)
Esempio n. 5
0
    def test_resource_operations_in_user_zone(self):
        # only do federation testing when REMOTE_USE_IRODS is True and irods docker containers
        # are set up properly
        if not super(TestUserZoneIRODSFederation,
                     self).is_federated_irods_available():
            return
        # test resource creation and "move" option in federated user zone
        fed_test_file_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_to_be_deleted)
        res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in User Zone',
            source_names=[fed_test_file_full_path],
            move=True)

        self.assertEqual(res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        user_path = '/{zone}/home/testuser/'.format(
            zone=settings.HS_USER_IRODS_ZONE)
        self.assertEqual(res.resource_federation_path, fed_path)
        # test original file in user test zone is removed after resource creation
        # since True is used for move when creating the resource
        self.assertFalse(
            self.irods_storage.exists(user_path + self.file_to_be_deleted))

        # test resource file deletion
        res.files.all().delete()
        self.assertEqual(res.files.all().count(),
                         0,
                         msg="Number of content files is not equal to 0")

        # test add multiple files and 'copy' option in federated user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_test_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        hydroshare.add_resource_files(
            res.short_id,
            source_names=[fed_test_file1_full_path, fed_test_file2_full_path],
            move=False)
        # test resource has two files
        self.assertEqual(res.files.all().count(),
                         2,
                         msg="Number of content files is not equal to 2")

        file_list = []
        for f in res.files.all():
            file_list.append(f.storage_path.split('/')[-1])
        self.assertTrue(
            self.file_one in file_list,
            msg='file 1 has not been added in the resource in user zone')
        self.assertTrue(
            self.file_two in file_list,
            msg='file 2 has not been added in the resource in user zone')
        # test original two files in user test zone still exist after adding them to the resource
        # since False  is used for move when creating the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
        self.assertTrue(self.irods_storage.exists(user_path + self.file_two))

        # test resource deletion
        resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(),
                          0,
                          msg='Number of resources not equal to 0')

        # test create new version resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False)
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id, self.user)
        new_res = hydroshare.create_new_version_resource(
            ori_res, new_res, self.user)
        # only need to test file-related attributes
        # ensure new versioned resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path,
                         new_res.resource_federation_path)
        # ensure new versioned resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(),
                         new_res.files.all().count())
        # delete resources to clean up
        resource.delete_resource(new_res.short_id)
        resource.delete_resource(ori_res.short_id)

        # test copy resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False)
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id,
                                                   self.user,
                                                   action='copy')
        new_res = hydroshare.copy_resource(ori_res, new_res)
        # only need to test file-related attributes
        # ensure new copied resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path,
                         new_res.resource_federation_path)
        # ensure new copied resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(),
                         new_res.files.all().count())
        # delete resources to clean up
        resource.delete_resource(new_res.short_id)
        resource.delete_resource(ori_res.short_id)

        # test folder operations in user zone
        fed_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        fed_file3_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_three)
        self.res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[
                fed_file1_full_path, fed_file2_full_path, fed_file3_full_path
            ],
            move=False)
        # make sure self.res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(self.res.resource_federation_path, fed_path)
        # resource should has only three files at this point
        self.assertEqual(self.res.files.all().count(),
                         3,
                         msg="resource file count didn't match")

        self.file_name_list = [self.file_one, self.file_two, self.file_three]
        super(TestUserZoneIRODSFederation, self).resource_file_oprs()

        # delete resources to clean up
        resource.delete_resource(self.res.short_id)

        # test adding files from federated user zone to an empty resource
        # created in hydroshare zone
        res = resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in HydroShare Zone')
        self.assertEqual(res.files.all().count(),
                         0,
                         msg="Number of content files is not equal to 0")
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        hydroshare.add_resource_files(res.short_id,
                                      source_names=[fed_test_file1_full_path],
                                      move=False)
        # test resource has one file
        self.assertEqual(res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        file_list = []
        for f in res.files.all():
            file_list.append(os.path.basename(f.storage_path))
        self.assertTrue(
            self.file_one in file_list,
            msg='file 1 has not been added in the resource in hydroshare zone')
        # test original file in user test zone still exist after adding it to the resource
        # since 'copy' is used for fed_copy_or_move when adding the file to the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))

        # test replication of this resource to user zone
        hydroshare.replicate_resource_bag_to_user_zone(self.user, res.short_id)
        self.assertTrue(self.irods_storage.exists(user_path + res.short_id +
                                                  '.zip'),
                        msg='replicated resource bag is not in the user zone')

        # test resource deletion
        resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(),
                          0,
                          msg='Number of resources not equal to 0')
        # test to make sure original file still exist after resource deletion
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
    def test_new_version_generic_resource(self):
        # test to make sure only owners can version a resource
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.CHANGE)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_generic, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_generic.short_id, self.nonowner)

        # add key/value metadata to original resource
        self.res_generic.extra_metadata = {'variable': 'temp', 'units': 'deg F'}
        self.res_generic.save()

        # print("res_generic.files are:")
        # for f in self.res_generic.files.all():
        #     print(f.storage_path)

        new_res_generic = hydroshare.create_empty_resource(self.res_generic.short_id,
                                                           self.owner)
        # test to make sure the new versioned empty resource has no content files
        self.assertEqual(new_res_generic.files.all().count(), 0)

        new_res_generic = hydroshare.create_new_version_resource(self.res_generic, new_res_generic,
                                                                 self.owner)

        # test the new versioned resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_generic, GenericResource))

        # test the new versioned resource has the correct content file with correct path copied over

        # print("new_res_generic.files are:")
        # for f in new_res_generic.files.all():
        #     print(f.storage_path)

        self.assertEqual(new_res_generic.files.all().count(), 2)

        # add each file of resource to list
        new_res_file_list = []
        # TODO: revise for new file handling
        for f in new_res_generic.files.all():
            new_res_file_list.append(f.resource_file.name)
        for f in self.res_generic.files.all():
            ori_res_no_id_file_path = f.resource_file.name[len(self.res_generic.short_id):]
            new_res_file_path = new_res_generic.short_id + ori_res_no_id_file_path
            self.assertIn(new_res_file_path, new_res_file_list,
                          msg='resource content path is not created correctly '
                              'for new versioned resource')

        # test key/value metadata copied over
        self.assertEqual(new_res_generic.extra_metadata, self.res_generic.extra_metadata)
        # test science metadata elements are copied from the original resource to the new versioned
        # resource
        self.assertEqual(new_res_generic.metadata.title.value,
                         self.res_generic.metadata.title.value,
                         msg='metadata title is not copied over to the new versioned resource')
        self.assertEqual(new_res_generic.creator, self.owner,
                         msg='creator is not copied over to the new versioned resource')

        # test to make sure a new unique identifier has been created for the new versioned resource
        self.assertIsNotNone(
            new_res_generic.short_id,
            msg='Unique identifier has not been created for new versioned resource.')
        self.assertNotEqual(new_res_generic.short_id, self.res_generic.short_id)

        # test to make sure the new versioned resource has the correct identifier
        self.assertEqual(new_res_generic.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_generic.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new versioned resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_generic.short_id)
        self.assertIn(id_url, [id.url for id in new_res_generic.metadata.identifiers.all()],
                      msg="Identifier url was not found for new versioned resource.")

        # test to make sure the new versioned resource is linked with the original resource via
        # isReplacedBy and isVersionOf metadata elements
        self.assertGreater(new_res_generic.metadata.relations.all().count(), 0,
                           msg="New versioned resource does has relation element.")
        self.assertIn('isVersionOf', [rel.type for rel in new_res_generic.metadata.relations.all()],
                      msg="No relation element of type 'isVersionOf' for new versioned resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                self.res_generic.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in new_res_generic.metadata.relations.all()],
                      msg="The original resource identifier is not set as value for isVersionOf "
                          "for new versioned resource.")
        self.assertIn('isReplacedBy',
                      [rel.type for rel in self.res_generic.metadata.relations.all()],
                      msg="No relation element of type 'isReplacedBy' for the original resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                new_res_generic.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in self.res_generic.metadata.relations.all()],
                      msg="The new versioned resource identifier is not set as value for "
                          "isReplacedBy for original resource.")

        # test isReplacedBy is removed after the new versioned resource is deleted
        hydroshare.delete_resource(new_res_generic.short_id)
        self.assertNotIn('isReplacedBy',
                         [rel.type for rel in self.res_generic.metadata.relations.all()],
                         msg="isReplacedBy is not removed from the original resource after "
                             "its versioned resource is deleted")
        # delete the original resource to make sure iRODS files are cleaned up
        hydroshare.delete_resource(self.res_generic.short_id)
    def test_new_version_raster_resource(self):
        # test to make sure only owners can version a resource
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.CHANGE)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        self.owner.uaccess.share_resource_with_user(self.res_raster, self.nonowner,
                                                    PrivilegeCodes.VIEW)
        with self.assertRaises(PermissionDenied):
            hydroshare.create_empty_resource(self.res_raster.short_id, self.nonowner)

        new_res_raster = hydroshare.create_empty_resource(self.res_raster.short_id,
                                                          self.owner)
        new_res_raster = hydroshare.create_new_version_resource(self.res_raster, new_res_raster,
                                                                self.owner)

        # test the new versioned resource has the same resource type as the original resource
        self.assertTrue(isinstance(new_res_raster, RasterResource))

        # test science metadata elements are copied from the original resource to the new versioned
        # resource
        self.assertTrue(new_res_raster.metadata.title.value == self.res_raster.metadata.title.value)
        self.assertTrue(new_res_raster.creator == self.owner)

        # test extended metadata elements are copied from the original resource to the new
        # versioned resource
        self.assertTrue(OriginalCoverage.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        self.assertEqual(new_res_raster.metadata.originalCoverage.value,
                         self.res_raster.metadata.originalCoverage.value,
                         msg="OriginalCoverage of new versioned resource is not equal to "
                             "that of the original resource")

        self.assertTrue(CellInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newcell = new_res_raster.metadata.cellInformation
        oldcell = self.res_raster.metadata.cellInformation
        self.assertEqual(newcell.rows, oldcell.rows,
                         msg="Rows of new versioned resource is not equal to that of "
                             "the original resource")
        self.assertEqual(newcell.columns, oldcell.columns,
                         msg="Columns of new versioned resource is not equal to that of the "
                             "original resource")
        self.assertEqual(newcell.cellSizeXValue, oldcell.cellSizeXValue,
                         msg="CellSizeXValue of new versioned resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellSizeYValue, oldcell.cellSizeYValue,
                         msg="CellSizeYValue of new versioned resource is not equal to "
                             "that of the original resource")
        self.assertEqual(newcell.cellDataType, oldcell.cellDataType,
                         msg="CellDataType of new versioned resource is not equal to "
                             "that of the original resource")

        self.assertTrue(BandInformation.objects.filter(
            object_id=new_res_raster.metadata.id).exists())
        newband = new_res_raster.metadata.bandInformations.first()
        oldband = self.res_raster.metadata.bandInformations.first()
        self.assertEqual(newband.name, oldband.name,
                         msg="Band name of new versioned resource is not equal to that of "
                             "the original resource")

        # test to make sure a new unique identifier has been created for the new versioned resource
        self.assertIsNotNone(new_res_raster.short_id, msg='Unique identifier has not been '
                                                          'created for new versioned resource.')
        self.assertNotEqual(new_res_raster.short_id, self.res_raster.short_id)

        # test to make sure the new versioned resource has 2 content file
        # since an additional vrt file is created
        self.assertEqual(new_res_raster.files.all().count(), 2)

        # test to make sure the new versioned resource has the correct identifier
        self.assertEqual(new_res_raster.metadata.identifiers.all().count(), 1,
                         msg="Number of identifier elements not equal to 1.")
        self.assertIn('hydroShareIdentifier',
                      [id.name for id in new_res_raster.metadata.identifiers.all()],
                      msg="hydroShareIdentifier name was not found for new versioned resource.")
        id_url = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                         new_res_raster.short_id)
        self.assertIn(id_url, [id.url for id in new_res_raster.metadata.identifiers.all()],
                      msg="Identifier url was not found for new versioned resource.")

        # test to make sure the new versioned resource is linked with the original resource via
        # isReplacedBy and isVersionOf metadata elements
        self.assertGreater(new_res_raster.metadata.relations.all().count(), 0,
                           msg="New versioned resource does has relation element.")
        self.assertIn('isVersionOf', [rel.type for rel in new_res_raster.metadata.relations.all()],
                      msg="No relation element of type 'isVersionOf' for new versioned resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                self.res_raster.short_id)
        self.assertIn(version_value, [rel.value for rel in new_res_raster.metadata.relations.all()],
                      msg="The original resource identifier is not set as value "
                          "for isVersionOf for new versioned resource.")
        self.assertIn('isReplacedBy',
                      [rel.type for rel in self.res_raster.metadata.relations.all()],
                      msg="No relation element of type 'isReplacedBy' for the original resource")
        version_value = '{}/resource/{}'.format(hydroshare.utils.current_site_url(),
                                                new_res_raster.short_id)
        self.assertIn(version_value,
                      [rel.value for rel in self.res_raster.metadata.relations.all()],
                      msg="The new versioned resource identifier is not set as value "
                          "for isReplacedBy for original resource.")

        # test isReplacedBy is removed after the new versioned resource is deleted
        hydroshare.delete_resource(new_res_raster.short_id)
        self.assertNotIn('isReplacedBy',
                         [rel.type for rel in self.res_raster.metadata.relations.all()],
                         msg="isReplacedBy is not removed from the original resource "
                             "after its versioned resource is deleted")
        # delete the original resource to make sure iRODS files are cleaned up
        hydroshare.delete_resource(self.res_raster.short_id)
    def test_resource_operations_in_user_zone(self):
        super(TestUserZoneIRODSFederation,
              self).assert_federated_irods_available()
        # test resource creation and "move" option in federated user zone
        fed_test_file_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_to_be_deleted)
        res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in User Zone',
            source_names=[fed_test_file_full_path],
            move=True)

        self.assertEqual(res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        user_path = '/{zone}/home/testuser/'.format(
            zone=settings.HS_USER_IRODS_ZONE)
        self.assertEqual(res.resource_federation_path, fed_path)
        # test original file in user test zone is removed after resource creation
        # since True is used for move when creating the resource
        file_path_name = user_path + self.file_to_be_deleted
        self.assertFalse(self.irods_storage.exists(file_path_name))

        # test django_irods CopyFiles() with an iRODS resource name being passed in
        # as input parameter to verify the file gets copied to the pass-in iRODS resource
        istorage = res.get_irods_storage()
        src_path = os.path.join(res.root_path, 'data', 'contents',
                                self.file_to_be_deleted)
        dest_path = file_path_name
        istorage.copyFiles(src_path, dest_path,
                           settings.HS_IRODS_LOCAL_ZONE_DEF_RES)
        # assert file did get copied over
        self.assertTrue(self.irods_storage.exists(file_path_name))
        stdout = self.irods_storage.session.run("ils", None, "-l",
                                                file_path_name)[0].split()
        # assert copied file gets written to the iRODS resource being passed into copyFiles() call
        self.assertEqual(stdout[2], settings.HS_IRODS_LOCAL_ZONE_DEF_RES)

        # test resource file deletion
        res.files.all().delete()
        self.assertEqual(res.files.all().count(),
                         0,
                         msg="Number of content files is not equal to 0")

        # test add multiple files and 'copy' option in federated user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_test_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        hydroshare.add_resource_files(
            res.short_id,
            source_names=[fed_test_file1_full_path, fed_test_file2_full_path],
            move=False)
        # test resource has two files
        self.assertEqual(res.files.all().count(),
                         2,
                         msg="Number of content files is not equal to 2")

        file_list = []
        for f in res.files.all():
            file_list.append(f.storage_path.split('/')[-1])
        self.assertTrue(
            self.file_one in file_list,
            msg='file 1 has not been added in the resource in user zone')
        self.assertTrue(
            self.file_two in file_list,
            msg='file 2 has not been added in the resource in user zone')
        # test original two files in user test zone still exist after adding them to the resource
        # since False  is used for move when creating the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
        self.assertTrue(self.irods_storage.exists(user_path + self.file_two))

        # test resource deletion
        hydroshare.resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(),
                          0,
                          msg='Number of resources not equal to 0')

        # test create new version resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False)
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id, self.user)
        new_res = hydroshare.create_new_version_resource(
            ori_res, new_res, self.user)
        # only need to test file-related attributes
        # ensure new versioned resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path,
                         new_res.resource_federation_path)
        # ensure new versioned resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(),
                         new_res.files.all().count())
        # delete resources to clean up
        hydroshare.resource.delete_resource(new_res.short_id)
        hydroshare.resource.delete_resource(ori_res.short_id)

        # test copy resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False)
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id,
                                                   self.user,
                                                   action='copy')
        new_res = hydroshare.copy_resource(ori_res, new_res)
        # only need to test file-related attributes
        # ensure new copied resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path,
                         new_res.resource_federation_path)
        # ensure new copied resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(),
                         new_res.files.all().count())
        # delete resources to clean up
        hydroshare.resource.delete_resource(new_res.short_id)
        hydroshare.resource.delete_resource(ori_res.short_id)

        # test folder operations in user zone
        fed_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        fed_file3_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_three)
        self.res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[
                fed_file1_full_path, fed_file2_full_path, fed_file3_full_path
            ],
            move=False)
        # make sure self.res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(
            zone=settings.HS_USER_IRODS_ZONE,
            user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(self.res.resource_federation_path, fed_path)
        # resource should has only three files at this point
        self.assertEqual(self.res.files.all().count(),
                         3,
                         msg="resource file count didn't match")

        self.file_name_list = [self.file_one, self.file_two, self.file_three]
        super(TestUserZoneIRODSFederation, self).resource_file_oprs()

        # delete resources to clean up
        hydroshare.resource.delete_resource(self.res.short_id)

        # test adding files from federated user zone to an empty resource
        # created in hydroshare zone
        res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in HydroShare Zone')
        self.assertEqual(res.files.all().count(),
                         0,
                         msg="Number of content files is not equal to 0")
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        hydroshare.add_resource_files(res.short_id,
                                      source_names=[fed_test_file1_full_path],
                                      move=False)
        # test resource has one file
        self.assertEqual(res.files.all().count(),
                         1,
                         msg="Number of content files is not equal to 1")

        file_list = []
        for f in res.files.all():
            file_list.append(os.path.basename(f.storage_path))
        self.assertTrue(
            self.file_one in file_list,
            msg='file 1 has not been added in the resource in hydroshare zone')
        # test original file in user test zone still exist after adding it to the resource
        # since 'copy' is used for fed_copy_or_move when adding the file to the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))

        # test replication of this resource to user zone even if the bag_modified AVU for this
        # resource is wrongly set to False when the bag for this resource does not exist and
        # need to be recreated
        res.setAVU('bag_modified', 'false')
        hydroshare.resource.replicate_resource_bag_to_user_zone(
            self.user, res.short_id)
        self.assertTrue(self.irods_storage.exists(user_path + res.short_id +
                                                  '.zip'),
                        msg='replicated resource bag is not in the user zone')

        # test resource deletion
        hydroshare.resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(),
                          0,
                          msg='Number of resources not equal to 0')
        # test to make sure original file still exist after resource deletion
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
    def test_resource_operations_in_user_zone(self):
        super(TestUserZoneIRODSFederation, self).assert_federated_irods_available()
        # test resource creation and "move" option in federated user zone
        fed_test_file_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_to_be_deleted)
        res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in User Zone',
            source_names=[fed_test_file_full_path],
            move=True
        )

        self.assertEqual(res.files.all().count(), 1,
                         msg="Number of content files is not equal to 1")
        fed_path = '/{zone}/home/{user}'.format(zone=settings.HS_USER_IRODS_ZONE,
                                                user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        user_path = '/{zone}/home/testuser/'.format(zone=settings.HS_USER_IRODS_ZONE)
        self.assertEqual(res.resource_federation_path, fed_path)
        # test original file in user test zone is removed after resource creation
        # since True is used for move when creating the resource
        file_path_name = user_path + self.file_to_be_deleted
        self.assertFalse(self.irods_storage.exists(file_path_name))

        # test django_irods CopyFiles() with an iRODS resource name being passed in
        # as input parameter to verify the file gets copied to the pass-in iRODS resource
        istorage = res.get_irods_storage()
        src_path = os.path.join(res.root_path, 'data', 'contents', self.file_to_be_deleted)
        dest_path = file_path_name
        istorage.copyFiles(src_path, dest_path, settings.HS_IRODS_LOCAL_ZONE_DEF_RES)
        # assert file did get copied over
        self.assertTrue(self.irods_storage.exists(file_path_name))
        stdout = self.irods_storage.session.run("ils", None, "-l", file_path_name)[0].split()
        # assert copied file gets written to the iRODS resource being passed into copyFiles() call
        self.assertEqual(stdout[2], settings.HS_IRODS_LOCAL_ZONE_DEF_RES)

        # test resource file deletion
        res.files.all().delete()
        self.assertEqual(res.files.all().count(), 0,
                         msg="Number of content files is not equal to 0")

        # test add multiple files and 'copy' option in federated user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_test_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        hydroshare.add_resource_files(
            res.short_id,
            source_names=[fed_test_file1_full_path, fed_test_file2_full_path],
            move=False)
        # test resource has two files
        self.assertEqual(res.files.all().count(), 2,
                         msg="Number of content files is not equal to 2")

        file_list = []
        for f in res.files.all():
            file_list.append(f.storage_path.split('/')[-1])
        self.assertTrue(self.file_one in file_list,
                        msg='file 1 has not been added in the resource in user zone')
        self.assertTrue(self.file_two in file_list,
                        msg='file 2 has not been added in the resource in user zone')
        # test original two files in user test zone still exist after adding them to the resource
        # since False  is used for move when creating the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))
        self.assertTrue(self.irods_storage.exists(user_path + self.file_two))

        # test resource deletion
        hydroshare.resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(), 0,
                          msg='Number of resources not equal to 0')

        # test create new version resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False
        )
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(zone=settings.HS_USER_IRODS_ZONE,
                                                user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(), 1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id, self.user)
        new_res = hydroshare.create_new_version_resource(ori_res, new_res, self.user)
        # only need to test file-related attributes
        # ensure new versioned resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path, new_res.resource_federation_path)
        # ensure new versioned resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(), new_res.files.all().count())
        # delete resources to clean up
        hydroshare.resource.delete_resource(new_res.short_id)
        hydroshare.resource.delete_resource(ori_res.short_id)

        # test copy resource in user zone
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        ori_res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_test_file1_full_path],
            move=False
        )
        # make sure ori_res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(zone=settings.HS_USER_IRODS_ZONE,
                                                user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(ori_res.resource_federation_path, fed_path)
        self.assertEqual(ori_res.files.all().count(), 1,
                         msg="Number of content files is not equal to 1")

        new_res = hydroshare.create_empty_resource(ori_res.short_id, self.user, action='copy')
        new_res = hydroshare.copy_resource(ori_res, new_res)
        # only need to test file-related attributes
        # ensure new copied resource is created in the same federation zone as original resource
        self.assertEqual(ori_res.resource_federation_path, new_res.resource_federation_path)
        # ensure new copied resource has the same number of content files as original resource
        self.assertEqual(ori_res.files.all().count(), new_res.files.all().count())
        # delete resources to clean up
        hydroshare.resource.delete_resource(new_res.short_id)
        hydroshare.resource.delete_resource(ori_res.short_id)

        # test folder operations in user zone
        fed_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        fed_file2_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_two)
        fed_file3_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_three)
        self.res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Original Generic Resource in User Zone',
            source_names=[fed_file1_full_path, fed_file2_full_path, fed_file3_full_path],
            move=False
        )
        # make sure self.res is created in federated user zone
        fed_path = '/{zone}/home/{user}'.format(zone=settings.HS_USER_IRODS_ZONE,
                                                user=settings.HS_LOCAL_PROXY_USER_IN_FED_ZONE)
        self.assertEqual(self.res.resource_federation_path, fed_path)
        # resource should has only three files at this point
        self.assertEqual(self.res.files.all().count(), 3,
                         msg="resource file count didn't match")

        self.file_name_list = [self.file_one, self.file_two, self.file_three]
        super(TestUserZoneIRODSFederation, self).resource_file_oprs()

        # delete resources to clean up
        hydroshare.resource.delete_resource(self.res.short_id)

        # test adding files from federated user zone to an empty resource
        # created in hydroshare zone
        res = hydroshare.resource.create_resource(
            resource_type='GenericResource',
            owner=self.user,
            title='My Test Generic Resource in HydroShare Zone'
        )
        self.assertEqual(res.files.all().count(), 0,
                         msg="Number of content files is not equal to 0")
        fed_test_file1_full_path = '/{zone}/home/testuser/{fname}'.format(
            zone=settings.HS_USER_IRODS_ZONE, fname=self.file_one)
        hydroshare.add_resource_files(
            res.short_id,
            source_names=[fed_test_file1_full_path],
            move=False)
        # test resource has one file
        self.assertEqual(res.files.all().count(), 1,
                         msg="Number of content files is not equal to 1")

        file_list = []
        for f in res.files.all():
            file_list.append(os.path.basename(f.storage_path))
        self.assertTrue(self.file_one in file_list,
                        msg='file 1 has not been added in the resource in hydroshare zone')
        # test original file in user test zone still exist after adding it to the resource
        # since 'copy' is used for fed_copy_or_move when adding the file to the resource
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))

        # test replication of this resource to user zone even if the bag_modified AVU for this
        # resource is wrongly set to False when the bag for this resource does not exist and
        # need to be recreated
        res.setAVU('bag_modified', 'false')
        hydroshare.resource.replicate_resource_bag_to_user_zone(self.user, res.short_id)
        self.assertTrue(self.irods_storage.exists(user_path + res.short_id + '.zip'),
                        msg='replicated resource bag is not in the user zone')

        # test resource deletion
        hydroshare.resource.delete_resource(res.short_id)
        self.assertEquals(BaseResource.objects.all().count(), 0,
                          msg='Number of resources not equal to 0')
        # test to make sure original file still exist after resource deletion
        self.assertTrue(self.irods_storage.exists(user_path + self.file_one))