diff --git a/src/central-server/admin-service/core/src/main/java/org/niis/xroad/cs/admin/core/entity/AddressChangeRequestEntity.java b/src/central-server/admin-service/core/src/main/java/org/niis/xroad/cs/admin/core/entity/AddressChangeRequestEntity.java index ec7a82e598..345048124b 100644 --- a/src/central-server/admin-service/core/src/main/java/org/niis/xroad/cs/admin/core/entity/AddressChangeRequestEntity.java +++ b/src/central-server/admin-service/core/src/main/java/org/niis/xroad/cs/admin/core/entity/AddressChangeRequestEntity.java @@ -53,8 +53,8 @@ public class AddressChangeRequestEntity extends RequestEntity { @Setter private String address; - public AddressChangeRequestEntity(Origin origin, SecurityServerId identifier, String comments, String address) { - super(origin, identifier, comments); + public AddressChangeRequestEntity(Origin origin, SecurityServerId identifier, String address) { + super(origin, identifier, null); this.address = address; } diff --git a/src/central-server/admin-service/core/src/main/java/org/niis/xroad/cs/admin/core/service/managementrequest/AddressChangeRequestHandler.java b/src/central-server/admin-service/core/src/main/java/org/niis/xroad/cs/admin/core/service/managementrequest/AddressChangeRequestHandler.java index 8bea11c425..1972b43d50 100644 --- a/src/central-server/admin-service/core/src/main/java/org/niis/xroad/cs/admin/core/service/managementrequest/AddressChangeRequestHandler.java +++ b/src/central-server/admin-service/core/src/main/java/org/niis/xroad/cs/admin/core/service/managementrequest/AddressChangeRequestHandler.java @@ -63,15 +63,25 @@ public AddressChangeRequest add(AddressChangeRequest request) { final SecurityServerIdEntity serverId = serverIds.findOne(SecurityServerIdEntity.create(request.getSecurityServerId())); final var requestEntity = new AddressChangeRequestEntity(request.getOrigin(), serverId, - request.getComments(), request.getServerAddress()); - final AddressChangeRequestEntity savedRequest = addressChangeRequests.save(requestEntity); + request.getServerAddress()); securityServerRepository.findBy(serverId) - .peek(server -> server.setAddress(request.getServerAddress())); - + .peek(server -> { + requestEntity.setComments(formatComment(server.getAddress(), request.getComments())); + server.setAddress(request.getServerAddress()); + }); + final AddressChangeRequestEntity savedRequest = addressChangeRequests.save(requestEntity); return requestMapper.toDto(savedRequest); } + private String formatComment(String oldAddress, String comment) { + String newComment = "Changing from '%s'.".formatted(oldAddress); + if (StringUtils.isNotBlank(comment)) { + newComment += " " + comment; + } + return newComment; + } + private void validate(AddressChangeRequest request) { if (StringUtils.isBlank(request.getServerAddress())) { throw new ValidationFailureException(MR_INVALID_SERVER_ADDRESS); diff --git a/src/central-server/admin-service/core/src/test/java/org/niis/xroad/cs/admin/core/entity/mapper/RequestMapperTest.java b/src/central-server/admin-service/core/src/test/java/org/niis/xroad/cs/admin/core/entity/mapper/RequestMapperTest.java index 73f37d317b..ba564a7c5a 100644 --- a/src/central-server/admin-service/core/src/test/java/org/niis/xroad/cs/admin/core/entity/mapper/RequestMapperTest.java +++ b/src/central-server/admin-service/core/src/test/java/org/niis/xroad/cs/admin/core/entity/mapper/RequestMapperTest.java @@ -142,7 +142,8 @@ void shouldMapOwnerChangeRequestEntity() { @Test void shouldMapAddressChangeRequestEntity() { var source = new AddressChangeRequestEntity(Origin.SECURITY_SERVER, SECURITY_SERVER_ID_ENTITY, - "comments", "https://server.address"); + "https://server.address"); + source.setComments("comments"); var result = mapper.toTarget(source); diff --git a/src/central-server/admin-service/core/src/test/java/org/niis/xroad/cs/admin/core/service/managementrequest/AddressChangeRequestHandlerTest.java b/src/central-server/admin-service/core/src/test/java/org/niis/xroad/cs/admin/core/service/managementrequest/AddressChangeRequestHandlerTest.java index 673df2b4e0..2131f32d24 100644 --- a/src/central-server/admin-service/core/src/test/java/org/niis/xroad/cs/admin/core/service/managementrequest/AddressChangeRequestHandlerTest.java +++ b/src/central-server/admin-service/core/src/test/java/org/niis/xroad/cs/admin/core/service/managementrequest/AddressChangeRequestHandlerTest.java @@ -30,6 +30,8 @@ import io.vavr.control.Option; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -46,7 +48,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -70,6 +71,9 @@ class AddressChangeRequestHandlerTest { @InjectMocks private AddressChangeRequestHandler handler; + @Captor + private ArgumentCaptor argumentCaptor; + @Test void canAutoApprove() { var request = new AddressChangeRequest(SECURITY_SERVER, securityServerId, ADDRESS); @@ -96,11 +100,13 @@ void add() { SecurityServerIdEntity securityServerIdEntity = mock(SecurityServerIdEntity.class); when(serverIds.findOne(SecurityServerIdEntity.create(securityServerId))).thenReturn(securityServerIdEntity); when(securityServerRepository.findBy(securityServerIdEntity)).thenReturn(Option.of(securityServerEntity)); - when(addressChangeRequests.save(isA(AddressChangeRequestEntity.class))).thenReturn(requestEntity); + when(securityServerEntity.getAddress()).thenReturn("ss.old"); + when(addressChangeRequests.save(argumentCaptor.capture())).thenReturn(requestEntity); handler.add(request); verify(securityServerEntity).setAddress(ADDRESS); + assertThat(argumentCaptor.getValue().getComments()).isEqualTo("Changing from 'ss.old'."); verify(requestMapper).toDto(requestEntity); }