@Override
    public GrokPattern update(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 (pattern.id() == null) {
            throw new ValidationException("Invalid pattern " + pattern);
        }
        WriteResult<GrokPattern, ObjectId> result = dbCollection.update(DBQuery.is("_id", new ObjectId(pattern.id())), pattern);
        if (result.isUpdateOfExisting()) {
            clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(pattern.name())));
            return pattern;
        }
        throw new ValidationException("Invalid pattern " + pattern);
    }
@Test
    @MongoDBFixtures("MongoDbGrokPatternServiceTest.json")
    public void update() throws ValidationException {
        assertThat(collection.countDocuments()).isEqualTo(3);

        GrokPattern toUpdate1 = GrokPattern.builder()
                .id("56250da2d400000000000001")
                .name("Test1")
                .pattern("123")
                .build();
        final GrokPattern updatedPattern1 = service.update(toUpdate1);
        assertThat(updatedPattern1.name()).matches(toUpdate1.name());
        assertThat(updatedPattern1.pattern()).matches(toUpdate1.pattern());
        assertThat(collection.countDocuments()).isEqualTo(3);

        GrokPattern toUpdate2 = GrokPattern.builder()
                .id("56250da2d400000000000001")
                .name("Testxxx")
                .pattern("123")
                .build();
        final GrokPattern updatedPattern2 = service.update(toUpdate2);
        assertThat(updatedPattern2.name()).matches(toUpdate2.name());
        assertThat(updatedPattern2.pattern()).matches(toUpdate2.pattern());
        assertThat(collection.countDocuments()).isEqualTo(3);

        GrokPattern toUpdate3 = GrokPattern.builder()
                .name("Testxxx")
                .pattern("123")
                .build();
        boolean thrown = false;
        try {
            service.update(toUpdate3);
        } catch (ValidationException e) {
            thrown = true;
        }
        assertThat(thrown).isTrue();
        assertThat(collection.countDocuments()).isEqualTo(3);

        GrokPattern toUpdate4 = GrokPattern.builder()
                .id("56250da2d400000000000321")
                .name("Testxxx")
                .pattern("123")
                .build();
        thrown = false;
        try {
            service.update(toUpdate4);
        } catch (ValidationException e) {
            thrown = true;
        }
        assertThat(thrown).isTrue();
        assertThat(collection.countDocuments()).isEqualTo(3);

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