Esempio n. 1
0
    def test_get_area_by_name(self):
        """Test case for get_area_by_name

        Retrieve a Area resource by unique name.
        """
        # check when name does not exist
        headers = {
            'Accept': 'application/json',
        }
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/{name}'.format(name='name_example'),
            method='GET',
            headers=headers)
        self.assertStatus(
            response, HTTPStatus.NOT_FOUND,
            'Response body is : ' + response.data.decode('utf-8'))

        # check when name exist exist =>

        # step1) add an entries
        # step2) check if I can get the entry via api

        # step 1
        self._addAreas()
        areas = getAreas()
        self.assertEqual(len(areas), 2)

        # step 2
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/{name}'.format(name='Luigi Pirelli'),
            method='GET',
            headers=headers)
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        # check when more than one key containing a search name =>
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/{name}'.format(name='Luigi'),
            method='GET',
            headers=headers)
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        result_areas = json.loads(response.data.decode('utf-8'))
        self.assertEqual(len(result_areas), 2)

        areas = getAreas()
        for area in result_areas:
            name = area['name']
            self.assertEqual(areas[name].to_dict(), area)
Esempio n. 2
0
    def test_get_area_by_date(self):
        """Test case for get_area_by_date

        Retrieve a Area with a specified data.
        """
        # check when date does not exist
        headers = {
            'Accept': 'application/json',
        }
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/date/{date}'.format(
                date='9999-12-31'),
            method='GET',
            headers=headers)
        self.assertStatus(
            response, HTTPStatus.NOT_FOUND,
            'Response body is : ' + response.data.decode('utf-8'))

        # check when date exist

        # step1) add an entries
        # step2) check if I can get the entry via api

        # step 1
        self._addAreas()
        areas = getAreas()
        self.assertEqual(len(areas), 2)

        # step 2
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/date/{date}'.format(
                date='2021-04-18'),
            method='GET',
            headers=headers)
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        result_areas = json.loads(response.data.decode('utf-8'))
        self.assertEqual(len(result_areas), 1)

        areas = getAreas()
        for area in result_areas:
            name = area['name']
            self.assertEqual(areas[name].to_dict(), area)
Esempio n. 3
0
    def test_get_area_by_properties(self):
        """Test case for get_area_by_properties

        Retrieve a Area with a specified data set of properties.
        """
        props = {"key1": "value1", "key2": "value2"}
        headers = {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        }
        response = self.client.open('/luipir/geo_test/1.0.0/areas/properties',
                                    method='POST',
                                    headers=headers,
                                    data=json.dumps(props),
                                    content_type='application/json')
        self.assertStatus(
            response, HTTPStatus.NOT_FOUND,
            'Response body is : ' + response.data.decode('utf-8'))

        # check when name exist exist =>

        # step1) add an entries
        # step2) check if I can get the entry via api
        # step3) check if nothing is found but something is stored

        # step 1
        self._addAreas()
        areas = getAreas()
        self.assertEqual(len(areas), 2)

        # step 2
        response = self.client.open('/luipir/geo_test/1.0.0/areas/properties',
                                    method='POST',
                                    headers=headers,
                                    data=json.dumps(props),
                                    content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        result_areas = json.loads(response.data.decode('utf-8'))
        self.assertEqual(len(result_areas), 1)
        self.assertEqual(result_areas[0]['name'], 'Luigi Pirelli')

        # step 3
        props = {"key1": "value1111", "key2": "value2222"}
        response = self.client.open('/luipir/geo_test/1.0.0/areas/properties',
                                    method='POST',
                                    headers=headers,
                                    data=json.dumps(props),
                                    content_type='application/json')
        self.assertStatus(
            response, HTTPStatus.NOT_FOUND,
            'Response body is : ' + response.data.decode('utf-8'))
def delete_are_by_name(name):  # noqa: E501
    """Delete a Area resource by name.

    Delete a Area resource by name.  # noqa: E501

    :param name: resource unique name
    :type name: str

    :rtype: List[Area]
    """
    areas = getAreas()
    if areas.pop(name, None) is None:
        abort(HTTPStatus.NOT_FOUND)

    return areas, HTTPStatus.OK
Esempio n. 5
0
def get_area_by_name(name):  # noqa: E501
    """Retrieve a Area resource by unique name.

    Retrieve a Area resource by unique name.  # noqa: E501

    :param name: resource unique name
    :type name: str

    :rtype: List[Area]
    """
    areas = getAreas()
    results = [val for key, val in areas.items() if name in key]
    if len(results) == 0:
        abort(HTTPStatus.NOT_FOUND)

    return results, HTTPStatus.OK
Esempio n. 6
0
def get_area_by_date(date):  # noqa: E501
    """Retrieve a Area with a specified data.

    Retrieve a Area resource by date.  # noqa: E501

    :param date: Get all resources containing the name
    :type date: str

    :rtype: List[Area]
    """
    areas = getAreas()
    result = [val for key, val in areas.items() if date == val.date]
    if len(result) == 0:
        abort(HTTPStatus.NOT_FOUND)

    return result, HTTPStatus.OK
Esempio n. 7
0
    def test_add_area(self):
        """Test case for add_area

        Add new/modify Area resource
        """
        # reset area
        setAreas({})
        areas = getAreas()
        self.assertEqual(len(areas), 0)

        area = {
            "date" : "2021-04-17",
            "name" : "Luigi Pirelli",
            "poly" : [ {
                "lat" : 43.111,
                "lon" : -8.111,
                "altitude" : 77.1
            }, {
                "lat" : 43.222,
                "lon" : -8.222,
                "altitude" : 77.2
            }, {
                "lat" : 43.333,
                "lon" : -8.333,
                "altitude" : 77.3
            } ],
            "props" : {
                "a_string_value" : "Hello!",
                "an_number_value" : "1111.222"
            }
        }
        headers = { 
            'Content-Type': 'application/json',
        }
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas',
            method='POST',
            headers=headers,
            data=json.dumps(area),
            content_type='application/json')
        self.assertStatus(response, HTTPStatus.NO_CONTENT, 'Response body is : ' + response.data.decode('utf-8'))

        self.assertEqual(len(areas), 1)
        self.assertTrue(area['name'] in areas.keys())
        self.assertEqual(areas[area['name']].to_dict(), area)

        return None
