[Resource Mapping Tool] make top-level Account id optional in ResourceMapping tool schema (#5569)

* [Resource Mapping Tool] make top-level Account id optional in ResourceMapping tool schema

Signed-off-by: junbo75 <68558268+junbo75@users.noreply.github.com>
This commit is contained in:
Junbo Liang
2021-11-11 16:11:52 -08:00
committed by GitHub
parent e4ca568433
commit b8f7767cd6
4 changed files with 56 additions and 9 deletions
@@ -69,17 +69,13 @@ class ViewEditController(QObject):
json_dict: Dict[str, any] = \
json_utils.convert_resources_to_json_dict(self._proxy_model.get_resources(), self._config_file_json_source)
configuration: Configuration = self._configuration_manager.configuration
if json_dict.get(json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME) == \
json_utils.RESOURCE_MAPPING_ACCOUNTID_TEMPLATE_VALUE:
json_dict[json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME] = configuration.account_id
if json_dict == self._config_file_json_source:
# skip because no difference found against existing json file
return True
# try to write in memory json content into json file
try:
configuration: Configuration = self._configuration_manager.configuration
config_file_full_path: str = file_utils.join_path(configuration.config_directory, config_file_name)
json_utils.write_into_json_file(config_file_full_path, json_dict)
self._config_file_json_source = json_dict
@@ -420,8 +420,30 @@ class TestViewEditController(TestCase):
self._mocked_view_edit_page.config_file_combobox.currentText.return_value = \
TestViewEditController._expected_config_file_name
expected_json_dict: Dict[str, any] = {
"dummyKey": "dummyValue",
self._expected_account_id_attribute_name: self._expected_account_id_template_vale}
"dummyKey": "dummyValue"
}
mock_json_utils.validate_resources_according_to_json_schema.return_value = []
mock_json_utils.convert_resources_to_json_dict.return_value = expected_json_dict
mock_file_utils.join_path.return_value = TestViewEditController._expected_config_file_full_path
mocked_call_args: call = self._mocked_view_edit_page.save_changes_button.clicked.connect.call_args[0]
mocked_call_args[0]() # triggering save_changes_button connected function
mock_json_utils.convert_resources_to_json_dict.assert_called_once()
mock_json_utils.write_into_json_file.assert_called_once_with(
TestViewEditController._expected_config_file_full_path, expected_json_dict)
self._mocked_proxy_model.override_all_resources_status.assert_called_once_with(
ResourceMappingAttributesStatus(ResourceMappingAttributesStatus.SUCCESS_STATUS_VALUE,
[ResourceMappingAttributesStatus.SUCCESS_STATUS_VALUE]))
@patch("controller.view_edit_controller.file_utils")
@patch("controller.view_edit_controller.json_utils")
def test_page_save_changes_button_json_file_saved_and_template_account_id_unchanged(
self, mock_json_utils: MagicMock, mock_file_utils: MagicMock) -> None:
self._mocked_view_edit_page.config_file_combobox.currentText.return_value = \
TestViewEditController._expected_config_file_name
expected_json_dict: Dict[str, any] = {
self._expected_account_id_attribute_name: self._expected_account_id_template_vale
}
mock_json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME = self._expected_account_id_attribute_name
mock_json_utils.RESOURCE_MAPPING_ACCOUNTID_TEMPLATE_VALUE = self._expected_account_id_template_vale
mock_json_utils.validate_resources_according_to_json_schema.return_value = []
@@ -430,7 +452,31 @@ class TestViewEditController(TestCase):
mocked_call_args: call = self._mocked_view_edit_page.save_changes_button.clicked.connect.call_args[0]
mocked_call_args[0]() # triggering save_changes_button connected function
assert expected_json_dict["AccountId"] == self._mocked_configuration_manager.configuration.account_id
assert expected_json_dict[mock_json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME] == self._expected_account_id_template_vale
mock_json_utils.convert_resources_to_json_dict.assert_called_once()
mock_json_utils.write_into_json_file.assert_called_once_with(
TestViewEditController._expected_config_file_full_path, expected_json_dict)
self._mocked_proxy_model.override_all_resources_status.assert_called_once_with(
ResourceMappingAttributesStatus(ResourceMappingAttributesStatus.SUCCESS_STATUS_VALUE,
[ResourceMappingAttributesStatus.SUCCESS_STATUS_VALUE]))
@patch("controller.view_edit_controller.file_utils")
@patch("controller.view_edit_controller.json_utils")
def test_page_save_changes_button_json_file_saved_and_empty_account_id_unchanged(
self, mock_json_utils: MagicMock, mock_file_utils: MagicMock) -> None:
self._mocked_view_edit_page.config_file_combobox.currentText.return_value = \
TestViewEditController._expected_config_file_name
expected_json_dict: Dict[str, any] = {
self._expected_account_id_attribute_name: ''
}
mock_json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME = self._expected_account_id_attribute_name
mock_json_utils.validate_resources_according_to_json_schema.return_value = []
mock_json_utils.convert_resources_to_json_dict.return_value = expected_json_dict
mock_file_utils.join_path.return_value = TestViewEditController._expected_config_file_full_path
mocked_call_args: call = self._mocked_view_edit_page.save_changes_button.clicked.connect.call_args[0]
mocked_call_args[0]() # triggering save_changes_button connected function
assert expected_json_dict[mock_json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME] == ''
mock_json_utils.convert_resources_to_json_dict.assert_called_once()
mock_json_utils.write_into_json_file.assert_called_once_with(
TestViewEditController._expected_config_file_full_path, expected_json_dict)
@@ -103,6 +103,11 @@ class TestJsonUtils(TestCase):
invalid_json_dict.pop(json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME)
self.assertRaises(KeyError, json_utils.validate_json_dict_according_to_json_schema, invalid_json_dict)
def test_validate_json_dict_according_to_json_schema_raise_error_when_json_dict_has_empty_accountid(self) -> None:
valid_json_dict: Dict[str, any] = copy.deepcopy(TestJsonUtils._expected_json_dict)
valid_json_dict[json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME] = ''
json_utils.validate_json_dict_according_to_json_schema(valid_json_dict)
def test_validate_json_dict_according_to_json_schema_pass_when_json_dict_has_template_accountid(self) -> None:
valid_json_dict: Dict[str, any] = copy.deepcopy(TestJsonUtils._expected_json_dict)
valid_json_dict[json_utils.RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME] = \
@@ -28,7 +28,7 @@ _RESOURCE_MAPPING_JSON_FORMAT_VERSION: str = "1.0.0"
RESOURCE_MAPPING_ACCOUNTID_JSON_KEY_NAME: str = "AccountId"
RESOURCE_MAPPING_ACCOUNTID_TEMPLATE_VALUE: str = "EMPTY"
_RESOURCE_MAPPING_ACCOUNTID_PATTERN: str = f"^[0-9]{{12}}|{RESOURCE_MAPPING_ACCOUNTID_TEMPLATE_VALUE}$"
_RESOURCE_MAPPING_ACCOUNTID_PATTERN: str = f"^[0-9]{{12}}$|{RESOURCE_MAPPING_ACCOUNTID_TEMPLATE_VALUE}|^$"
_RESOURCE_MAPPING_REGION_PATTERN: str = "^[a-z]{2}-[a-z]{4,9}-[0-9]{1}$"
_RESOURCE_MAPPING_VERSION_PATTERN: str = "^[0-9]{1}.[0-9]{1}.[0-9]{1}$"