Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2026 BearingPoint GmbH
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.data.rest.exception;

import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import org.lfenergy.compas.core.commons.model.ErrorResponse;
import org.lfenergy.compas.scl.data.exception.CompasAssignedLocationException;

@Provider
public class CompasAssignedLocationExceptionHandler implements ExceptionMapper<CompasAssignedLocationException> {

@Override
public Response toResponse(CompasAssignedLocationException exception) {
var response = new ErrorResponse();
response.addErrorMessage(exception.getErrorCode(), exception.getMessage());
return Response.status(Response.Status.CONFLICT).entity(response).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2026 BearingPoint GmbH
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.data.rest.exception;

import org.junit.jupiter.api.Test;
import org.lfenergy.compas.core.commons.model.ErrorResponse;
import org.lfenergy.compas.scl.data.exception.CompasAssignedLocationException;

import static jakarta.ws.rs.core.Response.Status.CONFLICT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class CompasAssignedLocationExceptionHandlerTest {

@Test
void toResponse_WhenCalledWithCompasAssignedLocationException_ThenConflictReturnedWithBody() {
var exception = new CompasAssignedLocationException("Location is assigned to a SCL file.");
var handler = new CompasAssignedLocationExceptionHandler();

var result = handler.toResponse(exception);
assertEquals(CONFLICT.getStatusCode(), result.getStatus());
var errorMessage = ((ErrorResponse) result.getEntity()).getErrorMessages().get(0);
assertEquals(exception.getErrorCode(), errorMessage.getCode());
assertEquals(exception.getMessage(), errorMessage.getMessage());
assertNull(errorMessage.getProperty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lfenergy.compas.scl.data.exception.CompasAssignedLocationException;
import org.lfenergy.compas.scl.data.rest.api.locations.Location;
import org.lfenergy.compas.scl.data.rest.exception.CompasAssignedLocationExceptionHandler;
import org.lfenergy.compas.scl.data.service.LocationsService;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand All @@ -14,6 +16,7 @@
import java.util.List;
import java.util.UUID;

import static jakarta.ws.rs.core.Response.Status.CONFLICT;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2026 BearingPoint GmbH
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.data.exception;

import org.lfenergy.compas.core.commons.exception.CompasException;

import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.INVALID_INPUT_ERROR_CODE;

public class CompasAssignedLocationException extends CompasException {

public CompasAssignedLocationException(String message) {
super(INVALID_INPUT_ERROR_CODE, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import org.lfenergy.compas.scl.data.exception.CompasAssignedLocationException;
import org.lfenergy.compas.scl.data.exception.CompasNoDataFoundException;
import org.lfenergy.compas.scl.data.exception.CompasSclDataServiceException;
import org.lfenergy.compas.scl.data.repository.LocationRepository;
Expand Down Expand Up @@ -72,8 +73,8 @@ public void deleteLocation(UUID locationId) {
.orElseThrow(() -> new CompasNoDataFoundException(
ERR_MSG_LOCATION_NOT_FOUND + locationId));
if (entity.assignedResources > 0) {
throw new CompasSclDataServiceException(INVALID_INPUT_ERROR_CODE,
"Deletion not allowed, location has " + entity.assignedResources + " assigned resources: " + locationId);
throw new CompasAssignedLocationException(
"Deletion not allowed, location " + locationId + " has " + entity.assignedResources + " assigned resources");
}
locationRepository.delete(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lfenergy.compas.scl.data.entities.Location;
import org.lfenergy.compas.scl.data.exception.CompasAssignedLocationException;
import org.lfenergy.compas.scl.data.exception.CompasNoDataFoundException;
import org.lfenergy.compas.scl.data.exception.CompasSclDataServiceException;
import org.lfenergy.compas.scl.data.repository.HistorizedSclFileRepository;
Expand Down Expand Up @@ -174,7 +175,7 @@ void deleteLocation_WhenHasAssignedResources_ThenThrowsCompasSclDataServiceExcep
var entity = buildEntity(id, "LOC_A", "Location A", "desc", 3);
when(locationRepository.findByIdOptional(id)).thenReturn(Optional.of(entity));

assertThrows(CompasSclDataServiceException.class,
assertThrows(CompasAssignedLocationException.class,
() -> locationsService.deleteLocation(id));
verify(locationRepository, never()).delete(any());
}
Expand Down
Loading