/// <summary> /// Adds the mock API. /// </summary> /// <param name="mockApi">The mock API.</param> /// <returns>Mock API definition</returns> public async Task <MockApiModel> AddMockApiAsync(MockApiModel mockApi, string collectionName) { var collectionEntity = await RetrieveCollectionEntity(collectionName); if (collectionEntity == null) { throw new CollectionNotFoundException(); } var table = await this.GetTableReferenceAsync(); var mockApiEntity = new MockApiEntity(mockApi.Name, collectionEntity.CollectionName) { RouteTemplate = mockApi.RouteTemplate, Verb = mockApi.Verb.ToString(), Body = mockApi.Body, Language = mockApi.Language.ToString() }; try { var insertOperation = TableOperation.Insert(mockApiEntity); await table.ExecuteAsync(insertOperation); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 409) { throw new ResourceConflictException(); } throw; } return(mockApi); }
/// <summary> /// Adds the mock API. /// </summary> /// <param name="mockApi">The mock API.</param> /// <returns>Mock API definition</returns> public Task <MockApiModel> AddMockApiAsync(MockApiModel mockApi, string collectionName) { var collectionEntity = table.SingleOrDefault(x => x.RowKey == collectionName.ToLower() && x.PartitionKey == collectionName.ToLower()); if (collectionEntity == null) { throw new ResourceNotFoundException(); } var mockApiEntity = new MockApiEntity(mockApi.Name, collectionEntity.Name) { RouteTemplate = mockApi.RouteTemplate, Verb = mockApi.Verb.ToString(), Body = mockApi.Body, Language = mockApi.Language.ToString() }; if (table.Any(x => x.RowKey == mockApiEntity.RowKey)) { throw new ResourceConflictException(); } table.Add(mockApiEntity); return(Task.FromResult(mockApi)); }
/// <summary> /// Runs the specified API to run. /// </summary> /// <param name="apiToRun">The API to run.</param> /// <param name="hostRequestContext">The host request context.</param> /// <returns>HttpResponseMessage</returns> public HttpResponseMessage Run(MockApiModel apiToRun, HttpRequestMessage hostRequestContext) { var requestContext = new JavaScriptRequestContext(hostRequestContext); var responseContext = new JavaScriptResponseContext(); JavaScriptRuntime.Run(apiToRun.Body, requestContext, responseContext); return(responseContext.CreateHostResponse(hostRequestContext)); }
/// <summary> /// Froms the domain model. /// </summary> /// <param name="model">The model.</param> /// <returns></returns> public static MockApiResourceModel FromDomainModel(MockApiModel model) => new MockApiResourceModel() { Name = model.Name, Body = model.Body, Language = model.Language, RouteTemplate = model.RouteTemplate, Verb = model.Verb };
public async Task GetCollectionWithOneApiShouldReturnCollection() { var name = "AddTwoNumbers"; var routeTemplate = "addition/{num1}/{num2}"; var body = "function(num1, num2) { return num1+num2;}"; var verb = MockApiHttpVerb.Get; var mockApi = new MockApiModel(name, routeTemplate, body, verb, MockApiLanguage.JavaScript); await this.repo.AddMockApiAsync(mockApi, collectionName); var collection = await GetCollectionReferenceModel(); collection.Name.Should().Be(collectionName); }
public async Task GetCollectionWithTwoApiShouldReturnTwoApis() { var mockApi1 = new MockApiModel("AddTwoNumbers1", "addition1/{num1}/{num2}", "function run(num1, num2) { return num1+num2;}", MockApiHttpVerb.Get, MockApiLanguage.JavaScript); var mockApi2 = new MockApiModel("AddTwoNumbers2", "addition2/{num1}/{num2}", "function run(num1, num2) { return num1+num2;}", MockApiHttpVerb.Get, MockApiLanguage.JavaScript); await this.repo.AddMockApiAsync(mockApi1, collectionName); await this.repo.AddMockApiAsync(mockApi2, collectionName); var collection = await GetCollectionModel(); collection.Name.Should().Be(collectionName); collection.MockApis.Count.Should().Be(2); collection.MockApis.First(x => x.Name == mockApi1.Name).ShouldBeEquivalentTo(mockApi1); collection.MockApis.First(x => x.Name == mockApi2.Name).ShouldBeEquivalentTo(mockApi2); }
private async Task <MockApiModel> AddMockApi(MockApiModel mockApi) { return(await this.repo.AddMockApiAsync(mockApi, collectionName)); }