### Question:
HttpRequestHandler extends SimpleChannelUpstreamHandler { protected HttpResponse handleRegister(HttpRequest req) { HttpResponse response = null; try { ClientCredentials creds = auth.issueClientCredentials(req); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(creds); log.debug("credentials:" + jsonString); response = Response.createOkResponse(jsonString); } catch (OAuthException ex) { response = Response.createOAuthExceptionResponse(ex); invokeExceptionHandler(ex, req); } catch (JsonGenerationException e1) { log.error("error handle register", e1); invokeExceptionHandler(e1, req); } catch (JsonMappingException e1) { log.error("error handle register", e1); invokeExceptionHandler(e1, req); } catch (IOException e1) { log.error("error handle register", e1); invokeExceptionHandler(e1, req); } if (response == null) { response = Response.createBadRequestResponse(Response.CANNOT_REGISTER_APP); } return response; }  @Override void messageReceived(ChannelHandlerContext ctx, MessageEvent e);  }### Answer:
@Test public void when_register_invoke_issue_client_credentials() throws Exception { HttpRequest req = mock(HttpRequest.class); given(req.getUri()).willReturn("http: AuthorizationServer auth = mock(AuthorizationServer.class); ClientCredentials creds = new ClientCredentials("TestDemoApp", "basic", "descr", "http: given(auth.issueClientCredentials(req)).willReturn(creds); handler.auth = auth; HttpResponse response = handler.handleRegister(req); verify(handler.auth).issueClientCredentials(req); String res = response.getContent().toString(CharsetUtil.UTF_8); assertTrue(res.contains("client_id")); }
@Test public void when_register_and_OAuth_exception_occurs_return_error() throws Exception { HttpRequest req = mock(HttpRequest.class); given(req.getUri()).willReturn( "http: AuthorizationServer auth = mock(AuthorizationServer.class); willThrow( new OAuthException(Response.NAME_OR_SCOPE_OR_URI_IS_NULL, HttpResponseStatus.BAD_REQUEST)).given(auth).issueClientCredentials(req); handler.auth = auth; HttpResponse response = handler.handleRegister(req); String res = response.getContent().toString(CharsetUtil.UTF_8); assertTrue(res.contains(Response.NAME_OR_SCOPE_OR_URI_IS_NULL)); }
@Test public void when_register_and_JSON_exception_occurs_return_error() throws Exception { HttpRequest req = mock(HttpRequest.class); AuthorizationServer auth = mock(AuthorizationServer.class); ClientCredentials creds = mock(ClientCredentials.class); willReturn(creds).given(auth).issueClientCredentials(req); handler.auth = auth; HttpResponse response = handler.handleRegister(req); assertEquals(response.getContent().toString(CharsetUtil.UTF_8), Response.CANNOT_REGISTER_APP); }
@Test public void when_OAuthException_return_response_with_exception_status() throws Exception { HttpRequest req = mock(HttpRequest.class); AuthorizationServer auth = mock(AuthorizationServer.class); OAuthException ex = new OAuthException(Response.NAME_OR_SCOPE_OR_URI_IS_NULL, HttpResponseStatus.BAD_REQUEST); willThrow(ex).given(auth).issueClientCredentials(req); handler.auth = auth; HttpResponse response = handler.handleRegister(req); assertEquals(response.getStatus(), ex.getHttpStatus()); }