def generate_mock_resource(self,
                            time_created=None,
                            time_modified=None,
                            uuid=uuid.uuid4().__str__()):
     title_1 = Title('no', self.random_word(6))
     title_2 = Title('en', self.random_word(6))
     titles = {
         title_1.language_code: title_1.title,
         title_2.language_code: title_2.title
     }
     creator_one = Creator('AUTHORITY_IDENTIFIER_1')
     creator_two = Creator('AUTHORITY_IDENTIFIER_2')
     creators = [creator_one, creator_two]
     metadata = Metadata(creators, 'https://hdl.handle.net/11250.1/1',
                         'LICENSE_IDENTIFIER_1', '2019', 'Unit', titles,
                         'text')
     file_metadata_1 = FileMetadata(
         self.random_word(6) + '.txt', 'text/plain',
         '595f44fec1e92a71d3e9e77456ba80d1', '987654321')
     file_metadata_2 = FileMetadata(
         self.random_word(6) + '.pdf', 'application/pdf',
         '71f920fa275127a7b60fa4d4d41432a3', '123456789')
     file_1 = File('FILE_IDENTIFIER_1', file_metadata_1)
     file_2 = File('FILE_IDENTIFIER_2', file_metadata_2)
     files = dict()
     files[file_1.identifier] = file_1.file_metadata
     files[file_2.identifier] = file_2.file_metadata
     return Resource(uuid, time_modified, time_created, metadata, files,
                     '*****@*****.**')
예제 #2
0
    def handler(self, event, context):
        if event is None or Constants.EVENT_BODY not in event or Constants.EVENT_HTTP_METHOD not in event:
            return response(http.HTTPStatus.BAD_REQUEST,
                            Constants.ERROR_INSUFFICIENT_PARAMETERS)

        body = json.loads(event[Constants.EVENT_BODY])
        http_method = event[Constants.EVENT_HTTP_METHOD]
        resource_dict_from_json = body.get(
            Constants.JSON_ATTRIBUTE_NAME_RESOURCE)

        try:
            resource = Resource.from_dict(resource_dict_from_json)
        except TypeError as e:
            return response(http.HTTPStatus.BAD_REQUEST, e.args[0])

        current_time = arrow.utcnow().isoformat()

        resource_not_none = resource is not None
        if http_method == Constants.HTTP_METHOD_POST and resource_not_none:
            try:
                validate_resource(resource)
            except ValueError as e:
                return response(http.HTTPStatus.BAD_REQUEST, e.args[0])
            generated_uuid = uuid.uuid4().__str__()
            ddb_response = self.insert_resource(generated_uuid, current_time,
                                                resource)
            ddb_response['resource_identifier'] = generated_uuid
            return response(http.HTTPStatus.CREATED, json.dumps(ddb_response))

        return response(http.HTTPStatus.BAD_REQUEST,
                        Constants.ERROR_INSUFFICIENT_PARAMETERS)
    def test_insert_resource(self):
        from src.classes.RequestHandler import RequestHandler
        dynamodb = self.setup_mock_database()
        request_handler = RequestHandler(dynamodb)

        resource = self.generate_mock_resource(None, None, None)
        event = generate_mock_event(Constants.OPERATION_INSERT, resource)
        handler_insert_response = request_handler.handler(event, None)

        resource_dict_from_json = json.loads(event[Constants.EVENT_BODY]).get(
            Constants.JSON_ATTRIBUTE_NAME_RESOURCE)
        resource_inserted = Resource.from_dict(resource_dict_from_json)

        self.assertEqual(
            handler_insert_response[Constants.RESPONSE_STATUS_CODE],
            http.HTTPStatus.CREATED, 'HTTP Status code not 201')

        resource_identifier = json.loads(handler_insert_response[
            Constants.RESPONSE_BODY]).get('resource_identifier')

        query_results = request_handler.get_table_connection().query(
            KeyConditionExpression=Key(
                Constants.DDB_FIELD_RESOURCE_IDENTIFIER).eq(
                    resource_identifier),
            ScanIndexForward=True)

        inserted_resource = query_results[
            Constants.DDB_RESPONSE_ATTRIBUTE_NAME_ITEMS][0]
        self.assertIsNotNone(
            inserted_resource[Constants.DDB_FIELD_CREATED_DATE],
            'Value not persisted as expected')
        self.assertIsNotNone(
            inserted_resource[Constants.DDB_FIELD_MODIFIED_DATE],
            'Value not persisted as expected')
        self.assertIsNotNone(inserted_resource[Constants.DDB_FIELD_METADATA],
                             'Value not persisted as expected')
        self.assertEqual(inserted_resource[Constants.DDB_FIELD_MODIFIED_DATE],
                         inserted_resource[Constants.DDB_FIELD_CREATED_DATE],
                         'Value not persisted as expected')
        self.assertEqual(inserted_resource[Constants.DDB_FIELD_METADATA],
                         resource_inserted.metadata,
                         'Value not persisted as expected')
        remove_mock_database(dynamodb)
