def test_read_negotiation(self): negotiation_handler = Negotiation(self) with self.assertRaises(BadRequest) as cm: negotiation_handler.read_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) with self.assertRaises(BadRequest) as cm: negotiation_handler.read_negotiation(sap) self.assertIn( 'The Service Agreement Proposal object (sap) is missing a negotiation_id value', cm.exception.message) negotiation = Mock() negotiation._id = '456' sap.negotiation_id = negotiation._id self.mock_read.return_value = negotiation neg_obj = negotiation_handler.read_negotiation(sap) self.assertEqual(neg_obj, negotiation)
def test_create_counter_proposal(self): with self.assertRaises(BadRequest) as cm: consumer_accept_sap = Negotiation.create_counter_proposal( proposal_status=ProposalStatusEnum.INITIAL) self.assertIn( 'The negotiation parameter must be a valid Negotiation object', cm.exception.message) negotiation_handler = Negotiation(self) sap = IonObject(OT.EnrollmentProposal, consumer=self.actor_identity._id, provider=self.org._id) negotiation = Mock() negotiation._id = '456' negotiation.type_ = RT.Negotiation negotiation.proposals = [sap] self.mock_read.return_value = negotiation self.mock_create.return_value = ['456', 2] neg_id = negotiation_handler.create_negotiation(sap) sap.negotiation_id = neg_id consumer_accept_sap = Negotiation.create_counter_proposal( negotiation, proposal_status=ProposalStatusEnum.COUNTER, originator=ProposalOriginatorEnum.PROVIDER) self.assertEqual(consumer_accept_sap.negotiation_id, negotiation._id) self.assertEqual(len(negotiation.proposals), 1) self.assertEqual(consumer_accept_sap.sequence_num, len(negotiation.proposals)) self.assertEqual(consumer_accept_sap.proposal_status, ProposalStatusEnum.COUNTER) self.assertEqual(consumer_accept_sap.originator, ProposalOriginatorEnum.PROVIDER)
def test_create_negotiation_fail_precondition(self): self.preconditions.check_method1.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) 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] with self.assertRaises(BadRequest) as cm: neg_id = negotiation_handler.create_negotiation(sap) self.assertIn( 'A precondition for this request has not been satisfied: preconditions.check_method1(sap.consumer)', cm.exception.message) self.assertEqual(len(negotiation.proposals), 0) self.assertEqual(self.preconditions.check_method1.called, True) self.assertEqual(self.preconditions.check_method2.called, False) self.assertEqual(self.accept_actions.accept_method.called, False)
def on_init(self): self.rr = self.clients.resource_registry self.event_pub = EventPublisher(process=self) self.negotiation_handler = Negotiation(self, negotiation_rules, self.event_pub) self.root_org_id = None
def test_update_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(Inconsistent) as cm: negotiation_handler.update_negotiation() self.assertIn( 'The Service Agreement Proposal must have a negotiation resource id associated with it', cm.exception.message) sap = IonObject(OT.EnrollmentProposal, consumer=self.actor_identity._id, provider=self.org._id) with self.assertRaises(Inconsistent) as cm: negotiation_handler.update_negotiation(sap) self.assertIn( 'The Service Agreement Proposal must have a negotiation resource id associated with it', cm.exception.message) negotiation = Mock() negotiation._id = '456' negotiation.type_ = RT.Negotiation negotiation.proposals = [] sap.negotiation_id = negotiation._id self.mock_read.return_value = negotiation self.mock_update.return_value = ['456', 2] neg_id = negotiation_handler.update_negotiation(sap) self.assertEqual(self.event_pub.publish_event.called, True) self.assertEqual(neg_id, negotiation._id) self.assertEqual(len(negotiation.proposals), 1) counter_sap = Negotiation.create_counter_proposal( negotiation, ProposalStatusEnum.REJECTED, ProposalOriginatorEnum.PROVIDER) neg_id = negotiation_handler.update_negotiation( counter_sap, 'Fake rejection reason') self.assertEqual(len(negotiation.proposals), 2) self.assertEqual(negotiation.negotiation_status, NegotiationStatusEnum.REJECTED) self.assertEquals(negotiation.reason, 'Fake rejection reason') counter_sap = Negotiation.create_counter_proposal( negotiation, ProposalStatusEnum.ACCEPTED, ProposalOriginatorEnum.PROVIDER) neg_id = negotiation_handler.update_negotiation(counter_sap) self.assertEqual(len(negotiation.proposals), 3) self.assertEqual(negotiation.negotiation_status, NegotiationStatusEnum.REJECTED) counter_sap = Negotiation.create_counter_proposal( negotiation, ProposalStatusEnum.ACCEPTED, ProposalOriginatorEnum.CONSUMER) neg_id = negotiation_handler.update_negotiation(counter_sap) self.assertEqual(len(negotiation.proposals), 4) self.assertEqual(negotiation.negotiation_status, NegotiationStatusEnum.ACCEPTED) self.assertEqual(self.accept_actions.accept_method.called, True)
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)