class CBUtilsTest(unittest.TestCase): def setUp(self): self.cb = CBUtils(instance='mock.cb.com', port="1026", verbosity=0) # self.cb = CBUtils(instance='195.235.93.78', port="10026", path_context="/cb/v1/contextEntities", path_query="/cb/v1/queryContext", # path_statistics="/cb/statistics", # path_subscription="/cb/v1/subscribeContext", # path_update="/cb/v1/updateContext", # path_version="/cb/version", # verbosity=0) @mock.patch('requests.request', side_effect=mocked_requests_get) def test_version(self, mock_requests): version = self.cb.version() print "### Test ---> Version: " + version.content eq_(200, version.status_code, msg="version to CB does not return 200") assert_in("orion", version.content, msg="bad data returned to query version to CB") assert_in("version", version.content, msg="bad data returned to query version to CB") @mock.patch('requests.request', side_effect=mocked_requests_get) def test_statistics(self, mock_requests): print "### Test ---> Statistics: " print self.cb.statistics().content @mock.patch('requests.request', side_effect=mocked_requests_get) def test_create_entity(self, mock_requests): print "### Test ---> Create a entity: " data0 = { 'ent_type': 'Sala', 'ent_pattern': 'false', 'ent_id': 'Sala01', 'attributes': [{ 'name': 'temperature', 'type': 'centigrade', 'value': '99' }] } self.cb.entity_append('x222', data0) print "### Test ---> Recover the entity1 (method1): " entity1 = self.cb.entity_get('x222', 'Sala01') eq_(200, entity1.status_code, msg="Error Code") print "### Test ---> Recover the entity2 (method2):" entity2 = self.cb.entities_get('x222', 'Sala', 'Sala01', 'false') eq_(200, entity2.status_code, msg="Error Code") print "### Test ---> Recover the entities wiht pattern (method3):" entity3 = self.cb.entities_get('x222', 'Sala', 'Sala.*', 'true') eq_(200, entity2.status_code, msg="Error Code") print "### Test ---> Compare all the entities recovered: " jsobj_1 = json.loads(entity1.content) jsobj_2 = json.loads(entity2.content) jsobj_3 = json.loads(entity3.content) # Validations eq_('Sala', jsobj_1['contextElement']['type'], msg="DATA ERROR 1 {}".format(jsobj_1['contextElement']['type'])) eq_('Sala01', jsobj_1['contextElement']['id'], msg="DATA ERROR 2 {}".format(jsobj_1['contextElement']['id'])) eq_('99', jsobj_1['contextElement']['attributes'][0]['value'], msg="DATA ERROR 3 {}".format( jsobj_1['contextElement']['attributes'][0]['value'])) eq_('temperature', jsobj_1['contextElement']['attributes'][0]['name'], msg="DATA ERROR 4 {}".format( jsobj_1['contextElement']['attributes'][0]['name'])) eq_(jsobj_1['contextElement'], jsobj_2['contextResponses'][0]['contextElement'], msg='## Not equals!\n Received1: {} \n Received2: {}'.format( jsobj_1['contextElement'], jsobj_2['contextResponses'][0]['contextElement'])) eq_(jsobj_3['contextResponses'][0]['contextElement'], jsobj_2['contextResponses'][0]['contextElement'], msg='## Not equals!\n Received5: {} \n Received2: {}'.format( jsobj_3['contextResponses'][0]['contextElement'], jsobj_2['contextResponses'][0]['contextElement'])) @mock.patch('requests.request', side_effect=mocked_requests_404) def test_missing_entities(self, mock_requests): print "### Test ---> Recover missing entities: " entityb1 = self.cb.entities_get('x222', 'Sala', 'S', 'false') eq_(200, entityb1.status_code, msg="Error Code") entityb2 = self.cb.entities_get('x222', 'Sal', 'Sala01', 'false') eq_(200, entityb2.status_code, msg="Error Code") # checking it further jsobj_b1 = json.loads(entityb1.content) jsobj_b2 = json.loads(entityb2.content) eq_('404', jsobj_b1['errorCode']['code'], msg="Error Code") eq_('No context element found', jsobj_b1['errorCode']['reasonPhrase'], msg="Error Body") eq_('404', jsobj_b2['errorCode']['code'], msg="Error Code") eq_('No context element found', jsobj_b2['errorCode']['reasonPhrase'], msg="Error Body") @mock.patch('requests.request', side_effect=mocked_requests_get) def test_update_entity(self, mock_requests): print "### Test ---> Update the entity: " data0 = { 'ent_type': 'Salass', 'ent_pattern': 'false', 'ent_id': 'Sala01', 'attributes': [{ 'name': 'temperature', 'type': 'centigrade', 'value': '99' }] } self.cb.entity_append('x222', data0) print "### Test ---> Recover the entity1 (method1): " entity1 = self.cb.entity_get('x222', 'Sala01') data1 = { 'ent_type': 'Salass', 'ent_pattern': 'false', 'ent_id': 'Sala01', 'attributes': [{ 'name': 'temperature', 'type': 'centigrade', 'value': '101' }] } self.cb.entity_update('x222', data1) print "### Test ---> Recover the updated entity: " entity2 = self.cb.entity_get('x222', 'Sala01') eq_(200, entity1.status_code, msg="Error Code") assert_in('101', entity2.content) @mock.patch('requests.request', side_effect=mocked_requests_get) def test_subscription(self, mock_requests): print "### Test ---> Add Subscription: " data0 = { 'ent_type': 'Salass', 'ent_pattern': 'false', 'ent_id': 'Sala01', 'attributes': [{ 'name': 'temperature', 'type': 'centigrade', 'value': '99' }] } self.cb.entity_append('x222', data0) subs0 = dict({ 'ent_type': 'Sala', 'ent_pattern': 'false', 'ent_id': 'Sala99', 'notify_url': 'http://localhost:5050/notify' }) sub = self.cb.subscription_add('x222', template_data=subs0) eq_(200, sub.status_code, msg="Error Code") jssub = json.loads(sub.content) ok_(jssub['subscribeResponse']['subscriptionId'], msg="No subscription") eq_(jssub['subscribeResponse']['duration'], 'PT5M', msg="No Duration") print "### Test ---> subscription added:" print "Subscription id: {}".format( jssub['subscribeResponse']['subscriptionId']) print "Subscription duration: {}".format( jssub['subscribeResponse']['duration'])
class CBUtilsTest(unittest.TestCase): def setUp(self): self.cb = CBUtils(instance='mock.cb.com', port="1026", verbosity=0) # self.cb = CBUtils(instance='195.235.93.78', port="10026", path_context="/cb/v1/contextEntities", path_query="/cb/v1/queryContext", # path_statistics="/cb/statistics", # path_subscription="/cb/v1/subscribeContext", # path_update="/cb/v1/updateContext", # path_version="/cb/version", # verbosity=0) @mock.patch('requests.request', side_effect=mocked_requests_get) def test_version(self, mock_requests): version = self.cb.version() print "### Test ---> Version: " + version.content eq_(200, version.status_code, msg="version to CB does not return 200") assert_in("orion", version.content, msg="bad data returned to query version to CB") assert_in("version", version.content, msg="bad data returned to query version to CB") @mock.patch('requests.request', side_effect=mocked_requests_get) def test_statistics(self, mock_requests): print "### Test ---> Statistics: " print self.cb.statistics().content @mock.patch('requests.request', side_effect=mocked_requests_get) def test_create_entity(self, mock_requests): print "### Test ---> Create a entity: " data0 = {'ent_type': 'Sala', 'ent_pattern': 'false', 'ent_id': 'Sala01', 'attributes': [{'name': 'temperature', 'type': 'centigrade', 'value': '99'}]} self.cb.entity_append('x222', data0) print "### Test ---> Recover the entity1 (method1): " entity1 = self.cb.entity_get('x222', 'Sala01') eq_(200, entity1.status_code, msg="Error Code") print "### Test ---> Recover the entity2 (method2):" entity2 = self.cb.entities_get('x222', 'Sala', 'Sala01', 'false') eq_(200, entity2.status_code, msg="Error Code") print "### Test ---> Recover the entities wiht pattern (method3):" entity3 = self.cb.entities_get('x222', 'Sala', 'Sala.*', 'true') eq_(200, entity2.status_code, msg="Error Code") print "### Test ---> Compare all the entities recovered: " jsobj_1 = json.loads(entity1.content) jsobj_2 = json.loads(entity2.content) jsobj_3 = json.loads(entity3.content) # Validations eq_('Sala', jsobj_1['contextElement']['type'], msg="DATA ERROR 1 {}".format(jsobj_1['contextElement']['type'])) eq_('Sala01', jsobj_1['contextElement']['id'], msg="DATA ERROR 2 {}".format(jsobj_1['contextElement']['id'])) eq_('99', jsobj_1['contextElement']['attributes'][0]['value'], msg="DATA ERROR 3 {}".format(jsobj_1['contextElement']['attributes'][0]['value'])) eq_('temperature', jsobj_1['contextElement']['attributes'][0]['name'], msg="DATA ERROR 4 {}".format(jsobj_1['contextElement']['attributes'][0]['name'])) eq_(jsobj_1['contextElement'], jsobj_2['contextResponses'][0]['contextElement'], msg='## Not equals!\n Received1: {} \n Received2: {}'.format( jsobj_1['contextElement'], jsobj_2['contextResponses'][0]['contextElement'])) eq_(jsobj_3['contextResponses'][0]['contextElement'], jsobj_2['contextResponses'][0]['contextElement'], msg='## Not equals!\n Received5: {} \n Received2: {}'.format( jsobj_3['contextResponses'][0]['contextElement'], jsobj_2['contextResponses'][0]['contextElement'])) @mock.patch('requests.request', side_effect=mocked_requests_404) def test_missing_entities(self, mock_requests): print "### Test ---> Recover missing entities: " entityb1 = self.cb.entities_get('x222', 'Sala', 'S', 'false') eq_(200, entityb1.status_code, msg="Error Code") entityb2 = self.cb.entities_get('x222', 'Sal', 'Sala01', 'false') eq_(200, entityb2.status_code, msg="Error Code") # checking it further jsobj_b1 = json.loads(entityb1.content) jsobj_b2 = json.loads(entityb2.content) eq_('404', jsobj_b1['errorCode']['code'], msg="Error Code") eq_('No context element found', jsobj_b1['errorCode']['reasonPhrase'], msg="Error Body") eq_('404', jsobj_b2['errorCode']['code'], msg="Error Code") eq_('No context element found', jsobj_b2['errorCode']['reasonPhrase'], msg="Error Body") @mock.patch('requests.request', side_effect=mocked_requests_get) def test_update_entity(self, mock_requests): print "### Test ---> Update the entity: " data0 = {'ent_type': 'Salass', 'ent_pattern': 'false', 'ent_id': 'Sala01', 'attributes': [{'name': 'temperature', 'type': 'centigrade', 'value': '99'}]} self.cb.entity_append('x222', data0) print "### Test ---> Recover the entity1 (method1): " entity1 = self.cb.entity_get('x222', 'Sala01') data1 = {'ent_type': 'Salass', 'ent_pattern': 'false', 'ent_id': 'Sala01', 'attributes': [{'name': 'temperature', 'type': 'centigrade', 'value': '101'}]} self.cb.entity_update('x222', data1) print "### Test ---> Recover the updated entity: " entity2 = self.cb.entity_get('x222', 'Sala01') eq_(200, entity1.status_code, msg="Error Code") assert_in('101', entity2.content) @mock.patch('requests.request', side_effect=mocked_requests_get) def test_subscription(self, mock_requests): print "### Test ---> Add Subscription: " data0 = {'ent_type': 'Salass', 'ent_pattern': 'false', 'ent_id': 'Sala01', 'attributes': [{'name': 'temperature', 'type': 'centigrade', 'value': '99'}]} self.cb.entity_append('x222', data0) subs0 = dict( {'ent_type': 'Sala', 'ent_pattern': 'false', 'ent_id': 'Sala99', 'notify_url': 'http://localhost:5050/notify'}) sub = self.cb.subscription_add('x222', template_data=subs0) eq_(200, sub.status_code, msg="Error Code") jssub = json.loads(sub.content) ok_(jssub['subscribeResponse']['subscriptionId'], msg="No subscription") eq_(jssub['subscribeResponse']['duration'], 'PT5M', msg="No Duration") print "### Test ---> subscription added:" print "Subscription id: {}".format(jssub['subscribeResponse']['subscriptionId']) print "Subscription duration: {}".format(jssub['subscribeResponse']['duration'])