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

EdgeHub: Fix error if client does not send product info #1496

Merged
merged 6 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -19,7 +19,7 @@ public ProductInfoStore(IKeyValueStore<string, string> productInfoEntityStore, s
public Task SetProductInfo(string id, string productInfo)
{
Preconditions.CheckNonWhiteSpace(id, nameof(id));
return this.productInfoEntityStore.Put(id, productInfo);
return !string.IsNullOrWhiteSpace(productInfo) ? this.productInfoEntityStore.Put(id, productInfo) : Task.CompletedTask;
}

public async Task<string> GetProductInfo(string id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ public async Task SmokeTest()
}
}

[Fact]
public async Task EmptyProductInfoTest()
{
// Arrange
string edgeProductInfo = "IoTEdge 1.0.7";
var storeProvider = new StoreProvider(new InMemoryDbStoreProvider());
IEntityStore<string, string> store = storeProvider.GetEntityStore<string, string>("productInfo");
var productInfoStore = new ProductInfoStore(store, edgeProductInfo);

// Act
await productInfoStore.SetProductInfo("d1", string.Empty);
string productInfoValue = await productInfoStore.GetProductInfo("d1");

// Assert
Assert.Equal(string.Empty, productInfoValue);
}

[Fact]
public void ProductInfoCtorTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public static T FromJson<T>(this string json)
{
if (string.IsNullOrWhiteSpace(json))
{
return default(T);
return typeof(T) == typeof(string)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When converting from Json to object, if the json is empty string, and object is string, then return the empty string (instead of default(string) which is null). This prevents null refs down the call stack

? (T)(object)json
: default(T);
}

return typeof(T) == typeof(string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public void FromJsonTest()
Assert.Equal(str, SerDeExtensions.FromJson<string>(str));
}

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(" some value")]
public void StringToJsonRoundtripTest(string input)
{
string value = SerDeExtensions.FromJson<string>(SerDeExtensions.ToJson(input));
Assert.Equal(input, value);
}

[Fact]
public void StringToBytesRoundtripTest()
{
Expand Down