Skip to content

Commit

Permalink
add POST metadata
Browse files Browse the repository at this point in the history
TODO: add the corresponding cli command
  • Loading branch information
kenuiuc committed Jul 31, 2021
1 parent 6a0a604 commit 1223090
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.easemob.im.server.api.metadata;

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

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class MetadataIT extends AbstractIT {
public MetadataIT() {
Expand Down Expand Up @@ -54,6 +58,65 @@ public void testMetadataGet() {
() -> this.service.user().delete(randomUsername).block(Duration.ofSeconds(3)));
}

@Test
public void testMetadataBatchGet() {
Map<String, String> aliceData = new HashMap<>();
aliceData.put("name", "alice");
aliceData.put("title", "java");
aliceData.put("employer", "easemob");

Map<String, String> bobData = new HashMap<>();
bobData.put("name", "bob");
bobData.put("gender", "male");
bobData.put("phone", "000-000-0000");
bobData.put("zip", "61801");

String password = "password";

String aliceName = String.format("it-%08d-%08d",
ThreadLocalRandom.current().nextInt(100000000),
Instant.now().toEpochMilli());

String bobName = String.format("it-%08d-%08d",
ThreadLocalRandom.current().nextInt(100000000),
Instant.now().toEpochMilli());

// post users and metadata
assertDoesNotThrow(() -> this.service.user().create(aliceName, password)
.block(Duration.ofSeconds(30)));
assertDoesNotThrow(() -> this.service.metadata().setMetadataToUser(aliceName, aliceData)
.block(Duration.ofSeconds(30)));
assertDoesNotThrow(() -> this.service.user().create(bobName, password)
.block(Duration.ofSeconds(30)));
assertDoesNotThrow(() -> this.service.metadata().setMetadataToUser(bobName, bobData)
.block(Duration.ofSeconds(30)));

// batch get metadata from users
EMBatchMetadata batchMetadata =
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());

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

Map<String, String> bobMetadata = data.get(bobName);
assertEquals(2, bobMetadata.size());
assertEquals("bob", bobMetadata.get("name"));
assertEquals("61801", bobMetadata.get("zip"));

// delete users
assertDoesNotThrow(
() -> this.service.user().delete(aliceName).block(Duration.ofSeconds(30)));
assertDoesNotThrow(
() -> this.service.user().delete(bobName).block(Duration.ofSeconds(30)));
}

