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_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)