Exemplo n.º 1
0
 def test_instance_not_inserted(self, mock_value_uniqueness, mock_get_cached_schema,
                                mocked_response_successful, mocked_query_create_instances,
                                mocked_property_must_map_a_unique_value):
     handler = MockHandler()
     params = ParamDict(handler, class_uri="http://somedomain/class", graph_uri="http://somedomain/graph")
     instance_data = {"http://www.w3.org/2000/01/rdf-schema#label": "teste"}
     with self.assertRaises(HTTPError) as e:
         create_instance(params, instance_data, "http://uri-teste")
         expected = ["The property (http://www.w3.org/2000/01/rdf-schema#label) defined in the schema (http://somedomain/class) must map a unique value. The value provided (teste) is already used by another instance."]
         self.assertEqual(json.loads(str(e.exception)), expected)
Exemplo n.º 2
0
 def test_instance_not_inserted(self, mock_value_uniqueness,
                                mock_get_cached_schema,
                                mocked_response_successful,
                                mocked_query_create_instances,
                                mocked_property_must_map_a_unique_value):
     handler = MockHandler()
     params = ParamDict(handler,
                        class_uri="http://somedomain/class",
                        graph_uri="http://somedomain/graph")
     instance_data = {"http://www.w3.org/2000/01/rdf-schema#label": "teste"}
     with self.assertRaises(HTTPError) as e:
         create_instance(params, instance_data, "http://uri-teste")
         expected = [
             "The property (http://www.w3.org/2000/01/rdf-schema#label) defined in the schema (http://somedomain/class) must map a unique value. The value provided (teste) is already used by another instance."
         ]
         self.assertEqual(json.loads(str(e.exception)), expected)
Exemplo n.º 3
0
    def put(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        instance_data = get_json_request_as_dict(self.request.body)
        instance_data = normalize_all_uris_recursively(instance_data)

        rdf_type_error = is_rdf_type_invalid(self.query_params, instance_data)
        if rdf_type_error:
            raise HTTPError(400, log_message=rdf_type_error)

        try:
            if not instance_exists(self.query_params):
                try:
                    schema = schema_resource.get_cached_schema(
                        self.query_params)
                except SchemaNotFound:
                    schema = None
                if schema is None:
                    msg = _(u"Class {0} doesn't exist in graph {1}.")
                    raise HTTPError(404,
                                    log_message=msg.format(
                                        self.query_params["class_uri"],
                                        self.query_params["graph_uri"]))
                instance_uri, instance_id = create_instance(
                    self.query_params, instance_data,
                    self.query_params["instance_uri"])
                resource_url = self.request.full_url()
                status = 201
                self.set_header("location", resource_url)
                self.set_header("X-Brainiak-Resource-URI", instance_uri)
            else:
                edit_instance(self.query_params, instance_data)
                status = 200
        except InstanceError as ex:
            raise HTTPError(400, log_message=str(ex))
        except SchemaNotFound as ex:
            raise HTTPError(404, log_message=str(ex))

        cache.purge_an_instance(self.query_params['instance_uri'])

        self.query_params["expand_object_properties"] = "1"
        instance_data = get_instance(self.query_params)

        if instance_data and settings.NOTIFY_BUS:
            self.query_params["instance_uri"] = instance_data["@id"]
            self._notify_bus(action="PUT", instance_data=instance_data)

        self.finalize(status)
Exemplo n.º 4
0
    def patch(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        patch_list = get_json_request_as_dict(self.request.body)

        # Retrieve original data
        instance_data = memoize(self.query_params,
                                get_instance,
                                key=build_instance_key(self.query_params),
                                function_arguments=self.query_params)

        if instance_data is not None:
            # Editing an instance
            instance_data = instance_data['body']
            instance_data.pop(
                'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', None)

            # compute patch
            changed_data = apply_patch(instance_data, patch_list)

            # Try to put
            edit_instance(self.query_params, changed_data)
            status = 200

            # Clear cache
            cache.purge_an_instance(self.query_params['instance_uri'])

            self.finalize(status)
        else:
            # Creating a new instance from patch list
            instance_data = get_instance_data_from_patch_list(patch_list)
            instance_data = normalize_all_uris_recursively(instance_data)

            rdf_type_error = is_rdf_type_invalid(self.query_params,
                                                 instance_data)
            if rdf_type_error:
                raise HTTPError(400, log_message=rdf_type_error)

            instance_uri, instance_id = create_instance(
                self.query_params, instance_data,
                self.query_params["instance_uri"])
            resource_url = self.request.full_url()
            status = 201
            self.set_header("location", resource_url)
            self.set_header("X-Brainiak-Resource-URI", instance_uri)

            self.finalize(status)
Exemplo n.º 5
0
    def post(self, context_name, class_name):
        valid_params = CLASS_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          **valid_params)
        del context_name
        del class_name

        try:
            schema = schema_resource.get_cached_schema(self.query_params)
        except SchemaNotFound:
            schema = None
        if schema is None:
            class_uri = self.query_params["class_uri"]
            graph_uri = self.query_params["graph_uri"]
            raise HTTPError(
                404,
                log_message=_(
                    u"Class {0} doesn't exist in context {1}.").format(
                        class_uri, graph_uri))

        try:
            instance_data = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400,
                            log_message=_(u"No JSON object could be decoded"))

        instance_data = normalize_all_uris_recursively(instance_data)

        try:
            (instance_uri,
             instance_id) = create_instance(self.query_params, instance_data)
        except InstanceError as ex:
            raise HTTPError(500, log_message=unicode(ex))

        instance_url = self.build_resource_url(instance_id)

        self.set_header("location", instance_url)
        self.set_header("X-Brainiak-Resource-URI", instance_uri)

        self.query_params["instance_uri"] = instance_uri
        self.query_params["instance_id"] = instance_id
        self.query_params["expand_object_properties"] = "1"

        instance_data = get_instance(self.query_params)

        if settings.NOTIFY_BUS:
            self._notify_bus(action="POST", instance_data=instance_data)

        self.finalize(201)
