JerseyTest

package resource.community;

import hibernate.community.AccessControl;
import hibernate.community.LoginRecord;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Before;
import org.junit.Test;
import resource.ApplicationConfig;
import resource.community.message.AccountJson;
import resource.community.message.GroupJson;
import resource.community.message.MessageJson;
import resource.community.message.ShareItemJson;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.sql.Timestamp;
import java.util.List;

import static junit.framework.TestCase.*;

/**
 * Created by liker on 06/08/2015 0006.
 * Group iTailor.hunters.neu.edu.cn
 */
public class CommunityResourceTest extends JerseyTest {

    private static final String BASEURI = "/accountS";
    WebTarget queryTarget = null;

    @Override
    protected Application configure() {
        return new ApplicationConfig();
    }

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
//        final URL resource = getClass().getClassLoader().getResource("gua.txt");
//        System.out.println(resource.toString());
//        final String file = resource.getFile();
//        final File f = new File(file);
//        final Invocation.Builder request = target(BASEURI).path("imagesServer").request();
//        Entity<File> e = Entity.entity(f, MediaType.TEXT_PLAIN_TYPE);
//        final Response response = request.post(e, Response.class);
//
//        String result = response.readEntity(String.class);
//        System.out.println(result);
        //        List<RestPm25Item> rsList = result.readEntity(new GenericType<List<RestPm25Item>>(){});
//         RestPm25Item[] rsList = result.readEntity(RestPm25Item[].class);

    }

    @Test
    public void testPostAccount() throws Exception {
        queryTarget = target(BASEURI)
//                .queryParam("email", "liker" + new Random().nextInt() + "@gmail.com");
                .queryParam("email", "liker.text@gmail.com");
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN)
                .header("password", "pwd4test");
        final Response response = invocationBuilder.post(null);
        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testGetAccount() throws Exception {
        queryTarget = target(BASEURI)
                .queryParam("email", "liker.text@gmail.com");
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", "pwd4test");
        final Response response = invocationBuilder.get();
        assertEquals(200, response.getStatus());
        AccountJson accountJson = response.readEntity(AccountJson.class);
        assertEquals("liker.text@gmail.com", accountJson.getEmail());
    }

    @Test
    public void testPutAccount() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson accountJson = communityResource.getAccount("liker.text@gmail.com", "pwd4test");
        queryTarget = target(BASEURI)
                .queryParam("accountID", accountJson.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", accountJson.getPassword());
        AccountJson accountJson2 = new AccountJson();
        accountJson2.setSync(true);
        accountJson2.setPassword("newPwd4testAgain");
        Entity<AccountJson> e = Entity.entity(accountJson2, MediaType.APPLICATION_JSON);
        final Response response = invocationBuilder.put(e, Response.class);
        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
        //406 not accepted
    }

    @Test
    public void testDeleteAccount() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        queryTarget = target(BASEURI)
                .queryParam("accountID", godJson.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN)
                .header("password", godJson.getPassword());
        final Response response = invocationBuilder.delete();
        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testPostMessage() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        communityResource.postAccount("liker.text@gmail.com", "newPwd4testAgain");
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        queryTarget = target(BASEURI + "/messageS")
                .queryParam("accountID", godJson.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN);

        MessageJson messageJson = new MessageJson();
        messageJson.setContext("2.This is a message 4 test!");

        Entity<MessageJson> e = Entity.entity(messageJson, MediaType.APPLICATION_JSON);
//        Entity<File> e = Entity.entity(messageJson, MediaType.);
        final Response response = invocationBuilder.post(e, Response.class);
