Skip to content

Commit

Permalink
rm EMBatchMetadata and add javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
kenuiuc committed Aug 5, 2021
1 parent 1223090 commit e920fa2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.easemob.im.server.api.metadata;

import com.easemob.im.server.api.AbstractIT;
import com.easemob.im.server.model.EMBatchMetadata;
import com.easemob.im.server.model.EMMetadata;
import org.junit.jupiter.api.Test;

import java.time.Duration;
Expand Down Expand Up @@ -31,11 +31,11 @@ public void testMetadataSet() {
ThreadLocalRandom.current().nextInt(100000000));
String randomPassword = randomUsername;
assertDoesNotThrow(() -> this.service.user().create(randomUsername, randomPassword)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(() -> this.service.metadata().setMetadataToUser(randomUsername, map)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(
() -> this.service.user().delete(randomUsername).block(Duration.ofSeconds(3)));
() -> this.service.user().delete(randomUsername).block(Duration.ofSeconds(10)));
}

@Test
Expand All @@ -49,13 +49,13 @@ public void testMetadataGet() {
ThreadLocalRandom.current().nextInt(100000000));
String randomPassword = randomUsername;
assertDoesNotThrow(() -> this.service.user().create(randomUsername, randomPassword)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(() -> this.service.metadata().setMetadataToUser(randomUsername, map)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(() -> this.service.metadata().getMetadataFromUser(randomUsername)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(
() -> this.service.user().delete(randomUsername).block(Duration.ofSeconds(3)));
() -> this.service.user().delete(randomUsername).block(Duration.ofSeconds(10)));
}

@Test
Expand Down Expand Up @@ -92,20 +92,19 @@ public void testMetadataBatchGet() {
.block(Duration.ofSeconds(30)));

// batch get metadata from users
EMBatchMetadata batchMetadata =
Map<String, EMMetadata> metadataMap =
assertDoesNotThrow(() -> this.service.metadata().getMetadataFromUsers(
Arrays.asList(aliceName, bobName),
Arrays.asList("name", "title", "zip")).block(Duration.ofSeconds(30)));
Map<String, Map<String, String>> data = batchMetadata.getData();
assertEquals(2, data.size());
assertEquals(2, metadataMap.size());

// check results
Map<String, String> aliceMetadata = data.get(aliceName);
Map<String, String> aliceMetadata = metadataMap.get(aliceName).getData();
assertEquals(2, aliceMetadata.size());
assertEquals("alice", aliceMetadata.get("name"));
assertEquals("java", aliceMetadata.get("title"));

Map<String, String> bobMetadata = data.get(bobName);
Map<String, String> bobMetadata = metadataMap.get(bobName).getData();
assertEquals(2, bobMetadata.size());
assertEquals("bob", bobMetadata.get("name"));
assertEquals("61801", bobMetadata.get("zip"));
Expand All @@ -119,7 +118,7 @@ public void testMetadataBatchGet() {

@Test
public void testMetadataGetUsage() {
assertDoesNotThrow(() -> this.service.metadata().getUsage().block(Duration.ofSeconds(3)));
assertDoesNotThrow(() -> this.service.metadata().getUsage().block(Duration.ofSeconds(10)));
}

@Test
Expand All @@ -133,15 +132,15 @@ public void testMetadataDelete() {
ThreadLocalRandom.current().nextInt(100000000));
String randomPassword = randomUsername;
assertDoesNotThrow(() -> this.service.user().create(randomUsername, randomPassword)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(() -> this.service.metadata().setMetadataToUser(randomUsername, map)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(() -> this.service.metadata().getMetadataFromUser(randomUsername)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(() -> this.service.metadata().deleteMetadataFromUser(randomUsername)
.block(Duration.ofSeconds(3)));
.block(Duration.ofSeconds(10)));
assertDoesNotThrow(
() -> this.service.user().delete(randomUsername).block(Duration.ofSeconds(3)));
() -> this.service.user().delete(randomUsername).block(Duration.ofSeconds(10)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.easemob.im.server.api.metadata.user.delete.MetadataDelete;
import com.easemob.im.server.api.metadata.user.get.MetadataGet;
import com.easemob.im.server.api.metadata.user.set.MetadataSet;
import com.easemob.im.server.model.EMBatchMetadata;
import com.easemob.im.server.model.EMMetadata;
import com.easemob.im.server.model.EMMetadataUsage;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -56,7 +55,15 @@ public Mono<EMMetadata> getMetadataFromUser(String username) {
return this.metadataGet.fromUser(username);
}

public Mono<EMBatchMetadata> getMetadataFromUsers(List<String> userNames, List<String> propertyNames) {
/**
* 批量获取用户属性
*
* @param userNames 用户名列表
* @param propertyNames 属性名列表
* @return (用户名 -> 属性) Map
* @see <a href="https://docs-im.easemob.com/im/server/ready/usermetadata#%E6%89%B9%E9%87%8F%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E5%B1%9E%E6%80%A7">批量获取用户属性</a>
*/
public Mono<Map<String, EMMetadata>> getMetadataFromUsers(List<String> userNames, List<String> propertyNames) {
return this.metadataGet.fromUsers(userNames, propertyNames);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.easemob.im.server.api.metadata.user.get;

import com.easemob.im.server.api.Context;
import com.easemob.im.server.model.EMBatchMetadata;
import com.easemob.im.server.model.EMMetadata;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

public class MetadataGet {
private Context context;
Expand All @@ -24,7 +24,7 @@ public Mono<EMMetadata> fromUser(String username) {
.map(MetadataGetUserResponse::toMetadata);
}

public Mono<EMBatchMetadata> fromUsers(List<String> userNames, List<String> propertyNames) {
public Mono<Map<String, EMMetadata>> fromUsers(List<String> userNames, List<String> propertyNames) {
return this.context.getHttpClient()
.flatMap(httpClient -> httpClient
.headers(headers -> headers.set("Content-Type", "application/json"))
Expand All @@ -36,6 +36,6 @@ public Mono<EMBatchMetadata> fromUsers(List<String> userNames, List<String> prop
(rsp, buf) -> this.context.getErrorMapper().apply(rsp).then(buf))
)
.map(buf -> this.context.getCodec().decode(buf, MetadataGetUsersResponse.class))
.map(MetadataGetUsersResponse::toBatchMetadata);
.map(MetadataGetUsersResponse::toMetadataMap);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.easemob.im.server.api.metadata.user.get;

import com.easemob.im.server.model.EMBatchMetadata;
import com.easemob.im.server.model.EMMetadata;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.HashMap;
import java.util.Map;

public class MetadataGetUsersResponse {
Expand All @@ -19,8 +20,12 @@ public Map<String, Map<String, String>> getData() {
return data;
}

public EMBatchMetadata toBatchMetadata() {
return new EMBatchMetadata(this.data);
public Map<String, EMMetadata> toMetadataMap() {
Map<String, EMMetadata> map = new HashMap<>();
data.forEach((userName, metadata) -> {
map.put(userName, new EMMetadata(metadata));
});
return map;
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.easemob.im.server.api.metadata.user.get;

import com.easemob.im.server.api.AbstractApiTest;
import com.easemob.im.server.model.EMBatchMetadata;
import com.easemob.im.server.model.EMMetadata;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
Expand All @@ -27,15 +27,14 @@ public void testMetadataGet() {

@Test
public void testMetadataBatchGet() {
EMBatchMetadata batchMetadata =
Map<String, EMMetadata> metadataMap =
assertDoesNotThrow(() -> this.metadataGet.fromUsers(
Arrays.asList("alice", "bob"),
Arrays.asList("title", "employer", "gender", "name"))
.block(Duration.ofSeconds(3)));

Map<String, Map<String, String>> data = batchMetadata.getData();
Map<String, String> aliceMetadata = data.get("alice");
Map<String, String> bobMetadata = data.get("bob");
Map<String, String> aliceMetadata = metadataMap.get("alice").getData();
Map<String, String> bobMetadata = metadataMap.get("bob").getData();
assertEquals("java_developer", aliceMetadata.get("title"));
assertEquals("alice wang", aliceMetadata.get("name"));
assertEquals("easemob", bobMetadata.get("employer"));
Expand Down

0 comments on commit e920fa2

Please sign in to comment.