|
17 | 17 | import com.ibm.cloud.sdk.core.http.HttpMediaType;
|
18 | 18 | import com.ibm.cloud.sdk.core.http.RequestBuilder;
|
19 | 19 | import com.ibm.cloud.sdk.core.test.TestUtils;
|
| 20 | +import com.ibm.cloud.sdk.core.test.model.generated.Car; |
| 21 | +import com.ibm.cloud.sdk.core.test.model.generated.Truck; |
| 22 | +import com.ibm.cloud.sdk.core.test.model.generated.Vehicle; |
| 23 | +import com.ibm.cloud.sdk.core.util.GsonSingleton; |
| 24 | + |
20 | 25 | import okhttp3.HttpUrl;
|
21 | 26 | import okhttp3.MediaType;
|
22 | 27 | import okhttp3.Request;
|
|
26 | 31 |
|
27 | 32 | import java.io.File;
|
28 | 33 | import java.io.IOException;
|
| 34 | +import java.io.InputStream; |
29 | 35 | import java.util.Arrays;
|
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.List; |
30 | 38 |
|
31 | 39 | import static org.junit.Assert.assertEquals;
|
32 | 40 | import static org.junit.Assert.assertNotNull;
|
@@ -196,6 +204,28 @@ public void testWithContentString() throws IOException {
|
196 | 204 |
|
197 | 205 | }
|
198 | 206 |
|
| 207 | + /** |
| 208 | + * Test with list of models. |
| 209 | + * |
| 210 | + * @throws IOException Signals that an I/O exception has occurred. |
| 211 | + */ |
| 212 | + @Test |
| 213 | + public void testBodyContentList() throws IOException { |
| 214 | + // add list of actual models |
| 215 | + final List<Vehicle> listOfModels = new ArrayList<>(); |
| 216 | + listOfModels.add(new Truck.Builder().vehicleType("Truck").make("Ford").engineType("raptor").build()); |
| 217 | + listOfModels.add(new Car.Builder().vehicleType("Car").make("Ford").bodyStyle("mach-e").build()); |
| 218 | + |
| 219 | + final Request request = RequestBuilder.post(HttpUrl.parse(urlWithQuery)) |
| 220 | + .bodyContent("application/json", listOfModels, null, (InputStream) null).build(); |
| 221 | + final RequestBody requestedBody = request.body(); |
| 222 | + final Buffer buffer = new Buffer(); |
| 223 | + requestedBody.writeTo(buffer); |
| 224 | + |
| 225 | + assertEquals(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(listOfModels), buffer.readUtf8()); |
| 226 | + assertEquals(HttpMediaType.JSON, requestedBody.contentType()); |
| 227 | + } |
| 228 | + |
199 | 229 | /**
|
200 | 230 | * Test with form object array.
|
201 | 231 | *
|
|
0 commit comments