Skip to content

Commit

Permalink
Merge pull request #18 from yury-awesome/feature/新增接口支持获取Mock数据信息
Browse files Browse the repository at this point in the history
Feature/新增接口支持获取mock数据信息
  • Loading branch information
yury-awesome authored Mar 12, 2020
2 parents 03b7d10 + ab54d8a commit e2a3f3c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [TestNG](#TestNG)
- [Junit4](#Junit4)
- [取消激活](#取消激活)
- [读取Mock数据](#读取Mock数据)
- [获取网络数据信息](#获取网络数据信息)
- [获取 flow List](#获取-flow-List)
- [获取 flow ID](#获取-flow-ID)
Expand Down Expand Up @@ -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 示例
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.meituan-dianping.lyrebird.sdk</groupId>
<artifactId>lyrebird-java-client</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>

<name>lyrebird-java-client</name>
<url>https://github.com/Meituan-Dianping/lyrebird-java-client</url>
Expand Down Expand Up @@ -162,6 +162,12 @@
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/meituan/lyrebird/Lyrebird.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/meituan/lyrebird/client/LyrebirdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ public interface LyrebirdService {

@GET("api/event/{channel}")
Call<Events> getEventList(@Path("channel") String channel);

@GET("api/data/{dataId}")
Call<LBMockData> getMockData(@Path("dataId") String dataId);
}
32 changes: 32 additions & 0 deletions src/main/java/com/meituan/lyrebird/client/api/LBMockData.java
Original file line number Diff line number Diff line change
@@ -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> T getResponseData() {
return JsonPath.parse(data.get("response")).read("$.data");
}

public Map getData() {
return data;
}

public void setData(Map data) {
this.data = data;
}
}
17 changes: 17 additions & 0 deletions src/test/java/com/meituan/lyrebird/test/TestFunctional.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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<String> 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));
}
}

0 comments on commit e2a3f3c

Please sign in to comment.