def test_list_records(self):
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)

        # Needed as can't throw exceptions in lambdas
        class AttributeErrorFake:
            def __init__(self):
                raise AttributeError("problem with the attribute")

        fake = flexmock(listRecords=AttributeErrorFake)
        flexmock(oaipmhclient).new_instances(fake)

        identify_fake1 = flexmock(
            baseURL=lambda: "http://127.0.0.1:9000/apps/oaimph")

        fake = flexmock(identify=lambda: identify_fake1,
                        baseURL=lambda: "%s/apps/oaipmh" % source)
        #                listRecords=lambda metadataPrefix: AttributeErrorFake)
        fake.should_receive('listRecords').and_raise(AttributeErrorFake)
        flexmock(oaipmhclient).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except OAIPMHError:
            pass
        else:
            self.assertTrue(False, "Expected OAIPMHError")
    def test_list_records(self):
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)

        # Needed as can't throw exceptions in lambdas
        class AttributeErrorFake:
            def __init__(self):
                raise AttributeError("problem with the attribute")

        fake = flexmock(listRecords=AttributeErrorFake)
        flexmock(oaipmhclient).new_instances(fake)

        identify_fake1 = flexmock(baseURL=lambda:
            "http://127.0.0.1:9000/apps/oaimph")

        fake = flexmock(identify=lambda: identify_fake1,
                        baseURL=lambda: "%s/apps/oaipmh" % source)
        #                listRecords=lambda metadataPrefix: AttributeErrorFake)
        fake.should_receive('listRecords').and_raise(AttributeErrorFake)
        flexmock(oaipmhclient).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except OAIPMHError:
            pass
        else:
            self.assertTrue(False, "Expected OAIPMHError")
    def test_mets_fails(self):
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)
        flexmock(tasks).should_receive("_registerExperimentDocument").and_raise(MetsParseError)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except MetsParseError:
            pass
        else:
            self.assertTrue(False, "Expected MetsParseError")
    def test_mets_fails(self):
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)
        flexmock(tasks).should_receive(
            "_registerExperimentDocument").and_raise(MetsParseError)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except MetsParseError:
            pass
        else:
            self.assertTrue(False, "Expected MetsParseError")
    def test_correct_run(self):
        """
        This is an initial test of a basic consumption of service
        """
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        local_ids = transfer_experiment(source)

        #TODO: pull experiment at local_id and compare to original exp

        user1 = User.objects.get(username="******")
        user2 = User.objects.get(username="******")
        print("local_ids=%s" % local_ids)
        exp = Experiment.objects.get(id=local_ids[0])
        self.assertEquals(exp.title, "test1")  # from METS file
        self.assertEquals(exp.description,
                          "this is the description audit message here")
        self.assertEquals(exp.created_by.username, user1.username)
        self.assertEquals(
            Author_Experiment.objects.filter(experiment=exp).count(), 2)
        from tardis.tardis_portal.models import ExperimentACL
        self.assertEquals(
            ExperimentACL.objects.filter(
                pluginId=django_user,
                experiment__id=exp.id,
                aclOwnershipType=ExperimentACL.OWNER_OWNED).count(), 2)

        self.assertTrue(len(local_ids) == 1)
        for local_id in local_ids:
            self.assertTrue(int(local_id) > 0)
    def test_read_public_experiment(self):
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)

        # Needed as can't throw exceptions in lambdas

        flexmock(tasks).should_receive('getURL').with_args(
            "%s/apps/reposproducer/expstate/%s/" % (source, self.exp.id)).and_raise(HTTPError("", "", "", "", None))

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except BadAccessError:
            pass
        else:
            self.assertTrue(False, "Expected BadAccessError")
    def test_correct_run(self):
        """
        This is an initial test of a basic consumption of service
        """
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        local_ids = transfer_experiment(source)

        #TODO: pull experiment at local_id and compare to original exp

        user1 = User.objects.get(username="******")
        user2 = User.objects.get(username="******")
        print("local_ids=%s" % local_ids)
        exp = Experiment.objects.get(id=local_ids[0])
        self.assertEquals(exp.title, "test1")  # from METS file
        self.assertEquals(exp.description, "this is the description audit message here")
        self.assertEquals(exp.created_by.username, user1.username)
        self.assertEquals(Author_Experiment.objects.filter(experiment=exp).count(), 2)
        from tardis.tardis_portal.models import ExperimentACL
        self.assertEquals(ExperimentACL.objects.filter(pluginId=django_user,
                                   experiment__id=exp.id,
                                   aclOwnershipType=ExperimentACL.OWNER_OWNED).count(), 2)



        self.assertTrue(len(local_ids) == 1)
        for local_id in local_ids:
            self.assertTrue(int(local_id) > 0)
    def test_read_public_experiment(self):
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)

        # Needed as can't throw exceptions in lambdas

        flexmock(tasks).should_receive('getURL').with_args(
            "%s/apps/reposproducer/expstate/%s/" %
            (source, self.exp.id)).and_raise(HTTPError("", "", "", "", None))

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except BadAccessError:
            pass
        else:
            self.assertTrue(False, "Expected BadAccessError")
    def test_no_repos(self):
        """Connection to a non-existence repository
        """
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)

        from oaipmh.error import IdDoesNotExistError

        # Needed as can't throw exceptions in lambdas
        class IdDoesNotExistErrorFake:
            def __init__(self):
                raise IdDoesNotExistError

        fake = flexmock(identify=IdDoesNotExistErrorFake)
        flexmock(oaipmhclient).new_instances(fake)
        #fake = flexmock().should_receive('identify').and_return("99").and_raise(AttributeError)
        #flexmock(Client).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except OAIPMHError:
            pass
        else:
            self.assertTrue(False, "Expected OAIPMHError")

        self._setup_mocks(source)

        # Needed as can't throw exceptions in lambdas
        class AttributeErrorFake:
            def __init__(self):
                raise AttributeError

        fake = flexmock(identify=AttributeErrorFake)
        flexmock(oaipmhclient).new_instances(fake)
        #fake = flexmock().should_receive('identify').and_return("99").and_raise(AttributeError)
        #flexmock(Client).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except ReposReadError:
            pass
        else:
            self.assertTrue(False, "Expected AttributeError")

        self._setup_mocks(source)

        # Needed as can't throw exceptions in lambdas
        class URLErrorFake:
            def __init__(self):
                raise URLError("problem connecting to the remote server")

        fake = flexmock(identify=URLErrorFake)
        flexmock(oaipmhclient).new_instances(fake)
        #fake = flexmock().should_receive('identify').and_return("99").and_raise(AttributeError)
        #flexmock(Client).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except URLError:
            pass
        else:
            self.assertTrue(False, "Expected URLError")

        self._setup_mocks(source)
        identify_fake1 = flexmock(
            baseURL=lambda: "http://127.0.0.1:8032/apps/oaimph")

        fake = flexmock(identify=lambda: identify_fake1)
        flexmock(oaipmhclient).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except BadAccessError:
            pass
        else:
            self.assertTrue(False, "Expected BadAccessError")
    def test_no_repos(self):
        """Connection to a non-existence repository
        """
        source = "http://127.0.0.1:9000"
        self._setup_mocks(source)

        from oaipmh.error import IdDoesNotExistError

        # Needed as can't throw exceptions in lambdas
        class IdDoesNotExistErrorFake:
            def __init__(self):
                raise IdDoesNotExistError

        fake = flexmock(identify=IdDoesNotExistErrorFake)
        flexmock(oaipmhclient).new_instances(fake)
        #fake = flexmock().should_receive('identify').and_return("99").and_raise(AttributeError)
        #flexmock(Client).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except OAIPMHError:
            pass
        else:
            self.assertTrue(False, "Expected OAIPMHError")

        self._setup_mocks(source)

        # Needed as can't throw exceptions in lambdas
        class AttributeErrorFake:
            def __init__(self):
                raise AttributeError

        fake = flexmock(identify=AttributeErrorFake)
        flexmock(oaipmhclient).new_instances(fake)
        #fake = flexmock().should_receive('identify').and_return("99").and_raise(AttributeError)
        #flexmock(Client).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except ReposReadError:
            pass
        else:
            self.assertTrue(False, "Expected AttributeError")

        self._setup_mocks(source)

        # Needed as can't throw exceptions in lambdas
        class URLErrorFake:
            def __init__(self):
                raise URLError("problem connecting to the remote server")

        fake = flexmock(identify=URLErrorFake)
        flexmock(oaipmhclient).new_instances(fake)
        #fake = flexmock().should_receive('identify').and_return("99").and_raise(AttributeError)
        #flexmock(Client).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except URLError:
            pass
        else:
            self.assertTrue(False, "Expected URLError")

        self._setup_mocks(source)
        identify_fake1 = flexmock(baseURL=lambda:
            "http://127.0.0.1:8032/apps/oaimph")

        fake = flexmock(identify=lambda: identify_fake1)
        flexmock(oaipmhclient).new_instances(fake)

        from tardis.apps.reposconsumer.tasks import transfer_experiment
        try:
            transfer_experiment(source)
        except BadAccessError:
            pass
        else:
            self.assertTrue(False, "Expected BadAccessError")