def test_layout_to_incidents_dict(self, tmpdir):
        """
        Given:
        - Incident types of the pack.

        When:
        - Creating a dict of key as layout ID and value as list of incident type IDs whom layout field equals to layout
          ID.

        Then:
        - Ensure expected dict is returned.

        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_incident_type('ExtraHop_Detection',
                                       util_load_json(self.INCIDENT_TYPE_ONE))
        fake_pack.create_incident_type('ExtraHop_Detection_2',
                                       util_load_json(self.INCIDENT_TYPE_TWO))
        fake_pack_path = fake_pack.path
        layout_converter = LayoutBelowSixConverter(Pack(fake_pack_path))
        result = LayoutBelowSixConverter.layout_to_indicators_or_incidents_dict(
            layout_converter.pack.incident_types)
        assert result == {
            'ExtraHop Detection':
            ['ExtraHop Detection', 'ExtraHop Detection 2']
        }
    def test_convert_dir(self, tmpdir):
        """
        Given:
        - Pack.

        When:
        - Converting every layout of version 6.0.0 and above to version 5.9.9 and below.

        Then:
        - Ensure expected layouts are created with expected values.
        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_incident_type('ExtraHop_Detection',
                                       util_load_json(self.INCIDENT_TYPE_ONE))
        fake_pack.create_layoutcontainer('ExtraHop Detection',
                                         util_load_json(self.LAYOUT_CONTAINER))
        fake_pack_path = fake_pack.path
        layout_converter = LayoutBelowSixConverter(Pack(fake_pack_path))
        layout_converter.convert_dir()
        test_data_json = util_load_json(
            os.path.join(
                __file__,
                f'{git_path()}/demisto_sdk/commands/convert/converters/layout/'
                'tests/test_data'
                '/layout_up_to_5_9_9_expected_convert_dir_test_file_output.json'
            ))
        for layout_field_name, layout_data in test_data_json.items():
            expected_new_layout_path = f'{str(layout_converter.pack.path)}/Layouts/layout-{layout_field_name}-' \
                                       'ExtraHop_Detection.json'
            assert os.path.exists(expected_new_layout_path)
            assert util_load_json(expected_new_layout_path) == layout_data
            os.remove(expected_new_layout_path)
 def convert(self) -> int:
     if self.server_version >= self.VERSION_6_0_0:
         layout_converter: LayoutBaseConverter = LayoutSixConverter(
             self.pack)
     else:
         layout_converter = LayoutBelowSixConverter(self.pack)
     convert_result = layout_converter.convert_dir()
     if not convert_result:
         click.secho(
             f'Converted Layouts successfully in pack: {str(self.pack.path)}',
             fg='green')
     return convert_result
    def test_calculate_new_layout_relative_path(self, tmpdir,
                                                dynamic_field_key: str,
                                                type_id: str,
                                                expected_suffix: str):
        """
        Given:
        - 'layout_id': Layout ID of the new layout created.
        - 'dynamic_key_field': The dynamic key field related to the new layout created until version 5.9.9.
        - 'type_id': Type ID of the corresponding incident/indicator

        When:
        - Calculating the path to the newly created layout.

        Then:
        - Ensure the expected path is returned.

        """
        layout_converter = LayoutBelowSixConverter(Pack(tmpdir))
        expected = f'{layout_converter.pack.path}/Layouts/{expected_suffix}'
        assert layout_converter.calculate_new_layout_relative_path(
            type_id, dynamic_field_key) == expected
    def test_calculate_from_version(self, tmpdir, layout_id: str,
                                    layout_kind: str, expected: str):
        """
        Given:
        - 'layout_id' Layout ID.
        - 'layout_kind' Layout kind.
        - 'current_old_layouts': Old layouts existing in the Layouts directory before the conversion.

        When:
        - Calculating the from version for the new layout to be created.

        Then:
        - Ensure that from version from existing layout is returned if corresponding layout exists with from version,
          else 'MINIMAL_FROM_VERSION' is returned.
        """
        full_layout_path = os.path.join(
            __file__,
            f'{git_path()}/demisto_sdk/commands/convert/converters/layout/tests/'
            'test_data/layout-close-ExtraHop_Detection.json')
        assert LayoutBelowSixConverter(Pack(tmpdir)).calculate_from_version(
            layout_id, layout_kind, [Layout(full_layout_path)]) == expected
    def test_build_old_layout(self, tmpdir, layout_id: str,
                              incident_type_id: str, dynamic_field_key: str,
                              dynamic_field_value: str, expected: Dict):
        """
        Given:
        - 'layout_id': Old layout ID to be created.
        - 'incident_type_id': Incident type ID corresponding to the newly created old layout.
        - 'dynamic_field_key': The dynamic field key from whom the layout will be created.
        - 'dynamic_field_value': The dynamic field value from whom the layout will be created.

        When:
        - Creating layout of the format below 6.0.0 version.

        Then:
        - Ensure expected dict is returned.

        """
        assert LayoutBelowSixConverter(Pack(tmpdir)).build_old_layout(
            layout_id,
            incident_type_id,
            dynamic_field_key,
            dynamic_field_value,
            from_version='4.1.0') == expected