Search in sources :

Example 1 with Result

use of com.dtflys.test.model.Result in project forest by dromara.

the class TestGenericForestClient method testRequest_upload_file.

@Test
public void testRequest_upload_file() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    TypeReference<Result<Integer>> typeReference = new TypeReference<Result<Integer>>() {
    };
    String path = Objects.requireNonNull(this.getClass().getResource("/test-img.jpg")).getPath();
    if (path.startsWith("/") && isWindows()) {
        path = path.substring(1);
    }
    File file = new File(path);
    Result<Integer> result = Forest.post("http://localhost:" + server.getPort()).contentTypeMultipartFormData().addFile("file", file).execute(typeReference);
    assertThat(result).isNotNull();
    assertThat(result.getStatus()).isEqualTo(1);
    assertThat(result.getData()).isEqualTo(2);
    mockRequest(server).assertMethodEquals("POST").assertPathEquals("/");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockResponse(okhttp3.mockwebserver.MockResponse) TypeReference(com.dtflys.forest.utils.TypeReference) File(java.io.File) Result(com.dtflys.test.model.Result) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 2 with Result

use of com.dtflys.test.model.Result in project forest by dromara.

the class TestGenericForestClient method testRequest_post_form_body_keys.

@Test
public void testRequest_post_form_body_keys() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    TypeReference<Result<Integer>> typeReference = new TypeReference<Result<Integer>>() {
    };
    ForestRequest request = Forest.post("http://localhost:" + server.getPort() + "/post").contentFormUrlEncoded().addBody("a", 1).addBody("b", 2);
    assertThat(request.body().nameValuesMapWithObject()).extracting("a", "b").contains(1, 2);
    Result<Integer> result = (Result<Integer>) request.execute(typeReference);
    assertThat(result).isNotNull();
    assertThat(result.getStatus()).isEqualTo(1);
    assertThat(result.getData()).isEqualTo(2);
    mockRequest(server).assertMethodEquals("POST").assertPathEquals("/post").assertHeaderEquals(ForestHeader.CONTENT_TYPE, ContentType.APPLICATION_X_WWW_FORM_URLENCODED).assertBodyEquals("a=1&b=2");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockResponse(okhttp3.mockwebserver.MockResponse) TypeReference(com.dtflys.forest.utils.TypeReference) ForestRequest(com.dtflys.forest.http.ForestRequest) Result(com.dtflys.test.model.Result) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 3 with Result

use of com.dtflys.test.model.Result in project forest by dromara.

the class TestGenericForestClient method testRequest_get_return_JavaObject.

@Test
public void testRequest_get_return_JavaObject() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    Result result = Forest.get("http://localhost:" + server.getPort()).execute(Result.class);
    assertThat(result).isNotNull();
    assertThat(result.getStatus()).isEqualTo(1);
    assertThat(result.getData()).isEqualTo("2");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Result(com.dtflys.test.model.Result) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 4 with Result

use of com.dtflys.test.model.Result in project forest by dromara.

the class JsonTest method testGson.

