def test_batch_b(self): print('in test_batch_b()') # The files files = ['batch_b_1.xml', 'batch_b_2.xml', 'batch_b_3.xml'] # Some other stuff e_id = 'e_1' mod_nums = [0, 5, 3] i = 0 # Just an index for us # Load up each file for filename in files: xml_doc = read_xml(os.path.join(SAMPLE_DIR, filename)) self.assertTrue(self.oadr_schema.validate(xml_doc), msg='Validation failed for "%s"' % filename) print(('"%s" is valid; Testing it against the payload handler.' % filename)) # Send it the message and generate a payload payload = self.event_handler.handle_payload(xml_doc) # Grab an event from self._events via the stateful methods, and check mod numbers evt = self.event_handler.db.get_event(e_id) mod_num = evt.mod_number # event.get_mod_number(evt) # evt = self.event_handler.get_event(e_id) # mod_num = event.get_mod_number(evt) # Make sure we hit the 'else' on the third itteration if i != 2: self.assertEqual( mod_num, mod_nums[i], msg='Mod number not the same! got=%d, should be=%d' % (mod_num, mod_nums[i])) else: self.assertNotEqual( mod_num, mod_nums[i], msg='Not good, the mod numbers are the same') print('Don\'t worry about that warning, it\'s what we want.') print('Expected Mod Number gotten.') # Check the payload if payload is not None: self.assertTrue(self.oadr_schema.validate(payload), msg='Return payload failed for %s' % filename) print(('Return payload for "%s" is valid.' % filename)) else: print(('No return payload generated for "%s"' % filename)) print('+' * 4) i += 1 print('test_batch_b() OK')
def test_batch_a(self): print("in test_batch_a()") # The files files = [ 'batch_a_1.xml', 'batch_a_2.xml', 'batch_a_3.xml', 'batch_a_4.xml' ] # Some other stuff e_id = 'e_1' mod_nums = [0, 1, 2, 4] i = 0 # Load up each file for filename in files: xml_doc = read_xml(os.path.join(SAMPLE_DIR, filename)) self.assertTrue(self.oadr_schema.validate(xml_doc), msg='Validation failed for "%s"' % filename) print(('"%s" is valid; Testing it against the payload handler.' % filename)) # Send it the message and generate a payload payload = self.event_handler.handle_payload(xml_doc) # Grab an event from self._events via the stateful methods, and check mod numbers evt = self.event_handler.get_event(e_id) mod_num = event.get_mod_number(evt) self.assertEqual( mod_num, mod_nums[i], msg='Mod number not the same! got=%d, should be=%d' % (mod_num, mod_nums[i])) print('Mod number same.') # Check the payload if payload is not None: self.assertTrue(self.oadr_schema.validate(payload), msg='Return payload failed for %s' % filename) # print('Return payload for "' + filename + '" is valid.') # print(etree.tostring(payload, pretty_print=True)) else: print(('No return payload generated for "%s"' % filename)) print('+' * 4) i += 1 print('test_batch_a() OK')
def test_handle_payload(self): print('in test_handle_payload()') # The files that we want to test against files = [ # 'sample_oadrDistributeEvent.xml', # First file generates validation errors, due to 'description' field 'sample_oadrDistributeEvent_W_no_targets.xml', 'sample_oadrDistributeEvent_W_something.xml' ] # Load up each individual file for filename in files: xml_doc = read_xml(os.path.join(SAMPLE_DIR, filename)) # print('XML for "%s":'%(filename)) # print(etree.tostring(xml_doc, pretty_print=True)) # Make sure it read in correctly print('Validating') self.assertTrue(self.oadr_schema.validate(xml_doc), msg='Validation falied for %s' % (filename)) print('OK') # send the xml file to the payload handler print('Sending sample XML to payload handler') payload = self.event_handler.handle_payload(xml_doc) print('OK') if payload is not None: # print('Return payload:') # print(etree.tostring(payload, pretty_print=True)) # See if what we got back was valid itself print('Validating response payload') self.assertTrue(self.oadr_schema.validate(payload), msg='Return payload failed for %s' % (filename)) print('OK') else: print('Payload was not generated') # Do this at the end of the loop print(('+' * 4)) print('handle_payload() OK')
def test_20b_1(self): print('In test_20b_1()') files = ['sample_oadrPayload_20b.xml'] # Load up each individual file for filename in files: xml_doc = read_xml(os.path.join(SAMPLE_DIR, filename)) # print('XML for "%s":'%(filename)) # print(etree.tostring(xml_doc, pretty_print=True)) # Make sure it read in correctly print('Validating') self.assertTrue(self.oadr_schema.validate(xml_doc), msg='Validation falied for %s' % filename) print('OK') # send the xml file to the payload handler print('Sending sample XML to payload handler') payload = self.event_handler.handle_payload(xml_doc) print('OK') if payload is not None: # print('Return payload:') # print(etree.tostring(payload, pretty_print=True)) # See if what we got back was valid itself print('Validating response payload') self.assertTrue(self.oadr_schema.validate(payload), msg='Return payload failed for %s' % filename) print('OK') else: print('Payload was not generated') # Do this at the end of the loop print('+' * 4) print('test_20b_1() OK')
def test_batch_c(self): # This test will have 8 xml files, 4 w/ valid target ids, 4 that are not. # The first four are OK, but the other's aren't. print('in test_batch_d()') # A little key: # 1 = good venID ven_py_ # 2 = good resourceID Resource_123 # 3 = good partyID Party_123 # 4 = good groupId Group_123 # 5 .. 8 follow the same pattern, but all are bad! # Override the current one with some new settings self.config['resource_id'] = 'Resource_123' self.config['party_id'] = 'Party_123' self.config['group_id'] = 'Group_123' self.event_handler = event.EventHandler(**self.config) # Start looping through i = 1 for i in range(1, 9): # Open the file and validate the XML xml_doc = read_xml(os.path.join(SAMPLE_DIR, 'batch_c_%i.xml' % i)) self.assertTrue(self.oadr_schema.validate(xml_doc), msg='Validation falied for "batch_c_%i.xml"' % i) print(('"batch_c_%i.xml" is valid.' % i)) # Send it to the handler payload = self.event_handler.handle_payload(xml_doc) if payload is not None: # print(etree.tostring(payload, pretty_print=True)) # Get some data status_code = payload.findtext( 'pyld:eiCreatedEvent/ei:eventResponses/ei:eventResponse/ei:responseCode', namespaces=event.NS_B) opt_type = payload.findtext( 'pyld:eiCreatedEvent/ei:eventResponses/ei:eventResponse/ei:optType', namespaces=event.NS_B) # First 4 should be '200' and 'optIn', else should be '403' and 'optOut' if i <= 4: self.assertEqual( status_code, '200', msg='Status code is not \'200\' for "batch_c_%i.xml"' % i) self.assertEqual( opt_type, 'optIn', msg='Opt type is not \'optIn\' for "batch_c_%i.xml"' % i) print('Status Code and Opt Type are OK') else: self.assertEqual( status_code, '403', msg='Status code is not \'403\' for "batch_c_%i.xml"' % i) self.assertEqual( opt_type, 'optOut', msg='Opt type is not \'optOut\' for "batch_c_%i.xml"' % i) print('Status Code and Opt Type are OK') else: print('Payload for "batch_c_%i.xml"' % i) print('+' * 4) print('test_batch_d() OK')