Package org.jrest4guice.sample.contact.test

Source Code of org.jrest4guice.sample.contact.test.JaasTest

package org.jrest4guice.sample.contact.test;

import java.util.List;

import junit.framework.Assert;

import org.apache.commons.httpclient.HttpClient;
import org.jrest4guice.client.JRestClient;
import org.jrest4guice.client.ModelMap;
import org.jrest4guice.client.Page;
import org.jrest4guice.sample.contact.entity.Role;
import org.jrest4guice.security.SecurityHelper;
import org.junit.BeforeClass;
import org.junit.Test;

@SuppressWarnings("unchecked")
public class JaasTest {
  private static JRestClient client;

  @BeforeClass
  public static void setUp() throws Exception {
    client = new JRestClient();
  }

  @Test
  public void testWithoutLogin() {
    try {
      this.doTest(client);
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
    }
  }

  @Test
  public void testWithLogin() {
    try {
      this.login(client.getHttpClient());
      this.doTest(client);
    } catch (Exception ex) {
      Assert.fail(ex.getMessage());
    }
  }

  /**
   * 登录
   * @param httpClient
   * @throws Exception
   */
  private void login(HttpClient httpClient) throws Exception {
    new SecurityHelper().login(httpClient, "http://localhost/full",
        "/login.jsp", "cnoss", "123");
  }

  private void doTest(JRestClient client) throws Exception {
    ModelMap<String, Object> urlParam = new ModelMap<String, Object>();
    urlParam.put("pageIndex", "1");
    urlParam.put("pageSize", "5");
    Object result;
    result = client.callRemote(
        "http://localhost/full/resource/user/cnoss/roles", "get",
        urlParam);

    Page<Role> page = (Page<Role>) result;
    Assert.assertNotNull(page);
    List<Role> roles = page.getResult();

    Assert.assertTrue(roles.size() == 3);
    System.out.println("\n\n所有角色如下:\n=======================");
    for (Role role : roles)
      System.out.println(role.getName());
  }
}
TOP

Related Classes of org.jrest4guice.sample.contact.test.JaasTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.