diff --git a/README.md b/README.md index b94f068..032b232 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ - [TestNG](#TestNG) - [Junit4](#Junit4) - [取消激活](#取消激活) + - [读取Mock数据](#读取Mock数据) - [获取网络数据信息](#获取网络数据信息) - [获取 flow List](#获取-flow-List) - [获取 flow ID](#获取-flow-ID) @@ -217,6 +218,19 @@ Lyrebird lyrebird = new Lyrebird(); lyrebird.deactivate(); ``` +#### 读取Mock数据 + +调用 getMockData(String dataId) 方法可以获取该数据组的 Mock 数据详情 +```java +Lyrebird lyrebird = new Lyrebird(); + +LBMockData lbMockData = lyrebird.getMockData("cfa0c589-8ef0-4885-b4f4-b9688c5af0d5"); +// 读取Mock数据名 +String mockDataName = lbMockData.getName(); +// 读取Mock Response (注意:Lyrebird服务端返回的是一个 Json 字符串,并不是一个 Json 对象) +Object mockDataResponse = lbMockData.getResponseData(); +``` + ### 获取网络数据信息 flow 示例 diff --git a/pom.xml b/pom.xml index c376de6..6df377e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.meituan-dianping.lyrebird.sdk lyrebird-java-client - 1.1.0 + 1.1.1 lyrebird-java-client https://github.com/Meituan-Dianping/lyrebird-java-client @@ -162,6 +162,12 @@ junit 4.12 + + org.hamcrest + hamcrest-all + 1.3 + test + com.squareup.okhttp3 mockwebserver diff --git a/src/main/java/com/meituan/lyrebird/Lyrebird.java b/src/main/java/com/meituan/lyrebird/Lyrebird.java index 274e9fe..58e9b75 100644 --- a/src/main/java/com/meituan/lyrebird/Lyrebird.java +++ b/src/main/java/com/meituan/lyrebird/Lyrebird.java @@ -142,6 +142,17 @@ public EventDetail[] getEventList(String channel) throws LyrebirdClientException return client.getEventList(channel).getEvents(); } + /** + * Get mock data by data id + * + * @param dataId mock data id + * @return + * @throws LyrebirdClientException + */ + public LBMockData getMockData(String dataId) throws LyrebirdClientException { + return client.getMockData(dataId); + } + /** * Get an object of socket io * diff --git a/src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java b/src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java index bc2579c..430c575 100644 --- a/src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java +++ b/src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java @@ -182,6 +182,21 @@ public Events getEventList(String channel) throws LyrebirdClientException { } } + /** + * get mock data by data id + * + * @param dataId mock data id + * @return + * @throws LyrebirdClientException + */ + public LBMockData getMockData(String dataId) throws LyrebirdClientException { + try{ + return lyrebirdService.getMockData(dataId).execute().body(); + } catch (IOException e) { + throw new LyrebirdClientException("Catch exception while get mock response data by data id ", e); + } + } + /** * Get an object of socket io * diff --git a/src/main/java/com/meituan/lyrebird/client/LyrebirdService.java b/src/main/java/com/meituan/lyrebird/client/LyrebirdService.java index 4e86746..a6b989a 100644 --- a/src/main/java/com/meituan/lyrebird/client/LyrebirdService.java +++ b/src/main/java/com/meituan/lyrebird/client/LyrebirdService.java @@ -27,4 +27,7 @@ public interface LyrebirdService { @GET("api/event/{channel}") Call getEventList(@Path("channel") String channel); + + @GET("api/data/{dataId}") + Call getMockData(@Path("dataId") String dataId); } diff --git a/src/main/java/com/meituan/lyrebird/client/api/LBMockData.java b/src/main/java/com/meituan/lyrebird/client/api/LBMockData.java new file mode 100644 index 0000000..53fb744 --- /dev/null +++ b/src/main/java/com/meituan/lyrebird/client/api/LBMockData.java @@ -0,0 +1,32 @@ +package com.meituan.lyrebird.client.api; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.jayway.jsonpath.JsonPath; +import java.util.Map; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class LBMockData extends BaseResponse { + @JsonProperty("data") + private Map data; + + public String getId() { + return data.get("id").toString(); + } + + public String getName() { + return data.get("name").toString(); + } + + public T getResponseData() { + return JsonPath.parse(data.get("response")).read("$.data"); + } + + public Map getData() { + return data; + } + + public void setData(Map data) { + this.data = data; + } +} diff --git a/src/test/java/com/meituan/lyrebird/test/TestFunctional.java b/src/test/java/com/meituan/lyrebird/test/TestFunctional.java index 5d78092..c1d67ed 100644 --- a/src/test/java/com/meituan/lyrebird/test/TestFunctional.java +++ b/src/test/java/com/meituan/lyrebird/test/TestFunctional.java @@ -5,6 +5,7 @@ import com.meituan.lyrebird.Lyrebird; import com.meituan.lyrebird.client.api.*; import com.meituan.lyrebird.client.exceptions.LyrebirdClientException; +import java.util.List; import okhttp3.mockwebserver.*; import org.junit.*; @@ -167,4 +168,20 @@ public void testEventList() throws LyrebirdClientException, InterruptedException Assert.assertEquals("com.lyrebird.java.client", JsonPath.parse(eventList[0].getContent()).read("$.page")); Assert.assertEquals("81825f43-5fd6-45f7-b22b-83493ca99e46", eventList[0].getEventID()); } + + @Test + public void testLBMockData() throws LyrebirdClientException { + this.mockServer.enqueue( + new MockResponse() + .setBody( + "{\"code\": 1000,\"data\": {\"id\": \"cfa0c589-8ef0-4885-b4f4-b9688c5af0d5\", \"name\": \"test-data\", \"response\": {\"data\": \"[{\\\"type\\\": \\\"scheme\\\", \\\"info\\\":{\\\"value\\\": \\\"test://www.lyrebird.java.sdk.com\\\"}, \\\"desc\\\": \\\"The scheme of target page\\\"}]\"}}, \"message\": \"success\"}")); + + LBMockData lbMockData = this.lyrebird.getMockData("cfa0c589-8ef0-4885-b4f4-b9688c5af0d5"); + assertEquals("cfa0c589-8ef0-4885-b4f4-b9688c5af0d5", lbMockData.getId()); + assertEquals("test-data", lbMockData.getName()); + + List urlScheme = JsonPath.parse(lbMockData.getResponseData().toString()).read("$[?(@.type == 'scheme')].info.value"); + assertEquals(1, urlScheme.size()); + assertEquals("test://www.lyrebird.java.sdk.com", urlScheme.get(0)); + } }