Search in sources :

Example 31 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokResourceTest method bulkUpdatePatternsFromTextFileWithLF.

@Test
public void bulkUpdatePatternsFromTextFileWithLF() throws Exception {
    final String patterns = Arrays.stream(GROK_LINES).collect(Collectors.joining("\n"));
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(patterns.getBytes(StandardCharsets.UTF_8));
    final GrokPattern expectedPattern = GrokPattern.create("TEST_PATTERN_0", "Foo");
    final Response response = grokResource.bulkUpdatePatternsFromTextFile(inputStream, true, null);
    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.ACCEPTED);
    assertThat(response.hasEntity()).isFalse();
    await().atMost(Duration.FIVE_SECONDS).until(() -> !subscriber.events.isEmpty());
    assertThat(subscriber.events).containsOnly(GrokPatternsUpdatedEvent.create(Collections.singleton(expectedPattern.name())));
}
Also used : Response(javax.ws.rs.core.Response) GrokPattern(org.graylog2.grok.GrokPattern) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 32 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class ContentPackServiceTest method uninstallContentPack.

@Test
public void uninstallContentPack() throws NotFoundException {
    /* Test successful uninstall */
    when(patternService.load("dead-beef1")).thenReturn(grokPattern);
    ContentPackUninstallation expectSuccess = ContentPackUninstallation.builder().skippedEntities(ImmutableSet.of()).failedEntities(ImmutableSet.of()).entities(nativeEntityDescriptors).build();
    ContentPackUninstallation resultSuccess = contentPackService.uninstallContentPack(contentPack, contentPackInstallation);
    assertThat(resultSuccess).isEqualTo(expectSuccess);
    /* Test skipped uninstall */
    when(contentPackInstallService.countInstallationOfEntityById(ModelId.of("dead-beef1"))).thenReturn((long) 2);
    ContentPackUninstallation expectSkip = ContentPackUninstallation.builder().skippedEntities(nativeEntityDescriptors).failedEntities(ImmutableSet.of()).entities(ImmutableSet.of()).build();
    ContentPackUninstallation resultSkip = contentPackService.uninstallContentPack(contentPack, contentPackInstallation);
    assertThat(resultSkip).isEqualTo(expectSkip);
    /* Test skipped uninstall */
    when(contentPackInstallService.countInstallationOfEntityById(ModelId.of("dead-beef1"))).thenReturn((long) 1);
    when(contentPackInstallService.countInstallationOfEntityByIdAndFoundOnSystem(ModelId.of("dead-beef1"))).thenReturn((long) 1);
    ContentPackUninstallation expectSkip2 = ContentPackUninstallation.builder().skippedEntities(nativeEntityDescriptors).failedEntities(ImmutableSet.of()).entities(ImmutableSet.of()).build();
    ContentPackUninstallation resultSkip2 = contentPackService.uninstallContentPack(contentPack, contentPackInstallation);
    assertThat(resultSkip2).isEqualTo(expectSkip2);
    /* Test not found while uninstall */
    when(contentPackInstallService.countInstallationOfEntityById(ModelId.of("dead-beef1"))).thenReturn((long) 1);
    when(contentPackInstallService.countInstallationOfEntityByIdAndFoundOnSystem(ModelId.of("dead-beef1"))).thenReturn((long) 0);
    when(patternService.load("dead-beef1")).thenThrow(new NotFoundException("Not found."));
    ContentPackUninstallation expectFailure = ContentPackUninstallation.builder().skippedEntities(ImmutableSet.of()).failedEntities(ImmutableSet.of()).entities(ImmutableSet.of()).build();
    ContentPackUninstallation resultFailure = contentPackService.uninstallContentPack(contentPack, contentPackInstallation);
    assertThat(resultFailure).isEqualTo(expectFailure);
}
Also used : ContentPackUninstallation(org.graylog2.contentpacks.model.ContentPackUninstallation) NotFoundException(org.graylog2.database.NotFoundException) Test(org.junit.Test)