예제 #4
0
    def handler(self, event, context):
        if event is None or Constants.EVENT_BODY not in event:
            return response(http.HTTPStatus.BAD_REQUEST,
                            'Insufficient parameters')
        else:
            body = json.loads(event[Constants.EVENT_BODY])
            operation = body.get(Constants.JSON_ATTRIBUTE_NAME_OPERATION)
            resource_dict_from_json = body.get(
                Constants.JSON_ATTRIBUTE_NAME_RESOURCE)

            try:
                resource = Resource.from_dict(resource_dict_from_json)
            except TypeError as e:
                return response(http.HTTPStatus.BAD_REQUEST, e.args[0])

            current_time = arrow.utcnow().isoformat().replace('+00:00', 'Z')

            if operation == Constants.OPERATION_INSERT and resource is not None:
                try:
                    validate_resource(operation, resource)
                except ValueError as e:
                    return response(http.HTTPStatus.BAD_REQUEST, e.args[0])
                generated_uuid = uuid.uuid4().__str__()
                ddb_response = self.insert_resource(generated_uuid,
                                                    current_time, resource)
                ddb_response['resource_identifier'] = generated_uuid
                return response(http.HTTPStatus.CREATED,
                                json.dumps(ddb_response))
            elif operation == Constants.OPERATION_MODIFY and resource is not None:
                try:
                    validate_resource(operation, resource)
                    ddb_response = self.modify_resource(current_time, resource)
                    ddb_response[
                        'resource_identifier'] = resource.resource_identifier
                    return response(http.HTTPStatus.OK,
                                    json.dumps(ddb_response))
                except ValueError as e:
                    return response(http.HTTPStatus.BAD_REQUEST, e.args[0])
            else:
                return response(http.HTTPStatus.BAD_REQUEST,
                                'Insufficient parameters')
    def test_modify_resource(self):
        from src.classes.RequestHandler import RequestHandler
        dynamodb = self.setup_mock_database()
        request_handler = RequestHandler(dynamodb)

        resource = self.generate_mock_resource(None, None, None)
        event = generate_mock_event(Constants.OPERATION_INSERT, resource)
        handler_insert_response = request_handler.handler(event, None)

        created_resource_identifier = json.loads(handler_insert_response[
            Constants.RESPONSE_BODY]).get('resource_identifier')
        resource_dict_from_json = json.loads(event[Constants.EVENT_BODY]).get(
            Constants.JSON_ATTRIBUTE_NAME_RESOURCE)
        resource_inserted = Resource.from_dict(resource_dict_from_json)
        resource_inserted.resource_identifier = created_resource_identifier

        for counter in range(2):
            resource = self.generate_mock_resource(
                None, None, resource_inserted.resource_identifier)
            event = generate_mock_event(Constants.OPERATION_MODIFY, resource)
            handler_modify_response = request_handler.handler(event, None)

            self.assertEqual(
                handler_modify_response[Constants.RESPONSE_STATUS_CODE],
                http.HTTPStatus.OK, 'HTTP Status code not 200')

        query_results = request_handler.get_table_connection().query(
            KeyConditionExpression=Key(
                Constants.DDB_FIELD_RESOURCE_IDENTIFIER).eq(
                    resource_inserted.resource_identifier),
            ScanIndexForward=True)

        self.assertEqual(
            len(query_results[Constants.DDB_RESPONSE_ATTRIBUTE_NAME_ITEMS]), 3,
            'Value not persisted as expected')

        initial_resource = query_results[
            Constants.DDB_RESPONSE_ATTRIBUTE_NAME_ITEMS][0]
        first_modification_resource = query_results[
            Constants.DDB_RESPONSE_ATTRIBUTE_NAME_ITEMS][1]
        second_modification_resource = query_results[
            Constants.DDB_RESPONSE_ATTRIBUTE_NAME_ITEMS][2]

        resource_created_date = initial_resource[
            Constants.DDB_FIELD_CREATED_DATE]

        self.assertEqual(
            first_modification_resource[Constants.DDB_FIELD_CREATED_DATE],
            resource_created_date, 'Value not persisted as expected')
        self.assertEqual(
            second_modification_resource[Constants.DDB_FIELD_CREATED_DATE],
            resource_created_date, 'Value not persisted as expected')
        self.assertEqual(initial_resource[Constants.DDB_FIELD_MODIFIED_DATE],
                         resource_created_date,
                         'Value not persisted as expected')
        self.assertNotEqual(
            first_modification_resource[Constants.DDB_FIELD_MODIFIED_DATE],
            resource_created_date, 'Value not persisted as expected')
        self.assertNotEqual(
            second_modification_resource[Constants.DDB_FIELD_MODIFIED_DATE],
            resource_created_date, 'Value not persisted as expected')
        self.assertNotEqual(
            first_modification_resource[Constants.DDB_FIELD_MODIFIED_DATE],
            second_modification_resource[Constants.DDB_FIELD_MODIFIED_DATE],
            'Value not persisted as expected')
        self.assertNotEqual(
            initial_resource[Constants.DDB_FIELD_METADATA],
            first_modification_resource[Constants.DDB_FIELD_METADATA],
            'Value not persisted as expected')
        self.assertNotEqual(
            initial_resource[Constants.DDB_FIELD_METADATA],
            second_modification_resource[Constants.DDB_FIELD_METADATA],
            'Value not persisted as expected')
        self.assertNotEqual(
            first_modification_resource[Constants.DDB_FIELD_METADATA],
            second_modification_resource[Constants.DDB_FIELD_METADATA],
            'Value not persisted as expected')
        remove_mock_database(dynamodb)