Authorization extends AcmeJsonResource { @Nullable public <T extends Challenge> T findChallenge(final String type) { return (T) getChallenges().stream() .filter(ch -> type.equals(ch.getType())) .reduce((a, b) -> {throw new AcmeProtocolException("Found more than one challenge of type " + type);}) .orElse(null); } protected  Authorization(Login login, URL location); Identifier getIdentifier(); Status getStatus(); @Nullable Instant getExpires(); boolean isWildcard(); List<Challenge> getChallenges(); @Nullable T findChallenge(final String type); @Nullable T findChallenge(Class<T> type); void deactivate();  }
@Test public void testFindChallenge() throws IOException { Authorization authorization = createChallengeAuthorization(); Challenge c1 = authorization.findChallenge(SNAILMAIL_TYPE); assertThat(c1, is(nullValue())); Challenge c2 = authorization.findChallenge(Http01Challenge.TYPE); assertThat(c2, is(notNullValue())); assertThat(c2, is(instanceOf(Http01Challenge.class))); Challenge c3 = authorization.findChallenge(Dns01Challenge.TYPE); assertThat(c3, is(notNullValue())); assertThat(c3, is(instanceOf(Dns01Challenge.class))); Challenge c4 = authorization.findChallenge(TlsAlpn01Challenge.TYPE); assertThat(c4, is(notNullValue())); assertThat(c4, is(instanceOf(TlsAlpn01Challenge.class))); }
@Test public void testFindChallengeByType() throws IOException { Authorization authorization = createChallengeAuthorization(); NonExistingChallenge c1 = authorization.findChallenge(NonExistingChallenge.class); assertThat(c1, is(nullValue())); Http01Challenge c2 = authorization.findChallenge(Http01Challenge.class); assertThat(c2, is(notNullValue())); Dns01Challenge c3 = authorization.findChallenge(Dns01Challenge.class); assertThat(c3, is(notNullValue())); TlsAlpn01Challenge c4 = authorization.findChallenge(TlsAlpn01Challenge.class); assertThat(c4, is(notNullValue())); }
@Test(expected = AcmeProtocolException.class) public void testFailDuplicateChallenges() throws IOException { Authorization authorization = createChallengeAuthorization(); authorization.findChallenge(DUPLICATE_TYPE); }