### Question:
ClientRegistrationFilter implements Filter { JsonValue performDynamicClientRegistration(final Context context, final JsonValue clientRegistrationConfiguration, final URI registrationEndpoint) throws RegistrationException { final Request request = new Request(); request.setMethod("POST"); request.setUri(registrationEndpoint); request.setEntity(clientRegistrationConfiguration.asMap()); final Response response; try { response = blockingCall(registrationHandler, context, request); } catch (InterruptedException e) { throw new RegistrationException(format("Interrupted while waiting for '%s' response", request.getUri()), e); } if (!CREATED.equals(response.getStatus())) { throw new RegistrationException("Cannot perform dynamic registration: this can be caused " + "by the distant server(busy, offline...) " + "or a malformed registration response."); } try { return getJsonContent(response); } catch (OAuth2ErrorException e) { throw new RegistrationException("Cannot perform dynamic registration: invalid response JSON content."); } } ClientRegistrationFilter(final Handler registrationHandler,
                                    final JsonValue config,
                                    final Heap heap,
                                    final String suffix); @Override Promise<Response, NeverThrowsException> filter(Context context,
                                                          Request request,
                                                          Handler next);  }### Answer:
@Test public void shouldPerformDynamicRegistration() throws Exception { final ClientRegistrationFilter crf = buildClientRegistrationFilter(); final Response response = new Response(); response.setStatus(CREATED); response.setEntity(json(object())); when(handler.handle(eq(context), any(Request.class))).thenReturn(newResponsePromise(response)); crf.performDynamicClientRegistration(context, getMetadata(), new URI(SAMPLE_URI + REGISTRATION_ENDPOINT)); verify(handler).handle(eq(context), captor.capture()); Request request = captor.getValue(); assertThat(request.getMethod()).isEqualTo("POST"); assertThat(request.getEntity().toString()).containsSequence("redirect_uris", "contact", "scopes"); }
@Test(expectedExceptions = RegistrationException.class) public void shouldFailWhenStatusCodeResponseIsDifferentFromCreated() throws Exception { final ClientRegistrationFilter crf = buildClientRegistrationFilter(); final Response response = new Response(); response.setStatus(Status.BAD_REQUEST); when(handler.handle(eq(context), any(Request.class))).thenReturn(newResponsePromise(response)); crf.performDynamicClientRegistration(context, getMetadata(), new URI(SAMPLE_URI + REGISTRATION_ENDPOINT)); }
@Test(expectedExceptions = RegistrationException.class) public void shouldFailWhenResponseHasInvalidResponseContent() throws Exception { final ClientRegistrationFilter crf = buildClientRegistrationFilter(); final Response response = new Response(); response.setStatus(CREATED); response.setEntity(array("invalid", "content")); when(handler.handle(eq(context), any(Request.class))).thenReturn(newResponsePromise(response)); crf.performDynamicClientRegistration(context, getMetadata(), new URI(SAMPLE_URI + REGISTRATION_ENDPOINT)); }