def test_create_parent__success(self, **kwargs):
        mock = kwargs['mock']
        session = start_session(mock)
        obj = ConfigObject(ENTERPRISE_SPEC_TEST)
        test_id = "741fc5d9-fce7-4f98-9172-e962be6ee3e2"

        assert obj.get_name() == "Enterprise"

        obj.name = "test_enterprise"
        resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]

        mock.post(build_standard_mock_url(resource_name),
                  status_code=201,
                  json=[{
                      "name": "test_enterprise",
                      "ID": test_id
                  }])

        session.root_object.current_child_name = resource_name
        session.root_object.create_child(obj)

        last_request = mock.last_request
        json_data = last_request.json()
        assert "ID" in json_data
        assert json_data["ID"] is None
        assert "name" in json_data
        assert json_data["name"] == "test_enterprise"

        assert obj.name == "test_enterprise"
        assert obj.id == test_id
    def test_find_child__not_found(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        parent_obj = ConfigObject(ENTERPRISE_SPEC_TEST)
        parent_test_id = "741fc5d9-fce7-4f98-9172-e962be6ee3e2"

        parent_obj.id = parent_test_id
        assert hasattr(parent_obj, 'id') is True

        fetcher = Fetcher(parent_obj, DOMAINTMPL_SPEC_TEST)

        parent_resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]
        child_resource_name = DOMAINTMPL_SPEC_TEST["model"]["resource_name"]

        mock.get(build_standard_mock_url(parent_resource_name + '/' +
                                         parent_test_id + '/' +
                                         child_resource_name),
                 status_code=200)

        objects = fetcher.get(filter='name is "test_domain_template"')

        last_request = mock.last_request
        request_headers = last_request.headers
        assert "X-Nuage-Filter" in request_headers
        assert request_headers["X-Nuage-Filter"] == (
            'name is "test_domain_template"')

        assert len(objects) == 0
    def test__invalid(self, attr, value, message):
        obj = ConfigObject(ENTERPRISE_SPEC_VALIDATE)

        # Name is required value, make sure it is set
        obj.name = 'a'

        setattr(obj, attr, value)

        assert obj.validate() is False
        attr_error = obj.errors[attr]
        assert message in attr_error['description']
    def test_create_child__conflict(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        parent_obj = ConfigObject(ENTERPRISE_SPEC_TEST)
        parent_test_id = "741fc5d9-fce7-4f98-9172-e962be6ee3e2"

        parent_obj.id = parent_test_id
        assert hasattr(parent_obj, 'id') is True

        child_obj = ConfigObject(DOMAINTMPL_SPEC_TEST)

        assert child_obj.get_name() == "DomainTemplate"

        child_obj.name = "test_domain_template"
        assert hasattr(child_obj, 'name') is True

        parent_resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]
        child_resource_name = DOMAINTMPL_SPEC_TEST["model"]["resource_name"]

        mock.post(build_standard_mock_url(parent_resource_name + "/" +
                                          parent_test_id + "/" +
                                          child_resource_name),
                  status_code=409,
                  json={
                      "errors": [{
                          "property":
                          "name",
                          "descriptions": [{
                              "title":
                              "Resource in use",
                              "description":
                              "name(test_domain_template) is in "
                              "use. Please retry with a different value."
                          }]
                      }],
                      "internalErrorCode":
                      2510
                  })

        parent_obj.current_child_name = child_resource_name
        with pytest.raises(BambouHTTPError) as e:
            parent_obj.create_child(child_obj)

        last_request = mock.last_request
        json_data = last_request.json()
        assert "ID" in json_data
        assert json_data["ID"] is None
        assert "name" in json_data
        assert json_data["name"] == "test_domain_template"

        assert '409' in str(e)
        assert 'in use' in str(e)
        assert 'test_domain_template' in str(e)
    def test_find_child__multiple(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        parent_obj = ConfigObject(ENTERPRISE_SPEC_TEST)
        parent_test_id = "741fc5d9-fce7-4f98-9172-e962be6ee3e2"

        parent_obj.id = parent_test_id
        assert hasattr(parent_obj, 'id') is True

        fetcher = Fetcher(parent_obj, DOMAINTMPL_SPEC_TEST)

        child_test_id_1 = "e5b683ed-5c24-4d43-bac9-181b6d4eb63b"
        child_test_id_2 = "e5b683ed-5c24-4d43-bac9-181b6d4eb63c"

        parent_resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]
        child_resource_name = DOMAINTMPL_SPEC_TEST["model"]["resource_name"]

        mock.get(build_standard_mock_url(parent_resource_name + '/' +
                                         parent_test_id + '/' +
                                         child_resource_name),
                 status_code=200,
                 json=[{
                     "name": "test_domain_template",
                     "ID": child_test_id_1
                 }, {
                     "name": "test_domain_template",
                     "ID": child_test_id_2
                 }])

        objects = fetcher.get(filter='name is "test_domain_template"')

        last_request = mock.last_request
        request_headers = last_request.headers
        assert "X-Nuage-Filter" in request_headers
        assert request_headers["X-Nuage-Filter"] == (
            'name is "test_domain_template"')

        assert len(objects) == 2
        obj_1 = objects[0]
        obj_2 = objects[1]

        assert obj_1.get_name() == "DomainTemplate"
        assert hasattr(obj_1, 'name') is True
        assert obj_1.name == "test_domain_template"
        assert hasattr(obj_1, 'id') is True
        assert obj_1.id == child_test_id_1

        assert obj_2.get_name() == "DomainTemplate"
        assert hasattr(obj_2, 'name') is True
        assert obj_2.name == "test_domain_template"
        assert hasattr(obj_2, 'id') is True
        assert obj_2.id == child_test_id_2
    def test__invalid_without_required(self, attr, value, message):
        obj = ConfigObject(ENTERPRISE_SPEC_VALIDATE)

        # Name is required value, make sure it is set
        obj.name = 'a'

        setattr(obj, attr, value)

        if (message == "value is mandatory"):
            assert obj.validate(skip_required_check=True) is True
        else:
            assert obj.validate(skip_required_check=True) is False
            attr_error = obj.errors[attr]
            assert message in attr_error['description']
    def test_find_child__invalid_parent(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        parent_obj = ConfigObject(ENTERPRISE_SPEC_TEST)
        parent_test_id = "741fc5d9-fce7-4f98-9172-e962be6ee3e2"

        parent_obj.id = parent_test_id
        assert hasattr(parent_obj, 'id') is True

        fetcher = Fetcher(parent_obj, DOMAINTMPL_SPEC_TEST)

        parent_resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]
        child_resource_name = DOMAINTMPL_SPEC_TEST["model"]["resource_name"]

        mock.get(build_standard_mock_url(parent_resource_name + '/' +
                                         parent_test_id + '/' +
                                         child_resource_name),
                 status_code=404,
                 json={
                     "description":
                     "Cannot find enterprise with ID "
                     "741fc5d9-fce7-4f98-9172-e962be6ee3e2",
                     "title":
                     "enterprise not found",
                     "errors": [{
                         "property":
                         "",
                         "descriptions": [{
                             "title":
                             "enterprise not found",
                             "description":
                             "Cannot find enterprise with ID "
                             "741fc5d9-fce7-4f98-9172-e962be6ee3e2"
                         }]
                     }]
                 })

        with pytest.raises(BambouHTTPError) as e:
            fetcher.get(filter='name is "test_domain_template"')

        last_request = mock.last_request
        request_headers = last_request.headers
        assert "X-Nuage-Filter" in request_headers
        assert request_headers["X-Nuage-Filter"] == (
            'name is "test_domain_template"')

        assert '404' in str(e)
        assert 'enterprise not found' in str(e)
        assert parent_test_id in str(e)
    def test_delete_child__success(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        obj = ConfigObject(DOMAINTMPL_SPEC_TEST)
        test_id = "e5b683ed-5c24-4d43-bac9-181b6d4eb63b"

        assert obj.get_name() == "DomainTemplate"

        obj.id = test_id
        assert hasattr(obj, 'id') is True
        obj.name = "test_domain_template"
        assert hasattr(obj, 'name') is True
        resource_name = DOMAINTMPL_SPEC_TEST["model"]["resource_name"]

        mock.delete(build_standard_mock_url(resource_name + "/" + test_id +
                                            "?responseChoice=1"),
                    status_code=204)

        obj.delete()

        last_request = mock.last_request
        json_data = last_request.json()
        assert "ID" in json_data
        assert json_data["ID"] == test_id
        assert "name" in json_data
        assert json_data["name"] == "test_domain_template"
    def test_delete_parent__success(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        obj = ConfigObject(ENTERPRISE_SPEC_TEST)
        test_id = "741fc5d9-fce7-4f98-9172-e962be6ee3e2"

        assert obj.get_name() == "Enterprise"

        obj.id = test_id
        assert hasattr(obj, 'id') is True
        obj.name = "test_enterprise"
        assert hasattr(obj, 'name') is True
        resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]

        mock.delete(build_standard_mock_url(resource_name + "/" + test_id +
                                            "?responseChoice=1"),
                    status_code=204)

        obj.delete()

        last_request = mock.last_request
        json_data = last_request.json()
        assert "ID" in json_data
        assert json_data["ID"] == test_id
        assert "name" in json_data
        assert json_data["name"] == "test_enterprise"
    def test_create_parent__conflict(self, **kwargs):
        mock = kwargs['mock']
        session = start_session(mock)
        obj = ConfigObject(ENTERPRISE_SPEC_TEST)

        assert obj.get_name() == "Enterprise"

        obj.name = "test_enterprise"
        assert hasattr(obj, "name")

        resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]

        mock.post(build_standard_mock_url(resource_name),
                  status_code=409,
                  json={
                      "errors": [{
                          "property":
                          "name",
                          "descriptions": [{
                              "title":
                              "Cannot create duplicate entity.",
                              "description":
                              "Another Enterprise with the same"
                              " name = test_enterprise exists."
                          }]
                      }],
                      "internalErrorCode":
                      9501
                  })

        session.root_object.current_child_name = resource_name
        with pytest.raises(BambouHTTPError) as e:
            session.root_object.create_child(obj)

        last_request = mock.last_request
        json_data = last_request.json()
        assert "ID" in json_data
        assert json_data["ID"] is None
        assert "name" in json_data
        assert json_data["name"] == "test_enterprise"

        assert '409' in str(e)
        assert 'duplicate' in str(e)
        assert 'test_enterprise exists' in str(e)
    def test_assign__invalid_id(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        parent_id = "bff6a2a3-0ac2-4891-b4e5-099741e6826d"
        child_id = "foobar"

        resource_name = NETMACGRP_SPEC_TEST["model"]["resource_name"]
        child_resource_name = ENTERPRISENET_SPEC_TEST["model"]["resource_name"]

        mock.put(build_standard_mock_url(resource_name + "/" + parent_id +
                                         "/" + child_resource_name),
                 status_code=409,
                 json={
                     "errors": [{
                         "property":
                         "",
                         "descriptions": [{
                             "title":
                             "Invalid id list for entity "
                             "enterprisenetwork",
                             "description":
                             "Invalid id list for entity "
                             "enterprisenetwork , invalid ids "
                             "[foobar]"
                         }]
                     }],
                     "internalErrorCode":
                     9450
                 })

        obj = ConfigObject(NETMACGRP_SPEC_TEST)
        obj.id = parent_id
        obj.current_child_name = child_resource_name

        child_obj = ConfigObject(ENTERPRISENET_SPEC_TEST)
        child_obj.id = child_id

        with pytest.raises(BambouHTTPError) as e:
            obj.assign([child_obj], nurest_object_type=None)

        last_request = mock.last_request
        json_data = last_request.json()
        assert json_data == [child_id]

        assert 'HTTP 409' in str(e)
        assert 'invalid id' in str(e)
        assert child_id in str(e)
    def test_create_child__success(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        parent_obj = ConfigObject(ENTERPRISE_SPEC_TEST)
        parent_test_id = "741fc5d9-fce7-4f98-9172-e962be6ee3e2"
        child_test_id = "e5b683ed-5c24-4d43-bac9-181b6d4eb63b"

        parent_obj.id = parent_test_id
        assert hasattr(parent_obj, 'id') is True

        child_obj = ConfigObject(DOMAINTMPL_SPEC_TEST)

        assert child_obj.get_name() == "DomainTemplate"

        child_obj.name = "test_domain_template"
        assert hasattr(child_obj, 'name') is True

        parent_resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]
        child_resource_name = DOMAINTMPL_SPEC_TEST["model"]["resource_name"]

        mock.post(build_standard_mock_url(parent_resource_name + "/" +
                                          parent_test_id + "/" +
                                          child_resource_name),
                  status_code=201,
                  json=[{
                      "name": "test_domain_template",
                      "ID": child_test_id
                  }])

        parent_obj.current_child_name = child_resource_name
        parent_obj.create_child(child_obj)

        last_request = mock.last_request
        json_data = last_request.json()
        assert "ID" in json_data
        assert json_data["ID"] is None
        assert "name" in json_data
        assert json_data["name"] == "test_domain_template"

        assert child_obj.name == "test_domain_template"
        assert child_obj.id == child_test_id
    def test_delete_child__not_found(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        obj = ConfigObject(DOMAINTMPL_SPEC_TEST)
        test_id = "e5b683ed-5c24-4d43-bac9-181b6d4eb63b"

        assert obj.get_name() == "DomainTemplate"

        obj.id = test_id
        assert hasattr(obj, 'id') is True
        obj.name = "test_domain_template"
        assert hasattr(obj, 'name') is True
        resource_name = DOMAINTMPL_SPEC_TEST["model"]["resource_name"]

        mock.delete(build_standard_mock_url(resource_name + "/" + test_id +
                                            "?responseChoice=1"),
                    status_code=404,
                    json={
                        "description":
                        "Cannot find domaintemplate with ID "
                        "e5b683ed-5c24-4d43-bac9-181b6d4eb63b",
                        "title":
                        "domaintemplate not found",
                        "errors": [{
                            "property":
                            "",
                            "descriptions": [{
                                "title":
                                "domaintemplate not found",
                                "description":
                                "Cannot find domaintemplate "
                                "with ID e5b683ed-5c24-4d43-bac9-"
                                "181b6d4eb63b"
                            }]
                        }]
                    })

        with pytest.raises(BambouHTTPError) as e:
            obj.delete()

        last_request = mock.last_request
        json_data = last_request.json()
        assert "ID" in json_data
        assert json_data["ID"] == test_id
        assert "name" in json_data
        assert json_data["name"] == "test_domain_template"

        assert '404' in str(e)
        assert 'domaintemplate not found' in str(e)
        assert test_id in str(e)
    def test_delete_parent__not_found(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        obj = ConfigObject(ENTERPRISE_SPEC_TEST)
        test_id = "741fc5d9-fce7-4f98-9172-e962be6ee3e2"

        assert obj.get_name() == "Enterprise"

        obj.id = test_id
        assert hasattr(obj, 'id') is True
        obj.name = "test_enterprise"
        assert hasattr(obj, 'name') is True
        resource_name = ENTERPRISE_SPEC_TEST["model"]["resource_name"]

        mock.delete(build_standard_mock_url(resource_name + "/" + test_id +
                                            "?responseChoice=1"),
                    status_code=404,
                    json={
                        "description":
                        "Cannot find enterprise with ID "
                        "741fc5d9-fce7-4f98-9172-e962be6ee3e2",
                        "title":
                        "enterprise not found",
                        "errors": [{
                            "property":
                            "",
                            "descriptions": [{
                                "title":
                                "enterprise not found",
                                "description":
                                "Cannot find enterprise "
                                "with ID 741fc5d9-fce7-4f98-9172-"
                                "e962be6ee3e2"
                            }]
                        }]
                    })

        with pytest.raises(BambouHTTPError) as e:
            obj.delete()

        last_request = mock.last_request
        json_data = last_request.json()
        assert "ID" in json_data
        assert json_data["ID"] == test_id
        assert "name" in json_data
        assert json_data["name"] == "test_enterprise"

        assert '404' in str(e)
        assert 'enterprise not found' in str(e)
        assert test_id in str(e)
    def test_assign__success(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        parent_id = "bff6a2a3-0ac2-4891-b4e5-099741e6826d"
        child_id = "0d4a68c5-b351-45a2-80e4-fdb880016ef5"

        resource_name = NETMACGRP_SPEC_TEST["model"]["resource_name"]
        child_resource_name = ENTERPRISENET_SPEC_TEST["model"]["resource_name"]

        mock.put(build_standard_mock_url(resource_name + "/" + parent_id +
                                         "/" + child_resource_name),
                 status_code=204)

        obj = ConfigObject(NETMACGRP_SPEC_TEST)
        obj.id = parent_id
        obj.current_child_name = child_resource_name

        child_obj = ConfigObject(ENTERPRISENET_SPEC_TEST)
        child_obj.id = child_id
        obj.assign([child_obj], nurest_object_type=None)

        last_request = mock.last_request
        json_data = last_request.json()
        assert json_data == [child_id]
    def test_unassign__success(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        test_id = "bff6a2a3-0ac2-4891-b4e5-099741e6826d"

        resource_name = NETMACGRP_SPEC_TEST["model"]["resource_name"]
        child_resource_name = ENTERPRISENET_SPEC_TEST["model"]["resource_name"]

        mock.put(build_standard_mock_url(resource_name + "/" + test_id + "/" +
                                         child_resource_name),
                 status_code=204)

        obj = ConfigObject(NETMACGRP_SPEC_TEST)
        obj.id = test_id
        obj.current_child_name = child_resource_name
        obj.assign([], nurest_object_type=None)

        last_request = mock.last_request
        json_data = last_request.json()
        assert json_data == list()
    def test__valid(self):
        obj = ConfigObject(ENTERPRISE_SPEC_VALIDATE)

        obj.name = 'a'
        assert hasattr(obj, 'name') is True
        assert obj.validate() is True

        obj.name = 'a' * 255
        assert hasattr(obj, 'name') is True
        assert obj.validate() is True

        obj.name = 'a'
        obj.enableapplicationperformancemanagement = False
        obj.floatingipsquota = 0
        obj.allowedforwardingclasses = []
        obj.encryptionmanagementmode = "DISABLED"
        assert obj.validate() is True

        obj.name = 'a' * 255
        obj.enableapplicationperformancemanagement = True
        obj.floatingipsquota = 250000
        obj.allowedforwardingclasses = ["A", "B"]
        obj.encryptionmanagementmode = "MANAGED"
        assert obj.validate() is True
    def test_new_object__success(self, **kwargs):
        mock = kwargs['mock']
        start_session(mock)
        obj = ConfigObject(ENTERPRISE_SPEC_TEST)

        assert obj.get_name() == "Enterprise"