Esempio n. 8
0
    def test_delete_are_by_not_existent_name(self):
        """Test case for delete_are_by_name in case Name does not exist
        """
        # first add an element with name 'Luigi Pirelli'
        self.test_add_area()
        areas = getAreas()
        self.assertEqual(len(areas), 1)

        headers = { 
            'Accept': 'application/json',
        }
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/{name}'.format(name='a not existent name'),
            method='DELETE',
            headers=headers)
        self.assertStatus(response, HTTPStatus.NOT_FOUND, 'Response body is : ' + response.data.decode('utf-8'))

        # no chenges
        self.assertEqual(len(areas), 1)
Esempio n. 9
0
def get_area_by_properties(request_body=None):  # noqa: E501
    """Retrieve a Area with a specified data set of properties.

    Retrieve a Area resource by properties dict.  # noqa: E501

    :param request_body: Area to retrieve with matching properties
    :type request_body: Dict[str, str]

    :rtype: List[Area]
    """
    if connexion.request.is_json:
        props = connexion.request.get_json()  # noqa: E501

    areas = getAreas()
    results = [val for key, val in areas.items() if props == val.props]
    if len(results) == 0:
        abort(HTTPStatus.NOT_FOUND)

    return results, HTTPStatus.OK
Esempio n. 10
0
    def test_delete_are_by_name(self):
        """Test case for delete_are_by_name

        Delete a Area resource by name.
        """
        # first add an element with name 'Luigi Pirelli'
        self.test_add_area()
        areas = getAreas()
        self.assertEqual(len(areas), 1)

        headers = { 
            'Accept': 'application/json',
        }
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/{name}'.format(name='Luigi Pirelli'),
            method='DELETE',
            headers=headers)
        self.assert200(response, 'Response body is : ' + response.data.decode('utf-8'))

        self.assertEqual(len(areas), 0)
