diff --git a/server/src/main/java/com/exacaster/lighter/application/sessions/exceptions/SessionAlreadyExistsException.java b/server/src/main/java/com/exacaster/lighter/application/sessions/exceptions/SessionAlreadyExistsException.java deleted file mode 100644 index 197de995..00000000 --- a/server/src/main/java/com/exacaster/lighter/application/sessions/exceptions/SessionAlreadyExistsException.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.exacaster.lighter.application.sessions.exceptions; - -public class SessionAlreadyExistsException extends RuntimeException { - private final String sessionId; - - public SessionAlreadyExistsException(String sessionId) { - super("Session already exists: " + sessionId); - this.sessionId = sessionId; - } - - public String getSessionId() { - return sessionId; - } - - @Override - public String toString() { - return "SessionAlreadyExistsException{" + - "sessionId='" + sessionId + '\'' + - '}'; - } -} diff --git a/server/src/main/java/com/exacaster/lighter/rest/exceptions/SessionAlreadyExistsExceptionHandler.java b/server/src/main/java/com/exacaster/lighter/rest/exceptions/ApplicationAlreadyExistsExceptionHandler.java similarity index 68% rename from server/src/main/java/com/exacaster/lighter/rest/exceptions/SessionAlreadyExistsExceptionHandler.java rename to server/src/main/java/com/exacaster/lighter/rest/exceptions/ApplicationAlreadyExistsExceptionHandler.java index 12457315..34f3c61d 100644 --- a/server/src/main/java/com/exacaster/lighter/rest/exceptions/SessionAlreadyExistsExceptionHandler.java +++ b/server/src/main/java/com/exacaster/lighter/rest/exceptions/ApplicationAlreadyExistsExceptionHandler.java @@ -1,6 +1,6 @@ package com.exacaster.lighter.rest.exceptions; -import com.exacaster.lighter.application.sessions.exceptions.SessionAlreadyExistsException; +import com.exacaster.lighter.storage.ApplicationAlreadyExistsException; import io.micronaut.context.annotation.Requires; import io.micronaut.http.HttpRequest; import io.micronaut.http.HttpResponse; @@ -17,23 +17,23 @@ @Produces @Singleton -@Requires(classes = SessionAlreadyExistsException.class) -public class SessionAlreadyExistsExceptionHandler implements ExceptionHandler> { +@Requires(classes = ApplicationAlreadyExistsException.class) +public class ApplicationAlreadyExistsExceptionHandler implements ExceptionHandler> { private final ErrorResponseProcessor responseProcessor; @Inject - public SessionAlreadyExistsExceptionHandler(ErrorResponseProcessor responseProcessor) { + public ApplicationAlreadyExistsExceptionHandler(ErrorResponseProcessor responseProcessor) { this.responseProcessor = responseProcessor; } @Override - public HttpResponse handle(HttpRequest request, SessionAlreadyExistsException exception) { + public HttpResponse handle(HttpRequest request, ApplicationAlreadyExistsException exception) { final ErrorContext.Builder contextBuilder = ErrorContext.builder(request).cause(exception); MutableHttpResponse response = HttpResponse.status(HttpStatus.CONFLICT); return responseProcessor.processResponse(contextBuilder - .error(new DetailedError(exception.getMessage(), Map.of("sessionId", exception.getSessionId()))) + .error(new DetailedError(exception.getMessage(), Map.of("sessionId", exception.getApplicationId()))) .build(), response); } } diff --git a/server/src/main/java/com/exacaster/lighter/storage/ApplicationAlreadyExistsException.java b/server/src/main/java/com/exacaster/lighter/storage/ApplicationAlreadyExistsException.java new file mode 100644 index 00000000..5b1b4ce9 --- /dev/null +++ b/server/src/main/java/com/exacaster/lighter/storage/ApplicationAlreadyExistsException.java @@ -0,0 +1,21 @@ +package com.exacaster.lighter.storage; + +public class ApplicationAlreadyExistsException extends RuntimeException { + private final String applicationId; + + public ApplicationAlreadyExistsException(String applicationId) { + super("Application already exists"); + this.applicationId = applicationId; + } + + public String getApplicationId() { + return applicationId; + } + + @Override + public String toString() { + return "ApplicationAlreadyExistsException{" + + "applicationId='" + applicationId + '\'' + + '}'; + } +} diff --git a/server/src/main/java/com/exacaster/lighter/storage/ApplicationStorage.java b/server/src/main/java/com/exacaster/lighter/storage/ApplicationStorage.java index bbc7a1b4..19c1b122 100644 --- a/server/src/main/java/com/exacaster/lighter/storage/ApplicationStorage.java +++ b/server/src/main/java/com/exacaster/lighter/storage/ApplicationStorage.java @@ -18,7 +18,7 @@ public interface ApplicationStorage { Application saveApplication(Application application); - Application insertApplication(Application application); + Application insertApplication(Application application) throws ApplicationAlreadyExistsException; List findApplicationsByStates(ApplicationType type, List states, SortOrder order, Integer from, Integer size); diff --git a/server/src/main/java/com/exacaster/lighter/storage/jdbc/JdbcApplicationStorage.java b/server/src/main/java/com/exacaster/lighter/storage/jdbc/JdbcApplicationStorage.java index 13e3d9f4..f81ddb02 100644 --- a/server/src/main/java/com/exacaster/lighter/storage/jdbc/JdbcApplicationStorage.java +++ b/server/src/main/java/com/exacaster/lighter/storage/jdbc/JdbcApplicationStorage.java @@ -6,6 +6,7 @@ import com.exacaster.lighter.application.ApplicationType; import com.exacaster.lighter.application.SubmitParams; import com.exacaster.lighter.storage.ApplicationStorage; +import com.exacaster.lighter.storage.ApplicationAlreadyExistsException; import com.exacaster.lighter.storage.SortOrder; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -15,6 +16,7 @@ import org.jdbi.v3.core.Jdbi; import org.jdbi.v3.core.mapper.RowMapper; import org.jdbi.v3.core.statement.StatementContext; +import org.jdbi.v3.core.statement.UnableToExecuteStatementException; import org.slf4j.Logger; import javax.sql.DataSource; @@ -92,10 +94,16 @@ public Application saveApplication(Application application) { @Override @Transactional public Application insertApplication(Application application) { - return jdbi.withHandle(handle -> { - insert(handle, application); - return application; - }); + try { + return jdbi.withHandle(handle -> { + insert(handle, application); + return application; + }); + } catch (UnableToExecuteStatementException e) { + //can not make it better till https://github.com/jdbi/jdbi/issues/566 is implemented + throw new ApplicationAlreadyExistsException(application.getId()); + } + } private static int update(Handle handle, Application application) { diff --git a/server/src/test/groovy/com/exacaster/lighter/storage/jdbc/JdbcApplicationStorageTest.groovy b/server/src/test/groovy/com/exacaster/lighter/storage/jdbc/JdbcApplicationStorageTest.groovy index ff924039..903e01a0 100644 --- a/server/src/test/groovy/com/exacaster/lighter/storage/jdbc/JdbcApplicationStorageTest.groovy +++ b/server/src/test/groovy/com/exacaster/lighter/storage/jdbc/JdbcApplicationStorageTest.groovy @@ -3,6 +3,7 @@ package com.exacaster.lighter.storage.jdbc import com.exacaster.lighter.application.ApplicationBuilder import com.exacaster.lighter.application.ApplicationState import com.exacaster.lighter.application.ApplicationType +import com.exacaster.lighter.storage.ApplicationAlreadyExistsException import com.exacaster.lighter.storage.SortOrder import io.micronaut.test.extensions.spock.annotation.MicronautTest import jakarta.inject.Inject @@ -91,4 +92,16 @@ class JdbcApplicationStorageTest extends Specification { // then: "fetching apps ignores soft deleted ones" // storage.findAllPermanentSessions().size() == 1 // } + + + def "insert"() { + given: + def savedSession = storage.saveApplication(newPermanentSession()) + + when: "inserting a session with id that already exists" + storage.insertApplication(savedSession) + + then: + thrown ApplicationAlreadyExistsException + } }