//        response.
        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testGetMessage() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        queryTarget = target(BASEURI + "/messageS")
                .queryParam("accountID", godJson.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());
        final Response response = invocationBuilder.get();
        List<MessageJson> rsList = response.readEntity(new GenericType<List<MessageJson>>() {
        });
        assertEquals(1, rsList.size());
        assertEquals(200, response.getStatus());
        //500 内部出错
    }

    @Test
    public void testDeleteMessage() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        MessageJson messageJson = new MessageJson();
        messageJson.setContext("3.This is a message 4 test!");
        communityResource.postMessage(godJson.getAccountID(), messageJson);
        List<MessageJson> messageJsons = communityResource.getMessage(godJson.getAccountID(), "newPwd4testAgain");
        queryTarget = target(BASEURI + "/messageS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("messageID", messageJsons.get(0).getMessageID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN)
                .header("password", godJson.getPassword());
        final Response response = invocationBuilder.delete();
        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testPostGroup() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        queryTarget = target(BASEURI + "/groupS")
                .queryParam("accountID", godJson.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN)
                .header("password", godJson.getPassword());

        GroupJson groupJson = new GroupJson();
        groupJson.setGroupName("new group for test!");
        Entity<GroupJson> e = Entity.entity(groupJson, MediaType.APPLICATION_JSON);
        final Response response = invocationBuilder.post(e, Response.class);

        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testGetGroup() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        GroupJson groupJson = new GroupJson();
        groupJson.setGroupName("3.new group for test!");
        groupJson.setSend(true);
        communityResource.postGroup(godJson.getAccountID(), godJson.getPassword(), groupJson);
        queryTarget = target(BASEURI + "/groupS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("groupName", groupJson.getGroupName());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());
        final Response response = invocationBuilder.get();
        GroupJson res = response.readEntity(GroupJson.class);
        assertEquals(200, response.getStatus());
        assertTrue(res.isSend());
    }

    @Test
    public void testDeleteGroup() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        GroupJson groupJson = new GroupJson();
        groupJson.setGroupName("4.new group for test!");
        groupJson.setSend(true);
        communityResource.postGroup(godJson.getAccountID(), godJson.getPassword(), groupJson);
        GroupJson godGroup = communityResource.getGroup(godJson.getAccountID(), groupJson.getGroupName(), godJson.getPassword());
        queryTarget = target(BASEURI + "/groupS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("groupID", godGroup.getGroupID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN)
                .header("password", godJson.getPassword());
        final Response response = invocationBuilder.delete();
        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testPutGroup() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");

        GroupJson groupJson = new GroupJson();
        groupJson.setGroupName("newGroupForChanged");
        groupJson.setSend(false);

        GroupJson godGroup = communityResource.getGroup(godJson.getAccountID(), "3.new group for test!", godJson.getPassword());

        queryTarget = target(BASEURI + "/groupS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("groupID", godGroup.getGroupID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());

        Entity<GroupJson> e = Entity.entity(groupJson, MediaType.APPLICATION_JSON);
        final Response response = invocationBuilder.put(e, Response.class);

        assertEquals(200, response.getStatus());
        GroupJson res = response.readEntity(GroupJson.class);
        assertTrue(!res.isSend());

    }

    @Test
    public void testPostGroupAccount() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        GroupJson godGroup = communityResource.getGroup(godJson.getAccountID(), "newGroupForChanged", godJson.getPassword());

        queryTarget = target(BASEURI + "/groupS/accountS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("groupID", godGroup.getGroupID())
                .queryParam("email", "liker.xu@foxmail.com");
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN)
                .header("password", godJson.getPassword());
        final Response response = invocationBuilder.post(null);
        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
        //500 内部错误
    }

    @Test
    public void testGetGroupAccount() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        AccountJson godJson2 = communityResource.getAccount("liker.xu@foxmail.com", "hellothea");

        queryTarget = target(BASEURI + "/groupS/accountS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("accountID2", godJson2.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());
        final Response response = invocationBuilder.get();
        AccountJson res = response.readEntity(AccountJson.class);
        assertEquals(200, response.getStatus());
        assertEquals("liker.xu@foxmail.com", res.getEmail());
    }

    @Test
    public void testDeleteGroupAccount() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        AccountJson godJson2 = communityResource.getAccount("liker.xu@foxmail.com", "hellothea");

        queryTarget = target(BASEURI + "/groupS/accountS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("accountID2", godJson2.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN)
                .header("password", godJson.getPassword());

        final Response response = invocationBuilder.delete();
        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));

    }

    @Test
    public void testPutAccessControl() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        GroupJson godGroup = communityResource.getGroup(godJson.getAccountID(), "newGroupForChanged", godJson.getPassword());

        queryTarget = target(BASEURI + "/groupS/accessControl")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("groupID", godGroup.getGroupID());
        AccessControl accessControl = new AccessControl();
        accessControl.setReceive(false);
        accessControl.setSend(false);
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());

        Entity<AccessControl> e = Entity.entity(accessControl, MediaType.APPLICATION_JSON);
        final Response response = invocationBuilder.put(e, Response.class);

        assertEquals(200, response.getStatus());
        AccessControl res = response.readEntity(AccessControl.class);
        assertTrue(!res.isSend());
        assertTrue(!res.isReceive());
        // 405

    }

    @Test
    public void testGetAccessControl() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        GroupJson godGroup = communityResource.getGroup(godJson.getAccountID(), "newGroupForChanged", godJson.getPassword());

        queryTarget = target(BASEURI + "/groupS/accessControl")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("groupID", godGroup.getGroupID());

        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());

        final Response response = invocationBuilder.get();
        assertEquals(200, response.getStatus());
        AccessControl res = response.readEntity(AccessControl.class);
        assertTrue(!res.isSend());
        assertTrue(!res.isReceive());
    }

