/// <summary> /// Updates an entity from a model. /// </summary> /// <param name="model">The model.</param> /// <param name="entity">The entity.</param> public static void UpdateEntity(this ServiceApiDescription model, ApiDescription entity) { // reset apidocument in order to rewrite string -> Strange behaviour entity.ApiDocument = null; Mapper.Map(model, entity); }
public async Task StoreAsync(ServiceApiDescription apiDescription) { var existing = _context.Apis.SingleOrDefault(x => x.ServiceId == apiDescription.ServiceId); if (existing == null) { _logger.LogDebug("Api for {serviceId} not found in database", apiDescription.ServiceId); var entity = apiDescription.ToEntity(); _context.Apis.Add(entity); } else { _logger.LogDebug("Api for {serviceId} found in database", apiDescription.ServiceId); apiDescription.UpdateEntity(existing); } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { _logger.LogWarning("exception updating Api for {serviceId} in database: {error}", apiDescription.ServiceId, ex.Message); } }
public void Can_Convert_To_And_From_Entity() { var model = new ServiceApiDescription(); var entity = model.ToEntity(); model = entity.ToModel(); entity.Should().NotBeNull(); model.Should().NotBeNull(); }
public void Can_Write_ServiceApiDescription_To_Json() { var apiDescription = new ApiDescriptionBuilder().Build(); var json = JsonConvert.SerializeObject(apiDescription, new OpenApiDocumentJsonConverter()); var newApiDescription = ServiceApiDescription.ReadFromJson(json); newApiDescription.Should().NotBeNull(); newApiDescription.ServiceId.Should().Be("Api1"); newApiDescription.ApiDocument.Should().NotBeNull(); newApiDescription.ApiDocument.Info.Should().NotBeNull(); newApiDescription.ApiDocument.Info.Title.Should().Be("My Api"); }
/// <summary> /// Store the Api description of the given service /// </summary> /// <param name="apiDescription">The api description</param> /// <returns></returns> public async Task StoreApiAsync(ServiceApiDescription apiDescription) { var existingItem = await _apiStore.FindByServiceIdAsync(apiDescription.ServiceId); if (existingItem == null) { await _apiStore.StoreAsync(apiDescription); } else { existingItem.ApiDocument = apiDescription.ApiDocument; await _apiStore.StoreAsync(existingItem); } }
public void Can_Read_ServiceApiDescription_From_Json() { var json = new ApiDocumentBuilder().BuildAsString(); json = $"{{\"serviceId\": \"Api1\", \"apiDocument\": {json} }}"; var apiDescription = ServiceApiDescription.ReadFromJson(json); apiDescription.Should().NotBeNull(); apiDescription.ServiceId.Should().Be("Api1"); apiDescription.ApiDocument.Should().NotBeNull(); apiDescription.ApiDocument.Info.Should().NotBeNull(); apiDescription.ApiDocument.Info.Title.Should().Be("My Api"); }
public async Task Registers_New_Service_When_Not_Exists() { var description = new ApiDescriptionBuilder().WithServiceId("MyApi").Build(); _store.Setup(s => s.FindByServiceIdAsync("NewService")).ReturnsAsync((ServiceApiDescription)null); ServiceApiDescription newDescription = null; _store.Setup(s => s.StoreAsync(It.IsAny <ServiceApiDescription>())).Callback <ServiceApiDescription>(s => newDescription = s).Returns(Task.CompletedTask); await _serviceRepository.StoreApiAsync(description); newDescription.Should().NotBeNull(); newDescription.ServiceId.Should().Be("MyApi"); newDescription.ApiDocument.Should().NotBeNull(); }
/// <summary> /// Stores the api description /// </summary> /// <param name="apiDescription">The api description to store.</param> /// <returns></returns> public Task StoreAsync(ServiceApiDescription apiDescription) { var existing = _apis.Find(s => s.ServiceId == apiDescription.ServiceId); if (existing == null) { _apis.Add(apiDescription); } else { existing.ApiDocument = apiDescription.ApiDocument; } return(Task.CompletedTask); }
public async Task Returns_Registered_Api_By_Id() { var responseMessage = await _client.GetAsync("/v1/api/Api2"); responseMessage.StatusCode.Should().Be(HttpStatusCode.OK); var content = await responseMessage.Content.ReadAsStringAsync(); content.Should().NotBeNullOrWhiteSpace(); var api = ServiceApiDescription.ReadFromJson(content); api.ServiceId.Should().Be("Api2"); api.ApiDocument.Should().NotBeNull(); api.ApiDocument.Info.Should().NotBeNull(); api.ApiDocument.Info.Title.Should().Be("My Api"); }
public async Task Returns_Item_When_Api_Exists() { var model = new ServiceApiDescription { ServiceId = "Returns_Item_When_Api_Exists" }; using (var context = new RepositoryDbContext(DbContextOptions, StoreOptions)) { context.Apis.Add(model.ToEntity()); context.SaveChanges(); } using (var context = new RepositoryDbContext(DbContextOptions, StoreOptions)) { var store = new ApiStore(context, new Mock <ILogger <ApiStore> >().Object); var item = await store.FindByServiceIdAsync(model.ServiceId); item.Should().NotBeNull(); } }
public async Task Publishes_An_Api_Description() { var requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "/v1/api/api1"); requestMessage.Content = new ApiDocumentBuilder().BuildAsContent(); var responseMessage = await _client.SendAsync(requestMessage); responseMessage.StatusCode.Should().Be(HttpStatusCode.OK); // check weather service was registred and is now listed responseMessage = await _client.GetAsync("/v1/api/api1"); responseMessage.StatusCode.Should().Be(HttpStatusCode.OK); var content = await responseMessage.Content.ReadAsStringAsync(); content.Should().NotBeNullOrWhiteSpace(); var apiDescription = ServiceApiDescription.ReadFromJson(content); apiDescription.Should().NotBeNull(); apiDescription.ApiDocument.Should().NotBeNull(); apiDescription.ApiDocument.Info.Should().NotBeNull(); apiDescription.ApiDocument.Info.Title.Should().Be("My Api"); }
/// <summary> /// Maps a model to an entity. /// </summary> /// <param name="model">The model.</param> /// <returns></returns> public static ApiDescription ToEntity(this ServiceApiDescription model) { return(Mapper.Map <ApiDescription>(model)); }