JsonFileBasedPersister { public void save(Path directory, T content) throws IOException { String versionToSet = version; String[] currentVersion; if (versionToSet != null) { currentVersion = version.split("_"); versionToSet = currentVersion[0]; } content.setDesignerVersionIfEmpty(versionToSet); validator.validate(content); try { write(jsonFile(directory, content.getId()), objectMapper.toPrettyJson(content, JsonViewPersistence.class)); Path metadataPath = updateMetadata(directory, content); if (content instanceof HasUUID && !StringUtils.isEmpty(((HasUUID) content).getUUID())) { saveInIndex(metadataPath, content); } } catch (RuntimeException e) { throw new IOException(e); } } JsonFileBasedPersister(JacksonObjectMapper objectMapper, BeanValidator validator); void save(Path directory, T content); Path updateMetadata(Path directory, T content); synchronized void saveInIndex(Path metadataPath, T content); void delete(Path directory, T content); Path jsonFile(Path directory, String id); synchronized void refreshIndexing(Path metadataFolder, List<T> pages); static final String INDEX_METADATA; }
@Test public void should_serialize_an_object_and_save_it_to_a_file() throws Exception { SimpleDesignerArtifact expectedObject = new SimpleDesignerArtifact("foo", "aName", 2); repository.save(repoDirectory, expectedObject); SimpleDesignerArtifact savedObject = getFromRepository("foo"); assertThat(savedObject).isEqualTo(expectedObject); }
@Test public void should_set_designer_version_while_saving_if_not_already_set() throws Exception { SimpleDesignerArtifact expectedObject = new SimpleDesignerArtifact("foo", "aName", 2); repository.save(repoDirectory, expectedObject); SimpleDesignerArtifact savedObject = getFromRepository("foo"); assertThat(savedObject.getDesignerVersion()).isEqualTo(DESIGNER_VERSION); }
@Test public void should_not_set_designer_version_while_saving_if_already_set() throws Exception { SimpleDesignerArtifact expectedObject = new SimpleDesignerArtifact("foo", "aName", 2); expectedObject.setDesignerVersion("alreadySetVersion"); repository.save(repoDirectory, expectedObject); SimpleDesignerArtifact savedObject = getFromRepository("foo"); assertThat(savedObject.getDesignerVersion()).isEqualTo("alreadySetVersion"); }
@Test(expected = IOException.class) public void should_throw_IOException_when_error_occurs_while_saving_a_object() throws Exception { doThrow(new RuntimeException()).when(objectMapper).toJson(anyObject(), any(Class.class)); repository.save(repoDirectory, new SimpleDesignerArtifact()); }
@Test public void should_validate_beans_before_saving_them() throws Exception { doThrow(ConstraintValidationException.class).when(validator).validate(any(Object.class)); try { repository.save(repoDirectory, new SimpleDesignerArtifact("object1", "object1", 1)); failBecauseExceptionWasNotThrown(ConstraintValidationException.class); } catch (ConstraintValidationException e) { assertThat(repoDirectory.resolve("object1.json").toFile()).doesNotExist(); } }
@Test public void should_persist_metadata_in_a_seperate_file() throws Exception { SimpleDesignerArtifact artifact = aSimpleObjectBuilder() .id("baz") .metadata("foobar") .build(); repository.save(repoDirectory, artifact); assertThat(new String(readAllBytes(repoDirectory.getParent().resolve(".metadata/baz.json")))) .isEqualTo("{\"favorite\":false,\"metadata\":\"foobar\"}"); }