public async void SaveDeviceRuleAsyncTest() { var deviceId = fixture.Create<string>(); var rules = fixture.Create<List<DeviceRule>>(); rules.ForEach(x => x.DeviceID = deviceId); _deviceRulesRepositoryMock.Setup(x => x.GetAllRulesForDeviceAsync(deviceId)).ReturnsAsync(rules); _deviceRulesRepositoryMock.Setup(x => x.SaveDeviceRuleAsync(It.IsNotNull<DeviceRule>())) .ReturnsAsync(new TableStorageResponse<DeviceRule>()); var newRule = new DeviceRule(); newRule.InitializeNewRule(deviceId); newRule.DataField = rules[0].DataField; var ret = await deviceRulesLogic.SaveDeviceRuleAsync(newRule); Assert.NotNull(ret.Entity); Assert.Equal(TableStorageResponseStatus.DuplicateInsert, ret.Status); newRule.InitializeNewRule(deviceId); newRule.DataField = "New data in DataField"; ret = await deviceRulesLogic.SaveDeviceRuleAsync(newRule); Assert.NotNull(ret); }
/// <summary> /// Retrieve an existing rule for editing. If none is found then a default, bare-bones rule is returned for creating new /// A new rule is not persisted until it is saved. Distinct Rules are defined by the combination key of deviceID and ruleId /// /// Use this method if you are not sure the desired rule exists /// </summary> /// <param name="deviceId"></param> /// <param name="ruleId"></param> /// <returns></returns> public async Task<DeviceRule> GetDeviceRuleOrDefaultAsync(string deviceId, string ruleId) { List<DeviceRule> rulesForDevice = await _deviceRulesRepository.GetAllRulesForDeviceAsync(deviceId); foreach(DeviceRule rule in rulesForDevice) { if(rule.RuleId == ruleId) { return rule; } } var createdRule = new DeviceRule(); createdRule.InitializeNewRule(deviceId); return createdRule; }
/// <summary> /// Generate a new rule with bare-bones configuration. This new rule can then be conigured and sent /// back through the SaveDeviceRuleAsync method to persist. /// </summary> /// <param name="deviceId"></param> /// <returns></returns> public async Task<DeviceRule> GetNewRuleAsync(string deviceId) { return await Task.Run(() => { var rule = new DeviceRule(); rule.InitializeNewRule(deviceId); return rule; }); }