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

        GrokPattern toSave;
        if (pattern.id() == null) {
            toSave = pattern.toBuilder().id(createId()).build();
        } else {
            toSave = pattern;
        }
        store.put(toSave.id(), toSave);
        clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(toSave.name())));

        return toSave;
    }
@Test
    public void save() throws Exception {
        // new pattern
        final GrokPattern pattern = service.save(GrokPattern.create("NEW", ".*"));

        assertThat(pattern).isNotNull();
        assertThat(pattern.id()).isNotEmpty();

        // check that updating works
        final GrokPattern updated = service.save(pattern.toBuilder().name("OTHERNAME").build());

        final GrokPattern loaded = service.load(pattern.id());

        assertThat(loaded).isEqualTo(updated);

        //check that using stored patterns works
        final GrokPattern newPattern = service.save(GrokPattern.create("NEWONE", "%{OTHERNAME}"));

        final GrokPattern newLoaded = service.load(newPattern.id());

        assertThat(newLoaded).isEqualTo(newPattern);


        // save should validate
        try {
            service.save(GrokPattern.create("INVALID", "*"));
            fail("Should throw ValidationException");
        } catch (ValidationException ignored) {
        }
    }