Skip to content

Commit

Permalink
fix(iot-service): Fix bug where setting desired properties to null di…
Browse files Browse the repository at this point in the history
…dn't delete them from the device

Github Bug #221
  • Loading branch information
timtay-microsoft committed Apr 26, 2018
1 parent f7a0815 commit 367d721
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,17 @@ public JsonElement toJsonElement()
/* SRS_TWIN_STATE_21_003: [If the tags is null, the toJsonElement shall not include the `tags` in the final JSON.] */
/* SRS_TWIN_STATE_21_004: [If the property is null, the toJsonElement shall not include the `properties` in the final JSON.] */
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
return gson.toJsonTree(this).getAsJsonObject();
JsonElement json = gson.toJsonTree(this).getAsJsonObject();

//since null values are lost when building the json tree, need to manually re-add properties as reported properties
// may have contained a property with a null value. Those must be preserved so users can delete properties
if (json != null && this.properties != null)
{
//Codes_SRS_TWIN_STATE_34_024: [The json element shall include all null desired and reported properties.]
json.getAsJsonObject().add("properties", this.properties.toJsonElement());
}

return json;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import org.junit.Test;
import tests.unit.com.microsoft.azure.sdk.iot.deps.Helpers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;

/**
* Unit tests for the TwinState
Expand All @@ -40,6 +38,15 @@ public class TwinStateTest
}
};

private final static TwinCollection PROPERTIES_WITH_NULL_VALUES = new TwinCollection()
{
{
put("prop1", null);
put("prop2", null);
put("prop3", null);
}
};

private final static String REGISTER_MANAGER_SAMPLE =
"\"deviceId\":\"validDeviceId\"," +
"\"generationId\":\"validGenerationId\"," +
Expand Down Expand Up @@ -222,6 +229,34 @@ public void toJsonElementReturnsEmptyJson()
Helpers.assertJson(jsonElement.toString(), "{}");
}

//Tests_SRS_TWIN_STATE_34_024: [The json element shall include all null desired and reported properties.]
@Test
public void toJsonElementPreservesNullDesiredProperties()
{
// arrange
TwinState twinState = new TwinState(null, PROPERTIES_WITH_NULL_VALUES, PROPERTIES);

// act
JsonElement jsonElement = Deencapsulation.invoke(twinState, "toJsonElement");

// assert
assertTrue(jsonElement.toString().contains(PROPERTIES_WITH_NULL_VALUES.toString()));
}

//Tests_SRS_TWIN_STATE_34_024: [The json element shall include all null desired and reported properties.]
@Test
public void toJsonElementPreservesNullReportedProperties()
{
// arrange
TwinState twinState = new TwinState(null, PROPERTIES, PROPERTIES_WITH_NULL_VALUES);

// act
JsonElement jsonElement = Deencapsulation.invoke(twinState, "toJsonElement");

// assert
assertTrue(jsonElement.toString().contains(PROPERTIES_WITH_NULL_VALUES.toString()));
}

/* SRS_TWIN_STATE_21_005: [The getTags shall return a TwinCollection with the stored tags.] */
/* SRS_TWIN_STATE_21_006: [The getDesiredProperty shall return a TwinCollection with the stored desired property.] */
/* SRS_TWIN_STATE_21_007: [The getReportedProperty shall return a TwinCollection with the stored reported property.] */
Expand Down

0 comments on commit 367d721

Please sign in to comment.