JsonFileBasedPersister { public synchronized void refreshIndexing(Path metadataFolder, List<T> pages) throws IOException { Path indexPath = jsonFile(metadataFolder, INDEX_METADATA); Map<String, String> refreshingIndex = new HashMap<>(); pages.forEach(page -> { String uuidIfExist = getUUIDIfExist(page); if (uuidIfExist!= null) { refreshingIndex.put(uuidIfExist, page.getId()); } }); writeIndexFile(indexPath, refreshingIndex); } 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_persist_all_artifact_id_in_index_when_refresh_indexing_is_called() throws Exception { List<Page> pages = new ArrayList<>(); Page page = PageBuilder.aPage().withUUID("baz-uuid").withId("page1").build(); Page page2 = PageBuilder.aPage().withUUID("foo-uuid").withId("page2").withName("page2").build(); pages.add(page); pages.add(page2); JsonFileBasedPersister<Page> pageRepository = new JsonFileBasedPersister<>(objectMapper, validator); Path metadataFolder = repoDirectory.resolve(".metadata"); metadataFolder.toFile().mkdir(); new Thread(() -> { try { pageRepository.refreshIndexing(metadataFolder, pages); } catch (IOException e) { throw new RuntimeException(e); } }).start(); Awaitility.await() .atMost(3, TimeUnit.SECONDS) .untilAsserted(new ThrowingRunnable() { @Override public void run() throws Throwable { String index = new String(readAllBytes(metadataFolder.resolve(".index.json"))); assertThat(index).contains("\"baz-uuid\":\"page1\"").contains("\"foo-uuid\":\"page2\""); } }); }