def handler(self, event, context): """ Request handler method for fetch resource function. """ if event is None or Constants.event_path_parameters() not in event: return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters()) if Constants.event_path_parameter_identifier() not in event[ Constants.event_path_parameters()]: return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters()) _identifier = event[Constants.event_path_parameters()][ Constants.event_path_parameter_identifier()] _http_method = event[Constants.event_http_method()] if _http_method == HttpConstants.http_method_get() and _identifier: _ddb_response = self.__retrieve_resource(_identifier) if len(_ddb_response[ Constants.ddb_response_attribute_name_items()]) == 0: return response(http.HTTPStatus.NOT_FOUND, json.dumps(_ddb_response)) return response(http.HTTPStatus.OK, json.dumps(_ddb_response)) return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
def modify_resource(self, modified_resource): ddb_response = self.table.query( KeyConditionExpression=Key(Constants.ddb_field_identifier()).eq( modified_resource[Constants.event_identifier()])) if len(ddb_response[Constants.ddb_response_attribute_name_items()]) == 0: raise ValueError('Resource with identifier ' + modified_resource[Constants.event_identifier()] + ' not found') ddb_response = self.table.put_item(Item=modified_resource) return ddb_response
def test_insert_resource(self): from resource_api.insert_resource.main.RequestHandler import RequestHandler dynamodb = self.setup_mock_database('eu-west-1', 'testing') request_handler = RequestHandler(dynamodb) resource = self.generate_mock_resource() event = generate_mock_event(HttpConstants.http_method_post(), resource) handler_insert_response = request_handler.handler(event, None) resource_dict_from_json = json.loads(event[Constants.event_body()]) resource_inserted = resource_dict_from_json self.assertEqual( handler_insert_response[Constants.response_status_code()], http.HTTPStatus.CREATED, 'HTTP Status code not 201') resource_identifier = resource_inserted[Constants.event_identifier()] query_results = request_handler.get_table_connection().query( KeyConditionExpression=Key( Constants.ddb_field_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_entity_description()], '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_entity_description()], resource_inserted['entityDescription'], 'Value not persisted as expected') remove_mock_database(dynamodb)