Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmann committed Feb 10, 2025
1 parent 987842d commit 2886e16
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Collection<RuleDao> loadAll() {

@Override
public void delete(String id) {
if (!scopedEntityMongoUtils.deleteById(id)) {
if (!scopedEntityMongoUtils.deleteById(id, false)) {
log.error("Unable to delete rule {}", id);
}
clusterBus.post(RulesChangedEvent.deletedRuleId(id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public String create(T entity) {
* @return true if a document was deleted, false otherwise.
*/
public boolean deleteById(String id) {
return deleteById(new ObjectId(id));
return deleteById(new ObjectId(id), true);
}

public boolean deleteById(String id, boolean checkMutability) {
return deleteById(new ObjectId(id), checkMutability);
}

/**
Expand All @@ -78,11 +82,13 @@ public boolean deleteById(String id) {
* @param id the document's id.
* @return true if a document was deleted, false otherwise.
*/
public boolean deleteById(ObjectId id) {
public boolean deleteById(ObjectId id, boolean checkMutability) {
final T entity = Optional.ofNullable(collection.find(idEq(id)).first())
.orElseThrow(() -> new IllegalArgumentException("Entity not found"));
ensureDeletability(entity);
ensureMutability(entity);
if (checkMutability) {
ensureMutability(entity);
}
return collection.deleteOne(idEq(id)).getDeletedCount() > 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@ExtendWith(MongoDBExtension.class)
Expand All @@ -60,6 +63,14 @@ void create() throws Exception {
verify(clusterEventBus).post(eq(RulesChangedEvent.updatedRuleId(savedRule.id())));
}

@Test
void createSystemRule() throws Exception {
final var rule = systemRule();
final var savedRule = ruleService.save(rule);
assertThat(ruleService.load(savedRule.id())).isEqualTo(rule.toBuilder().id(savedRule.id()).build());
verify(clusterEventBus).post(eq(RulesChangedEvent.updatedRuleId(savedRule.id())));
}

@Test
void update() throws Exception {
final var rule = dummyRule().toBuilder().id(new ObjectId().toHexString()).build();
Expand All @@ -68,6 +79,15 @@ void update() throws Exception {
verify(clusterEventBus).post(eq(RulesChangedEvent.updatedRuleId(savedRule.id())));
}

@Test
void updateSystemRuleThrows() throws Exception {
final var rule = systemRule().toBuilder().id(new ObjectId().toHexString()).build();
assertThatThrownBy(() -> ruleService.save(rule))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Immutable entity cannot be modified");
verify(clusterEventBus, times(0)).post(any());
}

@Test
void loadByName() throws Exception {
final var rule = dummyRule();
Expand All @@ -93,6 +113,15 @@ void delete() {
verify(clusterEventBus).post(eq(RulesChangedEvent.deletedRuleId(rule.id())));
}

@Test
void deleteSystemRule() {
final var rule = ruleService.save(systemRule().toBuilder().title("title 2").build());
assertThat(ruleService.loadAll()).hasSize(1);
ruleService.delete(rule.id());
assertThat(ruleService.loadAll()).hasSize(0);
verify(clusterEventBus).post(eq(RulesChangedEvent.deletedRuleId(rule.id())));
}

@Test
void loadNamed() {
final var rule1 = ruleService.save(dummyRule().toBuilder().title("title 1").build());
Expand All @@ -107,4 +136,8 @@ private static RuleDao dummyRule() {
return RuleDao.builder().title("a title").source("a source").build();
}

private static RuleDao systemRule() {
return RuleDao.builder().scope(SystemPipelineRuleScope.NAME).title("a sysytem rule").source("a source").build();
}

}

0 comments on commit 2886e16

Please sign in to comment.