Example 33 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokPatternFacadeTest method resolveMatchingDependecyForCreation.

@Test
public void resolveMatchingDependecyForCreation() throws ValidationException {
    final GrokPattern noDepGrokPattern = grokPatternService.save(GrokPattern.create("HALFLIFE", "\\d\\d"));
    final EntityDescriptor noDepEntityDescriptor = EntityDescriptor.create(ModelId.of(noDepGrokPattern.id()), ModelTypes.GROK_PATTERN_V1);
    final GrokPattern depGrokPattern = grokPatternService.save(GrokPattern.create("PORTAL", "\\d\\d"));
    final EntityDescriptor depEntityDescriptor = EntityDescriptor.create(ModelId.of(depGrokPattern.id()), ModelTypes.GROK_PATTERN_V1);
    final GrokPattern grokPattern = grokPatternService.save(GrokPattern.create("Test", "%{PORTAL}"));
    final EntityDescriptor entityDescriptor = EntityDescriptor.create(ModelId.of(grokPattern.id()), ModelTypes.GROK_PATTERN_V1);
    Graph graph = facade.resolveNativeEntity(entityDescriptor);
    assertThat(graph.nodes().toArray()).contains(depEntityDescriptor);
    assertThat(graph.nodes().toArray()).doesNotContain(noDepEntityDescriptor);
}
Also used : EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) Graph(com.google.common.graph.Graph) GrokPattern(org.graylog2.grok.GrokPattern) Test(org.junit.Test)

Example 34 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokPatternFacadeTest method delete.

@Test
public void delete() throws ValidationException {
    final GrokPattern grokPattern = grokPatternService.save(GrokPattern.create("Test1", "[a-z]+"));
    grokPatternService.save(GrokPattern.create("Test2", "[a-z]+"));
    assertThat(grokPatternService.loadAll()).hasSize(2);
    facade.delete(grokPattern);
    assertThat(grokPatternService.loadAll()).hasSize(1);
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern) Test(org.junit.Test)

Example 35 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokPatternFacadeTest method exportNativeEntity.

@Test
public void exportNativeEntity() {
    final GrokPattern grokPattern = GrokPattern.create("01234567890", "name", "pattern", null);
    final EntityDescriptor descriptor = EntityDescriptor.create(grokPattern.id(), ModelTypes.GROK_PATTERN_V1);
    final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
    final Entity entity = facade.exportNativeEntity(grokPattern, entityDescriptorIds);
    assertThat(entity).isInstanceOf(EntityV1.class);
    assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
    assertThat(entity.type()).isEqualTo(ModelTypes.GROK_PATTERN_V1);
    final EntityV1 entityV1 = (EntityV1) entity;
    final GrokPatternEntity grokPatternEntity = objectMapper.convertValue(entityV1.data(), GrokPatternEntity.class);
    assertThat(grokPatternEntity.name()).isEqualTo("name");
    assertThat(grokPatternEntity.pattern()).isEqualTo("pattern");
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) GrokPattern(org.graylog2.grok.GrokPattern) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) Test(org.junit.Test)

Aggregations

GrokPattern (org.graylog2.grok.GrokPattern)28 Test (org.junit.Test)15 ValidationException (org.graylog2.plugin.database.ValidationException)9 Timed (com.codahale.metrics.annotation.Timed)7 ApiOperation (io.swagger.annotations.ApiOperation)7 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)7 GrokException (io.krakens.grok.api.exception.GrokException)6 AuditEvent (org.graylog2.audit.jersey.AuditEvent)6 GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)6 NotFoundException (org.graylog2.database.NotFoundException)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 Response (javax.ws.rs.core.Response)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Entity (org.graylog2.contentpacks.model.entities.Entity)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 Before (org.junit.Before)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Grok (io.krakens.grok.api.Grok)2