def test_optional_requirements_config(self):
        class BaseRequirements(schema.Schema):
            driver = schema.StringItem(default="digitalocean", format="hidden")

        class SSHKeyFileSchema(schema.Schema):
            ssh_key_file = schema.StringItem(
                title="SSH Private Key",
                description=(
                    "The path to an SSH private key which will be used "
                    "to authenticate on the deployed VMs"
                ),
            )

        class SSHKeyNamesSchema(schema.Schema):
            ssh_key_names = schema.StringItem(
                title="SSH Key Names",
                description=(
                    "The names of an SSH key being managed on "
                    "DigitalOcean account which will be used to "
                    "authenticate on the deployed VMs"
                ),
            )

        class Requirements(BaseRequirements):
            title = "DigitalOcean"
            description = "DigitalOcean Cloud VM configuration requirements."

            personal_access_token = schema.StringItem(
                title="Personal Access Token",
                description=(
                    "This is the API access token which can be generated "
                    "under the API/Application on your account"
                ),
                required=True,
            )

            requirements_definition = schema.AnyOfItem(
                items=(
                    SSHKeyFileSchema.as_requirements_item(),
                    SSHKeyNamesSchema.as_requirements_item(),
                ),
            )(flatten=True)
            ssh_key_file = SSHKeyFileSchema(flatten=True)
            ssh_key_names = SSHKeyNamesSchema(flatten=True)

        expected = {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "title": "DigitalOcean",
            "description": "DigitalOcean Cloud VM configuration requirements.",
            "type": "object",
            "properties": {
                "driver": {
                    "default": "digitalocean",
                    "format": "hidden",
                    "type": "string",
                    "title": "driver",
                },
                "personal_access_token": {
                    "type": "string",
                    "description": (
                        "This is the API access token which can be "
                        "generated under the API/Application on your account"
                    ),
                    "title": "Personal Access Token",
                },
                "ssh_key_file": {
                    "type": "string",
                    "description": (
                        "The path to an SSH private key which will "
                        "be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Private Key",
                },
                "ssh_key_names": {
                    "type": "string",
                    "description": (
                        "The names of an SSH key being managed on DigitalOcean "
                        "account which will be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Key Names",
                },
            },
            "anyOf": [{"required": ["ssh_key_file"]}, {"required": ["ssh_key_names"]}],
            "required": ["personal_access_token"],
            "x-ordering": [
                "driver",
                "personal_access_token",
                "ssh_key_file",
                "ssh_key_names",
            ],
            "additionalProperties": False,
        }
        self.assertDictEqual(expected, Requirements.serialize())

        class Requirements2(BaseRequirements):
            title = "DigitalOcean"
            description = "DigitalOcean Cloud VM configuration requirements."

            personal_access_token = schema.StringItem(
                title="Personal Access Token",
                description=(
                    "This is the API access token which can be generated "
                    "under the API/Application on your account"
                ),
                required=True,
            )

            ssh_key_file = schema.StringItem(
                title="SSH Private Key",
                description=(
                    "The path to an SSH private key which will be used "
                    "to authenticate on the deployed VMs"
                ),
            )

            ssh_key_names = schema.StringItem(
                title="SSH Key Names",
                description=(
                    "The names of an SSH key being managed on "
                    "DigitalOcean account which will be used to "
                    "authenticate on the deployed VMs"
                ),
            )

            requirements_definition = schema.AnyOfItem(
                items=(
                    schema.RequirementsItem(requirements=["ssh_key_file"]),
                    schema.RequirementsItem(requirements=["ssh_key_names"]),
                ),
            )(flatten=True)

        expected = {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "title": "DigitalOcean",
            "description": "DigitalOcean Cloud VM configuration requirements.",
            "type": "object",
            "properties": {
                "driver": {
                    "default": "digitalocean",
                    "format": "hidden",
                    "type": "string",
                    "title": "driver",
                },
                "personal_access_token": {
                    "type": "string",
                    "description": (
                        "This is the API access token which can be "
                        "generated under the API/Application on your account"
                    ),
                    "title": "Personal Access Token",
                },
                "ssh_key_file": {
                    "type": "string",
                    "description": (
                        "The path to an SSH private key which will "
                        "be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Private Key",
                },
                "ssh_key_names": {
                    "type": "string",
                    "description": (
                        "The names of an SSH key being managed on DigitalOcean "
                        "account which will be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Key Names",
                },
            },
            "anyOf": [{"required": ["ssh_key_file"]}, {"required": ["ssh_key_names"]}],
            "required": ["personal_access_token"],
            "x-ordering": [
                "driver",
                "personal_access_token",
                "ssh_key_file",
                "ssh_key_names",
            ],
            "additionalProperties": False,
        }
        self.assertDictContainsSubset(expected, Requirements2.serialize())

        class Requirements3(schema.Schema):
            title = "DigitalOcean"
            description = "DigitalOcean Cloud VM configuration requirements."

            merge_reqs = Requirements(flatten=True)

        expected = {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "title": "DigitalOcean",
            "description": "DigitalOcean Cloud VM configuration requirements.",
            "type": "object",
            "properties": {
                "driver": {
                    "default": "digitalocean",
                    "format": "hidden",
                    "type": "string",
                    "title": "driver",
                },
                "personal_access_token": {
                    "type": "string",
                    "description": (
                        "This is the API access token which can be "
                        "generated under the API/Application on your account"
                    ),
                    "title": "Personal Access Token",
                },
                "ssh_key_file": {
                    "type": "string",
                    "description": (
                        "The path to an SSH private key which will "
                        "be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Private Key",
                },
                "ssh_key_names": {
                    "type": "string",
                    "description": (
                        "The names of an SSH key being managed on DigitalOcean "
                        "account which will be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Key Names",
                },
            },
            "anyOf": [{"required": ["ssh_key_file"]}, {"required": ["ssh_key_names"]}],
            "required": ["personal_access_token"],
            "x-ordering": [
                "driver",
                "personal_access_token",
                "ssh_key_file",
                "ssh_key_names",
            ],
            "additionalProperties": False,
        }
        self.assertDictContainsSubset(expected, Requirements3.serialize())

        class Requirements4(schema.Schema):
            title = "DigitalOcean"
            description = "DigitalOcean Cloud VM configuration requirements."

            merge_reqs = Requirements(flatten=True)

            ssh_key_file_2 = schema.StringItem(
                title="SSH Private Key",
                description=(
                    "The path to an SSH private key which will be used "
                    "to authenticate on the deployed VMs"
                ),
            )

            ssh_key_names_2 = schema.StringItem(
                title="SSH Key Names",
                description=(
                    "The names of an SSH key being managed on "
                    "DigitalOcean account which will be used to "
                    "authenticate on the deployed VMs"
                ),
            )

            requirements_definition_2 = schema.AnyOfItem(
                items=(
                    schema.RequirementsItem(requirements=["ssh_key_file_2"]),
                    schema.RequirementsItem(requirements=["ssh_key_names_2"]),
                ),
            )(flatten=True)

        expected = {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "title": "DigitalOcean",
            "description": "DigitalOcean Cloud VM configuration requirements.",
            "type": "object",
            "properties": {
                "driver": {
                    "default": "digitalocean",
                    "format": "hidden",
                    "type": "string",
                    "title": "driver",
                },
                "personal_access_token": {
                    "type": "string",
                    "description": (
                        "This is the API access token which can be "
                        "generated under the API/Application on your account"
                    ),
                    "title": "Personal Access Token",
                },
                "ssh_key_file": {
                    "type": "string",
                    "description": (
                        "The path to an SSH private key which will "
                        "be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Private Key",
                },
                "ssh_key_names": {
                    "type": "string",
                    "description": (
                        "The names of an SSH key being managed on DigitalOcean "
                        "account which will be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Key Names",
                },
                "ssh_key_file_2": {
                    "type": "string",
                    "description": (
                        "The path to an SSH private key which will "
                        "be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Private Key",
                },
                "ssh_key_names_2": {
                    "type": "string",
                    "description": (
                        "The names of an SSH key being managed on DigitalOcean "
                        "account which will be used to authenticate on the deployed VMs"
                    ),
                    "title": "SSH Key Names",
                },
            },
            "anyOf": [
                {"required": ["ssh_key_file"]},
                {"required": ["ssh_key_names"]},
                {"required": ["ssh_key_file_2"]},
                {"required": ["ssh_key_names_2"]},
            ],
            "required": ["personal_access_token"],
            "x-ordering": [
                "driver",
                "personal_access_token",
                "ssh_key_file",
                "ssh_key_names",
                "ssh_key_file_2",
                "ssh_key_names_2",
            ],
            "additionalProperties": False,
        }
        self.assertDictContainsSubset(expected, Requirements4.serialize())