@Test
public void testPostAuthentication() {

    /**
     * 测试传入错误参数的情况。
     */
    Authentication authentication = this.handler.postAuthentication(null, null);
    assertNotNull(authentication);
    Assert.assertNull(authentication.getPrincipal());
    /**
     * 测试参数正确的情况，但是ki4so 服务器的app对象为空的情况。
     */
    String username = "test";
    String password = "pwdsssss";
    UsernamePasswordCredential credential = new UsernamePasswordCredential(username, password);
    DefaultUserPrincipal principal = new DefaultUserPrincipal();
    principal.setId(username);
    //设置模拟服务。
    AppService appService = Mockito.mock(AppService.class);
    Mockito.when(appService.findKi4soServerApp()).thenReturn(null);
    this.handler.setAppService(appService);
    try {
        this.handler.postAuthentication(credential, principal);
        fail("应该抛出异常");
    } catch (NoKi4soKeyException e) {
    }
     /**
     * 测试参数正确的情况，但是ki4so 服务器的app对象不为空的情况。
     * 服务对应的key信息不为的正常情况。
     */
    String appId = "1000";
    String app2Id = "1001";
    String keyId = "1000000";
    String encStringValue = "ssssdafdsafdsafdsafdasfdsafdsa";
    String service = "http://loacahost:8080/ki4so-app/home.do";
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put(WebConstants.SERVICE_PARAM_NAME, service);
    credential.setParameters(parameters);
    App app = new App();
    app.setAppId(appId);
    
    App clientApp = new App();
    clientApp.setAppId(app2Id);
    
    Ki4soKey key = new Ki4soKey();
    key.setKeyId(keyId);
    
    Mockito.when(appService.findKi4soServerApp()).thenReturn(app);
    Mockito.when(appService.findAppByHost(service)).thenReturn(clientApp);
     
    KeyService keyService = Mockito.mock(KeyService.class);
    Mockito.when(keyService.findKeyByAppId(appId)).thenReturn(key);
    Mockito.when(keyService.findKeyByAppId(app2Id)).thenReturn(key);
    
    this.handler.setKeyService(keyService);
    EncryCredentialManager encryCredentialManager = Mockito.mock(EncryCredentialManager.class);
    Mockito.when(encryCredentialManager.encrypt(Mockito.any(EncryCredentialInfo.class))).thenReturn(encStringValue);
    this.handler.setEncryCredentialManager(encryCredentialManager);
    
    UserLoggedStatusStore userLoggedStatusStore = Mockito.mock(UserLoggedStatusStore.class);
    this.handler.setUserLoggedStatusStore(userLoggedStatusStore);
    
    authentication = this.handler.postAuthentication(credential, principal);
    assertNotNull(authentication);
}