Skip to content

Commit e60f91a

Browse files
authored
Mqtt tests for 722, address issue 723 (#725)
* Tests for PR #722 * Issue #723 - turn down verbosity of mqtt entity manager logging
1 parent 66c2de3 commit e60f91a

File tree

4 files changed

+134
-14
lines changed

4 files changed

+134
-14
lines changed

src/Client/NetDaemon.HassClient.Tests/ExtensionsTest/MqttEntityManagerTests/JsonNodeExtensionTests.cs

Lines changed: 126 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
using System.Collections;
22
using System.Text.Json.Nodes;
33
using NetDaemon.Extensions.MqttEntityManager.Helpers;
4+
using Newtonsoft.Json;
5+
using JsonSerializer = System.Text.Json.JsonSerializer;
6+
47
namespace NetDaemon.HassClient.Tests.ExtensionsTest.MqttEntityManagerTests;
58

69
public class JsonNodeExtensionTests
710
{
8-
class MergeTestData : IEnumerable<object[]>
11+
/// <summary>
12+
/// Test data for simple key=value structs
13+
/// </summary>
14+
class SimpleMergeTestData : IEnumerable<object[]>
915
{
1016
public IEnumerator<object[]> GetEnumerator()
1117
{
12-
yield return new object[] { J(new {a = "1", b = "2"}), J(new {}), J( new {a = "1", b ="2"})};
13-
yield return new object[] { J(new {a = "1", b = "2"}), J(new {c = "3"}), J( new {a = "1", b ="2", c = "3"})};
14-
yield return new object[] { J(new {a = "1", b = "2"}), J(new {a = "5"}), J( new {a = "5", b ="2"})};
18+
yield return new object[] { J(new { a = "1", b = "2" }), J(new { }), J(new { a = "1", b = "2" }) };
19+
yield return new object[]
20+
{ J(new { a = "1", b = "2" }), J(new { c = "3" }), J(new { a = "1", b = "2", c = "3" }) };
21+
yield return new object[] { J(new { a = "1", b = "2" }), J(new { a = "5" }), J(new { a = "5", b = "2" }) };
1522
}
1623

1724
IEnumerator IEnumerable.GetEnumerator()
@@ -20,9 +27,15 @@ IEnumerator IEnumerable.GetEnumerator()
2027
}
2128
}
2229

30+
/// <summary>
31+
/// Test for simple key=value structs
32+
/// </summary>
33+
/// <param name="target"></param>
34+
/// <param name="toMerge"></param>
35+
/// <param name="expected"></param>
2336
[Theory]
24-
[ClassData(typeof(MergeTestData))]
25-
public void CanMerge(JsonObject target, JsonObject? toMerge, JsonObject expected)
37+
[ClassData(typeof(SimpleMergeTestData))]
38+
public void CanMergeSimpleDictionaries(JsonObject target, JsonObject? toMerge, JsonObject expected)
2639
{
2740
target.AddRange(toMerge);
2841

@@ -33,6 +46,113 @@ public void CanMerge(JsonObject target, JsonObject? toMerge, JsonObject expected
3346
targetDict.Should().BeEquivalentTo(expectedDict);
3447
}
3548

49+
[Fact]
50+
public void CanAddComplexToSimple()
51+
{
52+
var o1 = new { a = 1, b = 2, c = 3 };
53+
var o2 = new { d = new { x = 8, y = 9 } };
54+
55+
var j1 = J(o1);
56+
57+
j1.AddRange(J(o2));
58+
var combined = JsonConvert.DeserializeObject<dynamic>(j1.ToJsonString());
59+
Assert.Equal("1", combined.a.ToString());
60+
Assert.Equal("2", combined.b.ToString());
61+
Assert.Equal("3", combined.c.ToString());
62+
Assert.Equal("8", combined.d.x.ToString());
63+
Assert.Equal("9", combined.d.y.ToString());
64+
}
65+
66+
[Fact]
67+
public void CanMergeSimpleIntoComplex()
68+
{
69+
var o1 = new { a = 1, b = 2, c = new { x = 8, y = 9 } };
70+
var o2 = new { a = 3 };
71+
72+
var j1 = J(o1);
73+
74+
j1.AddRange(J(o2));
75+
var combined = JsonConvert.DeserializeObject<dynamic>(j1.ToJsonString());
76+
Assert.Equal("3", combined.a.ToString());
77+
Assert.Equal("2", combined.b.ToString());
78+
Assert.Equal("8", combined.c.x.ToString());
79+
Assert.Equal("9", combined.c.y.ToString());
80+
}
81+
82+
[Fact]
83+
public void CanMergeComplexIntoSimple()
84+
{
85+
var o1 = new { a = 1, b = 2, c = 3 };
86+
var o2 = new { c = new { x = 8, y = 9 } };
87+
88+
var j1 = J(o1);
89+
90+
j1.AddRange(J(o2));
91+
var combined = JsonConvert.DeserializeObject<dynamic>(j1.ToJsonString());
92+
Assert.Equal("1", combined.a.ToString());
93+
Assert.Equal("2", combined.b.ToString());
94+
Assert.Equal("8", combined.c.x.ToString());
95+
Assert.Equal("9", combined.c.y.ToString());
96+
}
97+
98+
[Fact]
99+
public void CanMergeComplexIntoComplex()
100+
{
101+
var o1 = new { a = 1, b = new { q = 10, r = 11 }, c = 3 };
102+
var o2 = new { c = new { x = 8, y = 9 } };
103+
104+
var j1 = J(o1);
105+
106+
j1.AddRange(J(o2));
107+
var combined = JsonConvert.DeserializeObject<dynamic>(j1.ToJsonString());
108+
Assert.Equal("1", combined.a.ToString());
109+
Assert.Equal("10", combined.b.q.ToString());
110+
Assert.Equal("11", combined.b.r.ToString());
111+
Assert.Equal("8", combined.c.x.ToString());
112+
Assert.Equal("9", combined.c.y.ToString());
113+
}
114+
115+
[Fact]
116+
public void CanMergeMultiLayerComplex()
117+
{
118+
var o1 = new { a = 1, b = 2, c = 3 };
119+
var o2 = new
120+
{
121+
person =
122+
new
123+
{
124+
name = new
125+
{
126+
surname = "smith", forename = "john"
127+
},
128+
age = new
129+
{
130+
quantity = 11000, unit = "days"
131+
}
132+
},
133+
id = new
134+
{
135+
style = "numeric",
136+
value = 1234
137+
}
138+
};
139+
140+
var j1 = J(o1);
141+
j1.AddRange(J(o2));
142+
var combined = JsonConvert.DeserializeObject<dynamic>(j1.ToJsonString());
143+
144+
Assert.Equal("1", combined.a.ToString());
145+
Assert.Equal("2", combined.b.ToString());
146+
Assert.Equal("3", combined.c.ToString());
147+
Assert.Equal("smith", combined.person.name.surname.ToString());
148+
Assert.Equal("john", combined.person.name.forename.ToString());
149+
Assert.Equal("11000", combined.person.age.quantity.ToString());
150+
Assert.Equal("days", combined.person.age.unit.ToString());
151+
Assert.Equal("numeric", combined.id.style.ToString());
152+
Assert.Equal("1234", combined.id.value.ToString());
153+
}
154+
155+
36156
private static JsonObject J(dynamic o)
37157
{
38158
return JsonSerializer.SerializeToNode(o);

src/Extensions/NetDaemon.Extensions.MqttEntityManager/AssuredMqttConnection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public AssuredMqttConnection(
3131
{
3232
_logger = logger;
3333

34-
_logger.LogDebug("MQTT initiating connection");
34+
_logger.LogTrace("MQTT initiating connection");
3535
_connectionTask = Task.Run(() => ConnectAsync(mqttConfig.Value, mqttFactory));
3636
}
3737

@@ -49,7 +49,7 @@ public async Task<IManagedMqttClient> GetClientAsync()
4949

5050
private async Task ConnectAsync(MqttConfiguration mqttConfig, IMqttFactoryWrapper mqttFactory)
5151
{
52-
_logger.LogDebug("Connecting to MQTT broker at {Host}:{Port}/{UserName}",
52+
_logger.LogTrace("Connecting to MQTT broker at {Host}:{Port}/{UserName}",
5353
mqttConfig.Host, mqttConfig.Port, mqttConfig.UserName);
5454

5555
var clientOptions = new ManagedMqttClientOptionsBuilder()
@@ -66,7 +66,7 @@ private async Task ConnectAsync(MqttConfiguration mqttConfig, IMqttFactoryWrappe
6666

6767
await _mqttClient.StartAsync(clientOptions);
6868

69-
_logger.LogDebug("MQTT client is ready");
69+
_logger.LogTrace("MQTT client is ready");
7070
}
7171

7272
private Task MqttClientOnDisconnectedAsync(MqttClientDisconnectedEventArgs arg)
@@ -87,7 +87,7 @@ public void Dispose()
8787
return;
8888

8989
_disposed = true;
90-
_logger.LogInformation("MQTT disconnecting");
90+
_logger.LogTrace("MQTT disconnecting");
9191
_connectionTask?.Dispose();
9292
_mqttClient?.Dispose();
9393
}

src/Extensions/NetDaemon.Extensions.MqttEntityManager/MessageSender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private async Task PublishMessage(IManagedMqttClient mqttClient, string topic, s
5252
.WithQualityOfServiceLevel(qos)
5353
.Build();
5454

55-
_logger.LogDebug("MQTT sending to {Topic}: {Message}", message.Topic, message.ConvertPayloadToString());
55+
_logger.LogTrace("MQTT sending to {Topic}: {Message}", message.Topic, message.ConvertPayloadToString());
5656

5757
try
5858
{

src/Extensions/NetDaemon.Extensions.MqttEntityManager/MessageSubscriber.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ private Task OnMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs msg)
102102
{
103103
var payload = ByteArrayHelper.SafeToString(msg.ApplicationMessage.Payload);
104104
var topic = msg.ApplicationMessage.Topic;
105-
_logger.LogDebug("Subscription received {Payload} from {Topic}", payload, topic);
105+
_logger.LogTrace("Subscription received {Payload} from {Topic}", payload, topic);
106106

107107
if (!_subscribers.ContainsKey(topic))
108-
_logger.LogDebug("No subscription for topic={Topic}", topic);
108+
_logger.LogTrace("No subscription for topic={Topic}", topic);
109109
else
110110
{
111111
_subscribers[topic].Value.OnNext(payload);
@@ -126,7 +126,7 @@ public void Dispose()
126126
_isDisposed = true;
127127
foreach (var observer in _subscribers)
128128
{
129-
_logger.LogDebug("Disposing {Topic} subscription", observer.Key);
129+
_logger.LogTrace("Disposing {Topic} subscription", observer.Key);
130130
observer.Value.Value.OnCompleted();
131131
observer.Value.Value.Dispose();
132132
}

0 commit comments

Comments
 (0)