Example #1
0
    def test_create_negotiation(self):

        self.preconditions.check_method1.return_value = True
        self.preconditions.check_method2.return_value = False
        self.accept_actions.accept_method.return_value = None

        negotiation_rules = {
            OT.EnrollmentProposal: {
                'pre_conditions': ['preconditions.check_method1(sap.consumer)', 'not preconditions.check_method2(sap.provider,sap.consumer)'],
                'accept_action': 'accept_actions.accept_method(sap.provider,sap.consumer)'
            }}

        negotiation_handler = Negotiation(self, negotiation_rules)

        with self.assertRaises(BadRequest) as cm:
            negotiation_handler.create_negotiation()
        self.assertIn('The sap parameter must be a valid Service Agreement Proposal object',cm.exception.message)

        sap = IonObject(OT.EnrollmentProposal,consumer=self.actor_identity._id, provider=self.org._id )
        sap.sequence_num = 1  #Force an error
        with self.assertRaises(Inconsistent) as cm:
            negotiation_handler.create_negotiation(sap)
        self.assertIn('The specified Service Agreement Proposal has inconsistent status fields',cm.exception.message)

        sap = IonObject(OT.EnrollmentProposal,consumer=self.actor_identity._id, provider=self.org._id )
        sap.proposal_status = ProposalStatusEnum.COUNTER  #Force an error
        with self.assertRaises(Inconsistent) as cm:
            negotiation_handler.create_negotiation(sap)
        self.assertIn('The specified Service Agreement Proposal has inconsistent status fields',cm.exception.message)

        sap = IonObject(OT.EnrollmentProposal,consumer=self.actor_identity._id, provider=self.org._id )
        sap.negotiation_id = 'efefeff'  #Force an error
        with self.assertRaises(Inconsistent) as cm:
            negotiation_handler.create_negotiation(sap)
        self.assertIn('The specified Service Agreement Proposal cannot have a negotiation_id for an initial proposal',cm.exception.message)

        sap = IonObject(OT.EnrollmentProposal,consumer=self.actor_identity._id, provider=self.org._id )

        negotiation = Mock()
        negotiation._id = '456'
        negotiation.type_ = RT.Negotiation
        negotiation.proposals = []

        self.mock_read.return_value = negotiation
        self.mock_create.return_value = ['456', 2]

        neg_id = negotiation_handler.create_negotiation(sap)

        self.assertEqual(neg_id, negotiation._id)
        self.assertEqual(len(negotiation.proposals),0)

        self.assertEqual(self.preconditions.check_method1.called,True)
        self.assertEqual(self.preconditions.check_method2.called,True)
        self.assertEqual(self.accept_actions.accept_method.called,False)
Example #2
0
    def test_create_negotiation(self):

        self.preconditions.check_method1.return_value = True
        self.preconditions.check_method2.return_value = False
        self.accept_actions.accept_method.return_value = None

        negotiation_rules = {
            OT.EnrollmentProposal: {
                'pre_conditions': [
                    'preconditions.check_method1(sap.consumer)',
                    'not preconditions.check_method2(sap.provider,sap.consumer)'
                ],
                'accept_action':
                'accept_actions.accept_method(sap.provider,sap.consumer)'
            }
        }

        negotiation_handler = Negotiation(self, negotiation_rules)

        with self.assertRaises(BadRequest) as cm:
            negotiation_handler.create_negotiation()
        self.assertIn(
            'The sap parameter must be a valid Service Agreement Proposal object',
            cm.exception.message)

        sap = IonObject(OT.EnrollmentProposal,
                        consumer=self.actor_identity._id,
                        provider=self.org._id)
        sap.sequence_num = 1  #Force an error
        with self.assertRaises(Inconsistent) as cm:
            negotiation_handler.create_negotiation(sap)
        self.assertIn(
            'The specified Service Agreement Proposal has inconsistent status fields',
            cm.exception.message)

        sap = IonObject(OT.EnrollmentProposal,
                        consumer=self.actor_identity._id,
                        provider=self.org._id)
        sap.proposal_status = ProposalStatusEnum.COUNTER  #Force an error
        with self.assertRaises(Inconsistent) as cm:
            negotiation_handler.create_negotiation(sap)
        self.assertIn(
            'The specified Service Agreement Proposal has inconsistent status fields',
            cm.exception.message)

        sap = IonObject(OT.EnrollmentProposal,
                        consumer=self.actor_identity._id,
                        provider=self.org._id)
        sap.negotiation_id = 'efefeff'  #Force an error
        with self.assertRaises(Inconsistent) as cm:
            negotiation_handler.create_negotiation(sap)
        self.assertIn(
            'The specified Service Agreement Proposal cannot have a negotiation_id for an initial proposal',
            cm.exception.message)

        sap = IonObject(OT.EnrollmentProposal,
                        consumer=self.actor_identity._id,
                        provider=self.org._id)

        negotiation = Mock()
        negotiation._id = '456'
        negotiation.type_ = RT.Negotiation
        negotiation.proposals = []

        self.mock_read.return_value = negotiation
        self.mock_create.return_value = ['456', 2]

        neg_id = negotiation_handler.create_negotiation(sap)

        self.assertEqual(neg_id, negotiation._id)
        self.assertEqual(len(negotiation.proposals), 0)

        self.assertEqual(self.preconditions.check_method1.called, True)
        self.assertEqual(self.preconditions.check_method2.called, True)
        self.assertEqual(self.accept_actions.accept_method.called, False)