@Test public void of() { // Null checks assertThatThrownBy(() -> Entry.of(null, "/1.txt", EntryType.TEXT, "1")) .isInstanceOf(NullPointerException.class); assertThatThrownBy(() -> Entry.of(new Revision(1), null, EntryType.TEXT, "1")) .isInstanceOf(NullPointerException.class); assertThatThrownBy(() -> Entry.of(new Revision(1), "/1.txt", null, "1")) .isInstanceOf(NullPointerException.class); assertThatThrownBy(() -> Entry.of(new Revision(1), "/1.txt", EntryType.TEXT, null)) .isInstanceOf(NullPointerException.class); // Type check assertThatThrownBy(() -> Entry.of(new Revision(1), "/1.txt", EntryType.TEXT, new Object())) .isInstanceOf(ClassCastException.class); // Directory Entry.of(new Revision(1), "/a", EntryType.DIRECTORY, null); assertThatThrownBy(() -> Entry.of(new Revision(1), "/a", EntryType.DIRECTORY, "foo")) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("expected: null"); }
/** * Performs the specified {@link Query}. * * @return the {@link Entry} on a successful query. * The specified {@code other} on a failure due to missing entry. * * @see #get(Revision, Query) */ default <T> CompletableFuture<Entry<T>> getOrNull(Revision revision, Query<T> query) { requireNonNull(query, "query"); requireNonNull(revision, "revision"); return getOrNull(revision, query.path()).thenApply(entry -> { if (entry == null) { return null; } final EntryType entryType = entry.type(); if (!query.type().supportedEntryTypes().contains(entryType)) { throw new QueryExecutionException("unsupported entry type: " + entryType); } @SuppressWarnings("unchecked") final T entryContent = (T) entry.content(); try { return Entry.of(entry.revision(), query.path(), entryType, query.apply(entryContent)); } catch (CentralDogmaException e) { throw e; } catch (Exception e) { throw new QueryExecutionException(e); } }); }
/** * Applies the specified {@link Query} to the {@link Entry#content()} of the specified {@link Entry} and * returns the query result. * * @throws IllegalStateException if the specified {@link Entry} is a directory * @throws QuerySyntaxException if the syntax of specified {@link Query} is invalid * @throws QueryExecutionException if an {@link Exception} is raised while applying the specified * {@link Query} to the {@link Entry#content()} */ static <T> Entry<T> applyQuery(Entry<T> entry, Query<T> query) { requireNonNull(query, "query"); entry.content(); // Ensure that content is not null. final EntryType entryType = entry.type(); final QueryType queryType = query.type(); if (!queryType.supportedEntryTypes().contains(entryType)) { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); } if (queryType == IDENTITY) { return entry; } else if (queryType == JSON_PATH) { return Entry.of(entry.revision(), query.path(), entryType, query.apply(entry.content())); } else { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); } }
/** * Applies the specified {@link Query} to the {@link Entry#content()} of the specified {@link Entry} and * returns the query result. * * @throws IllegalStateException if the specified {@link Entry} is a directory * @throws QuerySyntaxException if the syntax of specified {@link Query} is invalid * @throws QueryExecutionException if an {@link Exception} is raised while applying the specified * {@link Query} to the {@link Entry#content()} */ static <T> Entry<T> applyQuery(Entry<T> entry, Query<T> query) { requireNonNull(query, "query"); entry.content(); // Ensure that content is not null. final EntryType entryType = entry.type(); final QueryType queryType = query.type(); if (!queryType.supportedEntryTypes().contains(entryType)) { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); } if (queryType == IDENTITY) { return entry; } else if (queryType == JSON_PATH) { return Entry.of(entry.revision(), query.path(), entryType, query.apply(entry.content())); } else { throw new QueryExecutionException("Unsupported entry type: " + entryType + " (query: " + query + ')'); } }
private void testUpsert(Change<?>[] upserts, EntryType entryType) { final Revision oldHeadRev = repo.normalizeNow(HEAD); for (int i = 0; i < upserts.length; i++) { final Change<?> change = upserts[i]; final Revision revision = repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, change).join(); // Ensure the revision is incremented. assertThat(revision.major()).isEqualTo(oldHeadRev.major() + i + 1); assertThat(repo.normalizeNow(HEAD)).isEqualTo(revision); // Ensure that the entries which were created in the previous revisions are retrieved // as well as the entry in the latest revision. final Map<String, Entry<?>> entries = repo.find(revision, allPattern).join(); assertThat(entries).hasSize(i + 1); for (int j = 0; j <= i; j++) { assertThat(entries).containsKey(upserts[j].path()); } } // Check the content of all entries. final Revision headRev = repo.normalizeNow(HEAD); final Map<String, Entry<?>> entries = Util.unsafeCast(repo.find(headRev, allPattern).join()); for (Change<?> c : upserts) { final String path = c.path(); if (entryType == EntryType.TEXT) { // Text must be sanitized so that the last line ends with \n. assertThat(entries).containsEntry(path, Entry.of(headRev, path, EntryType.TEXT, c.content() + "\n")); } else { assertThat(entries).containsEntry(path, Entry.of(headRev, path, entryType, c.content())); } } }