diff --git a/UPGRADING.md b/UPGRADING.md index dc077af9d35d..e9fcf63a59f9 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -29,9 +29,24 @@ packages, most prominently `com.mongodb.client.model.Filters`. Additionally, the following Java Code API changes are included in this release: -| File/method | Description | -|------------------------------------------------|-------------| -| `org.graylog.scheduler.JobSchedule#toDBUpdate` | removed | +| File/method | Description | +|-----------------------------------------------------------------------------------|------------------------------------------| +| `org.graylog.scheduler.JobSchedule#toDBUpdate` | removed | +| `org.graylog.scheduler.DBJobTriggerService#all` | replaced by streamAll | +| `org.graylog.scheduler.DBJobTriggerService#getAllForJob` | replaced by streamAllForJob | +| `org.graylog.scheduler.DBJobTriggerService#findByQuery` | replaced by streamByQuery | +| `org.graylog.events.processor.DBEventDefinitionService#getByNotificationId` | replaced by streamByNotificationId | +| `org.graylog.events.processor.DBEventDefinitionService#getSystemEventDefinitions` | replaced by streamSystemEventDefinitions | +| `org.graylog.events.processor.DBEventDefinitionService#getByArrayValue` | replaced by streamByArrayValue | +| `org.graylog2.lookup.db.DBCacheService#findByIds` | replaced by streamByIds | +| `org.graylog2.lookup.db.DBCacheService#findAll` | replaced by streamAll | +| `org.graylog2.lookup.db.DBDataAdapterService#findByIds` | replaced by streamByIds | +| `org.graylog2.lookup.db.DBDataAdapterService#findAll` | replaced by streamAll | +| `org.graylog2.lookup.db.DBLookupTableService#findByCacheIds` | replaced by streamByCacheIds | +| `org.graylog2.lookup.db.DBLookupTableService#findByDataAdapterIds` | replaced by streamByDataAdapterIds | +| `org.graylog2.lookup.db.DBLookupTableService#findAll` | replaced by streamAll | + +DBService classes' new streaming methods require streams to be closed after using - recommend using try-with-resource statements. ## REST API Endpoint Changes diff --git a/graylog2-server/src/main/java/org/graylog/events/contentpack/facade/EventDefinitionFacade.java b/graylog2-server/src/main/java/org/graylog/events/contentpack/facade/EventDefinitionFacade.java index 50f641033f26..0e63156361a4 100644 --- a/graylog2-server/src/main/java/org/graylog/events/contentpack/facade/EventDefinitionFacade.java +++ b/graylog2-server/src/main/java/org/graylog/events/contentpack/facade/EventDefinitionFacade.java @@ -24,6 +24,7 @@ import com.google.common.graph.GraphBuilder; import com.google.common.graph.ImmutableGraph; import com.google.common.graph.MutableGraph; +import jakarta.inject.Inject; import org.graylog.events.contentpack.entities.EventDefinitionEntity; import org.graylog.events.processor.DBEventDefinitionService; import org.graylog.events.processor.EventDefinitionDto; @@ -50,8 +51,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.inject.Inject; - import java.util.Map; import java.util.Optional; import java.util.Set; @@ -172,10 +171,12 @@ public EntityExcerpt createExcerpt(EventDefinitionDto nativeEntity) { @Override public Set listEntityExcerpts() { - return eventDefinitionService.streamAll() - .filter(ed -> ed.config().isContentPackExportable()) - .map(this::createExcerpt) - .collect(Collectors.toSet()); + try (var stream = eventDefinitionService.streamAll()) { + return stream + .filter(ed -> ed.config().isContentPackExportable()) + .map(this::createExcerpt) + .collect(Collectors.toSet()); + } } @Override diff --git a/graylog2-server/src/main/java/org/graylog/events/notifications/NotificationResourceHandler.java b/graylog2-server/src/main/java/org/graylog/events/notifications/NotificationResourceHandler.java index a05cb84879c4..c4c07e68b84c 100644 --- a/graylog2-server/src/main/java/org/graylog/events/notifications/NotificationResourceHandler.java +++ b/graylog2-server/src/main/java/org/graylog/events/notifications/NotificationResourceHandler.java @@ -17,6 +17,9 @@ package org.graylog.events.notifications; import com.google.common.collect.ImmutableList; +import jakarta.inject.Inject; +import jakarta.ws.rs.InternalServerErrorException; +import jakarta.ws.rs.NotFoundException; import org.graylog.events.processor.DBEventDefinitionService; import org.graylog.events.processor.EventDefinitionDto; import org.graylog.scheduler.DBJobDefinitionService; @@ -25,13 +28,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.inject.Inject; - -import jakarta.ws.rs.InternalServerErrorException; -import jakarta.ws.rs.NotFoundException; - import java.util.Map; import java.util.Optional; +import java.util.stream.Stream; public class NotificationResourceHandler { private static final Logger LOG = LoggerFactory.getLogger(NotificationResourceHandler.class); @@ -168,20 +167,21 @@ public boolean delete(String dtoId) { }); // Delete notification from existing events - eventDefinitionService.getByNotificationId(dtoId) - .forEach(eventDefinition -> { - LOG.debug("Removing notification <{}/{}> from event definition <{}/{}>", - dto.get().id(), dto.get().title(), - eventDefinition.id(), eventDefinition.title()); - final ImmutableList notifications = eventDefinition.notifications().stream() - .filter(entry -> !entry.notificationId().equals(dtoId)) - .collect(ImmutableList.toImmutableList()); - EventDefinitionDto updatedEventDto = eventDefinition.toBuilder() - .notifications(notifications) - .build(); - eventDefinitionService.save(updatedEventDto); - - }); + try (Stream eventDefinitionStream = eventDefinitionService.streamByNotificationId(dtoId)) { + eventDefinitionStream + .forEach(eventDefinition -> { + LOG.debug("Removing notification <{}/{}> from event definition <{}/{}>", + dto.get().id(), dto.get().title(), + eventDefinition.id(), eventDefinition.title()); + final ImmutableList notifications = eventDefinition.notifications().stream() + .filter(entry -> !entry.notificationId().equals(dtoId)) + .collect(ImmutableList.toImmutableList()); + EventDefinitionDto updatedEventDto = eventDefinition.toBuilder() + .notifications(notifications) + .build(); + eventDefinitionService.save(updatedEventDto); + }); + } LOG.debug("Deleting notification definition <{}/{}>", dto.get().id(), dto.get().title()); return notificationService.delete(dtoId) > 0; } diff --git a/graylog2-server/src/main/java/org/graylog/events/processor/DBEventDefinitionService.java b/graylog2-server/src/main/java/org/graylog/events/processor/DBEventDefinitionService.java index 278229163b75..c770f34e92eb 100644 --- a/graylog2-server/src/main/java/org/graylog/events/processor/DBEventDefinitionService.java +++ b/graylog2-server/src/main/java/org/graylog/events/processor/DBEventDefinitionService.java @@ -16,6 +16,7 @@ */ package org.graylog.events.processor; +import com.google.errorprone.annotations.MustBeClosed; import com.mongodb.client.MongoCollection; import jakarta.inject.Inject; import jakarta.validation.constraints.NotNull; @@ -176,39 +177,43 @@ private long doDeleteUnregister(String id, Supplier deleteSupplier) { } /** - * Returns the list of event definitions that is using the given notification ID. + * Returns the stream of event definitions that is using the given notification ID. * * @param notificationId the notification ID - * @return the event definitions with the given notification ID + * @return stream of the event definitions with the given notification ID */ - public List getByNotificationId(String notificationId) { + @MustBeClosed + public Stream streamByNotificationId(String notificationId) { final String field = String.format(Locale.US, "%s.%s", EventDefinitionDto.FIELD_NOTIFICATIONS, EventNotificationConfig.FIELD_NOTIFICATION_ID); - return stream(collection.find(eq(field, notificationId))).toList(); + return stream(collection.find(eq(field, notificationId))); } /** - * Returns the list of system event definitions + * Returns the stream of system event definitions * - * @return the matching event definitions + * @return stream of the matching event definitions */ - public List getSystemEventDefinitions() { - return stream(collection.find(eq(EventDefinitionDto.FIELD_SCOPE, SystemNotificationEventEntityScope.NAME))).toList(); + @MustBeClosed + public Stream streamSystemEventDefinitions() { + return stream(collection.find(eq(EventDefinitionDto.FIELD_SCOPE, SystemNotificationEventEntityScope.NAME))); } /** - * Returns the list of event definitions that contain the given value in the specified array field + * Returns the stream of event definitions that contain the given value in the specified array field. */ @NotNull - public List getByArrayValue(String arrayField, String field, String value) { - return stream(collection.find(elemMatch(arrayField, eq(field, value)))).toList(); + @MustBeClosed + public Stream streamByArrayValue(String arrayField, String field, String value) { + return stream(collection.find(elemMatch(arrayField, eq(field, value)))); } public boolean isMutable(EventDefinitionDto eventDefinition) { return scopedEntityMongoUtils.isMutable(eventDefinition); } + @MustBeClosed public Stream streamAll() { return stream(collection.find()); } diff --git a/graylog2-server/src/main/java/org/graylog/scheduler/DBJobTriggerService.java b/graylog2-server/src/main/java/org/graylog/scheduler/DBJobTriggerService.java index 863e9d9f87a9..01d05e02a781 100644 --- a/graylog2-server/src/main/java/org/graylog/scheduler/DBJobTriggerService.java +++ b/graylog2-server/src/main/java/org/graylog/scheduler/DBJobTriggerService.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.github.joschi.jadconfig.util.Duration; import com.google.common.primitives.Ints; +import com.google.errorprone.annotations.MustBeClosed; import com.mongodb.client.AggregateIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.model.Accumulators; @@ -51,6 +52,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.google.common.base.Strings.isNullOrEmpty; import static com.mongodb.client.model.Filters.and; @@ -144,12 +146,13 @@ public DBJobTriggerService(MongoConnection mongoConnection, } /** - * Loads all existing records and returns them. + * Streams all existing records and returns the stream. * - * @return list of records + * @return stream of records */ - public List all() { - return stream(collection.find().sort(descending(FIELD_ID))).toList(); + @MustBeClosed + public Stream streamAll() { + return stream(collection.find().sort(descending(FIELD_ID))); } /** @@ -170,24 +173,27 @@ public Optional get(String id) { * @return One found job trigger */ public Optional getOneForJob(String jobDefinitionId) { - final List triggers = getAllForJob(jobDefinitionId); - // We are currently expecting only one trigger per job definition. This will most probably change in the - // future once we extend our scheduler usage. - // TODO: Don't throw exception when there is more than one trigger for a job definition. - // To be able to do this, we need some kind of label system to make sure we can differentiate between - // automatically created triggers (e.g. by event definition) and manually created ones. - if (triggers.size() > 1) { - throw new IllegalStateException("More than one trigger for job definition <" + jobDefinitionId + ">"); + try (final Stream triggerStream = streamAllForJob(jobDefinitionId)) { + final List triggers = triggerStream.toList(); + // We are currently expecting only one trigger per job definition. This will most probably change in the + // future once we extend our scheduler usage. + // TODO: Don't throw exception when there is more than one trigger for a job definition. + // To be able to do this, we need some kind of label system to make sure we can differentiate between + // automatically created triggers (e.g. by event definition) and manually created ones. + if (triggers.size() > 1) { + throw new IllegalStateException("More than one trigger for job definition <" + jobDefinitionId + ">"); + } + return triggers.stream().findFirst(); } - return triggers.stream().findFirst(); } - public List getAllForJob(String jobDefinitionId) { + @MustBeClosed + public Stream streamAllForJob(String jobDefinitionId) { if (isNullOrEmpty(jobDefinitionId)) { throw new IllegalArgumentException("jobDefinitionId cannot be null or empty"); } - return stream(collection.find(eq(FIELD_JOB_DEFINITION_ID, jobDefinitionId))).toList(); + return stream(collection.find(eq(FIELD_JOB_DEFINITION_ID, jobDefinitionId))); } /** @@ -533,13 +539,14 @@ public Optional cancelTriggerByQuery(Bson query) { } /** - * Find triggers by using the provided query. Use judiciously! + * Stream triggers by using the provided query. Use judiciously! * * @param query The query - * @return All found JobTriggers + * @return Stream of all found JobTriggers */ - public List findByQuery(Bson query) { - return stream(collection.find(query).sort(descending(FIELD_UPDATED_AT))).toList(); + @MustBeClosed + public Stream streamByQuery(Bson query) { + return stream(collection.find(query).sort(descending(FIELD_UPDATED_AT))); } private record OverdueTrigger(@JsonProperty("_id") String type, @JsonProperty("count") long count) {} diff --git a/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupCacheFacade.java b/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupCacheFacade.java index 4931843c540a..d7082e9f7ae7 100644 --- a/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupCacheFacade.java +++ b/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupCacheFacade.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableSet; +import jakarta.inject.Inject; import org.graylog2.contentpacks.EntityDescriptorIds; import org.graylog2.contentpacks.model.ModelId; import org.graylog2.contentpacks.model.ModelType; @@ -40,12 +41,11 @@ import org.graylog2.plugin.PluginMetaData; import org.graylog2.plugin.lookup.LookupCacheConfiguration; -import jakarta.inject.Inject; - import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap; import static org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toValueMap; @@ -163,9 +163,11 @@ public EntityExcerpt createExcerpt(CacheDto cacheDto) { @Override public Set listEntityExcerpts() { - return cacheService.findAll().stream() - .map(this::createExcerpt) - .collect(Collectors.toSet()); + try (Stream cacheStream = cacheService.streamAll()) { + return cacheStream + .map(this::createExcerpt) + .collect(Collectors.toSet()); + } } @Override diff --git a/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupDataAdapterFacade.java b/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupDataAdapterFacade.java index 4258e1fd2391..3a2fcb8b21ba 100644 --- a/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupDataAdapterFacade.java +++ b/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupDataAdapterFacade.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableSet; +import jakarta.inject.Inject; import org.graylog2.contentpacks.EntityDescriptorIds; import org.graylog2.contentpacks.model.ModelId; import org.graylog2.contentpacks.model.ModelType; @@ -40,12 +41,11 @@ import org.graylog2.plugin.PluginMetaData; import org.graylog2.plugin.lookup.LookupDataAdapterConfiguration; -import jakarta.inject.Inject; - import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toReferenceMap; import static org.graylog2.contentpacks.model.entities.references.ReferenceMapUtils.toValueMap; @@ -163,9 +163,11 @@ public EntityExcerpt createExcerpt(DataAdapterDto dataAdapterDto) { @Override public Set listEntityExcerpts() { - return dataAdapterService.findAll().stream() - .map(this::createExcerpt) - .collect(Collectors.toSet()); + try (Stream dataAdapterStream = dataAdapterService.streamAll()) { + return dataAdapterStream + .map(this::createExcerpt) + .collect(Collectors.toSet()); + } } @Override diff --git a/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupTableFacade.java b/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupTableFacade.java index b600bf655a26..4fa5aab59ad3 100644 --- a/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupTableFacade.java +++ b/graylog2-server/src/main/java/org/graylog2/contentpacks/facades/LookupTableFacade.java @@ -23,6 +23,7 @@ import com.google.common.graph.GraphBuilder; import com.google.common.graph.ImmutableGraph; import com.google.common.graph.MutableGraph; +import jakarta.inject.Inject; import org.graylog2.contentpacks.EntityDescriptorIds; import org.graylog2.contentpacks.exceptions.ContentPackException; import org.graylog2.contentpacks.exceptions.DivergingEntityConfigurationException; @@ -45,12 +46,11 @@ import org.graylog2.lookup.dto.DataAdapterDto; import org.graylog2.lookup.dto.LookupTableDto; -import jakarta.inject.Inject; - import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; public class LookupTableFacade implements EntityFacade { public static final ModelType TYPE_V1 = ModelTypes.LOOKUP_TABLE_V1; @@ -202,9 +202,11 @@ public EntityExcerpt createExcerpt(LookupTableDto lookupTableDto) { @Override public Set listEntityExcerpts() { - return lookupTableService.findAll().stream() - .map(this::createExcerpt) - .collect(Collectors.toSet()); + try (Stream lookupTableStream = lookupTableService.streamAll()) { + return lookupTableStream + .map(this::createExcerpt) + .collect(Collectors.toSet()); + } } @Override diff --git a/graylog2-server/src/main/java/org/graylog2/decorators/LookupTableDecorator.java b/graylog2-server/src/main/java/org/graylog2/decorators/LookupTableDecorator.java index f2f8331488b5..a1909d2a0085 100644 --- a/graylog2-server/src/main/java/org/graylog2/decorators/LookupTableDecorator.java +++ b/graylog2-server/src/main/java/org/graylog2/decorators/LookupTableDecorator.java @@ -39,6 +39,7 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.google.common.base.Strings.isNullOrEmpty; import static org.graylog.tracing.GraylogSemanticAttributes.LOOKUP_CACHE_NAME; @@ -78,8 +79,10 @@ public Config(DBLookupTableService lookupTableService) { @Override public ConfigurationRequest getRequestedConfiguration() { - final Map lookupTables = lookupTableService.findAll().stream() - .collect(Collectors.toMap(LookupTableDto::name, LookupTableDto::title)); + final Map lookupTables; + try (Stream lookupTableStream = lookupTableService.streamAll()) { + lookupTables = lookupTableStream.collect(Collectors.toMap(LookupTableDto::name, LookupTableDto::title)); + } return new ConfigurationRequest() { { diff --git a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBCacheService.java b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBCacheService.java index 41df5a935395..623dfecc412d 100644 --- a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBCacheService.java +++ b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBCacheService.java @@ -16,6 +16,7 @@ */ package org.graylog2.lookup.db; +import com.google.errorprone.annotations.MustBeClosed; import com.mongodb.client.MongoCollection; import com.mongodb.client.model.IndexOptions; import com.mongodb.client.model.Indexes; @@ -33,15 +34,14 @@ import org.graylog2.lookup.events.CachesDeleted; import org.graylog2.lookup.events.CachesUpdated; -import java.util.Collection; import java.util.Optional; import java.util.Set; +import java.util.stream.Stream; import static com.mongodb.client.model.Filters.eq; import static org.graylog2.database.utils.MongoUtils.stream; import static org.graylog2.database.utils.MongoUtils.stringIdsIn; - public class DBCacheService { public static final String COLLECTION_NAME = "lut_caches"; @@ -115,11 +115,13 @@ public void deleteAndPostEventImmutable(String idOrName) { }); } - public Collection findByIds(Set idSet) { - return stream(collection.find(stringIdsIn(idSet))).toList(); + @MustBeClosed + public Stream streamByIds(Set idSet) { + return stream(collection.find(stringIdsIn(idSet))); } - public Collection findAll() { - return stream(collection.find()).toList(); + @MustBeClosed + public Stream streamAll() { + return stream(collection.find()); } } diff --git a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBDataAdapterService.java b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBDataAdapterService.java index 05fb04dd183c..4e40449c0e5b 100644 --- a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBDataAdapterService.java +++ b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBDataAdapterService.java @@ -16,6 +16,7 @@ */ package org.graylog2.lookup.db; +import com.google.errorprone.annotations.MustBeClosed; import com.mongodb.client.MongoCollection; import com.mongodb.client.model.IndexOptions; import com.mongodb.client.model.Indexes; @@ -33,9 +34,9 @@ import org.graylog2.lookup.events.DataAdaptersDeleted; import org.graylog2.lookup.events.DataAdaptersUpdated; -import java.util.Collection; import java.util.Optional; import java.util.Set; +import java.util.stream.Stream; import static com.mongodb.client.model.Filters.eq; import static org.graylog2.database.utils.MongoUtils.idEq; @@ -124,12 +125,13 @@ public void deleteAndPostEventImmutable(String idOrName) { }); } - public Collection findByIds(Set idSet) { - return stream(collection.find(stringIdsIn(idSet))).toList(); - + @MustBeClosed + public Stream streamByIds(Set idSet) { + return stream(collection.find(stringIdsIn(idSet))); } - public Collection findAll() { - return stream(collection.find()).toList(); + @MustBeClosed + public Stream streamAll() { + return stream(collection.find()); } } diff --git a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableConfigService.java b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableConfigService.java index 83f614de42a2..52c913b5b2c4 100644 --- a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableConfigService.java +++ b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableConfigService.java @@ -16,17 +16,17 @@ */ package org.graylog2.lookup.db; +import jakarta.inject.Inject; +import jakarta.inject.Singleton; import org.graylog2.lookup.LookupTableConfigService; import org.graylog2.lookup.dto.CacheDto; import org.graylog2.lookup.dto.DataAdapterDto; import org.graylog2.lookup.dto.LookupTableDto; -import jakarta.inject.Inject; -import jakarta.inject.Singleton; - import java.util.Collection; import java.util.Optional; import java.util.Set; +import java.util.stream.Stream; @Singleton public class DBLookupTableConfigService implements LookupTableConfigService { @@ -50,36 +50,50 @@ public Optional getTable(String id) { @Override public Collection loadAllTables() { - return dbTables.findAll(); + try (Stream lookupTableStream = dbTables.streamAll()) { + return lookupTableStream.toList(); + } } @Override public Collection findTablesForDataAdapterIds(Set ids) { - return dbTables.findByDataAdapterIds(ids); + try (Stream lookupTableStream = dbTables.streamByDataAdapterIds(ids)) { + return lookupTableStream.toList(); + } } @Override public Collection findTablesForCacheIds(Set ids) { - return dbTables.findByCacheIds(ids); + try (Stream lookupTableStream = dbTables.streamByCacheIds(ids)) { + return lookupTableStream.toList(); + } } @Override public Collection loadAllDataAdapters() { - return dbAdapters.findAll(); + try (Stream dataAdapterStream = dbAdapters.streamAll()) { + return dataAdapterStream.toList(); + } } @Override public Collection findDataAdaptersForIds(Set ids) { - return dbAdapters.findByIds(ids); + try (Stream dataAdapterStream = dbAdapters.streamByIds(ids)) { + return dataAdapterStream.toList(); + } } @Override public Collection loadAllCaches() { - return dbCaches.findAll(); + try (Stream cacheStream = dbCaches.streamAll()) { + return cacheStream.toList(); + } } @Override public Collection findCachesForIds(Set ids) { - return dbCaches.findByIds(ids); + try (Stream cacheStream = dbCaches.streamAll()) { + return cacheStream.toList(); + } } } diff --git a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableService.java b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableService.java index c803e454f767..e0da29a0bd9d 100644 --- a/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableService.java +++ b/graylog2-server/src/main/java/org/graylog2/lookup/db/DBLookupTableService.java @@ -16,6 +16,7 @@ */ package org.graylog2.lookup.db; +import com.google.errorprone.annotations.MustBeClosed; import com.mongodb.client.MongoCollection; import com.mongodb.client.model.IndexOptions; import com.mongodb.client.model.Indexes; @@ -37,6 +38,7 @@ import java.util.Optional; import java.util.function.Consumer; import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.mongodb.client.model.Filters.eq; import static com.mongodb.client.model.Filters.in; @@ -94,22 +96,25 @@ public void postBulkUpdate(Collection tables) { clusterEventBus.post(LookupTablesUpdated.create(tables)); } - public Collection findAll() { - return stream(collection.find()).toList(); + @MustBeClosed + public Stream streamAll() { + return stream(collection.find()); } public PaginatedList findPaginated(Bson query, Bson sort, int page, int perPage) { return paginationHelper.filter(query).sort(sort).perPage(perPage).page(page); } - public Collection findByCacheIds(Collection cacheIds) { + @MustBeClosed + public Stream streamByCacheIds(Collection cacheIds) { Bson query = in("cache", cacheIds.stream().map(ObjectId::new).collect(Collectors.toList())); - return stream(collection.find(query)).toList(); + return stream(collection.find(query)); } - public Collection findByDataAdapterIds(Collection dataAdapterIds) { + @MustBeClosed + public Stream streamByDataAdapterIds(Collection dataAdapterIds) { Bson query = in("data_adapter", dataAdapterIds.stream().map(ObjectId::new).collect(Collectors.toList())); - return stream(collection.find(query)).toList(); + return stream(collection.find(query)); } public void deleteAndPostEvent(String idOrName) { diff --git a/graylog2-server/src/main/java/org/graylog2/migrations/V20190705071400_AddEventIndexSetsMigration.java b/graylog2-server/src/main/java/org/graylog2/migrations/V20190705071400_AddEventIndexSetsMigration.java index fd8c7c871ec0..922459912bcc 100644 --- a/graylog2-server/src/main/java/org/graylog2/migrations/V20190705071400_AddEventIndexSetsMigration.java +++ b/graylog2-server/src/main/java/org/graylog2/migrations/V20190705071400_AddEventIndexSetsMigration.java @@ -212,23 +212,25 @@ private void createEventsStream(String streamId, String streamTitle, String stre } private void ensureSystemNotificationEventsDefinition() { - if (dbService.getSystemEventDefinitions().isEmpty()) { - EventDefinitionDto eventDto = - EventDefinitionDto.builder() - .title("System notification events") - .description("Reserved event definition for system notification events") - .alert(false) - .priority(1) - .keySpec(ImmutableList.of()) - .notificationSettings(EventNotificationSettings.builder() - .gracePeriodMs(0) // Defaults to 0 in the UI - .backlogSize(0) // Defaults to 0 in the UI - .build()) - .config(SystemNotificationEventProcessorConfig.builder().build()) - .storage(ImmutableList.of(PersistToStreamsStorageHandler.Config.createWithSystemEventsStream())) - .scope(SystemNotificationEventEntityScope.NAME) - .build(); - dbService.save(eventDto); + try (java.util.stream.Stream eventDefinitionStream = dbService.streamSystemEventDefinitions()) { + if (eventDefinitionStream.findAny().isEmpty()) { + EventDefinitionDto eventDto = + EventDefinitionDto.builder() + .title("System notification events") + .description("Reserved event definition for system notification events") + .alert(false) + .priority(1) + .keySpec(ImmutableList.of()) + .notificationSettings(EventNotificationSettings.builder() + .gracePeriodMs(0) // Defaults to 0 in the UI + .backlogSize(0) // Defaults to 0 in the UI + .build()) + .config(SystemNotificationEventProcessorConfig.builder().build()) + .storage(ImmutableList.of(PersistToStreamsStorageHandler.Config.createWithSystemEventsStream())) + .scope(SystemNotificationEventEntityScope.NAME) + .build(); + dbService.save(eventDto); + } } } } diff --git a/graylog2-server/src/main/java/org/graylog2/migrations/V20191129134600_CreateInitialUrlWhitelist.java b/graylog2-server/src/main/java/org/graylog2/migrations/V20191129134600_CreateInitialUrlWhitelist.java index b009270986f7..4c853db40968 100644 --- a/graylog2-server/src/main/java/org/graylog2/migrations/V20191129134600_CreateInitialUrlWhitelist.java +++ b/graylog2-server/src/main/java/org/graylog2/migrations/V20191129134600_CreateInitialUrlWhitelist.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.auto.value.AutoValue; +import jakarta.inject.Inject; import org.apache.commons.lang3.StringUtils; import org.graylog.autovalue.WithBeanGetter; import org.graylog.events.legacy.LegacyAlarmCallbackEventNotificationConfig; @@ -42,8 +43,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.inject.Inject; - import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.HashSet; @@ -100,10 +99,11 @@ public void upgrade() { private UrlWhitelist createWhitelist() { final Set entries = new HashSet<>(); - dataAdapterService.findAll() - .stream() - .map(this::extractFromDataAdapter) - .forEach(e -> e.ifPresent(entries::add)); + try (Stream dataAdapterStream = dataAdapterService.streamAll()) { + dataAdapterStream + .map(this::extractFromDataAdapter) + .forEach(e -> e.ifPresent(entries::add)); + } try (final Stream notificationsStream = notificationService.streamAll()) { notificationsStream.map(this::extractFromNotification) diff --git a/graylog2-server/src/main/java/org/graylog2/migrations/V20230523160600_PopulateEventDefinitionState.java b/graylog2-server/src/main/java/org/graylog2/migrations/V20230523160600_PopulateEventDefinitionState.java index b928645810fc..15b415aa990c 100644 --- a/graylog2-server/src/main/java/org/graylog2/migrations/V20230523160600_PopulateEventDefinitionState.java +++ b/graylog2-server/src/main/java/org/graylog2/migrations/V20230523160600_PopulateEventDefinitionState.java @@ -16,6 +16,7 @@ */ package org.graylog2.migrations; +import jakarta.inject.Inject; import org.graylog.events.event.EventDto; import org.graylog.events.processor.DBEventDefinitionService; import org.graylog.events.processor.EventDefinition; @@ -26,8 +27,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.inject.Inject; - import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.List; @@ -63,12 +62,14 @@ public void upgrade() { // Collect a list of all event definitions with a defined job (ie, enabled event definitions) as well as all // system event definitions to be marked as enabled List enabledEventDefinitionIds = new ArrayList<>(); - dbEventDefinitionService.streamAll().forEach(dto -> { - Optional jobDefinition = dbJobDefinitionService.getByConfigField(EventDto.FIELD_EVENT_DEFINITION_ID, dto.id()); - if (dto.scope().equals(SystemNotificationEventEntityScope.NAME) || jobDefinition.isPresent()) { - enabledEventDefinitionIds.add(dto.id()); - } - }); + try (var stream = dbEventDefinitionService.streamAll()) { + stream.forEach(dto -> { + Optional jobDefinition = dbJobDefinitionService.getByConfigField(EventDto.FIELD_EVENT_DEFINITION_ID, dto.id()); + if (dto.scope().equals(SystemNotificationEventEntityScope.NAME) || jobDefinition.isPresent()) { + enabledEventDefinitionIds.add(dto.id()); + } + }); + } // Mark enabled event definitions as such enabledEventDefinitionIds.forEach(id -> dbEventDefinitionService.updateState(id, EventDefinition.State.ENABLED)); diff --git a/graylog2-server/src/main/java/org/graylog2/notifications/NotificationSystemEventPublisher.java b/graylog2-server/src/main/java/org/graylog2/notifications/NotificationSystemEventPublisher.java index 9c86768bdaae..d4317780875f 100644 --- a/graylog2-server/src/main/java/org/graylog2/notifications/NotificationSystemEventPublisher.java +++ b/graylog2-server/src/main/java/org/graylog2/notifications/NotificationSystemEventPublisher.java @@ -17,6 +17,9 @@ package org.graylog2.notifications; import com.google.common.util.concurrent.AbstractExecutionThreadService; +import jakarta.inject.Inject; +import jakarta.inject.Named; +import jakarta.inject.Singleton; import org.graylog.events.processor.DBEventDefinitionService; import org.graylog.events.processor.EventDefinitionDto; import org.graylog.events.processor.EventProcessorEngine; @@ -26,10 +29,6 @@ import org.graylog.events.processor.systemnotification.SystemNotificationRenderService.RenderResponse; import org.slf4j.Logger; -import jakarta.inject.Inject; -import jakarta.inject.Named; -import jakarta.inject.Singleton; - import java.time.Duration; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -37,6 +36,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Stream; import static org.slf4j.LoggerFactory.getLogger; @@ -139,10 +139,11 @@ protected void shutDown() throws Exception { private void publish(Notification notification) { - final EventDefinitionDto systemEventDefinition = - dbEventDefinitionService.getSystemEventDefinitions().stream().findFirst() - .orElseThrow(() -> new IllegalStateException("System notification event definition not found")); - + final EventDefinitionDto systemEventDefinition; + try (Stream eventDefinitionStream = dbEventDefinitionService.streamSystemEventDefinitions()) { + systemEventDefinition = eventDefinitionStream.findFirst() + .orElseThrow(() -> new IllegalStateException("System notification event definition not found")); + } RenderResponse renderResponse; try { renderResponse = systemNotificationRenderService.render(notification); diff --git a/graylog2-server/src/main/java/org/graylog2/rest/resources/system/lookup/LookupTableResource.java b/graylog2-server/src/main/java/org/graylog2/rest/resources/system/lookup/LookupTableResource.java index 0c3e7c56a991..8073ba25865c 100644 --- a/graylog2-server/src/main/java/org/graylog2/rest/resources/system/lookup/LookupTableResource.java +++ b/graylog2-server/src/main/java/org/graylog2/rest/resources/system/lookup/LookupTableResource.java @@ -280,8 +280,11 @@ public LookupTablePage tables(@ApiParam(name = "page") @QueryParam("page") @Defa dataAdapterIds.add(dto.dataAdapterId()); }); - dbCacheService.findByIds(cacheIds.build()).forEach(cacheDto -> caches.add(CacheApi.fromDto(cacheDto))); - dbDataAdapterService.findByIds(dataAdapterIds.build()).forEach(dataAdapterDto -> dataAdapters.add(DataAdapterApi.fromDto(dataAdapterDto))); + try (Stream dataAdapterStream = dbDataAdapterService.streamByIds(dataAdapterIds.build()); + Stream cacheStream = dbCacheService.streamByIds(cacheIds.build())) { + dataAdapterStream.forEach(dataAdapterDto -> dataAdapters.add(DataAdapterApi.fromDto(dataAdapterDto))); + cacheStream.forEach(cacheDto -> caches.add(CacheApi.fromDto(cacheDto))); + } } return new LookupTablePage(query, @@ -312,8 +315,11 @@ public LookupTablePage get(@ApiParam(name = "idOrName") @PathParam("idOrName") @ Set adapters = Collections.emptySet(); if (resolveObjects) { - caches = dbCacheService.findByIds(Collections.singleton(tableDto.cacheId())).stream().map(CacheApi::fromDto).collect(Collectors.toSet()); - adapters = dbDataAdapterService.findByIds(Collections.singleton(tableDto.dataAdapterId())).stream().map(DataAdapterApi::fromDto).collect(Collectors.toSet()); + try (Stream dataAdapterStream = dbDataAdapterService.streamByIds(Collections.singleton(tableDto.dataAdapterId())); + Stream cacheStream = dbCacheService.streamByIds(Collections.singleton(tableDto.cacheId()))) { + caches = cacheStream.map(CacheApi::fromDto).collect(Collectors.toSet()); + adapters = dataAdapterStream.map(DataAdapterApi::fromDto).collect(Collectors.toSet()); + } } final PaginatedList result = PaginatedList.singleton(LookupTableApi.fromDto(tableDto), 1, 1); @@ -577,12 +583,15 @@ public DataAdapterApi createAdapter(@Valid @ApiParam DataAdapterApi newAdapter) @ApiOperation(value = "Delete the given data adapter", notes = "The data adapter cannot be in use by any lookup table, otherwise the request will fail.") public DataAdapterApi deleteAdapter(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName) { Optional dataAdapterDto = dbDataAdapterService.get(idOrName); - if (!dataAdapterDto.isPresent()) { + if (dataAdapterDto.isEmpty()) { throw new NotFoundException(); } DataAdapterDto dto = dataAdapterDto.get(); checkPermission(RestPermissions.LOOKUP_TABLES_DELETE, dto.id()); - boolean unused = dbTableService.findByDataAdapterIds(singleton(dto.id())).isEmpty(); + final boolean unused; + try (Stream lookupTableStream = dbTableService.streamByDataAdapterIds(singleton(dto.id()))) { + unused = lookupTableStream.findAny().isEmpty(); + } if (!unused) { throw new BadRequestException("The adapter is still in use, cannot delete."); } @@ -730,12 +739,15 @@ public CacheApi createCache(@ApiParam CacheApi newCache) { @ApiOperation(value = "Delete the given cache", notes = "The cache cannot be in use by any lookup table, otherwise the request will fail.") public CacheApi deleteCache(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName) { Optional cacheDto = dbCacheService.get(idOrName); - if (!cacheDto.isPresent()) { + if (cacheDto.isEmpty()) { throw new NotFoundException(); } CacheDto dto = cacheDto.get(); checkPermission(RestPermissions.LOOKUP_TABLES_DELETE, dto.id()); - boolean unused = dbTableService.findByCacheIds(singleton(dto.id())).isEmpty(); + final boolean unused; + try (Stream lookupTableStream = dbTableService.streamByCacheIds(singleton(dto.id()))) { + unused = lookupTableStream.findAny().isEmpty(); + } if (!unused) { throw new BadRequestException("The cache is still in use, cannot delete."); } diff --git a/graylog2-server/src/test/java/org/graylog/events/contentpack/facade/EventDefinitionFacadeTest.java b/graylog2-server/src/test/java/org/graylog/events/contentpack/facade/EventDefinitionFacadeTest.java index a99fe1f03f38..bcfb961a08b9 100644 --- a/graylog2-server/src/test/java/org/graylog/events/contentpack/facade/EventDefinitionFacadeTest.java +++ b/graylog2-server/src/test/java/org/graylog/events/contentpack/facade/EventDefinitionFacadeTest.java @@ -345,7 +345,10 @@ public void listExcerptsExcludesNonContentPackExportableEventDefinitions() { @Test @MongoDBFixtures("EventDefinitionFacadeTest.json") public void delete() { - long countBefore = eventDefinitionService.streamAll().count(); + long countBefore; + try (var stream = eventDefinitionService.streamAll()) { + countBefore = stream.count(); + } assertThat(countBefore).isEqualTo(1); final Optional eventDefinitionDto = eventDefinitionService.get( @@ -353,7 +356,10 @@ public void delete() { assertThat(eventDefinitionDto).isPresent(); facade.delete(eventDefinitionDto.get()); - long countAfter = eventDefinitionService.streamAll().count(); + long countAfter; + try (var stream = eventDefinitionService.streamAll()) { + countAfter = stream.count(); + } assertThat(countAfter).isEqualTo(0); } diff --git a/graylog2-server/src/test/java/org/graylog/events/legacy/LegacyAlertConditionMigratorTest.java b/graylog2-server/src/test/java/org/graylog/events/legacy/LegacyAlertConditionMigratorTest.java index 2a7becf03f9d..78e8662685fb 100644 --- a/graylog2-server/src/test/java/org/graylog/events/legacy/LegacyAlertConditionMigratorTest.java +++ b/graylog2-server/src/test/java/org/graylog/events/legacy/LegacyAlertConditionMigratorTest.java @@ -177,7 +177,9 @@ public void run() { // Make sure we use the NotificationResourceHandler to create the notifications verify(notificationResourceHandler, times(migratedCallbacks)).create(any(NotificationDto.class), any(Optional.class)); - assertThat(eventDefinitionService.streamAll().count()).isEqualTo(migratedConditions); + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.count()).isEqualTo(migratedConditions); + } assertThat(notificationService.streamAll().count()).isEqualTo(migratedCallbacks); final NotificationDto httpNotification = notificationService.streamAll() @@ -253,370 +255,390 @@ public void run() { assertThat(config.configuration().get("short_mode")).isEqualTo(false); }); - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Message Count - MORE")).findFirst()) + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Message Count - MORE")).findFirst()) .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(120000); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(10); - - assertThat(eventDefinition.notifications()).hasSize(2); - assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toList())) - .containsOnly(httpNotification.id(), httpNotificationWithoutTitle.id()); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0001"); - assertThat(config.query()).isEqualTo("hello:world"); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(10 * 60 * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); - assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Greater.class); - - final Expr.Greater greater = (Expr.Greater) expression; - - assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(1)); + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(120000); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(10); + + assertThat(eventDefinition.notifications()).hasSize(2); + assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toList())) + .containsOnly(httpNotification.id(), httpNotificationWithoutTitle.id()); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0001"); + assertThat(config.query()).isEqualTo("hello:world"); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(10 * 60 * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); + assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Greater.class); + + final Expr.Greater greater = (Expr.Greater) expression; + + assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(1)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Message Count - LESS")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0); - - assertThat(eventDefinition.notifications()).hasSize(2); - assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toList())) - .containsOnly(httpNotification.id(), httpNotificationWithoutTitle.id()); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0001"); - assertThat(config.query()).isEmpty(); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(4 * 60 * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); - assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Lesser.class); - - final Expr.Lesser lesser = (Expr.Lesser) expression; - - assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(42)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Message Count - LESS")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0); + + assertThat(eventDefinition.notifications()).hasSize(2); + assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toList())) + .containsOnly(httpNotification.id(), httpNotificationWithoutTitle.id()); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0001"); + assertThat(config.query()).isEmpty(); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(4 * 60 * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); + assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Lesser.class); + + final Expr.Lesser lesser = (Expr.Lesser) expression; + + assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(42)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - HIGHER - MEAN")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); - assertThat(eventDefinition.notifications()).isEmpty(); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); - assertThat(config.query()).isEqualTo("*"); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Average.NAME); - assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Greater.class); - - final Expr.Greater greater = (Expr.Greater) expression; - - assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(23)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Field Value - HIGHER - MEAN")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); + assertThat(eventDefinition.notifications()).isEmpty(); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); + assertThat(config.query()).isEqualTo("*"); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Average.NAME); + assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Greater.class); + + final Expr.Greater greater = (Expr.Greater) expression; + + assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(23)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - LOWER - SUM")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); - assertThat(eventDefinition.notifications()).isEmpty(); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); - assertThat(config.query()).isEqualTo("*"); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Sum.NAME); - assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Lesser.class); - - final Expr.Lesser lesser = (Expr.Lesser) expression; - - assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Field Value - LOWER - SUM")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); + assertThat(eventDefinition.notifications()).isEmpty(); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); + assertThat(config.query()).isEqualTo("*"); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Sum.NAME); + assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Lesser.class); + + final Expr.Lesser lesser = (Expr.Lesser) expression; + + assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - LOWER - MIN")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); - assertThat(eventDefinition.notifications()).isEmpty(); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); - assertThat(config.query()).isEqualTo("*"); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Min.NAME); - assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Lesser.class); - - final Expr.Lesser lesser = (Expr.Lesser) expression; - - assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Field Value - LOWER - MIN")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); + assertThat(eventDefinition.notifications()).isEmpty(); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); + assertThat(config.query()).isEqualTo("*"); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Min.NAME); + assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Lesser.class); + + final Expr.Lesser lesser = (Expr.Lesser) expression; + + assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - LOWER - MAX")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); - assertThat(eventDefinition.notifications()).isEmpty(); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); - assertThat(config.query()).isEqualTo("*"); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Max.NAME); - assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Lesser.class); - - final Expr.Lesser lesser = (Expr.Lesser) expression; - - assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Field Value - LOWER - MAX")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); + assertThat(eventDefinition.notifications()).isEmpty(); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); + assertThat(config.query()).isEqualTo("*"); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Max.NAME); + assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Lesser.class); + + final Expr.Lesser lesser = (Expr.Lesser) expression; + + assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - LOWER - STDDEV")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); - assertThat(eventDefinition.notifications()).isEmpty(); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); - assertThat(config.query()).isEqualTo("*"); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(StdDev.NAME); - assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Greater.class); - - final Expr.Greater greater = (Expr.Greater) expression; - - assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(23)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Field Value - LOWER - STDDEV")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15); + assertThat(eventDefinition.notifications()).isEmpty(); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002"); + assertThat(config.query()).isEqualTo("*"); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(StdDev.NAME); + assertThat(((HasField) config.series().get(0)).field()).isEqualTo("test_field_1"); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Greater.class); + + final Expr.Greater greater = (Expr.Greater) expression; + + assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(23)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Content - WITHOUT QUERY")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(120000); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(100); - - assertThat(eventDefinition.notifications()).hasSize(2); - assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())) - .containsOnly(emailNotification.id(), slackNotification.id()); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003"); - assertThat(config.query()).isEqualTo("test_field_2:\"hello\""); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); - assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Greater.class); - - final Expr.Greater greater = (Expr.Greater) expression; - - assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Field Content - WITHOUT QUERY")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(120000); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(100); + + assertThat(eventDefinition.notifications()).hasSize(2); + assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())) + .containsOnly(emailNotification.id(), slackNotification.id()); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003"); + assertThat(config.query()).isEqualTo("test_field_2:\"hello\""); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); + assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Greater.class); + + final Expr.Greater greater = (Expr.Greater) expression; + + assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Content - WITH QUERY")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0); - - assertThat(eventDefinition.notifications()).hasSize(2); - assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())) - .containsOnly(emailNotification.id(), slackNotification.id()); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003"); - assertThat(config.query()).isEqualTo("test_field_3:\"foo\" AND foo:bar"); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); - assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Greater.class); - - final Expr.Greater greater = (Expr.Greater) expression; - - assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Field Content - WITH QUERY")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0); + + assertThat(eventDefinition.notifications()).hasSize(2); + assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())) + .containsOnly(emailNotification.id(), slackNotification.id()); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003"); + assertThat(config.query()).isEqualTo("test_field_3:\"foo\" AND foo:bar"); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); + assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Greater.class); + + final Expr.Greater greater = (Expr.Greater) expression; + + assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0)); + }); }); }); }); - }); - - assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Untitled")).findFirst()) - .get() - .satisfies(eventDefinition -> { - assertThat(eventDefinition.alert()).isTrue(); - assertThat(eventDefinition.priority()).isEqualTo(2); - assertThat(eventDefinition.keySpec()).isEmpty(); - assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0); - assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0); - - assertThat(eventDefinition.notifications()).hasSize(2); - assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())) - .containsOnly(emailNotification.id(), slackNotification.id()); - - assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { - assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003"); - assertThat(config.query()).isEqualTo("test_field_3:\"foo\" AND foo:bar"); - assertThat(config.groupBy()).isEmpty(); - assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000); - assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); - - assertThat(config.series()).hasSize(1); - assertThat(config.series().get(0).id()).isNotBlank(); - assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); - assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); - - assertThat(config.conditions()).get().satisfies(conditions -> { - assertThat(conditions.expression()).get().satisfies(expression -> { - assertThat(expression).isInstanceOf(Expr.Greater.class); - - final Expr.Greater greater = (Expr.Greater) expression; - - assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); - assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0)); + } + + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.filter(ed -> ed.title().equals("Untitled")).findFirst()) + .get() + .satisfies(eventDefinition -> { + assertThat(eventDefinition.alert()).isTrue(); + assertThat(eventDefinition.priority()).isEqualTo(2); + assertThat(eventDefinition.keySpec()).isEmpty(); + assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0); + assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0); + + assertThat(eventDefinition.notifications()).hasSize(2); + assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())) + .containsOnly(emailNotification.id(), slackNotification.id()); + + assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> { + assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003"); + assertThat(config.query()).isEqualTo("test_field_3:\"foo\" AND foo:bar"); + assertThat(config.groupBy()).isEmpty(); + assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000); + assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000); + + assertThat(config.series()).hasSize(1); + assertThat(config.series().get(0).id()).isNotBlank(); + assertThat(config.series().get(0).type()).isEqualTo(Count.NAME); + assertThat(((HasOptionalField) config.series().get(0)).field()).isEmpty(); + + assertThat(config.conditions()).get().satisfies(conditions -> { + assertThat(conditions.expression()).get().satisfies(expression -> { + assertThat(expression).isInstanceOf(Expr.Greater.class); + + final Expr.Greater greater = (Expr.Greater) expression; + + assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id())); + assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0)); + }); }); }); }); - }); + } } @Test @@ -652,7 +674,9 @@ public void runWithMigrationStatus() { // Make sure we use the NotificationResourceHandler to create the notifications verify(notificationResourceHandler, times(migratedCallbacks)).create(any(NotificationDto.class), any(Optional.class)); - assertThat(eventDefinitionService.streamAll().count()).isEqualTo(migratedConditions); + try (var stream = eventDefinitionService.streamAll()) { + assertThat(stream.count()).isEqualTo(migratedConditions); + } assertThat(notificationService.streamAll().count()).isEqualTo(migratedCallbacks); } } diff --git a/graylog2-server/src/test/java/org/graylog/events/processor/DBEventProcessorServiceTest.java b/graylog2-server/src/test/java/org/graylog/events/processor/DBEventProcessorServiceTest.java index eedbf21fd6ea..894985c592bc 100644 --- a/graylog2-server/src/test/java/org/graylog/events/processor/DBEventProcessorServiceTest.java +++ b/graylog2-server/src/test/java/org/graylog/events/processor/DBEventProcessorServiceTest.java @@ -76,7 +76,10 @@ public void setUp() throws Exception { @Test @MongoDBFixtures("event-processors.json") public void loadPersisted() { - final List dtos = dbService.streamAll().collect(Collectors.toList()); + final List dtos; + try (var stream = dbService.streamAll()) { + dtos = stream.collect(Collectors.toList()); + } assertThat(dtos).hasSize(1); diff --git a/graylog2-server/src/test/java/org/graylog/events/processor/EventDefinitionHandlerTest.java b/graylog2-server/src/test/java/org/graylog/events/processor/EventDefinitionHandlerTest.java index 6325b0ea4b1b..53d006cf5389 100644 --- a/graylog2-server/src/test/java/org/graylog/events/processor/EventDefinitionHandlerTest.java +++ b/graylog2-server/src/test/java/org/graylog/events/processor/EventDefinitionHandlerTest.java @@ -443,7 +443,9 @@ public void delete() { public void schedule() { assertThat(eventDefinitionService.get("54e3deadbeefdeadbeef0000")).isPresent(); assertThat(jobDefinitionService.streamAll().count()).isEqualTo(0); - assertThat(jobTriggerService.all()).isEmpty(); + try (var stream = jobTriggerService.streamAll()) { + assertThat(stream).isEmpty(); + } handler.schedule("54e3deadbeefdeadbeef0000"); diff --git a/graylog2-server/src/test/java/org/graylog/scheduler/DBJobTriggerServiceTest.java b/graylog2-server/src/test/java/org/graylog/scheduler/DBJobTriggerServiceTest.java index 4da9a28ee071..3d84e22d99b2 100644 --- a/graylog2-server/src/test/java/org/graylog/scheduler/DBJobTriggerServiceTest.java +++ b/graylog2-server/src/test/java/org/graylog/scheduler/DBJobTriggerServiceTest.java @@ -53,6 +53,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import java.util.stream.Stream; import static java.util.Comparator.comparing; import static java.util.Objects.requireNonNull; @@ -105,10 +106,12 @@ private DBJobTriggerService serviceWithClock(JobSchedulerClock clock) { @MongoDBFixtures("job-triggers.json") public void loadPersistedTriggers() { // Sort by ID to make sure we have a defined order - final List all = dbJobTriggerService.all() - .stream() - .sorted(comparing(jobTriggerDto -> requireNonNull(jobTriggerDto.id()))) - .collect(ImmutableList.toImmutableList()); + final List all; + try (Stream triggerStream = dbJobTriggerService.streamAll()) { + all = triggerStream + .sorted(comparing(jobTriggerDto -> requireNonNull(jobTriggerDto.id()))) + .collect(ImmutableList.toImmutableList()); + } assertThat(all).hasSize(4); @@ -241,9 +244,11 @@ public void getAllForJob() { .hasMessageContaining("54e3deadbeefdeadbeefaff3"); // But we can also obtain all by calling following method: - assertThat(dbJobTriggerService.getAllForJob("54e3deadbeefdeadbeefaff3")) - .hasSize(2) - .allSatisfy(trigger -> assertThat(trigger.jobDefinitionId()).isEqualTo("54e3deadbeefdeadbeefaff3")); + try (var stream = dbJobTriggerService.streamAllForJob("54e3deadbeefdeadbeefaff3")) { + assertThat(stream) + .hasSize(2) + .allSatisfy(trigger -> assertThat(trigger.jobDefinitionId()).isEqualTo("54e3deadbeefdeadbeefaff3")); + } } @@ -879,19 +884,25 @@ public void deleteCompletedTooNew() { @Test @MongoDBFixtures("stale-job-triggers.json") public void forceReleaseOwnedTriggers() { - final Set triggerIds = dbJobTriggerService.all().stream() - .filter(dto -> JobTriggerStatus.RUNNING.equals(dto.status())) - .map(JobTriggerDto::id) - .collect(Collectors.toSet()); + final Set triggerIds; + try (Stream triggerStream = dbJobTriggerService.streamAll()) { + triggerIds = triggerStream + .filter(dto -> JobTriggerStatus.RUNNING.equals(dto.status())) + .map(JobTriggerDto::id) + .collect(Collectors.toSet()); + } assertThat(triggerIds).containsOnly("54e3deadbeefdeadbeef0001", "54e3deadbeefdeadbeef0002", "54e3deadbeefdeadbeef0004"); assertThat(dbJobTriggerService.forceReleaseOwnedTriggers()).isEqualTo(2); - final Set newTriggerIds = dbJobTriggerService.all().stream() - .filter(dto -> JobTriggerStatus.RUNNING.equals(dto.status())) - .map(JobTriggerDto::id) - .collect(Collectors.toSet()); + final Set newTriggerIds; + try (Stream triggerStream = dbJobTriggerService.streamAll()) { + newTriggerIds = triggerStream + .filter(dto -> JobTriggerStatus.RUNNING.equals(dto.status())) + .map(JobTriggerDto::id) + .collect(Collectors.toSet()); + } // Running triggers not owned by this node should not be released assertThat(newTriggerIds).containsOnly("54e3deadbeefdeadbeef0002"); @@ -900,19 +911,25 @@ public void forceReleaseOwnedTriggers() { @Test @MongoDBFixtures("stale-job-triggers.json") public void forceReleaseOwnedCancelledTriggers() { - final Set cancelledTriggerIds = dbJobTriggerService.all().stream() - .filter(JobTriggerDto::isCancelled) - .map(JobTriggerDto::id) - .collect(Collectors.toSet()); + final Set cancelledTriggerIds; + try (Stream triggerStream = dbJobTriggerService.streamAll()) { + cancelledTriggerIds = triggerStream + .filter(JobTriggerDto::isCancelled) + .map(JobTriggerDto::id) + .collect(Collectors.toSet()); + } assertThat(cancelledTriggerIds).containsOnly("54e3deadbeefdeadbeef0001"); assertThat(dbJobTriggerService.forceReleaseOwnedTriggers()).isEqualTo(2); - final Set newCancelledTriggerIds = dbJobTriggerService.all().stream() - .filter(JobTriggerDto::isCancelled) - .map(JobTriggerDto::id) - .collect(Collectors.toSet()); + final Set newCancelledTriggerIds; + try (Stream triggerStream = dbJobTriggerService.streamAll()) { + newCancelledTriggerIds = triggerStream + .filter(JobTriggerDto::isCancelled) + .map(JobTriggerDto::id) + .collect(Collectors.toSet()); + } // Force releasing triggers should reset the "is_cancelled" flag to false assertThat(newCancelledTriggerIds).isEmpty(); @@ -939,10 +956,13 @@ public void updateLockedJobTriggers() { service.updateLockedJobTriggers(); - List updatedJobTriggerIds = service.all().stream() - .filter(jobTriggerDto -> newLockTime.equals(jobTriggerDto.lock().lastLockTime())) - .map(JobTriggerDto::id) - .collect(Collectors.toList()); + final List updatedJobTriggerIds; + try (Stream triggerStream = service.streamAll()) { + updatedJobTriggerIds = triggerStream + .filter(jobTriggerDto -> newLockTime.equals(jobTriggerDto.lock().lastLockTime())) + .map(JobTriggerDto::id) + .collect(Collectors.toList()); + } assertThat(updatedJobTriggerIds).containsOnly("54e3deadbeefdeadbeef0001", "54e3deadbeefdeadbeef0002"); } diff --git a/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupCacheFacadeTest.java b/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupCacheFacadeTest.java index 81c4f2768d34..1b3071f80338 100644 --- a/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupCacheFacadeTest.java +++ b/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupCacheFacadeTest.java @@ -53,6 +53,7 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.Executors; +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -137,7 +138,9 @@ public void createNativeEntity() { ReferenceMapUtils.toReferenceMap(ImmutableMap.of("type", "none")) ), JsonNode.class)) .build(); - assertThat(cacheService.findAll()).isEmpty(); + try (Stream cacheStream = cacheService.streamAll()) { + assertThat(cacheStream).isEmpty(); + } final NativeEntity nativeEntity = facade.createNativeEntity(entity, Collections.emptyMap(), Collections.emptyMap(), "username"); final NativeEntityDescriptor descriptor = nativeEntity.descriptor(); @@ -150,7 +153,9 @@ public void createNativeEntity() { assertThat(cacheDto.description()).isEqualTo("No-op cache"); assertThat(cacheDto.config().type()).isEqualTo("none"); - assertThat(cacheService.findAll()).hasSize(1); + try (Stream cacheStream = cacheService.streamAll()) { + assertThat(cacheStream).hasSize(1); + } } @Test @@ -235,11 +240,15 @@ public void resolveEntityDescriptor() { public void delete() { final Optional cacheDto = cacheService.get("5adf24b24b900a0fdb4e52dd"); - assertThat(cacheService.findAll()).hasSize(1); + try (Stream cacheStream = cacheService.streamAll()) { + assertThat(cacheStream).hasSize(1); + } cacheDto.ifPresent(facade::delete); assertThat(cacheService.get("5adf24b24b900a0fdb4e52dd")).isEmpty(); - assertThat(cacheService.findAll()).isEmpty(); + try (Stream cacheStream = cacheService.streamAll()) { + assertThat(cacheStream).isEmpty(); + } } @Test diff --git a/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupDataAdapterFacadeTest.java b/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupDataAdapterFacadeTest.java index cb0d2063e9ad..309b0c15683e 100644 --- a/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupDataAdapterFacadeTest.java +++ b/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupDataAdapterFacadeTest.java @@ -134,7 +134,9 @@ public void createNativeEntity() { ReferenceMapUtils.toReferenceMap(Collections.emptyMap()) ), JsonNode.class)) .build(); - assertThat(dataAdapterService.findAll()).isEmpty(); + try (var stream = dataAdapterService.streamAll()) { + assertThat(stream).isEmpty(); + } final NativeEntity nativeEntity = facade.createNativeEntity(entity, Collections.emptyMap(), Collections.emptyMap(), "username"); @@ -144,7 +146,9 @@ public void createNativeEntity() { assertThat(nativeEntity.entity().title()).isEqualTo("HTTP DSV"); assertThat(nativeEntity.entity().description()).isEqualTo("HTTP DSV"); - assertThat(dataAdapterService.findAll()).hasSize(1); + try (var stream = dataAdapterService.streamAll()) { + assertThat(stream).hasSize(1); + } } @Test @@ -193,10 +197,14 @@ public void findExistingWithNoExistingEntity() { public void delete() { final Optional dataAdapterDto = dataAdapterService.get("5adf24a04b900a0fdb4e52c8"); - assertThat(dataAdapterService.findAll()).hasSize(1); + try (var stream = dataAdapterService.streamAll()) { + assertThat(stream).hasSize(1); + } dataAdapterDto.ifPresent(facade::delete); - assertThat(dataAdapterService.findAll()).isEmpty(); + try (var stream = dataAdapterService.streamAll()) { + assertThat(stream).isEmpty(); + } assertThat(dataAdapterService.get("5adf24a04b900a0fdb4e52c8")).isEmpty(); } diff --git a/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupTableFacadeTest.java b/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupTableFacadeTest.java index c35aadfd6e76..3d3b9733b26c 100644 --- a/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupTableFacadeTest.java +++ b/graylog2-server/src/test/java/org/graylog2/contentpacks/facades/LookupTableFacadeTest.java @@ -190,7 +190,9 @@ public void createNativeEntity() { final Map nativeEntities = ImmutableMap.of( cacheDescriptor, cacheDto, dataAdapterDescriptor, dataAdapterDto); - assertThat(lookupTableService.findAll()).isEmpty(); + try (var stream = lookupTableService.streamAll()) { + assertThat(stream).isEmpty(); + } final NativeEntity nativeEntity = facade.createNativeEntity(entity, Collections.emptyMap(), nativeEntities, "username"); @@ -205,7 +207,9 @@ public void createNativeEntity() { assertThat(nativeEntity.entity().defaultMultiValue()).isEqualTo("Default multi value"); assertThat(nativeEntity.entity().defaultMultiValueType()).isEqualTo(LookupDefaultValue.Type.OBJECT); - assertThat(lookupTableService.findAll()).hasSize(1); + try (var stream = lookupTableService.streamAll()) { + assertThat(stream).hasSize(1); + } } @Test @@ -333,10 +337,14 @@ public void findExistingWithNoExistingEntity() { public void delete() { final Optional lookupTableDto = lookupTableService.get("5adf24dd4b900a0fdb4e530d"); - assertThat(lookupTableService.findAll()).hasSize(1); + try (var stream = lookupTableService.streamAll()) { + assertThat(stream).hasSize(1); + } lookupTableDto.ifPresent(facade::delete); - assertThat(lookupTableService.findAll()).isEmpty(); + try (var stream = lookupTableService.streamAll()) { + assertThat(stream).isEmpty(); + } assertThat(lookupTableService.get("5adf24dd4b900a0fdb4e530d")).isEmpty(); } diff --git a/graylog2-server/src/test/java/org/graylog2/migrations/V20191129134600_CreateInitialUrlWhitelistTest.java b/graylog2-server/src/test/java/org/graylog2/migrations/V20191129134600_CreateInitialUrlWhitelistTest.java index 84f7ad65dbbd..e31539930e63 100644 --- a/graylog2-server/src/test/java/org/graylog2/migrations/V20191129134600_CreateInitialUrlWhitelistTest.java +++ b/graylog2-server/src/test/java/org/graylog2/migrations/V20191129134600_CreateInitialUrlWhitelistTest.java @@ -32,7 +32,7 @@ import org.mockito.MockitoAnnotations; import org.mockito.Spy; -import java.util.Collections; +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -65,7 +65,7 @@ public void createQuotedRegexEntry() { when(config.url()).thenReturn("https://www.graylog.com/${key}/test.json/${key}"); final DataAdapterDto dataAdapterDto = mock(DataAdapterDto.class); when(dataAdapterDto.config()).thenReturn(config); - when(dataAdapterService.findAll()).thenReturn(Collections.singleton(dataAdapterDto)); + when(dataAdapterService.streamAll()).thenReturn(Stream.of(dataAdapterDto)); migration.upgrade();