def _test_invalid_content(self, content):
        for path in [NETWORKS, PORTS, SUBNETS]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), POST, path.split('/'))
            with pytest.raises(BadRequestError):
                handler(NOT_RELEVANT, content, NOT_RELEVANT)

        for path in [
                NETWORK_ENTITY.format(network_id=7),
                PORT_ENTITY.format(port_id=7),
                SUBNET_ENTITY.format(subnet_id=7)
        ]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), PUT, path.split('/'))
            with pytest.raises(BadRequestError):
                handler(NOT_RELEVANT, content, NOT_RELEVANT)
    def test_post_port(self):
        nb_db = Mock()
        nb_db.add_port.return_value = {
            PortMapper.REST_PORT_ID: str(PORT_ID07),
            PortMapper.REST_PORT_NAME: 'port_name',
            PortMapper.REST_PORT_DEVICE_ID: 'device_id',
            PortMapper.REST_PORT_DEVICE_OWNER: 'oVirt',
            PortMapper.REST_PORT_NETWORK_ID: str(NETWORK_ID01),
            PortMapper.REST_PORT_MAC_ADDRESS: 'mac'
        }
        rest_input = ('{"port":{"name":"port_name", "mac_address":"mac",'
                      '"device_id":"device_id"}}')

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, PORTS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['port']['id'] == str(PORT_ID07)
        assert response_json['port']['name'] == 'port_name'
        assert response_json['port']['mac_address'] == 'mac'
        assert response_json['port']['device_id'] == 'device_id'
        assert response_json['port']['device_owner'] == 'oVirt'
        assert response_json['port']['network_id'] == str(NETWORK_ID01)

        rest_json = json.loads(rest_input)
        nb_db.add_port.assert_called_once_with(rest_json['port'])
    def test_delete_network(self):
        nb_db = Mock()

        handler, params = SelectingHandler.get_response_handler(
            responses(), DELETE,
            NETWORK_ENTITY.format(network_id=NETWORK_ID01).split('/'))
        handler(nb_db, NOT_RELEVANT, params)

        nb_db.delete_network.assert_called_once_with(str(NETWORK_ID01))
def test_post_tokens(mock_create_token, validate_token):
    handler, parameters = SelectingHandler.get_response_handler(
        responses(), POST, [TOKENS])
    response = handler(content=TOKEN_REQUEST, parameters=parameters)
    mock_create_token.assert_called_once_with(user_at_domain='joeuser',
                                              user_password='******')
    validate_token.assert_called_once_with(TOKEN)
    assert json.loads(response.body)['access']['token']['id'] == TOKEN
    assert response.code == 200
 def test_put_port(self):
     nb_db = Mock()
     nb_db.update_port.return_value = {
         PortMapper.REST_PORT_ID: str(PORT_ID07),
     }
     handler, params = SelectingHandler.get_response_handler(
         responses(), PUT, PORT_ENTITY.split('/'))
     response = handler(nb_db, '{"port" :{}}', {PORT_ID: str(PORT_ID07)})
     response_json = json.loads(response.body)
     assert response_json['port']['id'] == str(PORT_ID07)
    def test_delete_port(self):

        nb_db = Mock()

        handler, params = SelectingHandler.get_response_handler(
            responses(), DELETE,
            PORT_ENTITY.format(port_id=PORT_ID07).split('/'))
        handler(nb_db, NOT_RELEVANT, params)

        nb_db.delete_port.assert_called_once_with(str(PORT_ID07))