def add_area(area=None):  # noqa: E501
    """Add new/modify Area resource

    Add a new Area resource or modify existing if unique name already exist  # noqa: E501

    :param area: Area resource to add/substitute
    :type area: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        area = Area.from_dict(connexion.request.get_json())  # noqa: E501

        log.debug('Storing area: {}'.format(area))
        areas = getAreas()
        areas[area.name] = area
        setAreas(areas)
        # log.debug("Stored areas: {}".format(areas[area['name']].name))

    return None, HTTPStatus.NO_CONTENT
Esempio n. 12
0
def get_intersection(point3_d_dict=None):  # noqa: E501
    """Retrieve Area inside input polygon.

    Retrieve Area inside input polygon  # noqa: E501

    :param point3_d_dict: Polygon to retrieve inner Area resources
    :type point3_d_dict: list | bytes

    :rtype: List[Area]
    """
    if connexion.request.is_json:
        point3_d_dict = [
            Point3DDict.from_dict(d) for d in connexion.request.get_json()
        ]  # noqa: E501

    # build shaply polygon basing on input point list
    poly = geometry.Polygon([[p.lon, p.lat, p.altitude]
                             for p in point3_d_dict])

    # select all Areas intersecting input polygon
    areas = getAreas()
    results = []

    # loop on every feature but should use a spatial db to avoid this iteration
    # and use spatial index
    for key, area in areas.items():
        # creat polygon from area coordinates
        area_polygon = geometry.Polygon([[p.lon, p.lat, p.altitude]
                                         for p in area.poly])

        # not clear if have to return Areas contained in the input polygon or
        # return intersected ones modifing Poly of each Area with the intersection.
        # I choose to most (for me) logical solution e.g. contains operator
        if poly.contains(area_polygon):
            results.append(area)

    if len(results) == 0:
        abort(HTTPStatus.NOT_FOUND)

    return results, HTTPStatus.OK
Esempio n. 13
0
def get_intersect(point3_d_dict=None):  # noqa: E501
    """Retrieve a Area inteersecting posted polygon.

    Retrieve intersecting Area resources   # noqa: E501

    :param point3_d_dict: Polygon to retrieve intersecting Area resources
    :type point3_d_dict: list | bytes

    :rtype: List[Area]
    """
    if connexion.request.is_json:
        point3_d_dict = [
            Point3DDict.from_dict(d) for d in connexion.request.get_json()
        ]  # noqa: E501

    # build shaply polygon basing on input point list
    poly = geometry.Polygon([[p.lon, p.lat, p.altitude]
                             for p in point3_d_dict])

    # select all Areas intersecting input polygon
    areas = getAreas()
    results = []

    # loop on every feature but should use a spatial db to avoid this iteration
    # and use spatial index
    for key, area in areas.items():
        # creat polygon from area coordinates
        area_polygon = geometry.Polygon([[p.lon, p.lat, p.altitude]
                                         for p in area.poly])

        # check if intersect
        if poly.intersects(area_polygon):
            results.append(area)

    if len(results) == 0:
        abort(HTTPStatus.NOT_FOUND)

    return results, HTTPStatus.OK
Esempio n. 14
0
    def test_get_intersection(self):
        """Test case for get_intersection

        Retrieve Area inside input polygon.
        """
        # equal to the first Area
        polygon = [{
            "lat": 0,
            "lon": 0,
            "altitude": 0
        }, {
            "lat": 2,
            "lon": 0,
            "altitude": 1
        }, {
            "lat": 2,
            "lon": 2,
            "altitude": 2
        }, {
            "lat": 0,
            "lon": 2,
            "altitude": 3
        }]

        # first not found because no elements is stores
        headers = {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        }
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/intersection',
            method='POST',
            headers=headers,
            data=json.dumps(polygon),
            content_type='application/json')
        self.assertStatus(
            response, HTTPStatus.NOT_FOUND,
            'Response body is : ' + response.data.decode('utf-8'))

        # check when name exist exist =>

        # step1) add an entries
        # step2) check if I can get the entry via api
        # step3) check if nothing is found but something is stored
        self._addAreas()
        areas = getAreas()
        self.assertEqual(len(areas), 2)

        # step 2 - should get the the first Area
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/intersection',
            method='POST',
            headers=headers,
            data=json.dumps(polygon),
            content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        result_areas = json.loads(response.data.decode('utf-8'))
        self.assertEqual(len(result_areas), 1)
        self.assertEqual(result_areas[0]['name'], 'Luigi Pirelli')

        # step 3: set a polygon that do not intersect
        polygon = [{
            "lat": 4,
            "lon": 4,
            "altitude": 1
        }, {
            "lat": 5,
            "lon": 4,
            "altitude": 2
        }, {
            "lat": 5,
            "lon": 5,
            "altitude": 3
        }, {
            "lat": 4,
            "lon": 5,
            "altitude": 4
        }]
        response = self.client.open(
            '/luipir/geo_test/1.0.0/areas/intersection',
            method='POST',
            headers=headers,
            data=json.dumps(polygon),
            content_type='application/json')
        self.assertStatus(
            response, HTTPStatus.NOT_FOUND,
            'Response body is : ' + response.data.decode('utf-8'))