public async Task GetModelAsyncTest() { ITable table = Substitute.For <ITable>(); var modelsRegistry = new ModelsRegistry(table); var knownModelId = Guid.NewGuid(); var unknownModelId = Guid.NewGuid(); table.GetEntityAsync <ModelTableEntity>(knownModelId.ToString(), CancellationToken.None, Arg.Any <string[]>()) .Returns(Task.FromResult(new ModelTableEntity(knownModelId))); // Valid Model Id var model = await modelsRegistry.GetModelAsync(knownModelId, CancellationToken.None); Assert.IsNotNull(model); Assert.AreEqual(knownModelId, model.Id); // Invalid Model Id model = await modelsRegistry.GetModelAsync(unknownModelId, CancellationToken.None); Assert.IsNull(model); }
public async Task <IHttpActionResult> GetModel(CancellationToken cancellationToken, [FromUri] 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 read the model '{modelId}' from the registry"); ModelsRegistry modelsRegistry = WebAppContext.ModelsRegistry; Model model = await modelsRegistry.GetModelAsync(modelId.Value, cancellationToken); if (model == null) { Trace.TraceInformation($"Model with id '{modelId}' does not exists."); return(NotFound()); } return(Ok(model)); }