Exemplo n.º 7
0
    def test_check_neutron_responses_required_by_engine_are_present(self):
        for method in [GET, POST]:
            for path in [NETWORKS, PORTS, SUBNETS, ROUTERS]:
                handler, params = SelectingHandler.get_response_handler(
                    responses(), method, path.split('/'))
                assert handler is not None
                assert params is not None
                assert not params

        for method in [GET, PUT, DELETE]:
            for path in [
                    NETWORK_ENTITY.format(network_id=7),
                    PORT_ENTITY.format(port_id=7),
                    SUBNET_ENTITY.format(subnet_id=7),
                    ROUTER_ENTITY.format(router_id=7)
            ]:
                handler, params = SelectingHandler.get_response_handler(
                    responses(), method, path.split('/'))
                assert handler is not None
                assert params is not None
                assert params.values()[0] == '7'

        for path in [
                ADD_ROUTER_INTERFACE.format(router_id=7),
                DELETE_ROUTER_INTERFACE.format(router_id=7)
        ]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), PUT, path.split('/'))
            assert handler is not None
            assert params is not None
            assert params['router_id'] == '7'

        for path in [FLOATINGIPS, SECURITY_GROUPS]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), GET, path.split('/'))
            assert handler is not None

        # This is a test call engine makes to check if provider is alive
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, ''.split('/'))
        assert handler is not None
    def test_show_network(self):
        nb_db = Mock()
        nb_db.get_network.return_value = {
            NetworkMapper.REST_NETWORK_ID: str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME: NETWORK_NAME1,
        }
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, NETWORK_ENTITY.split('/'))

        response = handler(nb_db, NOT_RELEVANT, {NETWORK_ID: NETWORK_ID01})

        response_json = json.loads(response.body)
        assert response_json['network']['id'] == str(NETWORK_ID01)
        assert response_json['network']['name'] == NETWORK_NAME1
    def test_get_networks(self):
        nb_db = Mock()
        nb_db.list_networks.return_value = [{
            NetworkMapper.REST_NETWORK_ID:
            str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME:
            NETWORK_NAME1,
        }]
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, NETWORKS.split('/'))
        response = handler(nb_db, NOT_RELEVANT, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['networks'][0]['id'] == str(NETWORK_ID01)
        assert response_json['networks'][0]['name'] == NETWORK_NAME1
    def test_show_port(self):
        nb_db = Mock()
        nb_db.get_port.return_value = {
            PortMapper.REST_PORT_ID: str(PORT_ID07),
            PortMapper.REST_PORT_NAME: 'port_name',
            PortMapper.REST_PORT_SECURITY_GROUPS: [],
        }
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, PORT_ENTITY.split('/'))
        response = handler(nb_db, NOT_RELEVANT, {PORT_ID: PORT_ID07})

        response_json = json.loads(response.body)
        assert response_json['port']['id'] == str(PORT_ID07)
        assert response_json['port']['name'] == 'port_name'
        assert response_json['port']['security_groups'] == []
    def test_post_network(self):
        nb_db = Mock()
        nb_db.add_network.return_value = {
            NetworkMapper.REST_NETWORK_ID: str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME: NETWORK_NAME1,
            NetworkMapper.REST_TENANT_ID: ''
        }
        rest_input = '{"network":{"name":"network_name"}}'

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, NETWORKS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['network']['id'] == str(NETWORK_ID01)
        assert response_json['network']['name'] == NETWORK_NAME1

        rest_json = json.loads(rest_input)
        nb_db.add_network.assert_called_once_with(rest_json['network'])
Exemplo n.º 12
0
    def test_assign_responses(self):
        response_handlers = {}

        @rest('TEST1', 'test1', response_handlers)
        def decorated_method1():
            return 1

        @rest('TEST2', 'test2/test3', response_handlers)
        def decorated_method2():
            return 2

        @rest('TEST3', 'test4/*/test5/*', response_handlers)
        def decorated_method3():
            return 3

        @rest('TEST3', 'test4/test6/test5/*', response_handlers)
        def decorated_method4():
            return 4

        @rest('TEST3', 'test4/{id1}/test5/{id2}', response_handlers)
        def decorated_method5():
            return 5

        assert response_handlers['test1'][RESPONSE_VALUE_KEY]['TEST1']() == 1
        assert (response_handlers['test2']['test3'][RESPONSE_VALUE_KEY]
                ['TEST2']() == 2)
        assert (response_handlers['test4']['*']['test5']['*']
                [RESPONSE_VALUE_KEY]['TEST3']() == 3)
        assert (response_handlers['test4']['test6']['test5']['*']
                [RESPONSE_VALUE_KEY]['TEST3']() == 4)
        assert (response_handlers['test4'][RESPONSE_VALUE_PARAMETER]['id1']
                ['test5'][RESPONSE_VALUE_PARAMETER]['id2'][RESPONSE_VALUE_KEY]
                ['TEST3']() == 5)

        url = 'http://localhost:8080/v2.0/test4/11/test5/12'
        elements = filter(None, urlparse.urlparse(url).path.split('/'))[1:]
        handler, params = SelectingHandler.get_response_handler(
            response_handlers, 'TEST3', elements)
        assert handler() == 5
        assert params['id1'] == '11'
        assert params['id2'] == '12'
