Skip to content

Commit

Permalink
break(iot-dev, iot-serv): Allow twin versions to be null, fix version…
Browse files Browse the repository at this point in the history
… not showing when service client gets twin (#1500)

Nullable version means that users can opt out of caring about versions and just send property updates.
  • Loading branch information
timtay-microsoft authored Mar 23, 2022
1 parent cb7dbeb commit 0dd8b24
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public final Integer getVersion() {
/**
* Setter for the version.
*/
public final void setVersion(int version) {
public final void setVersion(Integer version) {
this.version = version;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void testPatchTwin() throws IOException, GeneralSecurityException, IotHub

// assert
assertTrue(TwinCommon.isPropertyInTwinCollection(twin.getDesiredProperties(), expectedDesiredPropertyKey, expectedDesiredPropertyValue));
assertNotNull(twin.getDesiredProperties().getVersion());
}

@Test
Expand Down Expand Up @@ -145,5 +146,6 @@ public void testReplaceTwin() throws IOException, GeneralSecurityException, IotH
// assert
assertFalse("old twin property was not deleted when twin client replaced it", TwinCommon.isPropertyInTwinCollection(twin.getDesiredProperties(), desiredPropertyToBeReplacedKey, desiredPropertyToBeReplacedValue));
assertTrue("new twin property was not saved when twin client added it using twinClient.replace", TwinCommon.isPropertyInTwinCollection(twin.getDesiredProperties(), expectedDesiredPropertyKey, expectedDesiredPropertyValue));
assertNotNull(twin.getDesiredProperties().getVersion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public WrappedTwin(Twin twin)

private JsonObject mapToJson(TwinCollection map)
{
return new JsonObject(map.toJsonElement().toString());
return new JsonObject(map.toJsonObject().toString());
}

public JsonObject toJsonObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,19 @@ public static Twin fromJson(String json)
twin.setVersion(twinState.getVersion());
twin.setETag(twinState.getETag());

twin.getTags().setVersion(twinState.getTags().getVersion());
if (twinState.getTags().size() > 0)
{
twin.getTags().putAll(twinState.getTags());
}

twin.getDesiredProperties().setVersion(twinState.getDesiredProperties().getVersion());
if (twinState.getDesiredProperties().size() > 0)
{
twin.getDesiredProperties().putAll(twinState.getDesiredProperties());
}

twin.getReportedProperties().setVersion(twinState.getReportedProperties().getVersion());
if (twinState.getReportedProperties().size() > 0)
{
twin.getReportedProperties().putAll(twinState.getReportedProperties());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.microsoft.azure.sdk.iot.service.ParserUtility;

import java.util.HashMap;
Expand Down Expand Up @@ -329,8 +330,13 @@ private static void addMetadata(TwinCollection twinCollection, Map<? extends Str
*
* @return The {@code JsonElement} with the content of this class.
*/
public JsonElement toJsonElement() {
return ParserUtility.mapToJsonElement(this);
public JsonObject toJsonObject() {
JsonObject object = ParserUtility.mapToJsonElement(this).getAsJsonObject();
if (this.version != null)
{
object.addProperty(VERSION_TAG, this.version);
}
return object;
}

/**
Expand Down Expand Up @@ -394,7 +400,7 @@ public final Integer getVersion() {
* Setter for the version of this twin collection.
* @param version the version.
*/
public final void setVersion(int version)
public final void setVersion(Integer version)
{
this.version = version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ JsonElement toJsonElement()

if (this.desired != null)
{
twinJson.add(DESIRED_PROPERTIES_TAG, this.desired.toJsonElement());
twinJson.add(DESIRED_PROPERTIES_TAG, this.desired.toJsonObject());
}

if (this.reported != null)
{
twinJson.add(REPORTED_PROPERTIES_TAG, this.reported.toJsonElement());
twinJson.add(REPORTED_PROPERTIES_TAG, this.reported.toJsonObject());
}

return twinJson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ public void toJsonElementSerializeSucceed()
};

// act
JsonElement jsonElement = twinCollection.toJsonElement();
JsonElement jsonElement = twinCollection.toJsonObject();

// assert
Helpers.assertJson(jsonElement.toString(), JSON_SAMPLE);
Expand Down Expand Up @@ -851,7 +851,7 @@ public void toJsonElementSerializeNullPropertySucceed()
" }\n";

// act
JsonElement jsonElement = twinCollection.toJsonElement();
JsonElement jsonElement = twinCollection.toJsonObject();

// assert
Helpers.assertJson(jsonElement.toString(), json);
Expand All @@ -867,7 +867,7 @@ public void toJsonElementNotIncludeMetadataOrVersion()
TwinCollection twinCollection = Deencapsulation.invoke(TwinCollection.class, "createFromRawCollection", rawMap);

// act
JsonElement jsonElement = twinCollection.toJsonElement();
JsonElement jsonElement = twinCollection.toJsonObject();

// assert
Helpers.assertJson(jsonElement.toString(), JSON_SAMPLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public void createFromDesiredPropertyJson()
TwinState twinState = TwinState.createFromDesiredPropertyJson(json);

// assert
Helpers.assertJson(twinState.getDesiredProperties().toJsonElement().toString(), json);
Helpers.assertJson(twinState.getDesiredProperties().toJsonObject().toString(), json);
}

/* SRS_TWIN_STATE_21_017: [The factory shall throw IllegalArgumentException if the JSON is null or empty.] */
Expand Down Expand Up @@ -448,7 +448,7 @@ public void createFromReportedPropertyJson()
TwinState twinState = TwinState.createFromReportedPropertyJson(json);

// assert
Helpers.assertJson(twinState.getReportedProperties().toJsonElement().toString(), json);
Helpers.assertJson(twinState.getReportedProperties().toJsonObject().toString(), json);
}

/* SRS_TWIN_STATE_21_020: [The factory shall throw IllegalArgumentException if the JSON is null or empty.] */
Expand Down

0 comments on commit 0dd8b24

Please sign in to comment.