@Test
public void testMetadataGetUsage() {
assertDoesNotThrow(() -> this.service.metadata().getUsage().block(Duration.ofSeconds(3)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
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;

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

/**
* 用户属性API。
Expand Down Expand Up @@ -54,6 +56,10 @@ public Mono<EMMetadata> getMetadataFromUser(String username) {
return this.metadataGet.fromUser(username);
}

public Mono<EMBatchMetadata> getMetadataFromUsers(List<String> userNames, List<String> propertyNames) {
return this.metadataGet.fromUsers(userNames, propertyNames);
}

/**
* 获取app用户属性当前所占空间
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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;

public class MetadataGet {
private Context context;

Expand All @@ -20,4 +23,19 @@ public Mono<EMMetadata> fromUser(String username) {
.map(buf -> this.context.getCodec().decode(buf, MetadataGetUserResponse.class))
.map(MetadataGetUserResponse::toMetadata);
}

public Mono<EMBatchMetadata> fromUsers(List<String> userNames, List<String> propertyNames) {
return this.context.getHttpClient()
.flatMap(httpClient -> httpClient
.headers(headers -> headers.set("Content-Type", "application/json"))
.post()
.uri("/metadata/user/get")
.send(Mono.create(sink -> sink.success(this.context.getCodec()
.encode(new MetadataGetUsersRequest(userNames, propertyNames)))))
.responseSingle(
(rsp, buf) -> this.context.getErrorMapper().apply(rsp).then(buf))
)
.map(buf -> this.context.getCodec().decode(buf, MetadataGetUsersResponse.class))
.map(MetadataGetUsersResponse::toBatchMetadata);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.easemob.im.server.api.metadata.user.get;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class MetadataGetUsersRequest {
@JsonProperty("targets")
private List<String> targets;

@JsonProperty("properties")
private List<String> properties;

@JsonCreator
public MetadataGetUsersRequest(@JsonProperty("targets") List<String> targets,
@JsonProperty("properties") List<String> properties) {
this.targets = targets;
this.properties = properties;
}

@Override public String toString() {
return "MetadataGetUsersRequest{" +
"targets=" + targets +
", properties=" + properties +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.easemob.im.server.api.metadata.user.get;

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

import java.util.Map;

public class MetadataGetUsersResponse {
@JsonProperty("data")
private Map<String, Map<String, String>> data;

@JsonCreator
public MetadataGetUsersResponse(@JsonProperty("data") Map<String, Map<String, String>> data) {
this.data = data;
}

public Map<String, Map<String, String>> getData() {
return data;
}

public EMBatchMetadata toBatchMetadata() {
return new EMBatchMetadata(this.data);
}

@Override
public String toString() {
return "MetadataGetUserResponse{" +
"data=" + data +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.easemob.im.server.model;

import java.util.Map;

public class EMBatchMetadata {
private Map<String, Map<String, String>> data;

public EMBatchMetadata(Map<String, Map<String, String>> data) {
this.data = data;
}

public Map<String, Map<String, String>> getData() {
return data;
}

@Override
public String toString() {
return "EMMetadata{" +
"data=" + data +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public EMMetadata(Map<String, String> data) {
this.data = data;
}

public Map<String, String> getData() {
return data;
}

@Override
public String toString() {
return "EMMetadata{" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
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.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.Arrays;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

class MetadataGetTest extends AbstractApiTest {
public class MetadataGetTest extends AbstractApiTest {
MetadataGet metadataGet = new MetadataGet(this.context);

public MetadataGetTest() {
this.server.addHandler("GET /easemob/demo/metadata/user/bob", this::handleMetadataGet);
this.server.addHandler("POST /easemob/demo/metadata/user/get", this::handleMetadataBatchGet);
}

@Test
public void testMetadataGet() {
assertDoesNotThrow(() -> this.metadataGet.fromUser("bob").block(Duration.ofSeconds(3)));
}

public JsonNode handleMetadataGet(JsonNode req) {
@Test
public void testMetadataBatchGet() {
EMBatchMetadata batchMetadata =
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");
assertEquals("java_developer", aliceMetadata.get("title"));
assertEquals("alice wang", aliceMetadata.get("name"));
assertEquals("easemob", bobMetadata.get("employer"));
assertEquals("male", bobMetadata.get("gender"));
}

private JsonNode handleMetadataGet(JsonNode req) {
ObjectNode data = this.objectMapper.createObjectNode();
data.put("nickname", "昵称");
data.put("avatar", "http://www.easemob.com/avatar.png");
Expand All @@ -32,4 +53,19 @@ public JsonNode handleMetadataGet(JsonNode req) {

return jsonRsp;
}

private JsonNode handleMetadataBatchGet(JsonNode req) {
ObjectNode aliceMetadata = this.objectMapper.createObjectNode();
aliceMetadata.put("title", "java_developer");
aliceMetadata.put("name", "alice wang");
ObjectNode bobMetadata = this.objectMapper.createObjectNode();
bobMetadata.put("employer", "easemob");
bobMetadata.put("gender", "male");
ObjectNode data = this.objectMapper.createObjectNode();
data.set("alice", aliceMetadata);
data.set("bob", bobMetadata);
ObjectNode jsonRsp = this.objectMapper.createObjectNode();
jsonRsp.set("data", data);
return jsonRsp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import static org.junit.jupiter.api.Assertions.*;

class MetadataSetUserTest extends AbstractApiTest {
public class MetadataSetUserTest extends AbstractApiTest {
MetadataSet metadataSetUser = new MetadataSet(this.context);

public MetadataSetUserTest() {
Expand All @@ -25,7 +25,7 @@ public void testMetadataSet() {
() -> this.metadataSetUser.toUser("bob", map).block(Duration.ofSeconds(3)));
}

public JsonNode handleMetadataSet(JsonNode req) {
private JsonNode handleMetadataSet(JsonNode req) {
ObjectNode data = this.objectMapper.createObjectNode();
data.put("nickname", "昵称");
data.put("avatar", "http://www.easemob.com/avatar.png");
Expand Down

0 comments on commit 1223090

Please sign in to comment.