def from_json(json_in, position, parent_name='', ancestry=None): """Forms a Step object from the provided JSON object. Args: json (JSON object): The JSON object to convert from. parent_name (str, optional): The name of the parent for ancestry purposes. Defaults to an empty string. ancestry (list[str], optional): The ancestry for the new Step object. Defaults to None. Returns: The Step object parsed from the JSON object. """ device = json_in['device'] if ('device' in json_in and json_in['device'] is not None and json_in['device'] != 'None') else '' risk = json_in['risk'] if 'risk' in json_in else 0 widgets = [] if 'widgets' in json_in: widgets = [(widget['app'], widget['name']) for widget in json_in['widgets'] if ('app' in widget and 'name' in widget)] step = Step(name=json_in['name'], action=json_in['action'], app=json_in['app'], device=device, risk=risk, inputs={ arg_name: arguments.Argument.from_json(arg_element) for arg_name, arg_element in json_in['input'].items() }, parent_name=parent_name, position=position, widgets=widgets, ancestry=ancestry) if json_in['next']: step.conditionals = [ NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json_in['next'] if next_step ] if json_in['errors']: step.errors = [ NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json_in['errors'] if next_step ] return step
def from_json(json, parent_name='', ancestry=None): step = Step(name=json['name'], action=json['action'], app=json['app'], device=json['device'], input={arg_name: arguments.Argument.from_json(arg_element) for arg_name, arg_element in json['input'].items()}, parent_name=parent_name, ancestry=ancestry) step.conditionals = [NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json['next']] step.errors = [NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json['errors']] return step
def test_from_json_with_parent_and_ancestry(self): json_in = {'name': 'name1', 'flags': []} next_step = NextStep.from_json(json_in, parent_name='parent', ancestry=['a', 'b']) self.__compare_init(next_step, 'name1', 'parent', [], ['a', 'b', 'name1'])
def test_to_from_json(self): filter_params = ['test_filter_action', ''] flags_params = [('', []), ('test_action', []), ('test_action', filter_params)] input_params = [('', '', None, []), ('test_name', '', None, []), ('test_name', 'test_parent', None, []), ('test_name', 'test_parent', ['a', 'b'], []), ('test_name', 'test_parent', ['a', 'b'], flags_params)] for (name, parent_name, ancestry, flag_params) in input_params: next_step = NextStep(name=name, parent_name=parent_name, ancestry=ancestry) if flag_params: flags = [] for flag_action, flag_filter_params in flag_params: flag = Flag(action=flag_action, parent_name=next_step.name, ancestry=next_step.ancestry) if filter_params: flag.filters = [ Filter(action=flag_action, parent_name=flag.name, ancestry=flag.ancestry) for flag_action in flag_filter_params ] flags.append(flag) next_step.flags = flags next_step_json = next_step.as_json() derived_next_step = NextStep.from_json(next_step_json, parent_name=parent_name, ancestry=ancestry) self.assertDictEqual(derived_next_step.as_json(), next_step_json) self.assertEqual(next_step.parent_name, derived_next_step.parent_name) self.assertListEqual(next_step.ancestry, derived_next_step.ancestry) derived_json_without_children = next_step_json derived_json_without_children['flags'] = [ flag['action'] for flag in derived_json_without_children['flags'] ] self.assertDictEqual( derived_next_step.as_json(with_children=False), derived_json_without_children) # check the ancestry of the flags original_flag_ancestries = [ list(flag.ancestry) for flag in next_step.flags ] derived_flag_ancestries = [ list(flag.ancestry) for flag in derived_next_step.flags ] self.assertEqual(len(original_flag_ancestries), len(derived_flag_ancestries)) for original_flag_ancestry, derived_flag_ancestry in zip( original_flag_ancestries, derived_flag_ancestries): self.assertListEqual(derived_flag_ancestry, original_flag_ancestry)
def test_from_json_with_status(self): json_in = {'name': 'name1', 'status': 'test_status', 'flags': []} next_step = NextStep.from_json(json_in) self.__compare_init(next_step, 'name1', '', [], ['', 'name1'], status='test_status')
def test_from_json_with_flags(self): flag_json = [{ 'action': 'Top Flag', 'args': {}, 'filters': [] }, { 'action': 'mod1_flag1', 'args': {}, 'filters': [] }] next_step = NextStep.from_json({'name': 'name1', 'flags': flag_json}) self.__compare_init(next_step, 'name1', '', flag_json, ['', 'name1'])
def from_json(json_in, position, parent_name='', ancestry=None): device = json_in['device'] if ('device' in json_in and json_in['device'] is not None and json_in['device'] != 'None') else '' risk = json_in['risk'] if 'risk' in json_in else 0 widgets = [] if 'widgets' in json_in: widgets = [(widget['app'], widget['name']) for widget in json_in['widgets'] if ('app' in widget and 'name' in widget)] step = Step(name=json_in['name'], action=json_in['action'], app=json_in['app'], device=device, risk=risk, inputs={ arg_name: arguments.Argument.from_json(arg_element) for arg_name, arg_element in json_in['input'].items() }, parent_name=parent_name, position=position, widgets=widgets, ancestry=ancestry) if json_in['next']: step.conditionals = [ NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json_in['next'] if next_step ] if json_in['errors']: step.errors = [ NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json_in['errors'] if next_step ] return step
def from_json(json_in, position, parent_name='', ancestry=None): """Forms a Step object from the provided JSON object. Args: json_in (dict): The JSON object to convert from. position (dict): position of the step node of the form {'x': <x position>, 'y': <y position>} parent_name (str, optional): The name of the parent for ancestry purposes. Defaults to an empty string. ancestry (list[str], optional): The ancestry for the new Step object. Defaults to None. Returns: The Step object parsed from the JSON object. """ device = json_in['device'] if ('device' in json_in and json_in['device'] is not None and json_in['device'] != 'None') else '' risk = json_in['risk'] if 'risk' in json_in else 0 widgets = [] if 'widgets' in json_in: widgets = [(widget['app'], widget['name']) for widget in json_in['widgets'] if ('app' in widget and 'name' in widget)] step = Step( name=json_in['name'], action=json_in['action'], app=json_in['app'], device=device, risk=risk, inputs=json_in['input'], parent_name=parent_name, position={key: str(value) for key, value in position.items()}, widgets=widgets, ancestry=ancestry) if json_in['next']: step.conditionals = [ NextStep.from_json(next_step, parent_name=step.name, ancestry=step.ancestry) for next_step in json_in['next'] if next_step ] return step
def test_from_json_name_only(self): json_in = {'name': 'name1', 'flags': []} next_step = NextStep.from_json(json_in) self.__compare_init(next_step, 'name1', '', [], ['', 'name1'])