SubjectIdReplacementDefinition implements Function<DittoHeaders, String> { @Override public String apply(final DittoHeaders dittoHeaders) { requireNonNull(dittoHeaders); return dittoHeaders.getAuthorizationContext().getFirstAuthorizationSubject() .map(AuthorizationSubject::getId) .orElseThrow(() -> new IllegalStateException("AuthorizationContext must be available when this " + "function is applied!")); } private  SubjectIdReplacementDefinition(); static SubjectIdReplacementDefinition getInstance(); @Override String apply(final DittoHeaders dittoHeaders);  }
@Test public void succeedsWhenHeadersContainSubjectId() { final String subjectId = "nginx:first"; final DittoHeaders validHeaders = DittoHeaders.newBuilder() .authorizationContext(AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED, AuthorizationSubject.newInstance(subjectId), AuthorizationSubject.newInstance("nginx:second"))) .build(); final String actualSubjectId = underTest.apply(validHeaders); assertThat(actualSubjectId).isEqualTo(subjectId); }
@Test public void failsWhenHeadersDoNotContainSubjectId() { assertThatExceptionOfType(IllegalStateException.class) .isThrownBy(() -> underTest.apply(DittoHeaders.empty())); }