JsonFileBasedLoader extends AbstractLoader<T> { @Override public List<T> findByObjectId(Path directory, String objectId) throws IOException { List<String> ids = new ArrayList<>(); ids.add(objectId); Map<String, List<T>> map = findByObjectIds(directory, ids); List<T> objects = map.get(objectId); if (objects == null) { return emptyList(); } else { return objects; } } JsonFileBasedLoader(JacksonObjectMapper objectMapper, Class<T> type); @Override List<T> findByObjectId(Path directory, String objectId); @Override Map<String, List<T>> findByObjectIds(Path directory, List<String> objectIds); @Override T getByUUID(Path directory, String uuid); @Override boolean contains(Path directory, String objectId);  }
@Test public void should_find_one_object_included_in_another_and_deserialize_it() throws Exception { SimpleDesignerArtifact object1 = aSimpleObjectBuilder().id("objet1").build(); SimpleDesignerArtifact object2 = aSimpleObjectBuilder().id("objet2").another(object1).build(); addToRepository(object1, object2); List<SimpleDesignerArtifact> objects = loader.findByObjectId(repoDirectory, "objet1"); assertThat(objects).containsOnly(object2); }
@Test public void should_returns_an_empty_list_when_directory_does_not_exist() throws Exception { assertThat(loader.findByObjectId(get("/does/not/exist"), "objectId")).isEmpty(); }
@Test public void should_not_fail_when_searching_object_by_id_and_repo_contains_an_hidden_file() throws Exception { temporaryFolder.newFolderPath("jsonrepository", ".DS_Store"); loader.findByObjectId(repoDirectory, "object"); }
@Test public void should_not_fail_when_searching_object_by_id_and_artifact_folder_has_no_model_file() throws Exception { Files.createDirectory(repoDirectory.resolve("artifactid")); List<SimpleDesignerArtifact> objects = loader.findByObjectId(repoDirectory, "object"); assertThat(objects).isEmpty(); }
@Test public void should_not_find_an_object_even_if_id_start_the_same_as_the_one_looking_for() throws Exception { addToRepository(aSimpleObjectBuilder().id("abcd").build()); List<SimpleDesignerArtifact> objects = loader.findByObjectId(repoDirectory, "abc"); assertThat(objects).isEmpty(); }
@Test public void should_not_find_object_included_in_another() throws Exception { SimpleDesignerArtifact object1 = aSimpleObjectBuilder().id("objet1").build(); SimpleDesignerArtifact object2 = aSimpleObjectBuilder().id("objet2").another(object1).build(); addToRepository(object1, object2); List<SimpleDesignerArtifact> objects = loader.findByObjectId(repoDirectory, "objet2"); assertThat(objects).isEmpty(); }
@Test(expected = IOException.class) public void should_throw_IOException_when_error_occurs_on_finding_object_by_id() throws Exception { SimpleDesignerArtifact object1 = aSimpleObjectBuilder().id("objet1").build(); SimpleDesignerArtifact object2 = aSimpleObjectBuilder().id("objet2").another(object1).build(); addToRepository(object1, object2); doThrow(new IOException()).when(objectMapper).fromJson(any(byte[].class), eq(SimpleDesignerArtifact.class)); loader.findByObjectId(repoDirectory, "objet1"); }