Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iot-dev): Fix bug where configuration content was not serialized correctly #1510

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,20 @@ public void crud_adm_configuration_e2e() throws Exception
{
// Arrange
ConfigurationsClientTestInstance testInstance = new ConfigurationsClientTestInstance();

// testing such that one configuration uses a floating point value and the other an integer so that
// the value is maintained as an int/float through serialization and deserialization
int expectedTemperatureValue = 66;
double expectedPressureValue = 28.02;

final HashMap<String, Object> testDeviceContent = new HashMap<String, Object>()
{
{
put("properties.desired.chiller-water", new HashMap<String, Object>()
{
{
put("temperature", 66);
put("pressure", 28);
put("temperature", expectedTemperatureValue);
put("pressure", expectedPressureValue);
}
}
);
Expand Down Expand Up @@ -163,8 +169,8 @@ public void crud_adm_configuration_e2e() throws Exception
String[] entry = pair.split("=");
actualMap.put(entry[0].trim(), entry[1].trim());
}
assertEquals(buildExceptionMessage("", hostName), "66.0", actualMap.get("temperature"));
assertEquals(buildExceptionMessage("", hostName), "28.0", actualMap.get("pressure"));
assertEquals(buildExceptionMessage("", hostName), expectedTemperatureValue, Integer.valueOf(actualMap.get("temperature")).intValue());
assertEquals(buildExceptionMessage("", hostName), expectedPressureValue, Double.valueOf(actualMap.get("pressure")).doubleValue(), 0);
assertEquals(buildExceptionMessage("", hostName), "SELECT deviceId FROM devices WHERE properties.reported.chillerWaterSettings.status=\'pending\'",
configRetrieved.getMetrics().getQueries().get("waterSettingsPending"));
assertEquals(buildExceptionMessage("", hostName), "properties.reported.chillerProperties.model=\'4000x\'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public ConfigurationContentParser toConfigurationContentParser()
ConfigurationContentParser parser = new ConfigurationContentParser();
parser.setModulesContent(this.modulesContent);
parser.setDeviceContent(this.deviceContent);
parser.setModuleContent(this.moduleContent);
return parser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public void applyConfigurationContentOnDevice(String deviceId, ConfigurationCont

URL url = IotHubConnectionString.getUrlApplyConfigurationContent(this.hostName, deviceId);

HttpRequest request = createRequest(url, HttpMethod.POST, content.toConfigurationContentParser().toJson().getBytes(StandardCharsets.UTF_8));
timtay-microsoft marked this conversation as resolved.
Show resolved Hide resolved
HttpRequest request = createRequest(url, HttpMethod.POST, content.toConfigurationContentParser().toJsonElement().toString().getBytes(StandardCharsets.UTF_8));

request.send();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.microsoft.azure.sdk.iot.service.configurations.serializers;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.*;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.microsoft.azure.sdk.iot.service.ParserUtility;
Expand Down Expand Up @@ -36,7 +33,7 @@ public class ConfigurationContentParser
@Setter
private Map<String, Object> deviceContent;

private final transient static Gson gson = new Gson();
private final transient static Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
timtay-microsoft marked this conversation as resolved.
Show resolved Hide resolved

/**
* Empty constructor: Used only to keep GSON happy.
Expand All @@ -51,7 +48,6 @@ public ConfigurationContentParser()
*/
public ConfigurationContentParser(String json)
{
//Codes_SRS_CONFIGURATION_CONTENT_PARSER_28_001: [If the provided json is null, empty an IllegalArgumentException shall be thrown.]
if (json == null || json.isEmpty())
{
throw new IllegalArgumentException("The provided json cannot be null or empty");
Expand All @@ -60,14 +56,10 @@ public ConfigurationContentParser(String json)
ConfigurationContentParser configurationContentParser;
try
{
//Codes_SRS_CONFIGURATION_CONTENT_PARSER_28_002: [The constructor shall take the provided json and convert
// it into a new ConfigurationContentParser and return it.]
configurationContentParser = gson.fromJson(json, ConfigurationContentParser.class);
}
catch (JsonSyntaxException e)
{
//Codes_SRS_CONFIGURATION_CONTENT_PARSER_28_003: [If the provided json cannot be parsed into a Configuration
// ContentParser object, an IllegalArgumentException shall be thrown.]
throw new IllegalArgumentException("The provided json could not be parsed");
}

Expand All @@ -76,30 +68,23 @@ public ConfigurationContentParser(String json)
this.deviceContent = configurationContentParser.deviceContent;
}

public String toJson()
timtay-microsoft marked this conversation as resolved.
Show resolved Hide resolved
{
return gson.toJson(this);
}

public JsonElement toJsonElement()
{
JsonObject contentJson = new JsonObject();

/* Codes_SRS_CONFIGURATION_METRICS_PARSER_28_009: [If the modulesContent is null, the toJsonElement shall not include the `modulesContent` in the final JSON.] */
if (this.modulesContent != null)
if (this.modulesContent != null && this.modulesContent.size() > 0)
timtay-microsoft marked this conversation as resolved.
Show resolved Hide resolved
{
Map<String, Object> map = new HashMap<>();
map.putAll(this.modulesContent);
contentJson.add(MODULES_CONTENT_NAME, ParserUtility.mapToJsonElement(map));
}

/* Codes_SRS_CONFIGURATION_METRICS_PARSER_28_010: [If the deviceContent is null, the toJsonElement shall not include the `deviceContent` in the final JSON.]*/
if (this.deviceContent != null)
if (this.deviceContent != null && this.deviceContent.size() > 0)
{
contentJson.add(DEVICE_CONTENT_NAME, ParserUtility.mapToJsonElement(this.deviceContent));
}

if (this.moduleContent != null)
if (this.moduleContent != null && this.moduleContent.size() > 0)
{
contentJson.add(MODULE_CONTENT_NAME, ParserUtility.mapToJsonElement(this.moduleContent));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.microsoft.azure.sdk.iot.service.configurations.serializers;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.*;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.microsoft.azure.sdk.iot.service.ParserUtility;
Expand Down Expand Up @@ -99,7 +96,12 @@ public class ConfigurationParser
@Setter
private String eTag;

private final transient static Gson gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().create();
private final transient static Gson gson =
new GsonBuilder()
.enableComplexMapKeySerialization()
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.serializeNulls()
.create();

/**
* Empty constructor: Used only to keep GSON happy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void constructorFromJson()
{
//arrange
String json = "{\"modulesContent\":{\"properties\":{\"c\":\"abc\",\"d\":\"def\"}}, " +
"\"deviceContent\":{\"properties.desired.settings1\": {\"c\":3,\"d\":4}}}";
"\"deviceContent\":{\"properties.desired.settings1\": {\"c\":3,\"d\":4.1}}}";

//act
ConfigurationContentParser parser = new ConfigurationContentParser(json);
Expand All @@ -48,8 +48,8 @@ public void constructorFromJson()
assertEquals("abc", moduleContentMap.get("c"));
assertEquals("def", moduleContentMap.get("d"));
Map<String, Object> deviceContentMap = ((Map<String,Object>)(parser.getDeviceContent().get("properties.desired.settings1")));
assertEquals((double)3, deviceContentMap.get("c"));
assertEquals((double)4, deviceContentMap.get("d"));
assertEquals(3l, deviceContentMap.get("c"));
assertEquals(4.1, deviceContentMap.get("d"));
}

//Tests_SRS_CONFIGURATION_CONTENT_PARSER_28_003: [If the provided json cannot be parsed into a ConfigurationContentParser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public void applyConfigurationContentOnDeviceSuccess() throws Exception
mockedConfigurationContent.toConfigurationContentParser();
result = mockedConfigurationContentParser;

mockedConfigurationContentParser.toJson();
mockedConfigurationContentParser.toJsonElement().toString();
result = expectedJson;
}
};
Expand Down