Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@
<version>${powermock-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package com.ibm.cloud.sdk.core.service.model;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

Expand All @@ -27,7 +27,7 @@ public abstract class DynamicModel<T> implements ObjectModel {
private TypeToken<T> additionalPropertyTypeToken;

// The set of dynamic properties associated with this object.
private Map<String, T> dynamicProperties = new HashMap<>();
private Map<String, T> dynamicProperties = new LinkedHashMap<>();

/**
* Force use of 1-arg ctor.
Expand Down Expand Up @@ -85,7 +85,7 @@ public T get(String key) {
* a map containing arbitrary properties to set on this object
*/
public void setProperties(Map<String, T> properties) {
this.dynamicProperties = new HashMap<String, T>();
this.dynamicProperties = new LinkedHashMap<String, T>();
if (properties != null) {
this.dynamicProperties.putAll(properties);
}
Expand All @@ -97,7 +97,7 @@ public void setProperties(Map<String, T> properties) {
* @return a copy of the map containing arbitrary properties set on this object
*/
public Map<String, T> getProperties() {
return new HashMap<String, T>(this.dynamicProperties);
return new LinkedHashMap<String, T>(this.dynamicProperties);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.testng.annotations.Test;

import com.google.gson.JsonObject;
Expand Down Expand Up @@ -211,9 +213,10 @@ public void testWithContentString() throws IOException {
* Test with list of models.
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws JSONException Signals that a JSON parsing error has occured.
*/
@Test
public void testBodyContentList() throws IOException {
public void testBodyContentList() throws IOException, JSONException {
// add list of actual models
final List<Vehicle> listOfModels = new ArrayList<>();
listOfModels.add(new Truck.Builder().vehicleType("Truck").make("Ford").engineType("raptor").build());
Expand All @@ -224,8 +227,11 @@ public void testBodyContentList() throws IOException {
final RequestBody requestedBody = request.body();
final Buffer buffer = new Buffer();
requestedBody.writeTo(buffer);

assertEquals(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(listOfModels), buffer.readUtf8());
try {
JSONAssert.assertEquals(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(listOfModels), buffer.readUtf8(), false);
} catch (JSONException e) {
throw e;
}
assertEquals(HttpMediaType.JSON, requestedBody.contentType());
}

Expand Down Expand Up @@ -475,17 +481,21 @@ public void testResolveRequestUrlNull() {
/**
* Test bodyContent() with a model instance.
* @throws IOException
* @throws JSONException
*/
@Test
public void testBodyContent1() throws IOException {
public void testBodyContent1() throws IOException, JSONException {
Truck truck = new Truck.Builder().vehicleType("Truck").make("Ford").engineType("raptor").build();
final Request request = RequestBuilder.post(HttpUrl.parse(urlWithQuery))
.bodyContent("application/json", truck, null, (InputStream) null).build();
final RequestBody requestedBody = request.body();
final Buffer buffer = new Buffer();
requestedBody.writeTo(buffer);

assertEquals(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(truck), buffer.readUtf8());
try {
JSONAssert.assertEquals(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(truck), buffer.readUtf8(), false);
} catch (JSONException e) {
throw e;
}
assertEquals(HttpMediaType.JSON, requestedBody.contentType());
}

Expand All @@ -502,25 +512,29 @@ public void testBodyContent2() throws IOException {
final RequestBody requestedBody = request.body();
final Buffer buffer = new Buffer();
requestedBody.writeTo(buffer);

assertEquals(jsonString, buffer.readUtf8());
assertEquals(MediaType.parse("application/json"), requestedBody.contentType());
}

/**
* Test bodyContent() with a multiple inputs (JSON input should win).
* @throws IOException
* @throws JSONException
*/
@Test
public void testBodyContent3() throws IOException {
public void testBodyContent3() throws IOException, JSONException {
Truck truck = new Truck.Builder().vehicleType("Truck").make("Ford").engineType("raptor").build();
final Request request = RequestBuilder.post(HttpUrl.parse(urlWithQuery))
.bodyContent("application/json", truck, "BAD JSON PATCH BODY", "BAD INPUTSTREAM REQUEST BODY").build();
final RequestBody requestedBody = request.body();
final Buffer buffer = new Buffer();
requestedBody.writeTo(buffer);

assertEquals(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(truck), buffer.readUtf8());
try {
JSONAssert.assertEquals(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(truck), buffer.readUtf8(), false);
} catch (JSONException e) {
throw e;
}
assertEquals(HttpMediaType.JSON, requestedBody.contentType());
}

Expand Down
12 changes: 8 additions & 4 deletions src/test/java/com/ibm/cloud/sdk/core/util/RequestUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ public void testOmit() {
public void testOmitWithNulls() {
final Map<String, Object> params = createMap();

assertEquals(params.keySet().toArray(), RequestUtils.omit(params).keySet().toArray());
assertEquals(params.values().toArray(), RequestUtils.omit(params).values().toArray());
Map<String, Object> omitted = RequestUtils.omit(params);

assertTrue(omitted.keySet().containsAll(Lists.newArrayList("A", "B", "C", "D")));
assertTrue(omitted.values().containsAll(Lists.newArrayList(1, 2, 3, 4)));

assertNull(RequestUtils.omit(null));
}
Expand All @@ -97,9 +99,11 @@ public void testPick() {
@Test
public void testPickWithNulls() {
final Map<String, Object> params = createMap();

Map<String, Object> omitted = RequestUtils.omit(params);

assertEquals(params.keySet().toArray(), RequestUtils.pick(params).keySet().toArray());
assertEquals(params.values().toArray(), RequestUtils.pick(params).values().toArray());
assertTrue(omitted.keySet().containsAll(Lists.newArrayList("A", "B", "C", "D")));
assertTrue(omitted.values().containsAll(Lists.newArrayList(1, 2, 3, 4)));

assertNull(RequestUtils.pick(null));
}
Expand Down