public async Task SetDefaultModelIdAsyncTest() { ITable table = Substitute.For <ITable>(); var modelsRegistry = new ModelsRegistry(table); // Scenario 1: Invalid Model // Validate set api is not called table.GetEntityAsync <ModelTableEntity>(Arg.Any <string>(), CancellationToken.None, Arg.Any <string[]>()) .Returns(Task.FromResult <ModelTableEntity>(null)); table.InsertOrReplaceEntityAsync(Arg.Any <ModelTableEntity>(), CancellationToken.None) .Returns(Task.FromResult(true)); var unknownId = Guid.NewGuid(); bool result = await modelsRegistry.SetDefaultModelIdAsync(unknownId, CancellationToken.None); Assert.IsFalse(result); await table.DidNotReceive().InsertOrReplaceEntityAsync(Arg.Any <ModelIdTableEntity>(), Arg.Any <CancellationToken>()); // Scenario 2: Valid Model but Status not Complete // Validate set api is not called var knownId = Guid.NewGuid(); table.GetEntityAsync <ModelTableEntity>(knownId.ToString(), CancellationToken.None, Arg.Any <string[]>()) .Returns( Task.FromResult(new ModelTableEntity(knownId) { ModelStatus = ModelStatus.InProgress.ToString() })); result = await modelsRegistry.SetDefaultModelIdAsync(knownId, CancellationToken.None); Assert.IsFalse(result); await table.DidNotReceive() .InsertOrReplaceEntityAsync(Arg.Any <ModelIdTableEntity>(), Arg.Any <CancellationToken>()); // Scenario 3: Valid Model with Status as Complete // Validate set api is called table.GetEntityAsync <ModelTableEntity>(knownId.ToString(), CancellationToken.None, Arg.Any <string[]>()) .Returns( Task.FromResult(new ModelTableEntity(knownId) { ModelStatus = ModelStatus.Completed.ToString() })); table.InsertOrReplaceEntityAsync(Arg.Any <ModelIdTableEntity>(), CancellationToken.None).Returns(Task.FromResult(true)); result = await modelsRegistry.SetDefaultModelIdAsync(knownId, CancellationToken.None); Assert.IsTrue(result); await table.Received(1).InsertOrReplaceEntityAsync( Arg.Is <ModelIdTableEntity>( entity => entity.RowKey == ModelsRegistry.DefaultModelIdKeyName && entity.ModelId == knownId), CancellationToken.None); }
public async Task <IHttpActionResult> SetDefaultModel(CancellationToken cancellationToken, Guid?modelId) { if (!modelId.HasValue) { var message = $"{nameof(modelId)} is not valid"; Trace.TraceVerbose(message); return(BadRequest(message)); } // set the model id to context ContextManager.ModelId = modelId; Trace.TraceVerbose($"Trying to set '{modelId}' as the default model in the registry"); ModelsRegistry modelsRegistry = WebAppContext.ModelsRegistry; bool result = await modelsRegistry.SetDefaultModelIdAsync(modelId.Value, cancellationToken); if (!result) { Trace.TraceInformation($"Failed setting model with id '{modelId}' as the default model"); return(NotFound()); } return(Ok()); }