Skip to content
Snippets Groups Projects
Commit 0e3f82bf authored by OZGCloud's avatar OZGCloud
Browse files

OZG-3625 delete all commands

parent 5be93df4
No related branches found
No related tags found
No related merge requests found
Showing
with 125 additions and 10 deletions
...@@ -29,12 +29,27 @@ public class CommandExecutedEvent extends ApplicationEvent { ...@@ -29,12 +29,27 @@ public class CommandExecutedEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final Command command;
@Deprecated
protected CommandExecutedEvent(String commandId) { protected CommandExecutedEvent(String commandId) {
super(commandId); super(commandId);
command = null;
}
protected CommandExecutedEvent(Command command) {
super(command.getId());
this.command = command;
} }
@Override @Override
public String getSource() { public String getSource() {
return (String) super.getSource(); return (String) super.getSource();
} }
public Command getCommand() {
return command;
}
} }
\ No newline at end of file
...@@ -77,6 +77,11 @@ ...@@ -77,6 +77,11 @@
<artifactId>pluto-utils</artifactId> <artifactId>pluto-utils</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>de.itvsh.ozg.pluto</groupId>
<artifactId>pluto-command</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>de.itvsh.kop.notification</groupId> <groupId>de.itvsh.kop.notification</groupId>
......
...@@ -23,19 +23,43 @@ ...@@ -23,19 +23,43 @@
*/ */
package de.itvsh.ozg.pluto.command; package de.itvsh.ozg.pluto.command;
import java.util.Objects;
import java.util.function.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import lombok.extern.log4j.Log4j2;
@Component @Component
@Log4j2
public class CommandEventListener { public class CommandEventListener {
private static final String DELETION_ORDER = "VORGANG_LOESCHEN";
private static final Predicate<Command> IS_DELETION_COMMAND = command -> Objects.nonNull(command) &&
(StringUtils.equals(command.getOrder(), DELETION_ORDER));
@Autowired @Autowired
private CommandService commandService; private CommandService commandService;
@EventListener @EventListener
public void setStatusFinished(CommandExecutedEvent event) { public void setStatusFinished(CommandExecutedEvent event) {
commandService.setCommandFinished(event.getSource()); commandService.setCommandFinished(event.getSource());
handleVorgangDeleted(event.getCommand());
}
void handleVorgangDeleted(Command command) {
if (IS_DELETION_COMMAND.test(command)) {
try {
commandService.deleteAllByVorgang(command.getVorgangId());
} catch (RuntimeException e) {
LOG.error("Error on deleting Commands of Vorgang " + command.getVorgangId(), e);
}
}
} }
@EventListener @EventListener
......
...@@ -140,4 +140,8 @@ class CommandRepository { ...@@ -140,4 +140,8 @@ class CommandRepository {
} }
return update; return update;
} }
public void deleteAllByVorgang(String vorgangId) {
mongoOperations.remove(query(where(PersistedCommand.FIELD_VORGANG_ID).is(vorgangId)), PersistedCommand.COLLECTION_NAME);
}
} }
...@@ -155,4 +155,8 @@ public class CommandService { ...@@ -155,4 +155,8 @@ public class CommandService {
public void setPreviousState(String commandId, Map<String, Object> previousState) { public void setPreviousState(String commandId, Map<String, Object> previousState) {
repository.patch(commandId, previousState); repository.patch(commandId, previousState);
} }
public void deleteAllByVorgang(String vorgangId) {
repository.deleteAllByVorgang(vorgangId);
}
} }
\ No newline at end of file
...@@ -80,10 +80,11 @@ public class SearchEventListener { ...@@ -80,10 +80,11 @@ public class SearchEventListener {
@EventListener @EventListener
public void onVorgangDeleted(VorgangDeletedEvent event) { public void onVorgangDeleted(VorgangDeletedEvent event) {
String vorgangId = event.getCommand().getVorgangId();
try { try {
searchService.deleteVorgang(event.getVorgangId()); searchService.deleteVorgang(vorgangId);
} catch (RuntimeException e) { } catch (RuntimeException e) {
LOG.error("Error on deleting Vorgang (" + event.getVorgangId() + ") from search index.", e); LOG.error("Error on deleting Vorgang (" + vorgangId + ") from search index.", e);
} }
} }
......
package de.itvsh.ozg.pluto.vorgang; package de.itvsh.ozg.pluto.vorgang;
import de.itvsh.ozg.pluto.command.Command;
import de.itvsh.ozg.pluto.command.CommandExecutedEvent; import de.itvsh.ozg.pluto.command.CommandExecutedEvent;
import lombok.Getter; import lombok.Getter;
@Getter @Getter
public class VorgangDeletedEvent extends CommandExecutedEvent { public class VorgangDeletedEvent extends CommandExecutedEvent {
private final String vorgangId; protected VorgangDeletedEvent(Command command) {
super(command);
protected VorgangDeletedEvent(String commandId, String vorgangId) {
super(commandId);
this.vorgangId = vorgangId;
} }
} }
...@@ -85,7 +85,7 @@ public class VorgangEventListener { ...@@ -85,7 +85,7 @@ public class VorgangEventListener {
vorgangService.deleteVorgang(vorgangId); vorgangService.deleteVorgang(vorgangId);
fileService.deleteAllByVorgang(vorgangId); fileService.deleteAllByVorgang(vorgangId);
publishEvent(new VorgangDeletedEvent(command.getId(), vorgangId)); publishEvent(new VorgangDeletedEvent(command));
} catch (RuntimeException e) { } catch (RuntimeException e) {
LOG.error("Unexpected Error on deleting Vorgang.", e); LOG.error("Unexpected Error on deleting Vorgang.", e);
publisher.publishEvent(new CommandFailedEvent(command.getId(), e.getMessage())); publisher.publishEvent(new CommandFailedEvent(command.getId(), e.getMessage()));
......
...@@ -191,6 +191,7 @@ public class VorgangService { ...@@ -191,6 +191,7 @@ public class VorgangService {
} }
public void deleteVorgang(String vorgangId) { public void deleteVorgang(String vorgangId) {
//TODO check version
var stub = stubMapper.fromVorgang(loadById(vorgangId)); var stub = stubMapper.fromVorgang(loadById(vorgangId));
repository.saveStub(stub); repository.saveStub(stub);
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
*/ */
package de.itvsh.ozg.pluto.command; package de.itvsh.ozg.pluto.command;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
...@@ -30,6 +32,8 @@ import org.junit.jupiter.api.Test; ...@@ -30,6 +32,8 @@ import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import de.itvsh.ozg.pluto.vorgang.VorgangTestFactory;
class CommandEventListenerTest { class CommandEventListenerTest {
@InjectMocks // NOSONAR @InjectMocks // NOSONAR
...@@ -48,6 +52,33 @@ class CommandEventListenerTest { ...@@ -48,6 +52,33 @@ class CommandEventListenerTest {
verify(commandService).setCommandFinished(CommandTestFactory.ID); verify(commandService).setCommandFinished(CommandTestFactory.ID);
} }
@Nested
class HandleVorgangDelete {
@Test
void shouldIgnoreNull() {
assertThatCode(() -> listener.handleVorgangDeleted(null)).doesNotThrowAnyException();
}
@Test
void shouldDeleteCommandsOnVorgangDeletion() {
listener.handleVorgangDeleted(buildDeletionCommand());
verify(commandService).deleteAllByVorgang(VorgangTestFactory.ID);
}
@Test
void shouldHandleExcpetion() {
doThrow(RuntimeException.class).when(commandService).deleteAllByVorgang(any());
assertThatCode(() -> listener.handleVorgangDeleted(buildDeletionCommand())).doesNotThrowAnyException();
}
private Command buildDeletionCommand() {
return CommandTestFactory.createBuilder().order("VORGANG_LOESCHEN").build();
}
}
} }
@Nested @Nested
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
package de.itvsh.ozg.pluto.command; package de.itvsh.ozg.pluto.command;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
...@@ -39,6 +40,7 @@ import org.junit.jupiter.params.ParameterizedTest; ...@@ -39,6 +40,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import de.itvsh.kop.common.test.DataITCase; import de.itvsh.kop.common.test.DataITCase;
import de.itvsh.ozg.pluto.vorgang.Vorgang; import de.itvsh.ozg.pluto.vorgang.Vorgang;
...@@ -196,7 +198,7 @@ class CommandRepositoryITCase { ...@@ -196,7 +198,7 @@ class CommandRepositoryITCase {
@BeforeEach @BeforeEach
void init() { void init() {
command = repository.save(command); command = mongoOperations.save(command);
} }
@Test @Test
...@@ -211,4 +213,25 @@ class CommandRepositoryITCase { ...@@ -211,4 +213,25 @@ class CommandRepositoryITCase {
PREVIOUS_STATE_STATUS_VALUE); PREVIOUS_STATE_STATUS_VALUE);
} }
} }
@Nested
class TestDeleteAllByVorgang {
@Test
void shouldDeleteCommand() {
mongoOperations.save(CommandTestFactory.create());
repository.deleteAllByVorgang(VorgangTestFactory.ID);
assertThat(mongoOperations.count(query(new Criteria()), Command.COLLECTION_NAME)).isZero();
}
@Test
void shouldNotDeleteOther() {
mongoOperations.save(CommandTestFactory.create());
repository.deleteAllByVorgang("other");
assertThat(mongoOperations.count(query(new Criteria()), Command.COLLECTION_NAME)).isOne();
}
}
} }
\ No newline at end of file
...@@ -309,4 +309,14 @@ class CommandServiceTest { ...@@ -309,4 +309,14 @@ class CommandServiceTest {
verify(repository).patch(anyString(), any()); verify(repository).patch(anyString(), any());
} }
} }
@Nested
class TestDeleteAllByVorgang {
@Test
void shouldCallRepository() {
service.deleteAllByVorgang(VorgangTestFactory.ID);
verify(repository).deleteAllByVorgang(VorgangTestFactory.ID);
}
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment