### Question:
TokenRequest { protected void checkMandatoryParams() throws OAuthException { if (clientId == null || clientId.isEmpty()) { throw new OAuthException(String.format(Response.MANDATORY_PARAM_MISSING, CLIENT_ID), HttpResponseStatus.BAD_REQUEST); } if (clientSecret == null || clientSecret.isEmpty()) { throw new OAuthException(String.format(Response.MANDATORY_PARAM_MISSING, CLIENT_SECRET), HttpResponseStatus.BAD_REQUEST); } if (grantType == null || grantType.isEmpty()) { throw new OAuthException(String.format(Response.MANDATORY_PARAM_MISSING, GRANT_TYPE), HttpResponseStatus.BAD_REQUEST); } } TokenRequest(HttpRequest request); void validate(); String getUserId(); void setUserId(String userId); String getGrantType(); String getCode(); String getRedirectUri(); String getClientId(); String getRefreshToken(); void setClientId(String clientId); String getScope(); String getUsername(); String getPassword(); String getClientSecret(); static final String AUTHORIZATION_CODE; static final String REFRESH_TOKEN; static final String CLIENT_CREDENTIALS; static final String PASSWORD; }### Answer:
@Test public void when_clientId_empty_check_mandatory_params_throws_exception() throws Exception { String clientId = ""; String content = "grant_type=" + TokenRequest.PASSWORD + "&username=rossi&client_id=" + clientId + "&client_secret=" + clientSecret; ChannelBuffer buf = ChannelBuffers.copiedBuffer(content.getBytes(CharsetUtil.UTF_8)); given(req.getContent()).willReturn(buf); TokenRequest tokenReq = new TokenRequest(req); String errorMsg = null; try { tokenReq.checkMandatoryParams(); } catch (OAuthException e) { errorMsg = e.getMessage(); } assertEquals(errorMsg, String.format(Response.MANDATORY_PARAM_MISSING, TokenRequest.CLIENT_ID)); }
@Test public void when_grantType_empty_check_mandatory_params_throws_exception() throws Exception { String content = "grant_type=" + "" +"&username=rossi&client_id=" + clientId + "&client_secret=" + clientSecret; ChannelBuffer buf = ChannelBuffers.copiedBuffer(content.getBytes(CharsetUtil.UTF_8)); given(req.getContent()).willReturn(buf); TokenRequest tokenReq = new TokenRequest(req); String errorMsg = null; try { tokenReq.checkMandatoryParams(); } catch (OAuthException e) { errorMsg = e.getMessage(); } assertEquals(errorMsg, String.format(Response.MANDATORY_PARAM_MISSING, TokenRequest.GRANT_TYPE)); }