//    @Test
//    public void testGetTimeLine() throws Exception {
//
//        CommunityResource accountResource = new CommunityResource();
//        AccountJson godJson = accountResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
//
//        queryTarget = target(BASEURI + "/groupS/accessControl")
//                .queryParam("accountID", godJson.getAccountID())
//                .queryParam("limit", 1);
//        final Invocation.Builder invocationBuilder
//                = queryTarget.request(MediaType.APPLICATION_JSON)
//                .header("password", godJson.getPassword());
//
//        final Response response = invocationBuilder.get();
//
//        List<ShareItemJson> rsList = response.readEntity(new GenericType<List<ShareItemJson>>() {});
//        assertEquals(0, rsList.size());
//        assertEquals(200, response.getStatus());
//
//    }

    @Test
    public void testDeleteTimelineShareItem() throws Exception {

    }

    @Test
    public void testGetTimeLineShareItems() throws Exception {

    }

    @Test
    public void testPostTimeLineShareItemsComment() throws Exception {

    }

    @Test
    public void testDeleteTimeLineShareItemsComment() throws Exception {

    }

    @Test
    public void testGetTimeLineShareItemsComment() throws Exception {

    }

    @Test
    public void testPostLoginRecords() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");

        queryTarget = target(BASEURI + "/loginRecordS")
                .queryParam("email", godJson.getEmail());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN)
                .header("password", godJson.getPassword());

        LoginRecord loginRecord = new LoginRecord();
        loginRecord.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        loginRecord.setUDID("456765434567765");
        loginRecord.setIp("127.0.0.1");

        Entity<LoginRecord> e = Entity.entity(loginRecord, MediaType.APPLICATION_JSON);
        final Response response = invocationBuilder.post(e, Response.class);

        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testDeleteLoginRecords() throws Exception {

    }

    @Test
    public void testGetLoginRecords() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");

        queryTarget = target(BASEURI + "/loginRecordS")
                .queryParam("email", godJson.getEmail())
                .queryParam("limit", 1);
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());

        final Response response = invocationBuilder.get();

        List<LoginRecord> rsList = response.readEntity(new GenericType<List<LoginRecord>>() {
        });
        if (rsList == null) {
            assertEquals(204, response.getStatus());
        } else {
            assertEquals(200, response.getStatus());
            assertEquals(1, rsList.size());
        }
    }

    @Test
    public void testPostAccounts() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");

        queryTarget = target(BASEURI + "/accountS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("email", "thea.zhu@foxmail.com");

        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN);

        final Response response = invocationBuilder.post(null);

        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testDeleteAccounts() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");
        AccountJson godJson2 = communityResource.getAccount("thea.zhu@foxmail.com", "hellothea");
        communityResource.postAccounts(godJson.getAccountID(), godJson2.getEmail());
        queryTarget = target(BASEURI + "/accountS")
                .queryParam("accountID", godJson.getAccountID())
                .queryParam("accountID2", godJson2.getAccountID());

        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.TEXT_PLAIN);

        final Response response = invocationBuilder.delete();

        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testGetAccounts() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");

        queryTarget = target(BASEURI + "/accountS")
                .queryParam("accountID", godJson.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON);
        final Response response = invocationBuilder.get();

        List<AccountJson> rsList = response.readEntity(new GenericType<List<AccountJson>>() {
        });
        assertEquals(200, response.getStatus());
        assertNotNull(rsList);
    }

    @Test
    public void testPostShareItems() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");

        queryTarget = target(BASEURI + "/shareItemS")
                .queryParam("accountID", godJson.getAccountID());
        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());

        ShareItemJson shareItemJson = new ShareItemJson();
//        shareItemJson.setCommentIDs(null);
//        shareItemJson.setCreatedTime(new Timestamp(System.currentTimeMillis()));

        Entity<ShareItemJson> e = Entity.entity(shareItemJson, MediaType.APPLICATION_JSON);
        final Response response = invocationBuilder.post(e, Response.class);

        assertEquals(200, response.getStatus());
        assertTrue(response.readEntity(boolean.class));
    }

    @Test
    public void testDeleteShareItems() throws Exception {

    }

    @Test
    public void testGetShareItems() throws Exception {

    }

    @Test
    public void testPutRootGroup() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");

        queryTarget = target(BASEURI + "/rootGroup")
                .queryParam("accountID", godJson.getAccountID());

        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());
        GroupJson groupJson = new GroupJson();
        groupJson.setSend(false);
        groupJson.setReceive(false);
        groupJson.setGroupName("My Group");
        Entity<GroupJson> e = Entity.entity(groupJson, MediaType.APPLICATION_JSON);
        final Response response = invocationBuilder.put(e, Response.class);

        assertEquals(200, response.getStatus());
        GroupJson res = response.readEntity(GroupJson.class);
        assertEquals("My Group", res.getGroupName());
    }

    @Test
    public void testGetRootGroup() throws Exception {
        CommunityResource communityResource = new CommunityResource();
        AccountJson godJson = communityResource.getAccount("liker.text@gmail.com", "newPwd4testAgain");

        queryTarget = target(BASEURI + "/rootGroup")
                .queryParam("accountID", godJson.getAccountID());

        final Invocation.Builder invocationBuilder
                = queryTarget.request(MediaType.APPLICATION_JSON)
                .header("password", godJson.getPassword());

        final Response response = invocationBuilder.get();
        assertEquals(200, response.getStatus());
        GroupJson res = response.readEntity(GroupJson.class);
        assertTrue(res != null);
    }

    @Test
    public void testPutUser() throws Exception {

    }

    @Test
    public void testGetUser() throws Exception {

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值