public void DiscoveriesTest() { var tests = new[] { new { Q = new SlugMapping { Address = BasicAddress, Slug = BasicSlug }, Resource = new Resource { Address = BasicAddress, Start = BasicStart }, Expected = new { Address = BasicAddress, Start = BasicStart, Slug = BasicSlug } }, }; foreach (var test in tests) { var logger = new Mock <ILogger <MQTTLiason> >(); var generator = new Mock <IMQTTGenerator>(); var sharedOpts = Options.Create(new SharedOpts { Resources = new[] { test.Q }.ToList(), }); generator.Setup(x => x.BuildDiscovery(test.Q.Slug, nameof(Resource.Start), It.IsAny <System.Reflection.AssemblyName>(), false)) .Returns(new TwoMQTT.Models.MQTTDiscovery()); var mqttLiason = new MQTTLiason(logger.Object, generator.Object, sharedOpts); var results = mqttLiason.Discoveries(); var result = results.FirstOrDefault(); Assert.IsNotNull(result, "A discovery should exist."); } }
public void MapDataTest() { var tests = new[] { new { Q = new SlugMapping { MACAddress = BasicMACAddress, Slug = BasicSlug }, Resource = new Resource { Mac = BasicMACAddress, State = BasicState }, Expected = new { MACAddress = BasicMACAddress, State = BasicStateString, Slug = BasicSlug, Found = true } }, new { Q = new SlugMapping { MACAddress = BasicMACAddress, Slug = BasicSlug }, Resource = new Resource { Mac = $"{BasicMACAddress}-fake", State = BasicState }, Expected = new { MACAddress = string.Empty, State = string.Empty, Slug = string.Empty, Found = false } }, }; foreach (var test in tests) { var logger = new Mock <ILogger <MQTTLiason> >(); var generator = new Mock <IMQTTGenerator>(); var sharedOpts = Options.Create(new SharedOpts { Resources = new[] { test.Q }.ToList(), }); generator.Setup(x => x.BuildDiscovery(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <System.Reflection.AssemblyName>(), false)) .Returns(new TwoMQTT.Models.MQTTDiscovery()); generator.Setup(x => x.StateTopic(test.Q.Slug, It.IsAny <string>())) .Returns($"totes/{test.Q.Slug}/topic/{nameof(Resource.State)}"); generator.Setup(x => x.BooleanOnOff(BasicState)).Returns(BasicStateString); var mqttLiason = new MQTTLiason(logger.Object, generator.Object, sharedOpts); var results = mqttLiason.MapData(test.Resource); var actual = results.FirstOrDefault(); Assert.AreEqual(test.Expected.Found, results.Any(), "The mapping should exist if found."); if (test.Expected.Found) { Assert.IsTrue(actual.topic.Contains(test.Expected.Slug), "The topic should contain the expected MACAddress."); Assert.AreEqual(test.Expected.State, actual.payload, "The payload be the expected State."); } } }
public void MapDataTest() { var tests = new[] { new { Q = new SlugMapping { LRID = BasicLRID, Slug = BasicSlug }, Resource = new Resource { LitterRobotId = BasicLRID, UnitStatus = BasicUnitStatus }, Expected = new { LRID = BasicLRID, UnitStatus = BasicUnitStatus, Slug = BasicSlug, Found = true } }, new { Q = new SlugMapping { LRID = BasicLRID, Slug = BasicSlug }, Resource = new Resource { LitterRobotId = $"{BasicLRID}-fake", UnitStatus = BasicUnitStatus }, Expected = new { LRID = string.Empty, UnitStatus = string.Empty, Slug = string.Empty, Found = false } }, }; foreach (var test in tests) { var logger = new Mock <ILogger <MQTTLiason> >(); var generator = new Mock <IMQTTGenerator>(); var sharedOpts = Options.Create(new SharedOpts { Resources = new[] { test.Q }.ToList(), }); generator.Setup(x => x.BuildDiscovery(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <System.Reflection.AssemblyName>(), false)) .Returns(new TwoMQTT.Models.MQTTDiscovery()); generator.Setup(x => x.StateTopic(test.Q.Slug, nameof(Resource.UnitStatus))) .Returns($"totes/{test.Q.Slug}/topic/{nameof(Resource.UnitStatus)}"); var mqttLiason = new MQTTLiason(logger.Object, generator.Object, sharedOpts); var results = mqttLiason.MapData(test.Resource); var actual = results.Where(x => x.topic != null).FirstOrDefault(x => x.topic.Contains(nameof(Resource.UnitStatus))); Assert.AreEqual(test.Expected.Found, results.Any(), "The mapping should exist if found."); if (test.Expected.Found) { Assert.IsTrue(actual.topic.Contains(test.Expected.Slug), "The topic should contain the expected LRID."); Assert.AreEqual(test.Expected.UnitStatus, actual.payload, "The payload be the expected UnitStatus."); } } }