public void CanAddItem()
        {
            // Arrange
            var device = DeviceTestData.FullDevice();

            device.Id = device.Id + "1";

            var expectedAddedTopics = device.Traits
                                      .SelectMany(trait => trait.State)
                                      .Where(x => x.Value.Topic != null)
                                      .Select(x => x.Value.Topic);

            var repository = new GoogleDeviceRepository(_logMock.Object, _messageHubMock.Object, _testFilePath);

            // Act
            repository.Add(device);
            var deviceConfigurationString = File.ReadAllText(_testFilePath);
            var result = new Dictionary <string, Device>(JsonConvert.DeserializeObject <Dictionary <string, Device> >(deviceConfigurationString));

            // Assert
            Assert.True(repository.Contains(device.Id));
            Assert.True(result.ContainsKey(device.Id));

            foreach (var expectedTopic in expectedAddedTopics)
            {
                _messageHubMock.Verify(x => x.Publish(It.Is <ConfigSubscriptionChangeEvent>(changeEvent => changeEvent.AddedSubscriptions.Contains(expectedTopic))), Times.Exactly(1));
            }
        }
        public void CanUpdateItem()
        {
            // Arrange
            var device      = DeviceTestData.FullDevice();
            var oldDeviceId = device.Id + "1";

            var expectedDeletedTopics = device.Traits
                                        .SelectMany(trait => trait.State)
                                        .Where(x => x.Value.Topic != null)
                                        .Select(x => x.Value.Topic)
                                        .ToList();

            device.Traits.Clear();
            device.Traits.Add(new DeviceTrait {
                Trait    = GoogleHome.Models.TraitType.Brightness,
                Commands = new Dictionary <string, IDictionary <string, string> >
                {
                    {
                        "action.devices.commands.BrightnessAbsolute",
                        new Dictionary <string, string>
                        {
                            {
                                "brightness",
                                "test/device/brightness/set"
                            }
                        }
                    }
                },
                State = new Dictionary <string, DeviceState>
                {
                    {
                        "brightness",
                        new DeviceState
                        {
                            Topic    = "/test/device/brightness",
                            ValueMap = null
                        }
                    }
                }
            });

            var expectedAddedTopics = device.Traits
                                      .SelectMany(trait => trait.State)
                                      .Where(x => x.Value.Topic != null)
                                      .Select(x => x.Value.Topic);

            var repository = new GoogleDeviceRepository(_logMock.Object, _messageHubMock.Object, _testFilePath);

            // Act
            repository.Update(oldDeviceId, device);
            var deviceConfigurationString = File.ReadAllText(_testFilePath);
            var result = new Dictionary <string, Device>(JsonConvert.DeserializeObject <Dictionary <string, Device> >(deviceConfigurationString));

            // Assert
            Assert.True(!repository.Contains(oldDeviceId));
            Assert.True(repository.Contains(device.Id));

            Assert.True(!result.ContainsKey(oldDeviceId));
            Assert.True(result.ContainsKey(device.Id));

            foreach (var expectedTopic in expectedAddedTopics)
            {
                _messageHubMock.Verify(x => x.Publish(It.Is <ConfigSubscriptionChangeEvent>(changeEvent => changeEvent.AddedSubscriptions.Contains(expectedTopic))), Times.Exactly(1));
            }

            foreach (var expectedTopic in expectedDeletedTopics)
            {
                _messageHubMock.Verify(x => x.Publish(It.Is <ConfigSubscriptionChangeEvent>(changeEvent => changeEvent.DeletedSubscriptions.Contains(expectedTopic))), Times.Exactly(1));
            }
        }