def test_handler_missing_event(self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     handler_modify_response = request_handler.handler(None, None)
     self.assertEqual(
         handler_modify_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400')
     remove_mock_database(dynamodb)
 def test_handler_missing_resource_in_event_body(self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     event = generate_mock_event(Constants.OPERATION_INSERT, None)
     handler_modify_response = request_handler.handler(event, None)
     self.assertEqual(
         handler_modify_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400')
     remove_mock_database(dynamodb)
 def test_handler_insert_resource_with_identifier(self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     resource = self.generate_mock_resource()
     event = generate_mock_event(Constants.HTTP_METHOD_POST, resource)
     handler_insert_response = request_handler.handler(event, None)
     self.assertEqual(
         handler_insert_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400')
     remove_mock_database(dynamodb)
 def test_handler_insert_resource(self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     resource = self.generate_mock_resource()
     event = generate_mock_event(Constants.OPERATION_INSERT, resource)
     handler_insert_response = request_handler.handler(event, None)
     self.assertEqual(
         handler_insert_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.CREATED, 'HTTP Status code not 201')
     remove_mock_database(dynamodb)
 def test_handler_missing_operation_in_event_body(self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     resource = self.generate_mock_resource(
         None, None, self.EXISTING_RESOURCE_IDENTIFIER)
     event = generate_mock_event(None, resource)
     handler_modify_response = request_handler.handler(event, None)
     self.assertEqual(
         handler_modify_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400')
     remove_mock_database(dynamodb)
 def test_handler_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, self.EXISTING_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')
     remove_mock_database(dynamodb)
 def test_handler_unknown_http_method_in_event(self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     resource = self.generate_mock_resource(
         None, None, self.EXISTING_RESOURCE_IDENTIFIER)
     event = generate_mock_event('INVALID_HTTP_METHOD', resource)
     handler_response = request_handler.handler(event, None)
     self.assertEqual(handler_response[Constants.RESPONSE_STATUS_CODE],
                      http.HTTPStatus.BAD_REQUEST,
                      'HTTP Status code not 400')
     remove_mock_database(dynamodb)
 def test_handler_insert_resource_empty_resource_metadata_in_event_body(
         self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     resource = self.generate_mock_resource(None, None, None)
     resource.metadata = Metadata(None, None, None, None, None, None)
     event = generate_mock_event(Constants.HTTP_METHOD_POST, resource)
     handler_insert_response = request_handler.handler(event, None)
     self.assertEqual(
         handler_insert_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.CREATED, 'HTTP Status code not 201')
     remove_mock_database(dynamodb)
 def test_handler_modify_resource_unexpected_resource_field_in_event_body(
         self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     event = {
         "body":
         "{\"operation\": \"MODIFY\",\"resource\": {\"resource_identifier\": "
         "\"ebf20333-35a5-4a06-9c58-68ea688a9a8b\", \"registrator\": \"[email protected]\", \"owner\": \"[email protected]\", \"files\": \"{}\", \"metadata\": {}}}"
     }
     handler_modify_response = request_handler.handler(event, None)
     self.assertEqual(
         handler_modify_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400')
     remove_mock_database(dynamodb)
 def test_handler_insert_resource_invalid_resource_in_event_body(self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     event = {
         "httpMethod":
         "POST",
         "body":
         "{\"resource\": {\"owners\": \"[email protected]\", \"files\": {}, \"metadata\": \"invalid type\"}}"
     }
     handler_insert_response = request_handler.handler(event, None)
     self.assertEqual(
         handler_insert_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400')
     remove_mock_database(dynamodb)
 def test_handler_modify_resource_unknown_resource_identifier_in_event_body(
         self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     resource = self.generate_mock_resource(
         None, None, 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
     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.BAD_REQUEST, 'HTTP Status code not 400')
     self.assertEqual(
         handler_modify_response['body'],
         'Resource with identifier xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx not found',
         'Did not get expected error message')
     remove_mock_database(dynamodb)
 def test_handler_modify_resource_created_date_missing_in_existing_resource(
         self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     resource = self.generate_mock_resource(
         None, None, self.EXISTING_RESOURCE_IDENTIFIER_MISSING_CREATED_DATE)
     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.BAD_REQUEST, 'HTTP Status code not 400')
     self.assertEqual(
         handler_modify_response[Constants.RESPONSE_BODY],
         'Resource with identifier acf20333-35a5-4a06-9c58-68ea688a9a9c has no '
         + Constants.DDB_FIELD_CREATED_DATE + ' in DB',
         'HTTP Status code not 400')
     remove_mock_database(dynamodb)
    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)
 def test_handler_modify_resource_invalid_resource_metadata_type_in_event_body(
         self):
     from src.classes.RequestHandler import RequestHandler
     dynamodb = self.setup_mock_database()
     request_handler = RequestHandler(dynamodb)
     event = {
         "body":
         "{\"operation\": \"MODIFY\",\"resource\": {\"resource_identifier\": "
         "\"ebf20333-35a5-4a06-9c58-68ea688a9a8b\", \"owner\": \"[email protected]\", \"files\": {}, \"metadata\": \"invalid type\"}}"
     }
     handler_modify_response = request_handler.handler(event, None)
     self.assertEqual(
         handler_modify_response[Constants.RESPONSE_STATUS_CODE],
         http.HTTPStatus.BAD_REQUEST, 'HTTP Status code not 400')
     self.assertEqual(
         handler_modify_response[Constants.RESPONSE_BODY],
         'Resource with identifier ebf20333-35a5-4a06-9c58-68ea688a9a8b has invalid attribute type for metadata',
         'HTTP Status code not 400')
     remove_mock_database(dynamodb)
    def test_handler_retrieve_resource(self):
        from src.classes.RequestHandler import RequestHandler
        dynamodb = self.setup_mock_database()
        request_handler = RequestHandler(dynamodb)

        event = {
            "body":
            "{\"operation\": \"RETRIEVE\",\"resource\": {\"resource_identifier\": \"ebf20333-35a5-4a06-9c58-68ea688a9a8b\"}}"
        }

        handler_retrieve_response = request_handler.handler(event, None)

        handler_retrieve_response_json = json.loads(
            handler_retrieve_response[Constants.RESPONSE_BODY])

        self.assertEqual(
            handler_retrieve_response[Constants.RESPONSE_STATUS_CODE], 200,
            'HTTP Status code not 200')
        self.assertEqual(
            handler_retrieve_response_json[
                Constants.DDB_RESPONSE_ATTRIBUTE_NAME_ITEMS][0][
                    Constants.DDB_FIELD_RESOURCE_IDENTIFIER],
            EXISTING_RESOURCE_IDENTIFIER, 'Value not retrieved as expected')
        remove_mock_database(dynamodb)
    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)
    def test_modify_resource(self):
        from src.classes.RequestHandler import RequestHandler
        dynamodb = self.setup_mock_database()
        request_handler = RequestHandler(dynamodb)

        for counter in range(2):
            resource = self.generate_mock_resource(
                None, None, self.EXISTING_RESOURCE_IDENTIFIER)
            event = generate_mock_event(Constants.HTTP_METHOD_PUT, 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(
                    self.EXISTING_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 = self.EXISTING_RESOURCE_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(self.EXISTING_RESOURCE_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)