def prepare_resource_support(self, resource_type='', resource_id=''): """Returns a structured dict with information to help create/update a resource @param resource_type str @param resource_id str @retval resource_data GenericPrepareSupport @throws BadRequest A parameter is missing @throws NotFound An object with the specified resource_id does not exist """ if not resource_type: raise BadRequest("The resource_type parameter is required") extended_resource_handler = ExtendedResourceContainer(self, self) resource_data = extended_resource_handler.create_prepare_resource_support(resource_id=resource_id, prepare_resource_type=OT.GenericPrepareSupport, origin_resource_type=resource_type) #Fill out service request information for creating a instrument device extended_resource_handler.set_service_requests(resource_data.create_request, 'resource_registry', 'create', { "object": "$(object)" }) #Fill out service request information for creating a instrument device extended_resource_handler.set_service_requests(resource_data.update_request, 'resource_registry', 'update', { "object": "$(object)" }) return resource_data
def prepare_user_info_support(self, user_info_id=''): """ Returns the object containing the data to create/update a user info resource """ # TODO: Precondition or check: only the actor identity associated with this user or an ORG_MANAGER/ION_MANAGER # can edit this user info resource extended_resource_handler = ExtendedResourceContainer(self) resource_data = extended_resource_handler.create_prepare_resource_support(user_info_id, OT.UserInfoPrepareSupport) return resource_data
def prepare_user_info_support(self, user_info_id=''): """ Returns the object containing the data to create/update a user info resource """ # TODO: Precondition or check: only the actor identity associated with this user or an ORG_MANAGER/ION_MANAGER # can edit this user info resource extended_resource_handler = ExtendedResourceContainer(self) resource_data = extended_resource_handler.create_prepare_resource_support( user_info_id, OT.UserInfoPrepareSupport) return resource_data
def xtest_create_extended_resource_container(self): mock_clients = self._create_service_mock('resource_registry') self.clients = mock_clients self.container = Mock() extended_resource_handler = ExtendedResourceContainer( self, mock_clients.resource_registry) instrument_device = Mock() instrument_device._id = '123' instrument_device.name = "MyInstrument" instrument_device.type_ = RT.TestInstrument instrument_device.lcstate = LCS.DRAFT instrument_device.availability = AS.PRIVATE instrument_device2 = Mock() instrument_device2._id = '456' instrument_device2.name = "MyInstrument2" instrument_device2.type_ = RT.TestInstrument actor_identity = Mock() actor_identity._id = '111' actor_identity.name = "Foo" actor_identity.type_ = RT.ActorIdentity actor_identity = Mock() actor_identity._id = '1112' actor_identity.name = "Foo2" actor_identity.type_ = RT.ActorIdentity user_info = Mock() user_info._id = '444' user_info.name = "John Doe" user_info.email = "*****@*****.**" user_info.phone = "555-555-5555" user_info.variables = [{ "name": "subscribeToMailingList", "value": "False" }] user_info2 = Mock() user_info2._id = '445' user_info2.name = "aka Evil Twin" user_info2.email = "*****@*****.**" user_info2.phone = "555-555-5555" user_info2.variables = [{ "name": "subscribeToMailingList", "value": "False" }] # ActorIdentity to UserInfo association actor_identity_to_info_association = Mock() actor_identity_to_info_association._id = '555' actor_identity_to_info_association.s = "111" actor_identity_to_info_association.st = RT.ActorIdentity actor_identity_to_info_association.p = PRED.hasInfo actor_identity_to_info_association.o = "444" actor_identity_to_info_association.ot = RT.UserInfo # ActorIdentity to UserInfo association actor_identity_to_info_association2 = Mock() actor_identity_to_info_association2._id = '556' actor_identity_to_info_association2.s = "1112" actor_identity_to_info_association2.st = RT.ActorIdentity actor_identity_to_info_association2.p = PRED.hasInfo actor_identity_to_info_association2.o = "445" actor_identity_to_info_association2.ot = RT.UserInfo # ActorIdentity to Instrument Device association Instrument_device_to_actor_identity_association = Mock() Instrument_device_to_actor_identity_association._id = '666' Instrument_device_to_actor_identity_association.s = "123" Instrument_device_to_actor_identity_association.st = RT.TestInstrument Instrument_device_to_actor_identity_association.p = PRED.hasOwner Instrument_device_to_actor_identity_association.o = "111" Instrument_device_to_actor_identity_association.ot = RT.ActorIdentity # ActorIdentity to Instrument Device association Instrument_device_to_actor_identity_association2 = Mock() Instrument_device_to_actor_identity_association2._id = '667' Instrument_device_to_actor_identity_association2.s = "456" Instrument_device_to_actor_identity_association2.st = RT.TestInstrument Instrument_device_to_actor_identity_association2.p = PRED.hasOwner Instrument_device_to_actor_identity_association2.o = "111" Instrument_device_to_actor_identity_association2.ot = RT.ActorIdentity with self.assertRaises(BadRequest) as cm: extended_user = extended_resource_handler.create_extended_resource_container( RT.ActorIdentity, '111') self.assertIn( 'The requested resource ActorIdentity is not extended from ResourceContainer', cm.exception.message) mock_clients.resource_registry.read.return_value = instrument_device mock_clients.resource_registry.find_objects.return_value = ([ actor_identity ], [Instrument_device_to_actor_identity_association]) mock_clients.resource_registry.find_subjects.return_value = (None, None) mock_clients.resource_registry.find_associations.return_value = [ actor_identity_to_info_association, Instrument_device_to_actor_identity_association ] mock_clients.resource_registry.read_mult.return_value = [user_info] extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 2) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.TestInstrument) self.assertEquals(extended_res.resource_object.name, 'TestSystem_Resource') self.assertEquals(extended_res.owner_count, 2) self.assertEquals(extended_res.single_owner.name, user_info.name) self.assertEquals(len(extended_res.lcstate_transitions), 6) self.assertEquals( set(extended_res.lcstate_transitions.keys()), set(['develop', 'deploy', 'retire', 'plan', 'integrate', 'delete'])) self.assertEquals(len(extended_res.availability_transitions), 2) self.assertEquals(set(extended_res.availability_transitions.keys()), set(['enable', 'announce'])) extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResourceDevice, '123') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 2) with self.assertRaises(Inconsistent) as cm: extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResourceBad, '123') #Test adding extra paramaters to methods extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123', resource_name='AltSystem_Resource') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 2) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.TestInstrument) self.assertEquals(extended_res.resource_object.name, 'AltSystem_Resource') #Test field exclusion extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123', ext_exclude=['owners']) self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 0) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.TestInstrument) #Test the list of ids interface extended_res_list = extended_resource_handler.create_extended_resource_container_list( OT.TestExtendedResource, ['123', '456']) self.assertEqual(len(extended_res_list), 2) self.assertEquals(extended_res_list[0].resource, instrument_device) self.assertEquals(len(extended_res_list[0].owners), 2) self.assertEquals(extended_res_list[0].resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.TestInstrument) #Test create_prepare_update_resource prepare_create = extended_resource_handler.create_prepare_resource_support( prepare_resource_type=OT.TestPrepareUpdateResource) self.assertEqual(prepare_create.type_, OT.TestPrepareUpdateResource) self.assertEqual(prepare_create._id, '') prepare_update = extended_resource_handler.create_prepare_resource_support( resource_id='123', prepare_resource_type=OT.TestPrepareUpdateResource) self.assertEqual(prepare_update.type_, OT.TestPrepareUpdateResource) self.assertEqual(prepare_update._id, '123')
def test_create_extended_resource_container(self): mock_clients = self._create_service_mock('resource_registry') self.clients = mock_clients self.container = Mock() extended_resource_handler = ExtendedResourceContainer(self, mock_clients.resource_registry) instrument_device = Mock() instrument_device._id = '123' instrument_device.name = "MyInstrument" instrument_device.type_ = RT.InstrumentDevice instrument_device.lcstate = LCS.DRAFT instrument_device.availability = AS.PRIVATE instrument_device2 = Mock() instrument_device2._id = '456' instrument_device2.name = "MyInstrument2" instrument_device2.type_ = RT.InstrumentDevice actor_identity = Mock() actor_identity._id = '111' actor_identity.name = "Foo" actor_identity.type_ = RT.ActorIdentity actor_identity = Mock() actor_identity._id = '1112' actor_identity.name = "Foo2" actor_identity.type_ = RT.ActorIdentity user_info = Mock() user_info._id = '444' user_info.name = "John Doe" user_info.email = "*****@*****.**" user_info.phone = "555-555-5555" user_info.variables = [{"name": "subscribeToMailingList", "value": "False"}] user_info2 = Mock() user_info2._id = '445' user_info2.name = "aka Evil Twin" user_info2.email = "*****@*****.**" user_info2.phone = "555-555-5555" user_info2.variables = [{"name": "subscribeToMailingList", "value": "False"}] # ActorIdentity to UserInfo association actor_identity_to_info_association = Mock() actor_identity_to_info_association._id = '555' actor_identity_to_info_association.s = "111" actor_identity_to_info_association.st = RT.ActorIdentity actor_identity_to_info_association.p = PRED.hasInfo actor_identity_to_info_association.o = "444" actor_identity_to_info_association.ot = RT.UserInfo # ActorIdentity to UserInfo association actor_identity_to_info_association2 = Mock() actor_identity_to_info_association2._id = '556' actor_identity_to_info_association2.s = "1112" actor_identity_to_info_association2.st = RT.ActorIdentity actor_identity_to_info_association2.p = PRED.hasInfo actor_identity_to_info_association2.o = "445" actor_identity_to_info_association2.ot = RT.UserInfo # ActorIdentity to Instrument Device association Instrument_device_to_actor_identity_association = Mock() Instrument_device_to_actor_identity_association._id = '666' Instrument_device_to_actor_identity_association.s = "123" Instrument_device_to_actor_identity_association.st = RT.InstrumentDevice Instrument_device_to_actor_identity_association.p = PRED.hasOwner Instrument_device_to_actor_identity_association.o = "111" Instrument_device_to_actor_identity_association.ot = RT.ActorIdentity # ActorIdentity to Instrument Device association Instrument_device_to_actor_identity_association2 = Mock() Instrument_device_to_actor_identity_association2._id = '667' Instrument_device_to_actor_identity_association2.s = "456" Instrument_device_to_actor_identity_association2.st = RT.InstrumentDevice Instrument_device_to_actor_identity_association2.p = PRED.hasOwner Instrument_device_to_actor_identity_association2.o = "111" Instrument_device_to_actor_identity_association2.ot = RT.ActorIdentity with self.assertRaises(BadRequest) as cm: extended_user = extended_resource_handler.create_extended_resource_container(RT.ActorIdentity, '111') self.assertIn( 'The requested resource ActorIdentity is not extended from ResourceContainer',cm.exception.message) mock_clients.resource_registry.read.return_value = instrument_device mock_clients.resource_registry.find_objects.return_value = ([actor_identity], [Instrument_device_to_actor_identity_association]) mock_clients.resource_registry.find_subjects.return_value = (None,None) mock_clients.resource_registry.find_associations.return_value = [actor_identity_to_info_association, Instrument_device_to_actor_identity_association] mock_clients.resource_registry.read_mult.return_value = [user_info] extended_res = extended_resource_handler.create_extended_resource_container(OT.TestExtendedResource, '123') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners),2) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.InstrumentDevice) self.assertEquals(extended_res.resource_object.name, 'TestSystem_Resource') self.assertEquals(extended_res.owner_count, 2) self.assertEquals(extended_res.single_owner.name, user_info.name) self.assertEquals(len(extended_res.lcstate_transitions), 6) self.assertEquals(set(extended_res.lcstate_transitions.keys()), set(['develop', 'deploy', 'retire', 'plan', 'integrate', 'delete'])) self.assertEquals(len(extended_res.availability_transitions), 2) self.assertEquals(set(extended_res.availability_transitions.keys()), set(['enable', 'announce'])) extended_res = extended_resource_handler.create_extended_resource_container(OT.TestExtendedResourceDevice, '123') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners),2) with self.assertRaises(Inconsistent) as cm: extended_res = extended_resource_handler.create_extended_resource_container(OT.TestExtendedResourceBad, '123') #Test adding extra paramaters to methods extended_res = extended_resource_handler.create_extended_resource_container(OT.TestExtendedResource, '123', resource_name='AltSystem_Resource') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners),2) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.InstrumentDevice) self.assertEquals(extended_res.resource_object.name, 'AltSystem_Resource') #Test field exclusion extended_res = extended_resource_handler.create_extended_resource_container(OT.TestExtendedResource, '123',ext_exclude=['owners']) self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners),0) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.InstrumentDevice) #Test the list of ids interface extended_res_list = extended_resource_handler.create_extended_resource_container_list(OT.TestExtendedResource, ['123','456']) self.assertEqual(len(extended_res_list), 2) self.assertEquals(extended_res_list[0].resource, instrument_device) self.assertEquals(len(extended_res_list[0].owners),2) self.assertEquals(extended_res_list[0].resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.InstrumentDevice) #Test create_prepare_update_resource prepare_create = extended_resource_handler.create_prepare_resource_support(prepare_resource_type=OT.TestPrepareUpdateResource) self.assertEqual(prepare_create.type_, OT.TestPrepareUpdateResource) self.assertEqual(prepare_create._id, '') prepare_update = extended_resource_handler.create_prepare_resource_support(resource_id='123',prepare_resource_type=OT.TestPrepareUpdateResource) self.assertEqual(prepare_update.type_, OT.TestPrepareUpdateResource) self.assertEqual(prepare_update._id, '123')