ReferencePlaceholder { public static Optional<ReferencePlaceholder> fromCharSequence(@Nullable final CharSequence input) { if (input == null) { return Optional.empty(); } final Matcher matcher = referencePlaceholderPattern.matcher(input); if (matcher.find()) { final ReferencedEntityType referencedEntityType = ReferencedEntityType.fromString(matcher.group(1)); final EntityId entityId = DefaultEntityId.of(matcher.group(2)); final JsonPointer referencedField = JsonPointer.of(matcher.group(3)); return Optional.of(new ReferencePlaceholder(referencedEntityType, entityId, referencedField)); } return Optional.empty(); } private  ReferencePlaceholder(final ReferencedEntityType referencedEntityType, final EntityId referencedEntityId,
            final JsonPointer referencedField); static Optional<ReferencePlaceholder> fromCharSequence(@Nullable final CharSequence input); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void fromCharSequence() { final Optional<ReferencePlaceholder> referencePlaceholder = ReferencePlaceholder.fromCharSequence("{{ref:things/" + THING_ID + "/policyId}}"); assertThat(referencePlaceholder).isPresent(); assertThat(referencePlaceholder.get().getReferencedEntityId().toString()).isEqualTo(THING_ID.toString()); DittoJsonAssertions.assertThat(referencePlaceholder.get().getReferencedField()) .isEqualTo( JsonPointer.of("policyId")); assertThat(referencePlaceholder.get().getReferencedEntityType()).isEqualTo( ReferencePlaceholder.ReferencedEntityType.THINGS); }
@Test public void fromCharSequenceWithInvalidPlaceholderIsEmpty() { assertThat(ReferencePlaceholder.fromCharSequence("{{things/" + THING_ID + "/policyId}}")).isNotPresent(); assertThat(ReferencePlaceholder.fromCharSequence("{{ref:things" + THING_ID + "/policyId}}")).isNotPresent(); assertThat(ReferencePlaceholder.fromCharSequence("{{ref:things/ " + THING_ID + "/policyId}}")).isNotPresent(); assertThat(ReferencePlaceholder.fromCharSequence("{{ref:things/" + THING_ID + "}}")).isNotPresent(); assertThat(ReferencePlaceholder.fromCharSequence(null)).isNotPresent(); }
@Test public void fromCharSequenceWithUnsupportedEntityTypeThrowsException() { assertThatThrownBy(() -> ReferencePlaceholder.fromCharSequence("{{ref:topologies/namespace:thingid/policyId}}")) .isInstanceOf(GatewayPlaceholderReferenceNotSupportedException.class); }