public async Task <DeviceModelScriptApiModel> PostAsync(IFormFile file) { if (file == null) { this.log.Warn("No data provided"); throw new BadRequestException("No data provided."); } if (file.ContentType != TEXT_JAVASCRIPT) { this.log.Warn("Wrong content type provided", () => new { file.ContentType }); throw new BadRequestException("Wrong content type provided."); } var deviceModelScript = new DeviceModelScript(); try { var content = this.javascriptInterpreter.Validate(file.OpenReadStream()); deviceModelScript.Content = content; deviceModelScript.Name = file.FileName; } catch (Exception e) { throw new BadRequestException(e.Message); } return(DeviceModelScriptApiModel.FromServiceModel(await this.simulationScriptService.InsertAsync(deviceModelScript))); }
public void ItCreatesDeviceModelScriptWhenSimulationScriptNotFoundInUpserting() { // Arrange var id = Guid.NewGuid().ToString(); var deviceModelScript = new DeviceModelScript { Id = id, ETag = "Etag" }; this.TheScriptDoesntExist(id); this.storage .Setup(x => x.UpdateAsync( STORAGE_COLLECTION, It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(this.BuildValueApiModel(deviceModelScript)); // Act this.target.UpsertAsync(deviceModelScript).Wait(TimeSpan.FromSeconds(30)); // Assert - the app uses PUT with given ID this.storage.Verify(x => x.UpdateAsync( STORAGE_COLLECTION, id, It.IsAny <string>(), null)); }
/// <summary> /// Create a device model script. /// </summary> public async Task <DeviceModelScript> InsertAsync(DeviceModelScript deviceModelScript) { deviceModelScript.Created = DateTimeOffset.UtcNow; deviceModelScript.Modified = deviceModelScript.Created; if (string.IsNullOrEmpty(deviceModelScript.Id)) { deviceModelScript.Id = Guid.NewGuid().ToString(); } this.log.Debug("Creating a device model script.", () => new { deviceModelScript }); try { // Note: using UpdateAsync because the service generates the ID var result = await this.storage.UpdateAsync( STORAGE_COLLECTION, deviceModelScript.Id, JsonConvert.SerializeObject(deviceModelScript), null); deviceModelScript.ETag = result.ETag; } catch (Exception e) { this.log.Error("Failed to insert new device model script into storage", () => new { deviceModelScript, e }); throw new ExternalDependencyException( "Failed to insert new device model script into storage", e); } return(deviceModelScript); }
private ValueApiModel BuildValueApiModel(DeviceModelScript deviceModelScript) { return(new ValueApiModel { Key = deviceModelScript.Id, Data = JsonConvert.SerializeObject(deviceModelScript), ETag = deviceModelScript.ETag }); }
/// <summary> /// Create or replace a device model script. /// </summary> public async Task <DeviceModelScript> UpsertAsync(DeviceModelScript deviceModelScript) { var id = deviceModelScript.Id; var eTag = deviceModelScript.ETag; try { var item = await this.GetAsync(id); if (item.ETag == eTag) { // Replace a custom device model script deviceModelScript.Created = item.Created; deviceModelScript.Modified = DateTimeOffset.UtcNow; this.log.Debug("Modifying a custom device model script.", () => new { deviceModelScript }); var result = await this.storage.UpdateAsync( STORAGE_COLLECTION, id, JsonConvert.SerializeObject(deviceModelScript), eTag); // Return the new ETag provided by the storage deviceModelScript.ETag = result.ETag; } else { this.log.Error("Invalid ETag.", () => new { CurrentETag = item.ETag, ETagProvided = eTag }); throw new ConflictingResourceException("Invalid ETag. Device model script ETag is:'" + item.ETag + "'."); } } catch (ConflictingResourceException) { throw; } catch (ResourceNotFoundException) { this.log.Info("Creating a new device model script via PUT", () => new { deviceModelScript }); var result = await this.InsertAsync(deviceModelScript); deviceModelScript.ETag = result.ETag; } catch (Exception exception) { this.log.Error("Something went wrong while upserting the device model script.", () => new { deviceModelScript }); throw new ExternalDependencyException("Failed to upsert: " + exception.Message, exception); } return(deviceModelScript); }
public void ItThrowsExternalDependencyExceptionWhenFailedFetchingDeviceModelScriptInStorage() { // Arrange var deviceModelScript = new DeviceModelScript { Id = "id", ETag = "Etag" }; // Act var ex = Record.Exception(() => this.target.UpsertAsync(deviceModelScript).Result); // Assert Assert.IsType <ExternalDependencyException>(ex.InnerException); }
public void ItFailsToUpsertWhenUnableToFetchScriptFromStorage() { // Arrange var deviceModelScript = new DeviceModelScript { Id = "id", ETag = "Etag" }; this.storage .Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())) .ThrowsAsync(new SomeException()); // Act & Assert Assert.ThrowsAsync <ExternalDependencyException>( async() => await this.target.UpsertAsync(deviceModelScript)) .Wait(Constants.TEST_TIMEOUT); }
public void ItThrowsConflictingResourceExceptionIfEtagDoesNotMatchInUpserting() { // Arrange var id = Guid.NewGuid().ToString(); var deviceModelScriptInStorage = new DeviceModelScript { Id = id, ETag = "ETag" }; this.TheScriptExists(id, deviceModelScriptInStorage); // Act & Assert var deviceModelScript = new DeviceModelScript { Id = id, ETag = "not-matching-Etag" }; Assert.ThrowsAsync <ConflictingResourceException>( async() => await this.target.UpsertAsync(deviceModelScript)) .Wait(Constants.TEST_TIMEOUT); }
public async Task <DeviceModelScriptApiModel> PutAsync( IFormFile file, string eTag, string id) { if (file == null) { this.log.Warn("No data provided"); throw new BadRequestException("No data provided."); } if (string.IsNullOrEmpty(id)) { this.log.Warn("No id provided"); throw new BadRequestException("No id provided."); } if (string.IsNullOrEmpty(eTag)) { this.log.Warn("No ETag provided"); throw new BadRequestException("No ETag provided."); } var simulationScript = new DeviceModelScript { ETag = eTag, Id = id }; try { var reader = new StreamReader(file.OpenReadStream()); simulationScript.Content = reader.ReadToEnd(); simulationScript.Name = file.FileName; } catch (Exception e) { throw new BadRequestException(e.Message); } return(DeviceModelScriptApiModel.FromServiceModel(await this.simulationScriptService.UpsertAsync(simulationScript))); }
public void ItThrowsExceptionWhenInsertDeviceModelScriptFailed() { // Arrange var deviceModelScript = new DeviceModelScript { Id = "id", ETag = "Etag" }; this.storage .Setup(x => x.UpdateAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .ThrowsAsync(new SomeException()); // Act & Assert Assert.ThrowsAsync <ExternalDependencyException>( async() => await this.target.InsertAsync(deviceModelScript)) .Wait(Constants.TEST_TIMEOUT); }
public void DeviceModelScriptsCanBeUpserted() { // Arrange var id = Guid.NewGuid().ToString(); var deviceModelScript = new DeviceModelScript { Id = id, ETag = "oldEtag" }; this.TheScriptExists(id, deviceModelScript); var updatedSimulationScript = new DeviceModelScript { Id = id, ETag = "newETag" }; this.storage .Setup(x => x.UpdateAsync( STORAGE_COLLECTION, It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(this.BuildValueApiModel(updatedSimulationScript)); // Act this.target.UpsertAsync(deviceModelScript) .Wait(Constants.TEST_TIMEOUT); // Assert this.storage.Verify(x => x.GetAsync(STORAGE_COLLECTION, id), Times.Once); this.storage.Verify(x => x.UpdateAsync( STORAGE_COLLECTION, id, It.Is <string>(json => JsonConvert.DeserializeObject <DeviceModelScript>(json).Id == id && !json.Contains("ETag")), "oldEtag"), Times.Once()); Assert.Equal(updatedSimulationScript.Id, deviceModelScript.Id); // The call to UpsertAsync() modifies the object, so the ETags will match Assert.Equal(updatedSimulationScript.ETag, deviceModelScript.ETag); }
public void ItFailsToUpsertWhenStorageUpdateFails() { // Arrange var id = Guid.NewGuid().ToString(); var deviceModelScript = new DeviceModelScript { Id = id, ETag = "Etag" }; this.TheScriptExists(id, deviceModelScript); this.storage .Setup(x => x.UpdateAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .ThrowsAsync(new SomeException()); // Act & Assert Assert.ThrowsAsync <ExternalDependencyException>( async() => await this.target.UpsertAsync(deviceModelScript)) .Wait(Constants.TEST_TIMEOUT); }
public void ItCreatesDeviceModelScriptInStorage() { // Arrange var id = Guid.NewGuid().ToString(); var eTag = Guid.NewGuid().ToString(); var deviceModelScript = new DeviceModelScript { Id = id, ETag = eTag }; this.storage .Setup(x => x.UpdateAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(this.BuildValueApiModel(deviceModelScript)); // Act DeviceModelScript result = this.target.InsertAsync(deviceModelScript).Result; // Assert Assert.NotNull(result); Assert.Equal(deviceModelScript.Id, result.Id); Assert.Equal(deviceModelScript.ETag, result.ETag); this.storage.Verify( x => x.UpdateAsync(STORAGE_COLLECTION, deviceModelScript.Id, It.IsAny <string>(), null), Times.Once()); }
private void TheScriptExists(string id, DeviceModelScript deviceModelScript) { this.storage .Setup(x => x.GetAsync(STORAGE_COLLECTION, id)) .ReturnsAsync(this.BuildValueApiModel(deviceModelScript)); }