Exemplo n.º 13
0
    def test_post_routers(self, mock_connection):
        nb_db = OvnNorth()
        nb_db._add_router = Mock()
        nb_db._add_router.return_value = OvnRouterRow(
            'uuid', 'router1', {
                RouterMapper.OVN_ROUTER_GATEWAY_NETWORK: 'network_id',
                RouterMapper.OVN_ROUTER_GATEWAY_SUBNET: 'subnet_id',
                RouterMapper.OVN_ROUTER_GATEWAY_IP: '1.1.1.1',
            })
        rest_input = '''{
            "router": {
                "name": "router1",
                "external_gateway_info": {
                    "enable_snat": "true",
                    "external_fixed_ips": [{
                        "ip_address": "172.24.4.6",
                        "subnet_id": "b930d7f6-ceb7-40a0-8b81-a425dd994ccf"
                    }],
                    "network_id": "ae34051f-aa6c-4c75-abf5-50dc9ac99ef3"
                }
            }
        }'''

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, ROUTERS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        router = response_json['router']
        assert router[RouterMapper.REST_ROUTER_NAME] == 'router1'
        assert router[RouterMapper.REST_ROUTER_ID] == 'uuid'

        gateway = router[RouterMapper.REST_ROUTER_EXTERNAL_GATEWAY_INFO]
        ips = gateway[RouterMapper.REST_ROUTER_FIXED_IPS][0]
        assert gateway[RouterMapper.REST_ROUTER_NETWORK_ID] == 'network_id'
        assert ips[RouterMapper.REST_ROUTER_SUBNET_ID] == 'subnet_id'
        assert ips[RouterMapper.REST_ROUTER_IP_ADDRESS] == '1.1.1.1'

        nb_db._add_router.assert_called_once_with(
            'router1', True, 'ae34051f-aa6c-4c75-abf5-50dc9ac99ef3',
            'b930d7f6-ceb7-40a0-8b81-a425dd994ccf', '172.24.4.6')
