Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add and perfect java examples #94

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public Object handle(EasemobAPI easemobAPI) {
try {
result = easemobAPI.invokeEasemobAPI();
} catch (ApiException e) {

/**
* 请求状态码非200时,捕获异常,也可以抛出上层处理。
*/
if (e.getCode() == 401) {
logger.info("The current token is invalid, re-generating token for you and calling it again");
TokenUtil.initTokenByProp();
Expand Down
89 changes: 65 additions & 24 deletions emchat-server-java/src/test/java/com/easemob/ChatGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@

/**
* Created by easemob on 2017/3/22.
* API 文档地址:http://docs.easemob.com/im/100serverintegration/60groupmgmt
*/
public class ChatGroupTest {
private EasemobChatGroup easemobChatGroup = new EasemobChatGroup();
private static final Logger logger = LoggerFactory.getLogger(ChatGroupTest.class);

/**
* 分页获取 APP 下的群组
*/
@Test
public void getChatGroups() {
Long limit = 5L;
Expand All @@ -21,88 +25,125 @@ public void getChatGroups() {
logger.info(result.toString());
}

/**
* 获取群组详情
*/
@Test
public void getGroupsInfo() {
String[] grousIds = new String[2];
grousIds[0] = "11189173157890";
grousIds[1] = "259168197054300592";
grousIds[0] = "49985791000577";
grousIds[1] = "49985853915137";
Object result = easemobChatGroup.getChatGroupDetails(grousIds);
logger.info(result.toString());
}

/**
* 创建一个群组
*/
@Test
public void createGroup() {
Group group = new Group();
group.groupname("groupA").desc("a new group")._public(true).maxusers(50).approval(false).owner("stringa");
group.groupname("groupA").desc("a new group")._public(true).maxusers(50).approval(false).owner("easemob_test_a_004");
Object result = easemobChatGroup.createChatGroup(group);
logger.info(result.toString());
}

/**
* 修改群组信息
*/
@Test
public void changeGroupInfo() {
ModifyGroup group = new ModifyGroup();
String groupId = "11361107116036";
String groupId = "49985853915137";
group.description("change group info").groupname("changed group").maxusers(300);
Object result = easemobChatGroup.modifyChatGroup(groupId, group);
logger.info(result.toString());
}

/**
* 分页获取群组成员
*/
@Test
public void getUsersFromGroup() {
String groupId = "49985853915137";
Object result = easemobChatGroup.getChatGroupUsers(groupId);
logger.info(result.toString());
}

/**
* 添加群组成员[批量]
*/
@Test
public void addUsers() {
String groupId = "11361107116036";
String groupId = "49985853915137";
UserNames userNames = new UserNames();
UserName userList = new UserName();
userList.add("qwqwqw");
userList.add("qwqwqww");
userList.add("easemob_test_b_004");
userList.add("easemob_test_b_003");
userNames.usernames(userList);
Object result = easemobChatGroup.addBatchUsersToChatGroup(groupId, userNames);
logger.info(result.toString());
}

/**
* 移除群组成员[批量]
*/
@Test
public void removeUsersFromGroup() {
String groupId = "11361107116036";
String groupId = "49985853915137";
String[] userIds = new String[2];
userIds[0] = "qwqwqw";
userIds[1] = "qwqwqww";
userIds[0] = "easemob_test_b_004";
userIds[1] = "easemob_test_b_003";
Object result = easemobChatGroup.removeBatchUsersFromChatGroup(groupId, userIds);
logger.info(result.toString());
}

/**
* 转让群组
*/
@Test
public void getUsersFromGroup() {
String groupId = "11361107116036";
Object result = easemobChatGroup.getChatGroupUsers(groupId);
public void transferGroupOwner() {
String groupId = "49985853915137";
NewOwner newOwner = new NewOwner();
newOwner.newowner("easemob_test_b_004");
Object result = easemobChatGroup.transferChatGroupOwner(groupId, newOwner);
logger.info(result.toString());
}

/**
* 查询群组黑名单
*/
@Test
public void transferGroupOwner() {
String groupId = "11361107116036";
NewOwner newOwner = new NewOwner();
newOwner.newowner("qwqwqww");
Object result = easemobChatGroup.transferChatGroupOwner(groupId, newOwner);
public void getChatGroupBlockUsers(){
String groupId = "49985853915137";
Object result = easemobChatGroup.getChatGroupBlockUsers(groupId);
logger.info(result.toString());
}

/**
* 添加用户至群组黑名单
*/
@Test
public void addBlockUsers() {
String groupId = "11361107116036";
String groupId = "49985853915137";
UserNames userNames = new UserNames();
UserName userList = new UserName();
userList.add("qwqwqw");
userList.add("qwqwqww");
userList.add("easemob_test_b_004");
userList.add("easemob_test_b_003");
userNames.usernames(userList);
Object result = easemobChatGroup.addBatchBlockUsersToChatGroup(groupId, userNames);
logger.info(result.toString());
}

/**
* 从群组黑名单移除用户
*/
@Test
public void removeBlockUser() {
String groupId = "11361107116036";
String groupId = "49985853915137";
String[] userIds = new String[2];
userIds[0] = "qwqwqw";
userIds[1] = "qwqwqww";
userIds[0] = "easemob_test_b_004";
userIds[1] = "easemob_test_b_003";
Object result = easemobChatGroup.removeBatchBlockUsersFromChatGroup(groupId, userIds);
logger.info(result.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

/**
* Created by easemob on 2017/3/22.
* API 文档地址:http://docs.easemob.com/im/100serverintegration/30chatlog
*/
public class ChatMessagesTest {
private EasemobChatMessage easemobChatMessage = new EasemobChatMessage();
private static final Logger logger = LoggerFactory.getLogger(ChatMessagesTest.class);
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMddHH");

/**
* 获取聊天记录文件url
*/
@Test
public void getChatMessagesDownloadUrl() {
Calendar calendar = Calendar.getInstance();
Expand Down
42 changes: 34 additions & 8 deletions emchat-server-java/src/test/java/com/easemob/ChatRoomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,79 @@

/**
* Created by easemob on 2017/3/23.
* API 地址:http://docs.easemob.com/im/100serverintegration/70chatroommgmt
*/
public class ChatRoomTest {

private EasemobChatRoom easemobChatRoom = new EasemobChatRoom();
private static final Logger logger = LoggerFactory.getLogger(ChatRoomTest.class);

/**
* 创建一个聊天室
*/
@Test
public void createChatRoom() {
Chatroom chatroom = new Chatroom();
chatroom.name("myChatRoom").description("test chatroom").maxusers(200).owner("stringa");
chatroom.name("myChatRoom").description("test chatroom").maxusers(200).owner("easemob_test_a_004");
Object result = easemobChatRoom.createChatRoom(chatroom);
logger.info(result.toString());
}

/**
* 修改聊天室信息
*/
@Test
public void changeChatRoomInfo() {
String roomId = "11368499576837";
String roomId = "49986023784449";
ModifyChatroom chatroom = new ModifyChatroom();
chatroom.name("changeRoom").description("test changeromm info").maxusers(100);
Object result = easemobChatRoom.modifyChatRoom(roomId, chatroom);
logger.info(result.toString());
}

/**
* 获取 APP 中所有的聊天室
*/
@Test
public void getAllRoom() {
Object result = easemobChatRoom.getAllChatRooms();
logger.info(result.toString());
}

/**
* 获取聊天室详情
*/
@Test
public void addUsersToRoom() {
public void getChatRoomDetail(){
String roomId = "11368499576837";
Object result = easemobChatRoom.getChatRoomDetail(roomId);
logger.info(result.toString());
}

/**
* 添加聊天室成员
*/
@Test
public void addUsersToRoom() {
String roomId = "49986023784449";
UserNames userNames = new UserNames();
UserName userList = new UserName();
userList.add("qwqwqw");
userList.add("qwqwqww");
userList.add("easemob_test_b_004");
userList.add("easemob_test_b_003");
userNames.usernames(userList);
Object result = easemobChatRoom.addBatchUsersToChatRoom(roomId, userNames);
logger.info(result.toString());
}

/**
* 删除聊天室成员
*/
@Test
public void deleteUsersFromRoom() {
String roomId = "11368499576837";
String roomId = "49986023784449";
String[] userIds = new String[2];
userIds[0] = "qwqwqw";
userIds[1] = "qwqwqww";
userIds[0] = "easemob_test_b_004";
userIds[1] = "easemob_test_b_003";
Object result = easemobChatRoom.removeBatchUsersFromChatRoom(roomId, userIds);
logger.info(result.toString());
}
Expand Down
8 changes: 8 additions & 0 deletions emchat-server-java/src/test/java/com/easemob/FileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@

/**
* Created by easemob on 2017/3/22.
* API 文档地址:http://docs.easemob.com/im/100serverintegration/40fileoperation
*/
public class FileTest {
private EasemobFile easemobFile = new EasemobFile();

/**
* 上传文件
* 示例:上传图片文件
*/
@Test
public void uploadFile() {
String path = TokenUtil.class.getClassLoader().getResource("pic/chick.jpg").getPath();
Expand All @@ -22,6 +27,9 @@ public void uploadFile() {
Assert.assertNotNull(result);
}

/**
* 下载文件
*/
@Test
public void downloadFile() {
String uuid = "5deca060-0ea9-11e7-959e-0d3820191bac";
Expand Down
Loading