Skip to content

Commit

Permalink
#116 - integration test added for postgresql insert error.
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Jan 11, 2024
1 parent 874dcfa commit e045377
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.homihq.db2rest.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponseException;

import java.net.URI;
import java.time.Instant;

public class GenericDataAccessException extends ErrorResponseException {


public GenericDataAccessException(String message) {
super(HttpStatus.BAD_REQUEST, asProblemDetail(message), null);
}

private static ProblemDetail asProblemDetail(String message) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, message);
problemDetail.setTitle("Generic Data Access Error");
problemDetail.setType(URI.create("https://github.com/kdhrubo/db2rest/generic-error"));
problemDetail.setProperty("errorCategory", "Data-access-error");
problemDetail.setProperty("timestamp", Instant.now());
return problemDetail;
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/homihq/db2rest/rest/create/CreateService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.homihq.db2rest.rest.create;

import com.homihq.db2rest.config.Db2RestConfigProperties;
import com.homihq.db2rest.exception.GenericDataAccessException;
import com.homihq.db2rest.mybatis.DB2RestRenderingStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,6 +12,7 @@
import org.mybatis.dynamic.sql.insert.render.BatchInsert;
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
Expand Down Expand Up @@ -43,14 +45,19 @@ public int save(String schemaName, String tableName, Map<String,Object> data) {
generalInsertDSL.set(table.column(key)).toValue(data.get(key));
}


GeneralInsertStatementProvider insertStatement = generalInsertDSL.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);

log.debug("SQL - {}", insertStatement.getInsertStatement());
log.debug("SQL - row - {}", insertStatement.getParameters());

int rows = namedParameterJdbcTemplate.update(insertStatement.getInsertStatement(), insertStatement.getParameters());
int rows = 0;

try {
rows = namedParameterJdbcTemplate.update(insertStatement.getInsertStatement(), insertStatement.getParameters());
}
catch(DataAccessException e) {
throw new GenericDataAccessException(e.getMessage());
}
log.debug("Inserted - {} row(s)", rows);

return rows;
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,35 @@ void create() throws Exception {
.andDo(document("create-a-film"));

}

@Test
@DisplayName("Create a film with error.")
void createError() throws Exception {

var json = """
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 1,
"original_language_id" : null,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries",
"country" : "USA"
}
""";


mockMvc.perform(post("/film").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andDo(print())
.andDo(document("create-a-film-error"));

}
}

0 comments on commit e045377

Please sign in to comment.