Exemplo n.º 6
0
    def post(self, context_name, class_name):
        valid_params = CLASS_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          **valid_params)
        del context_name
        del class_name

        try:
            schema = schema_resource.get_cached_schema(self.query_params)
        except SchemaNotFound:
            schema = None
        if schema is None:
            class_uri = self.query_params["class_uri"]
            graph_uri = self.query_params["graph_uri"]
            raise HTTPError(404, log_message=_(u"Class {0} doesn't exist in context {1}.").format(class_uri, graph_uri))

        try:
            instance_data = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400, log_message=_(u"No JSON object could be decoded"))

        instance_data = normalize_all_uris_recursively(instance_data)

        try:
            (instance_uri, instance_id) = create_instance(self.query_params, instance_data)
        except InstanceError as ex:
            raise HTTPError(500, log_message=unicode(ex))

        instance_url = self.build_resource_url(instance_id)

        self.set_header("location", instance_url)
        self.set_header("X-Brainiak-Resource-URI", instance_uri)

        self.query_params["instance_uri"] = instance_uri
        self.query_params["instance_id"] = instance_id
        self.query_params["expand_object_properties"] = "1"

        instance_data = get_instance(self.query_params)

        if settings.NOTIFY_BUS:
            self._notify_bus(action="POST", instance_data=instance_data)

        self.finalize(201)
Exemplo n.º 7
0
    def put(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        try:
            instance_data = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400,
                            log_message=_("No JSON object could be decoded"))

        instance_data = normalize_all_uris_recursively(instance_data)

        RDFS_TYPE = "http://www.w3.org/2000/01/rdf-schema#type"
        rdfs_type = instance_data.get(RDFS_TYPE)

        if rdfs_type:
            class_uri = self.query_params["class_uri"]
            if (rdfs_type == class_uri):
                instance_data.pop(RDFS_TYPE)
            else:
                msg = u"Incompatible values for rdfs:type <{0}> and class URI <{1}>"
                msg = msg.format(rdfs_type, class_uri)
                raise HTTPError(400, log_message=msg)

        try:
            if not instance_exists(self.query_params):
                try:
                    schema = schema_resource.get_cached_schema(
                        self.query_params)
                except SchemaNotFound:
                    schema = None
                if schema is None:
                    msg = _(u"Class {0} doesn't exist in graph {1}.")
                    raise HTTPError(404,
                                    log_message=msg.format(
                                        self.query_params["class_uri"],
                                        self.query_params["graph_uri"]))
                instance_uri, instance_id = create_instance(
                    self.query_params, instance_data,
                    self.query_params["instance_uri"])
                resource_url = self.request.full_url()
                status = 201
                self.set_header("location", resource_url)
                self.set_header("X-Brainiak-Resource-URI", instance_uri)
            else:
                edit_instance(self.query_params, instance_data)
                status = 200
        except InstanceError as ex:
            raise HTTPError(400, log_message=unicode(ex))
        except SchemaNotFound as ex:
            raise HTTPError(404, log_message=unicode(ex))

        cache.purge_an_instance(self.query_params['instance_uri'])

        self.query_params["expand_object_properties"] = "1"
        instance_data = get_instance(self.query_params)

        if instance_data and settings.NOTIFY_BUS:
            self.query_params["instance_uri"] = instance_data["@id"]
            self._notify_bus(action="PUT", instance_data=instance_data)

        self.finalize(status)
Exemplo n.º 8
0
    def put(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        try:
            instance_data = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400, log_message=_("No JSON object could be decoded"))

        instance_data = normalize_all_uris_recursively(instance_data)

        RDFS_TYPE = "http://www.w3.org/2000/01/rdf-schema#type"
        rdfs_type = instance_data.get(RDFS_TYPE)

        if rdfs_type:
            class_uri = self.query_params["class_uri"]
            if (rdfs_type == class_uri):
                instance_data.pop(RDFS_TYPE)
            else:
                msg = u"Incompatible values for rdfs:type <{0}> and class URI <{1}>"
                msg = msg.format(rdfs_type, class_uri)
                raise HTTPError(400, log_message=msg)

        try:
            if not instance_exists(self.query_params):
                try:
                    schema = schema_resource.get_cached_schema(self.query_params)
                except SchemaNotFound:
                    schema = None
                if schema is None:
                    msg = _(u"Class {0} doesn't exist in graph {1}.")
                    raise HTTPError(404, log_message=msg.format(self.query_params["class_uri"],
                                                                self.query_params["graph_uri"]))
                instance_uri, instance_id = create_instance(self.query_params, instance_data, self.query_params["instance_uri"])
                resource_url = self.request.full_url()
                status = 201
                self.set_header("location", resource_url)
                self.set_header("X-Brainiak-Resource-URI", instance_uri)
            else:
                edit_instance(self.query_params, instance_data)
                status = 200
        except InstanceError as ex:
            raise HTTPError(400, log_message=unicode(ex))
        except SchemaNotFound as ex:
            raise HTTPError(404, log_message=unicode(ex))

        cache.purge_an_instance(self.query_params['instance_uri'])

        self.query_params["expand_object_properties"] = "1"
        instance_data = get_instance(self.query_params)

        if instance_data and settings.NOTIFY_BUS:
            self.query_params["instance_uri"] = instance_data["@id"]
            self._notify_bus(action="PUT", instance_data=instance_data)

        self.finalize(status)