Search in sources :

Example 11 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class PipelineRuleFacadeTest method delete.

@Test
@MongoDBFixtures("PipelineRuleFacadeTest.json")
public void delete() throws NotFoundException {
    final RuleDao ruleDao = ruleService.loadByName("debug");
    assertThat(ruleService.loadAll()).hasSize(2);
    facade.delete(ruleDao);
    assertThatThrownBy(() -> ruleService.loadByName("debug")).isInstanceOf(NotFoundException.class);
    assertThat(ruleService.loadAll()).hasSize(1);
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 12 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class PipelineRuleFacadeTest method exportEntity.

@Test
public void exportEntity() {
    final RuleDao pipelineRule = RuleDao.builder().id("id").title("title").description("description").source("rule \"debug\"\nwhen\n  true\nthen\n  debug($message.message);\nend").build();
    final EntityDescriptor descriptor = EntityDescriptor.create("id", ModelTypes.PIPELINE_RULE_V1);
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
    final Entity entity = facade.exportNativeEntity(pipelineRule, entityDescriptorIds);
    assertThat(entity).isInstanceOf(EntityV1.class);
    assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
    assertThat(entity.type()).isEqualTo(ModelTypes.PIPELINE_RULE_V1);
    final EntityV1 entityV1 = (EntityV1) entity;
    final PipelineRuleEntity ruleEntity = objectMapper.convertValue(entityV1.data(), PipelineRuleEntity.class);
    assertThat(ruleEntity.title()).isEqualTo(ValueReference.of("title"));
    assertThat(ruleEntity.description()).isEqualTo(ValueReference.of("description"));
    assertThat(ruleEntity.source().asString(Collections.emptyMap())).startsWith("rule \"debug\"\n");
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) PipelineEntity(org.graylog2.contentpacks.model.entities.PipelineEntity) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) Test(org.junit.Test)

Example 13 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class PipelineFacadeTest method resolve.

@Test
@MongoDBFixtures("PipelineFacadeTest/pipelines.json")
public void resolve() {
    final Stage stage = Stage.builder().stage(0).match(Stage.Match.EITHER).ruleReferences(ImmutableList.of("debug", "no-op")).build();
    RuleDao ruleDao1 = RuleDao.builder().id("2342353045938450345").title("debug").source("rule \\\"debug\\\"\\nrule \\\"no-op\\\"\\nend\"").build();
    org.graylog.plugins.pipelineprocessor.ast.Rule rule1 = org.graylog.plugins.pipelineprocessor.ast.Rule.builder().id("1").name("debug").when(mock(LogicalExpression.class)).then(Collections.emptyList()).build();
    RuleDao ruleDao2 = RuleDao.builder().id("2342353045938450346").title("no-op").source("rule \\\"debug\\\"\\nrule \\\"no-op\\\"\\nend\"").build();
    org.graylog.plugins.pipelineprocessor.ast.Rule rule2 = org.graylog.plugins.pipelineprocessor.ast.Rule.builder().id("2").name("no-op").when(mock(LogicalExpression.class)).then(Collections.emptyList()).build();
    stage.setRules(ImmutableList.of(rule1, rule2));
    final Pipeline pipeline = Pipeline.builder().id("5a85c4854b900afd5d662be3").name("Test").stages(ImmutableSortedSet.of(stage)).build();
    when(pipelineRuleParser.parsePipeline(eq("dummy"), anyString())).thenReturn(pipeline);
    when(ruleService.findByName("no-op")).thenReturn(Optional.of(ruleDao1));
    when(ruleService.findByName("debug")).thenReturn(Optional.of(ruleDao2));
    final EntityDescriptor pipelineEntity = EntityDescriptor.create("5a85c4854b900afd5d662be3", ModelTypes.PIPELINE_V1);
    final Graph<EntityDescriptor> graph = facade.resolveNativeEntity(pipelineEntity);
    final EntityDescriptor streamEntity = EntityDescriptor.create("5adf23894b900a0fdb4e517d", ModelTypes.STREAM_V1);
    final EntityDescriptor ruleEntity1 = EntityDescriptor.create("2342353045938450345", ModelTypes.PIPELINE_RULE_V1);
    final EntityDescriptor ruleEntity2 = EntityDescriptor.create("2342353045938450346", ModelTypes.PIPELINE_RULE_V1);
    assertThat(graph.nodes()).containsOnly(pipelineEntity, streamEntity, ruleEntity1, ruleEntity2);
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) LogicalExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Stage(org.graylog.plugins.pipelineprocessor.ast.Stage) Pipeline(org.graylog.plugins.pipelineprocessor.ast.Pipeline) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 14 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class PipelineRuleFacadeTest method findExisting.

