@Override
    public GrokPattern save(GrokPattern pattern) throws ValidationException {
        try {
            if (!validate(pattern)) {
                throw new ValidationException("Invalid pattern " + pattern);
            }
        } catch (GrokException | PatternSyntaxException e) {
            throw new ValidationException("Invalid pattern " + pattern + "\n" + e.getMessage());
        }

        if (loadByName(pattern.name()).isPresent()) {
            throw new ValidationException("Grok pattern " + pattern.name() + " already exists");
        }

        final WriteResult<GrokPattern, ObjectId> result = dbCollection.save(pattern);
        final GrokPattern savedGrokPattern = result.getSavedObject();

        clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(savedGrokPattern.name())));

        return savedGrokPattern;
    }
@Test
    public void saveInvalidGrokPattern() {
        assertThat(collection.countDocuments()).isEqualTo(0);

        assertThatThrownBy(() -> service.save(GrokPattern.create("Test", "%{")))
                .isInstanceOf(ValidationException.class);
        assertThat(collection.countDocuments()).isEqualTo(0);

        assertThatThrownBy(() -> service.save(GrokPattern.create("", "[a-z]+")))
                .isInstanceOf(ValidationException.class);
        assertThat(collection.countDocuments()).isEqualTo(0);

        assertThatThrownBy(() -> service.save(GrokPattern.create("Test", "")))
                .isInstanceOf(IllegalArgumentException.class);
        assertThat(collection.countDocuments()).isEqualTo(0);

        verify(clusterEventBus, never()).post(any(GrokPatternsUpdatedEvent.class));
    }