AbstractAcmeProvider implements AcmeProvider { @Override public Challenge createChallenge(Login login, JSON data) { Objects.requireNonNull(login, "login"); Objects.requireNonNull(data, "data"); String type = data.get("type").asString(); BiFunction<Login, JSON, Challenge> constructor = CHALLENGES.get(type); if (constructor != null) { return constructor.apply(login, data); } if (data.contains("token")) { return new TokenChallenge(login, data); } else { return new Challenge(login, data); } }  @Override Connection connect(URI serverUri); @Override JSON directory(Session session, URI serverUri); @Override Challenge createChallenge(Login login, JSON data);  }
@Test public void testCreateChallenge() { Login login = mock(Login.class); AbstractAcmeProvider provider = new TestAbstractAcmeProvider(); Challenge c1 = provider.createChallenge(login, getJSON("httpChallenge")); assertThat(c1, not(nullValue())); assertThat(c1, instanceOf(Http01Challenge.class)); Challenge c2 = provider.createChallenge(login, getJSON("httpChallenge")); assertThat(c2, not(sameInstance(c1))); Challenge c3 = provider.createChallenge(login, getJSON("dnsChallenge")); assertThat(c3, not(nullValue())); assertThat(c3, instanceOf(Dns01Challenge.class)); Challenge c4 = provider.createChallenge(login, getJSON("tlsAlpnChallenge")); assertThat(c4, not(nullValue())); assertThat(c4, instanceOf(TlsAlpn01Challenge.class)); JSON json6 = new JSONBuilder() .put("type", "foobar-01") .put("url", "https: .toJSON(); Challenge c6 = provider.createChallenge(login, json6); assertThat(c6, not(nullValue())); assertThat(c6, instanceOf(Challenge.class)); JSON json7 = new JSONBuilder() .put("type", "foobar-01") .put("token", "abc123") .put("url", "https: .toJSON(); Challenge c7 = provider.createChallenge(login, json7); assertThat(c7, not(nullValue())); assertThat(c7, instanceOf(TokenChallenge.class)); try { JSON json8 = new JSONBuilder() .put("url", "https: .toJSON(); provider.createChallenge(login, json8); fail("Challenge without type was accepted"); } catch (AcmeProtocolException ex) { } try { provider.createChallenge(login, null); fail("null was accepted"); } catch (NullPointerException ex) { } }