class TestNeutronResponse(object):
    def test_check_neutron_responses_required_by_engine_are_present(self):
        for method in [GET, POST]:
            for path in [NETWORKS, PORTS, SUBNETS, ROUTERS]:
                handler, params = SelectingHandler.get_response_handler(
                    responses(), method, path.split('/'))
                assert handler is not None
                assert params is not None
                assert not params

        for method in [GET, PUT, DELETE]:
            for path in [
                    NETWORK_ENTITY.format(network_id=7),
                    PORT_ENTITY.format(port_id=7),
                    SUBNET_ENTITY.format(subnet_id=7),
                    ROUTER_ENTITY.format(router_id=7)
            ]:
                handler, params = SelectingHandler.get_response_handler(
                    responses(), method, path.split('/'))
                assert handler is not None
                assert params is not None
                assert params.values()[0] == '7'

        for path in [
                ADD_ROUTER_INTERFACE.format(router_id=7),
                DELETE_ROUTER_INTERFACE.format(router_id=7)
        ]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), PUT, path.split('/'))
            assert handler is not None
            assert params is not None
            assert params['router_id'] == '7'

        # This is a test call engine makes to check if provider is alive
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, ''.split('/'))
        assert handler is not None

    handler, params = SelectingHandler.get_response_handler(
        responses(), GET, [''])
    assert handler is not None

    def _test_invalid_content(self, content):
        for path in [NETWORKS, PORTS, SUBNETS]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), POST, path.split('/'))
            with pytest.raises(BadRequestError):
                handler(NOT_RELEVANT, content, NOT_RELEVANT)

        for path in [
                NETWORK_ENTITY.format(network_id=7),
                PORT_ENTITY.format(port_id=7),
                SUBNET_ENTITY.format(subnet_id=7)
        ]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), PUT, path.split('/'))
            with pytest.raises(BadRequestError):
                handler(NOT_RELEVANT, content, NOT_RELEVANT)

    def test_invalid_content_structure(self):
        self._test_invalid_content('{"invalid": null}')

    def test_invalid_content_json(self):
        self._test_invalid_content('invalid JSON')

    def test_show_network(self):
        nb_db = Mock()
        nb_db.get_network.return_value = {
            NetworkMapper.REST_NETWORK_ID: str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME: NETWORK_NAME1,
        }
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, NETWORK_ENTITY.split('/'))

        response = handler(nb_db, NOT_RELEVANT, {NETWORK_ID: NETWORK_ID01})

        response_json = json.loads(response.body)
        assert response_json['network']['id'] == str(NETWORK_ID01)
        assert response_json['network']['name'] == NETWORK_NAME1

    def test_show_port(self):
        nb_db = Mock()
        nb_db.get_port.return_value = {
            PortMapper.REST_PORT_ID: str(PORT_ID07),
            PortMapper.REST_PORT_NAME: 'port_name',
            PortMapper.REST_PORT_SECURITY_GROUPS: [],
        }
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, PORT_ENTITY.split('/'))
        response = handler(nb_db, NOT_RELEVANT, {PORT_ID: PORT_ID07})

        response_json = json.loads(response.body)
        assert response_json['port']['id'] == str(PORT_ID07)
        assert response_json['port']['name'] == 'port_name'
        assert response_json['port']['security_groups'] == []

    def test_get_networks(self):
        nb_db = Mock()
        nb_db.list_networks.return_value = [{
            NetworkMapper.REST_NETWORK_ID:
            str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME:
            NETWORK_NAME1,
        }]
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, NETWORKS.split('/'))
        response = handler(nb_db, NOT_RELEVANT, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['networks'][0]['id'] == str(NETWORK_ID01)
        assert response_json['networks'][0]['name'] == NETWORK_NAME1

    def test_get_ports(self):
        nb_db = Mock()
        nb_db.list_ports.return_value = [{
            PortMapper.REST_PORT_ID:
            str(PORT_ID07),
            PortMapper.REST_PORT_NAME:
            'port_name',
            PortMapper.REST_PORT_SECURITY_GROUPS: [],
        }]
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, PORTS.split('/'))

        response = handler(nb_db, NOT_RELEVANT, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['ports'][0]['id'] == str(PORT_ID07)
        assert response_json['ports'][0]['name'] == 'port_name'
        assert response_json['ports'][0]['security_groups'] == []

    def test_delete_network(self):
        nb_db = Mock()

        handler, params = SelectingHandler.get_response_handler(
            responses(), DELETE,
            NETWORK_ENTITY.format(network_id=NETWORK_ID01).split('/'))
        handler(nb_db, NOT_RELEVANT, params)

        nb_db.delete_network.assert_called_once_with(str(NETWORK_ID01))

    def test_delete_port(self):

        nb_db = Mock()

        handler, params = SelectingHandler.get_response_handler(
            responses(), DELETE,
            PORT_ENTITY.format(port_id=PORT_ID07).split('/'))
        handler(nb_db, NOT_RELEVANT, params)

        nb_db.delete_port.assert_called_once_with(str(PORT_ID07))

    def test_post_network(self):
        nb_db = Mock()
        nb_db.add_network.return_value = {
            NetworkMapper.REST_NETWORK_ID: str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME: NETWORK_NAME1,
            NetworkMapper.REST_TENANT_ID: ''
        }
        rest_input = '{"network":{"name":"network_name"}}'

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, NETWORKS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['network']['id'] == str(NETWORK_ID01)
        assert response_json['network']['name'] == NETWORK_NAME1

        rest_json = json.loads(rest_input)
        nb_db.add_network.assert_called_once_with(rest_json['network'])

    def test_post_port(self):
        nb_db = Mock()
        nb_db.add_port.return_value = {
            PortMapper.REST_PORT_ID: str(PORT_ID07),
            PortMapper.REST_PORT_NAME: 'port_name',
            PortMapper.REST_PORT_DEVICE_ID: 'device_id',
            PortMapper.REST_PORT_DEVICE_OWNER: 'oVirt',
            PortMapper.REST_PORT_NETWORK_ID: str(NETWORK_ID01),
            PortMapper.REST_PORT_MAC_ADDRESS: 'mac'
        }
        rest_input = ('{"port":{"name":"port_name", "mac_address":"mac",'
                      '"device_id":"device_id"}}')

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, PORTS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['port']['id'] == str(PORT_ID07)
        assert response_json['port']['name'] == 'port_name'
        assert response_json['port']['mac_address'] == 'mac'
        assert response_json['port']['device_id'] == 'device_id'
        assert response_json['port']['device_owner'] == 'oVirt'
        assert response_json['port']['network_id'] == str(NETWORK_ID01)

        rest_json = json.loads(rest_input)
        nb_db.add_port.assert_called_once_with(rest_json['port'])

    def test_put_port(self):
        nb_db = Mock()
        nb_db.update_port.return_value = {
            PortMapper.REST_PORT_ID: str(PORT_ID07),
        }
        handler, params = SelectingHandler.get_response_handler(
            responses(), PUT, PORT_ENTITY.split('/'))
        response = handler(nb_db, '{"port" :{}}', {PORT_ID: str(PORT_ID07)})
        response_json = json.loads(response.body)
        assert response_json['port']['id'] == str(PORT_ID07)
Exemplo n.º 15
0
class TestNeutronResponse(object):
    def test_check_neutron_responses_required_by_engine_are_present(self):
        for method in [GET, POST]:
            for path in [NETWORKS, PORTS, SUBNETS, ROUTERS]:
                handler, params = SelectingHandler.get_response_handler(
                    responses(), method, path.split('/'))
                assert handler is not None
                assert params is not None
                assert not params

        for method in [GET, PUT, DELETE]:
            for path in [
                    NETWORK_ENTITY.format(network_id=7),
                    PORT_ENTITY.format(port_id=7),
                    SUBNET_ENTITY.format(subnet_id=7),
                    ROUTER_ENTITY.format(router_id=7)
            ]:
                handler, params = SelectingHandler.get_response_handler(
                    responses(), method, path.split('/'))
                assert handler is not None
                assert params is not None
                assert params.values()[0] == '7'

        for path in [
                ADD_ROUTER_INTERFACE.format(router_id=7),
                DELETE_ROUTER_INTERFACE.format(router_id=7)
        ]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), PUT, path.split('/'))
            assert handler is not None
            assert params is not None
            assert params['router_id'] == '7'

        for path in [FLOATINGIPS, SECURITY_GROUPS]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), GET, path.split('/'))
            assert handler is not None

        # This is a test call engine makes to check if provider is alive
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, ''.split('/'))
        assert handler is not None

    handler, params = SelectingHandler.get_response_handler(
        responses(), GET, [''])
    assert handler is not None

    def _test_invalid_content(self, content):
        for path in [NETWORKS, PORTS, SUBNETS]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), POST, path.split('/'))
            with pytest.raises(BadRequestError):
                handler(NOT_RELEVANT, content, NOT_RELEVANT)

        for path in [
                NETWORK_ENTITY.format(network_id=7),
                PORT_ENTITY.format(port_id=7),
                SUBNET_ENTITY.format(subnet_id=7)
        ]:
            handler, params = SelectingHandler.get_response_handler(
                responses(), PUT, path.split('/'))
            with pytest.raises(BadRequestError):
                handler(NOT_RELEVANT, content, NOT_RELEVANT)

    def test_invalid_content_structure(self):
        self._test_invalid_content('{"invalid": null}')

    def test_invalid_content_json(self):
        self._test_invalid_content('invalid JSON')

    def test_show_network(self):
        nb_db = Mock()
        nb_db.get_network.return_value = {
            NetworkMapper.REST_NETWORK_ID: str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME: NETWORK_NAME1,
        }
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, NETWORK_ENTITY.split('/'))

        response = handler(nb_db, NOT_RELEVANT, {NETWORK_ID: NETWORK_ID01})

        response_json = json.loads(response.body)
        assert response_json['network']['id'] == str(NETWORK_ID01)
        assert response_json['network']['name'] == NETWORK_NAME1

    def test_show_port(self):
        nb_db = Mock()
        nb_db.get_port.return_value = {
            PortMapper.REST_PORT_ID: str(PORT_ID07),
            PortMapper.REST_PORT_NAME: 'port_name',
            PortMapper.REST_PORT_SECURITY_GROUPS: [],
        }
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, PORT_ENTITY.split('/'))
        response = handler(nb_db, NOT_RELEVANT, {PORT_ID: PORT_ID07})

        response_json = json.loads(response.body)
        assert response_json['port']['id'] == str(PORT_ID07)
        assert response_json['port']['name'] == 'port_name'
        assert response_json['port']['security_groups'] == []

    def test_get_networks(self):
        nb_db = Mock()
        nb_db.list_networks.return_value = [{
            NetworkMapper.REST_NETWORK_ID:
            str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME:
            NETWORK_NAME1,
        }]
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, NETWORKS.split('/'))
        response = handler(nb_db, NOT_RELEVANT, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['networks'][0]['id'] == str(NETWORK_ID01)
        assert response_json['networks'][0]['name'] == NETWORK_NAME1

    def test_get_ports(self):
        nb_db = Mock()
        nb_db.list_ports.return_value = [{
            PortMapper.REST_PORT_ID:
            str(PORT_ID07),
            PortMapper.REST_PORT_NAME:
            'port_name',
            PortMapper.REST_PORT_SECURITY_GROUPS: [],
        }]
        handler, params = SelectingHandler.get_response_handler(
            responses(), GET, PORTS.split('/'))

        response = handler(nb_db, NOT_RELEVANT, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['ports'][0]['id'] == str(PORT_ID07)
        assert response_json['ports'][0]['name'] == 'port_name'
        assert response_json['ports'][0]['security_groups'] == []

    def test_delete_network(self):
        nb_db = Mock()

        handler, params = SelectingHandler.get_response_handler(
            responses(), DELETE,
            NETWORK_ENTITY.format(network_id=NETWORK_ID01).split('/'))
        handler(nb_db, NOT_RELEVANT, params)

        nb_db.delete_network.assert_called_once_with(str(NETWORK_ID01))

    def test_delete_port(self):

        nb_db = Mock()

        handler, params = SelectingHandler.get_response_handler(
            responses(), DELETE,
            PORT_ENTITY.format(port_id=PORT_ID07).split('/'))
        handler(nb_db, NOT_RELEVANT, params)

        nb_db.delete_port.assert_called_once_with(str(PORT_ID07))

    def test_post_network(self):
        nb_db = Mock()
        nb_db.add_network.return_value = {
            NetworkMapper.REST_NETWORK_ID: str(NETWORK_ID01),
            NetworkMapper.REST_NETWORK_NAME: NETWORK_NAME1,
            NetworkMapper.REST_TENANT_ID: ''
        }
        rest_input = '{"network":{"name":"network_name"}}'

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, NETWORKS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['network']['id'] == str(NETWORK_ID01)
        assert response_json['network']['name'] == NETWORK_NAME1

        rest_json = json.loads(rest_input)
        nb_db.add_network.assert_called_once_with(rest_json['network'])

    def test_post_port(self):
        nb_db = Mock()
        nb_db.add_port.return_value = {
            PortMapper.REST_PORT_ID: str(PORT_ID07),
            PortMapper.REST_PORT_NAME: 'port_name',
            PortMapper.REST_PORT_DEVICE_ID: 'device_id',
            PortMapper.REST_PORT_DEVICE_OWNER: 'oVirt',
            PortMapper.REST_PORT_NETWORK_ID: str(NETWORK_ID01),
            PortMapper.REST_PORT_MAC_ADDRESS: 'mac'
        }
        rest_input = ('{"port":{"name":"port_name", "mac_address":"mac",'
                      '"device_id":"device_id"}}')

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, PORTS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        assert response_json['port']['id'] == str(PORT_ID07)
        assert response_json['port']['name'] == 'port_name'
        assert response_json['port']['mac_address'] == 'mac'
        assert response_json['port']['device_id'] == 'device_id'
        assert response_json['port']['device_owner'] == 'oVirt'
        assert response_json['port']['network_id'] == str(NETWORK_ID01)

        rest_json = json.loads(rest_input)
        nb_db.add_port.assert_called_once_with(rest_json['port'])

    def test_put_port(self):
        nb_db = Mock()
        nb_db.update_port.return_value = {
            PortMapper.REST_PORT_ID: str(PORT_ID07),
        }
        handler, params = SelectingHandler.get_response_handler(
            responses(), PUT, PORT_ENTITY.split('/'))
        response = handler(nb_db, '{"port" :{}}', {PORT_ID: str(PORT_ID07)})
        response_json = json.loads(response.body)
        assert response_json['port']['id'] == str(PORT_ID07)

    @mock.patch('ovsdbapp.backend.ovs_idl.connection', autospec=False)
    def test_post_routers(self, mock_connection):
        nb_db = OvnNorth()
        nb_db._add_router = Mock()
        nb_db._add_router.return_value = OvnRouterRow(
            'uuid', 'router1', {
                RouterMapper.OVN_ROUTER_GATEWAY_NETWORK: 'network_id',
                RouterMapper.OVN_ROUTER_GATEWAY_SUBNET: 'subnet_id',
                RouterMapper.OVN_ROUTER_GATEWAY_IP: '1.1.1.1',
            })
        rest_input = '''{
            "router": {
                "name": "router1",
                "external_gateway_info": {
                    "enable_snat": "true",
                    "external_fixed_ips": [{
                        "ip_address": "172.24.4.6",
                        "subnet_id": "b930d7f6-ceb7-40a0-8b81-a425dd994ccf"
                    }],
                    "network_id": "ae34051f-aa6c-4c75-abf5-50dc9ac99ef3"
                }
            }
        }'''

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, ROUTERS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        router = response_json['router']
        assert router[RouterMapper.REST_ROUTER_NAME] == 'router1'
        assert router[RouterMapper.REST_ROUTER_ID] == 'uuid'

        gateway = router[RouterMapper.REST_ROUTER_EXTERNAL_GATEWAY_INFO]
        ips = gateway[RouterMapper.REST_ROUTER_FIXED_IPS][0]
        assert gateway[RouterMapper.REST_ROUTER_NETWORK_ID] == 'network_id'
        assert ips[RouterMapper.REST_ROUTER_SUBNET_ID] == 'subnet_id'
        assert ips[RouterMapper.REST_ROUTER_IP_ADDRESS] == '1.1.1.1'

        nb_db._add_router.assert_called_once_with(
            'router1', True, 'ae34051f-aa6c-4c75-abf5-50dc9ac99ef3',
            'b930d7f6-ceb7-40a0-8b81-a425dd994ccf', '172.24.4.6')
def _test_invalid_content(content):
    handler, parameters = SelectingHandler.get_response_handler(
        responses(), POST, [TOKENS])
    with pytest.raises(BadRequestError):
        handler(content, NOT_RELEVANT)