Skip to content

Commit

Permalink
feat: when updating Security Server address, saving previous one in a…
Browse files Browse the repository at this point in the history
… comment (#1888)

Refs: XRDDEV-227
  • Loading branch information
justasnortal authored Dec 8, 2023
1 parent 917d44c commit 7bc12c3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -70,6 +71,9 @@ class AddressChangeRequestHandlerTest {
@InjectMocks
private AddressChangeRequestHandler handler;

@Captor
private ArgumentCaptor<AddressChangeRequestEntity> argumentCaptor;

@Test
void canAutoApprove() {
var request = new AddressChangeRequest(SECURITY_SERVER, securityServerId, ADDRESS);
Expand All @@ -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);
}

Expand Down

0 comments on commit 7bc12c3

Please sign in to comment.