public void testGson() {
    String jsonSource = gsonConverter.encodeToString(map);
    assertNotNull(jsonSource);
    Map newMap = gsonConverter.convertToJavaObject(jsonSource, Map.class);
    assertEquals(map, newMap);
    String jsonSource2 = gsonConverter.encodeToString(contacts);
    assertNotNull(jsonSource2);
    List<Contact> newList = gsonConverter.convertToJavaObject(jsonSource2, new TypeToken<List<Contact>>() {
    }.getType());
    assertNotNull(newList);
    assertEquals(newList.get(0).getAge(), new Integer(20));
    Type type = new TypeToken<Result<List<Contact>>>() {
    }.getType();
    String jsonSource3 = gsonConverter.convertToJson(result, type);
    assertNotNull(jsonSource3);
    Result<List<Contact>> newResult = gsonConverter.convertToJavaObject(jsonSource3, type);
    assertNotNull(newResult);
    assertEquals(newResult.getStatus(), new Integer(1));
    String locationText = "{\"status\":\"1\",\"data\":{\"province\":\"江苏省\",\"cross_list\":[{\"distance\":\"191.482\",\"direction\":\"West\",\"name\":\"联谊路--绿溪路\",\"weight\":\"120\",\"level\":\"45000, 45000\",\"longitude\":\"121.0512567\",\"crossid\":\"021H51F0090093015--021H51F009009851\",\"width\":\"8, 8\",\"latitude\":\"31.31579861\"},{\"distance\":\"233.802\",\"direction\":\"NorthEast\",\"name\":\"陆家浜南路--陆家浜北路\",\"weight\":\"120\",\"level\":\"45000, 45000\",\"longitude\":\"121.0476761\",\"crossid\":\"021H51F00900930--021H51F0090093005\",\"width\":\"8, 8\",\"latitude\":\"31.31397833\"},{\"distance\":\"233.802\",\"direction\":\"NorthEast\",\"name\":\"陆家浜南路--绿溪路\",\"weight\":\"120\",\"level\":\"45000, 45000\",\"longitude\":\"121.0476761\",\"crossid\":\"021H51F00900930--021H51F009009851\",\"width\":\"8, 8\",\"latitude\":\"31.31397833\"}],\"code\":\"1\",\"tel\":\"0512\",\"cityadcode\":\"320500\",\"areacode\":\"0512\",\"timestamp\":\"1466430882.37\",\"pos\":\"在昆山市陆家镇人民政府附近, 在绿溪路旁边, 靠近联谊路--绿溪路路口\",\"road_list\":[{\"distance\":\"79\",\"direction\":\"North\",\"name\":\"绿溪路\",\"level\":\"5\",\"longitude\":\"121.05\",\"width\":\"8\",\"roadid\":\"021H51F009009851\",\"latitude\":\"31.3149\"},{\"distance\":\"152\",\"direction\":\"SouthEast\",\"name\":\"教堂路\",\"level\":\"5\",\"longitude\":\"121.048\",\"width\":\"4\",\"roadid\":\"021H51F0090092871\",\"latitude\":\"31.3163\"},{\"distance\":\"191\",\"direction\":\"West\",\"name\":\"联谊路\",\"level\":\"5\",\"longitude\":\"121.051\",\"width\":\"8\",\"roadid\":\"021H51F0090093015\",\"latitude\":\"31.3158\"}],\"result\":\"true\",\"message\":\"Successful.\",\"desc\":\"江苏省,苏州市,昆山市\",\"city\":\"苏州市\",\"districtadcode\":\"320583\",\"district\":\"昆山市\",\"country\":\"中国\",\"provinceadcode\":\"320000\",\"version\":\"2.0-3.0.6168.2019\",\"adcode\":\"320583\",\"poi_list\":[{\"distance\":\"161\",\"direction\":\"West\",\"tel\":\"0512-57877735;0512-57671209;0512-57879719\",\"name\":\"昆山市陆家人民医院\",\"weight\":\"0.0\",\"typecode\":\"090100\",\"longitude\":\"121.047556\",\"address\":\"陆家镇镇北路21号\",\"latitude\":\"31.315543\",\"type\":\"医疗保健服务;综合医院;综合医院\",\"poiid\":\"B020016BMR\"},{\"distance\":\"146\",\"direction\":\"SouthWest\",\"tel\":\"\",\"name\":\"中共陆家镇委政法委\",\"weight\":\"0.0\",\"typecode\":\"130105\",\"longitude\":\"121.048380\",\"address\":\"菉溪路22号\",\"latitude\":\"31.314508\",\"type\":\"政府机构及社会团体;政府机关;乡镇级政府及事业单位\",\"poiid\":\"B0FFF9XX2W\"},{\"distance\":\"141\",\"direction\":\"SouthWest\",\"tel\":\"0512-57671003\",\"name\":\"昆山市陆家镇人民政府\",\"weight\":\"0.0\",\"typecode\":\"130105\",\"longitude\":\"121.048398\",\"address\":\"菉溪路22号\",\"latitude\":\"31.314555\",\"type\":\"政府机构及社会团体;政府机关;乡镇级政府及事业单位\",\"poiid\":\"B020007YBU\"},{\"distance\":\"141\",\"direction\":\"SouthWest\",\"tel\":\"\",\"name\":\"陆家镇人民代表大会\",\"weight\":\"0.0\",\"typecode\":\"130105\",\"longitude\":\"121.048398\",\"address\":\"菉溪路22号\",\"latitude\":\"31.314555\",\"type\":\"政府机构及社会团体;政府机关;乡镇级政府及事业单位\",\"poiid\":\"B020007YBT\"},{\"distance\":\"141\",\"direction\":\"SouthWest\",\"tel\":\"\",\"name\":\"中共昆山市陆家镇委员会\",\"weight\":\"0.0\",\"typecode\":\"130105\",\"longitude\":\"121.048398\",\"address\":\"菉溪路22号\",\"latitude\":\"31.314555\",\"type\":\"政府机构及社会团体;政府机关;乡镇级政府及事业单位\",\"poiid\":\"B020008HW1\"}]}}";
    Result<Location> locationResult = gsonConverter.convertToJavaObject(locationText, new TypeReference<Result<Location>>() {
    }.getType());
    assertNotNull(locationResult);
}
Also used : Contact(com.dtflys.test.model.Contact) Result(com.dtflys.test.model.Result) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) TypeToken(com.google.gson.reflect.TypeToken) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(com.alibaba.fastjson.TypeReference) HashMap(java.util.HashMap) Map(java.util.Map) Location(com.dtflys.test.model.Location)