@Test
@MongoDBFixtures("PipelineRuleFacadeTest.json")
public void findExisting() {
    final Entity entity = EntityV1.builder().id(ModelId.of("debug")).type(ModelTypes.PIPELINE_RULE_V1).data(objectMapper.convertValue(PipelineRuleEntity.create(ValueReference.of("debug"), ValueReference.of("Debug"), ValueReference.of("rule \"debug\"\nwhen\n  true\nthen\n  debug($message.message);\nend")), JsonNode.class)).build();
    final NativeEntity<RuleDao> existingRule = facade.findExisting(entity, Collections.emptyMap()).orElseThrow(AssertionError::new);
    assertThat(existingRule.descriptor().id()).isEqualTo(ModelId.of("5adf25034b900a0fdb4e5338"));
    assertThat(existingRule.descriptor().type()).isEqualTo(ModelTypes.PIPELINE_RULE_V1);
    assertThat(existingRule.entity().title()).isEqualTo("debug");
    assertThat(existingRule.entity().description()).isEqualTo("Debug");
    assertThat(existingRule.entity().source()).startsWith("rule \"debug\"\n");
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) PipelineEntity(org.graylog2.contentpacks.model.entities.PipelineEntity) PipelineRuleEntity(org.graylog2.contentpacks.model.entities.PipelineRuleEntity) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 15 with RuleDao

use of org.graylog.plugins.pipelineprocessor.db.RuleDao in project graylog2-server by Graylog2.

the class PipelineFacadeTest method resolveEntityDescriptor.

@Test
@MongoDBFixtures("PipelineFacadeTest/pipelines.json")
public void resolveEntityDescriptor() {
    final Stage stage = Stage.builder().stage(0).match(Stage.Match.EITHER).ruleReferences(Collections.singletonList("no-op")).build();
    final Pipeline pipeline = Pipeline.builder().id("5a85c4854b900afd5d662be3").name("Test").stages(ImmutableSortedSet.of(stage)).build();
    when(pipelineRuleParser.parsePipeline("dummy", "pipeline \"Test\"\nstage 0 match either\nrule \"debug\"\nrule \"no-op\"\nend")).thenReturn(pipeline);
    RuleDao ruleDao = RuleDao.builder().id("2342353045938450345").title("no-op").source("rule \\\"debug\\\"\\nrule \\\"no-op\\\"\\nend\"").build();
    when(ruleService.findByName("no-op")).thenReturn(Optional.of(ruleDao));
    final EntityDescriptor descriptor = EntityDescriptor.create("5a85c4854b900afd5d662be3", ModelTypes.PIPELINE_V1);
    final Graph<EntityDescriptor> graph = facade.resolveNativeEntity(descriptor);
    assertThat(graph.nodes()).containsOnly(descriptor, EntityDescriptor.create("5adf23894b900a0fdb4e517d", ModelTypes.STREAM_V1), EntityDescriptor.create("2342353045938450345", ModelTypes.PIPELINE_RULE_V1));
}
Also used : RuleDao(org.graylog.plugins.pipelineprocessor.db.RuleDao) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) Stage(org.graylog.plugins.pipelineprocessor.ast.Stage) Pipeline(org.graylog.plugins.pipelineprocessor.ast.Pipeline) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

RuleDao (org.graylog.plugins.pipelineprocessor.db.RuleDao)19 Test (org.junit.Test)10 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)4 PipelineRuleEntity (org.graylog2.contentpacks.model.entities.PipelineRuleEntity)4 NotFoundException (org.graylog2.database.NotFoundException)4 ApiOperation (io.swagger.annotations.ApiOperation)3 BadRequestException (javax.ws.rs.BadRequestException)3 Rule (org.graylog.plugins.pipelineprocessor.ast.Rule)3 ParseException (org.graylog.plugins.pipelineprocessor.parser.ParseException)3 AuditEvent (org.graylog2.audit.jersey.AuditEvent)3 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 DateTime (org.joda.time.DateTime)3 POST (javax.ws.rs.POST)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Api (io.swagger.annotations.Api)1