@Test
public void testGetUgi() throws IOException {

  conf.set(DFSConfigKeys.FS_DEFAULT_NAME_KEY, "hdfs://localhost:4321/");
  HttpServletRequest request = mock(HttpServletRequest.class);
  ServletContext context = mock(ServletContext.class);
  String user = "TheDoctor";
  Text userText = new Text(user);
  DelegationTokenIdentifier dtId = new DelegationTokenIdentifier(userText,
      userText, null);
  Token<DelegationTokenIdentifier> token = new Token<DelegationTokenIdentifier>(
      dtId, new DummySecretManager(0, 0, 0, 0));
  String tokenString = token.encodeToUrlString();
  when(request.getParameter(JspHelper.DELEGATION_PARAMETER_NAME)).thenReturn(
      tokenString);
  when(request.getRemoteUser()).thenReturn(user);
  //Test attribute in the url to be used as service in the token.
  when(request.getParameter(JspHelper.NAMENODE_ADDRESS)).thenReturn(
      "1.1.1.1:1111");
  conf.set(DFSConfigKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
  UserGroupInformation.setConfiguration(conf);
  verifyServiceInToken(context, request, "1.1.1.1:1111");
  
  //Test attribute name.node.address 
  //Set the nnaddr url parameter to null.
  when(request.getParameter(JspHelper.NAMENODE_ADDRESS)).thenReturn(null);
  InetSocketAddress addr = new InetSocketAddress("localhost", 2222);
  when(context.getAttribute(NameNodeHttpServer.NAMENODE_ADDRESS_ATTRIBUTE_KEY))
      .thenReturn(addr);
  verifyServiceInToken(context, request, addr.getAddress().getHostAddress()
      + ":2222");
  
  //Test service already set in the token
  token.setService(new Text("3.3.3.3:3333"));
  tokenString = token.encodeToUrlString();
  //Set the name.node.address attribute in Servlet context to null
  when(context.getAttribute(NameNodeHttpServer.NAMENODE_ADDRESS_ATTRIBUTE_KEY))
      .thenReturn(null);
  when(request.getParameter(JspHelper.DELEGATION_PARAMETER_NAME)).thenReturn(
      tokenString);
  verifyServiceInToken(context, request, "3.3.3.3:3333");
}