Example 5 with Result

use of com.dtflys.test.model.Result in project forest by dromara.

the class JsonTest method testFastjson.

public void testFastjson() {
    String jsonSource = fastjsonConverter.encodeToString(map);
    assertNotNull(jsonSource);
    Map newMap = fastjsonConverter.convertToJavaObject(jsonSource, Map.class);
    assertEquals(map, newMap);
    String jsonSource2 = fastjsonConverter.encodeToString(contacts);
    assertNotNull(jsonSource2);
    List<Contact> newList = fastjsonConverter.convertToJavaObject(jsonSource2, new TypeReference<List<Contact>>() {
    });
    assertNotNull(newList);
    assertEquals(newList.get(0).getAge(), new Integer(20));
    String jsonSource3 = fastjsonConverter.encodeToString(result);
    assertNotNull(jsonSource3);
    Result<List<Contact>> newResult = fastjsonConverter.convertToJavaObject(jsonSource3, new TypeReference<Result<List<Contact>>>() {
    });
    assertNotNull(newResult);
    assertEquals(newResult.getStatus(), new Integer(1));
    String locationText = "{\"status\":\"1\",\"data\":{\"province\":\"江苏省\",\"cross_list\":[{\"distance\":\"191.482\",\"direction\":\"West\",\"name\":\"联谊路--绿溪路\",\"weight\":\"120\",\"level\":\"45000, 45000\",\"longitude\":\"121.0512567\",\"crossid\":\"021H51F0090093015--021H51F009009851\",\"width\":\"8, 8\",\"latitude\":\"31.31579861\"},{\"distance\":\"233.802\",\"direction\":\"NorthEast\",\"name\":\"陆家浜南路--陆家浜北路\",\"weight\":\"120\",\"level\":\"45000, 45000\",\"longitude\":\"121.0476761\",\"crossid\":\"021H51F00900930--021H51F0090093005\",\"width\":\"8, 8\",\"latitude\":\"31.31397833\"},{\"distance\":\"233.802\",\"direction\":\"NorthEast\",\"name\":\"陆家浜南路--绿溪路\",\"weight\":\"120\",\"level\":\"45000, 45000\",\"longitude\":\"121.0476761\",\"crossid\":\"021H51F00900930--021H51F009009851\",\"width\":\"8, 8\",\"latitude\":\"31.31397833\"}],\"code\":\"1\",\"tel\":\"0512\",\"cityadcode\":\"320500\",\"areacode\":\"0512\",\"timestamp\":\"1466430882.37\",\"pos\":\"在昆山市陆家镇人民政府附近, 在绿溪路旁边, 靠近联谊路--绿溪路路口\",\"road_list\":[{\"distance\":\"79\",\"direction\":\"North\",\"name\":\"绿溪路\",\"level\":\"5\",\"longitude\":\"121.05\",\"width\":\"8\",\"roadid\":\"021H51F009009851\",\"latitude\":\"31.3149\"},{\"distance\":\"152\",\"direction\":\"SouthEast\",\"name\":\"教堂路\",\"level\":\"5\",\"longitude\":\"121.048\",\"width\":\"4\",\"roadid\":\"021H51F0090092871\",\"latitude\":\"31.3163\"},{\"distance\":\"191\",\"direction\":\"West\",\"name\":\"联谊路\",\"level\":\"5\",\"longitude\":\"121.051\",\"width\":\"8\",\"roadid\":\"021H51F0090093015\",\"latitude\":\"31.3158\"}],\"result\":\"true\",\"message\":\"Successful.\",\"desc\":\"江苏省,苏州市,昆山市\",\"city\":\"苏州市\",\"districtadcode\":\"320583\",\"district\":\"昆山市\",\"country\":\"中国\",\"provinceadcode\":\"320000\",\"version\":\"2.0-3.0.6168.2019\",\"adcode\":\"320583\",\"poi_list\":[{\"distance\":\"161\",\"direction\":\"West\",\"tel\":\"0512-57877735;0512-57671209;0512-57879719\",\"name\":\"昆山市陆家人民医院\",\"weight\":\"0.0\",\"typecode\":\"090100\",\"longitude\":\"121.047556\",\"address\":\"陆家镇镇北路21号\",\"latitude\":\"31.315543\",\"type\":\"医疗保健服务;综合医院;综合医院\",\"poiid\":\"B020016BMR\"},{\"distance\":\"146\",\"direction\":\"SouthWest\",\"tel\":\"\",\"name\":\"中共陆家镇委政法委\",\"weight\":\"0.0\",\"typecode\":\"130105\",\"longitude\":\"121.048380\",\"address\":\"菉溪路22号\",\"latitude\":\"31.314508\",\"type\":\"政府机构及社会团体;政府机关;乡镇级政府及事业单位\",\"poiid\":\"B0FFF9XX2W\"},{\"distance\":\"141\",\"direction\":\"SouthWest\",\"tel\":\"0512-57671003\",\"name\":\"昆山市陆家镇人民政府\",\"weight\":\"0.0\",\"typecode\":\"130105\",\"longitude\":\"121.048398\",\"address\":\"菉溪路22号\",\"latitude\":\"31.314555\",\"type\":\"政府机构及社会团体;政府机关;乡镇级政府及事业单位\",\"poiid\":\"B020007YBU\"},{\"distance\":\"141\",\"direction\":\"SouthWest\",\"tel\":\"\",\"name\":\"陆家镇人民代表大会\",\"weight\":\"0.0\",\"typecode\":\"130105\",\"longitude\":\"121.048398\",\"address\":\"菉溪路22号\",\"latitude\":\"31.314555\",\"type\":\"政府机构及社会团体;政府机关;乡镇级政府及事业单位\",\"poiid\":\"B020007YBT\"},{\"distance\":\"141\",\"direction\":\"SouthWest\",\"tel\":\"\",\"name\":\"中共昆山市陆家镇委员会\",\"weight\":\"0.0\",\"typecode\":\"130105\",\"longitude\":\"121.048398\",\"address\":\"菉溪路22号\",\"latitude\":\"31.314555\",\"type\":\"政府机构及社会团体;政府机关;乡镇级政府及事业单位\",\"poiid\":\"B020008HW1\"}]}}";
    Result<Location> locationResult = fastjsonConverter.convertToJavaObject(locationText, new TypeReference<Result<Location>>() {
    });
    assertNotNull(locationResult);
}
Also used : Contact(com.dtflys.test.model.Contact) Result(com.dtflys.test.model.Result) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Location(com.dtflys.test.model.Location)

Aggregations

Result (com.dtflys.test.model.Result)6 BaseClientTest (com.dtflys.test.http.BaseClientTest)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 Test (org.junit.Test)4 TypeReference (com.dtflys.forest.utils.TypeReference)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ForestRequest (com.dtflys.forest.http.ForestRequest)2 Contact (com.dtflys.test.model.Contact)2 Location (com.dtflys.test.model.Location)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 TypeReference (com.alibaba.fastjson.TypeReference)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 TypeToken (com.google.gson.reflect.TypeToken)1 File (java.io.File)1 Type (java.lang.reflect.Type)1 LinkedHashMap (java.util.LinkedHashMap)1