public async void Create_ShallReturnBadRequestOnAddFailure() { var aeTitle = "AET"; var claraAeTitle = new SourceApplicationEntity { AeTitle = aeTitle, HostIp = "host" }; _repository.Setup(p => p.AddAsync(It.IsAny <SourceApplicationEntity>(), It.IsAny <CancellationToken>())).Throws(new Exception("error")); var result = await _controller.Create(claraAeTitle); var objectResult = result.Result as ObjectResult; Assert.NotNull(objectResult); var problem = objectResult.Value as ProblemDetails; Assert.NotNull(problem); Assert.Equal("Error adding new Source Application Entity.", problem.Title); Assert.Equal($"error", problem.Detail); Assert.Equal((int)HttpStatusCode.InternalServerError, problem.Status); _repository.Verify(p => p.AddAsync(It.IsAny <SourceApplicationEntity>(), It.IsAny <CancellationToken>()), Times.Once()); }
public async void Create_ShallPropagateErrorBackToCaller() { var response = new HttpOperationResponse <object>(); response.Response = new HttpResponseMessage(HttpStatusCode.OK); response.Response.Content = new StringContent("Go!Clara!"); _kubernetesClient .Setup(p => p.CreateNamespacedCustomObjectWithHttpMessagesAsync(It.IsAny <CustomResourceDefinition>(), It.IsAny <object>())) .Throws(new HttpOperationException("error message") { Response = new HttpResponseMessageWrapper(new HttpResponseMessage(HttpStatusCode.Conflict), "error content") }); var sourceAeTitle = new SourceApplicationEntity { AeTitle = "MySCP", HostIp = "localhost" }; var result = await _controller.Create(sourceAeTitle); _kubernetesClient.Verify(p => p.CreateNamespacedCustomObjectWithHttpMessagesAsync(It.IsAny <CustomResourceDefinition>(), It.IsAny <object>()), Times.Once()); Assert.NotNull(result); var objectResult = result.Result as ObjectResult; Assert.NotNull(objectResult); var problem = objectResult.Value as ProblemDetails; Assert.NotNull(problem); Assert.Equal("error message", problem.Detail); Assert.Equal("error content", problem.Title); Assert.Equal((int)HttpStatusCode.Conflict, problem.Status.Value); }
public async Task <ActionResult <string> > Create(SourceApplicationEntity item) { try { if (!item.IsValid(_dicomAdapterRepository.AsQueryable().Select(p => p.AeTitle), out IList <string> validationErrors)) { throw new ConfigurationException(string.Join(Environment.NewLine, validationErrors)); } await _dicomAdapterRepository.AddAsync(item); await _dicomAdapterRepository.SaveChangesAsync(); _logger.Log(LogLevel.Information, $"DICOM source added AE Title={item.AeTitle}, Host/IP={item.HostIp}."); return(CreatedAtAction(nameof(GetAeTitle), new { aeTitle = item.AeTitle }, item)); } catch (ConfigurationException ex) { return(Problem(title: "Validation error.", statusCode: (int)System.Net.HttpStatusCode.BadRequest, detail: ex.Message)); } catch (Exception ex) { _logger.Log(LogLevel.Error, ex, "Error adding new Source Application Entity."); return(Problem(title: "Error adding new Source Application Entity.", statusCode: (int)System.Net.HttpStatusCode.InternalServerError, detail: ex.Message)); } }
public async void Create_ShallReturnCreatedJson() { var mockLogger = new Mock <ILogger <AeTitleJobProcessorValidator> >(); _serviceProvider.Setup(p => p.GetService(typeof(ILogger <AeTitleJobProcessorValidator>))).Returns(mockLogger.Object); var response = new HttpOperationResponse <object>(); response.Response = new HttpResponseMessage(HttpStatusCode.OK); response.Response.Content = new StringContent("Go!Clara"); _kubernetesClient .Setup(p => p.CreateNamespacedCustomObjectWithHttpMessagesAsync(It.IsAny <CustomResourceDefinition>(), It.IsAny <object>())) .Returns(() => { return(Task.FromResult(response)); }); var claraAeTitle = new SourceApplicationEntity { AeTitle = "AET", HostIp = "localhost" }; var result = await _controller.Create(claraAeTitle); _kubernetesClient.Verify(p => p.CreateNamespacedCustomObjectWithHttpMessagesAsync(It.IsAny <CustomResourceDefinition>(), It.IsAny <object>()), Times.Once()); Assert.NotNull(result); var contentResult = result.Result as ContentResult; Assert.NotNull(contentResult); Assert.Equal(response.Response.Content.AsString(), contentResult.Content); }
public async void Delete_ShallReturnProblemOnFailure() { var value = "AET"; var entity = new SourceApplicationEntity { AeTitle = value, HostIp = "host" }; _repository.Setup(p => p.FindAsync(It.IsAny <string>())).Returns(Task.FromResult(entity)); _repository.Setup(p => p.Remove(It.IsAny <SourceApplicationEntity>())).Throws(new Exception("error")); var result = await _controller.Delete(value); var objectResult = result.Result as ObjectResult; Assert.NotNull(objectResult); var problem = objectResult.Value as ProblemDetails; Assert.NotNull(problem); Assert.Equal("Error deleting Source Application Entity.", problem.Title); Assert.Equal("error", problem.Detail); Assert.Equal((int)HttpStatusCode.InternalServerError, problem.Status); _repository.Verify(p => p.FindAsync(value), Times.Once()); }
public void SourceApplicationEntity_Valid() { var sourceApplicationEntity = new SourceApplicationEntity(); sourceApplicationEntity.AeTitle = "AET"; sourceApplicationEntity.HostIp = "HOSTNAME"; Assert.True(sourceApplicationEntity.IsValid(new List <string>(), out _)); }
public void SourceApplicationEntity_InvalidWhenHostIsEmpty() { var sourceApplicationEntity = new SourceApplicationEntity(); sourceApplicationEntity.HostIp = " "; sourceApplicationEntity.AeTitle = "AET"; Assert.False(sourceApplicationEntity.IsValid(new List <string>(), out _)); }
public void SourceApplicationEntity_ShallThrowOnNull() { SourceApplicationEntity sourceApplicationEntity = null; Assert.Throws <ArgumentNullException>(() => sourceApplicationEntity.IsValid(new List <string>(), out _)); sourceApplicationEntity = new SourceApplicationEntity(); Assert.Throws <ArgumentNullException>(() => sourceApplicationEntity.IsValid(null, out _)); }
public bool IsSourceValid(SourceApplicationEntity source) { Guard.Against.Null(source, nameof(source)); var valid = true; valid &= IsAeTitleValid("DicomAdapter>dicom>scp>sources", source.AeTitle); valid &= IsValidHostNameIp($"DicomAdapter>dicom>scp>sources>{source.AeTitle}", source.HostIp); return(valid); }
public void SourceApplicationEntity_InvalidWhenAeTitleIsEmpty() { var sourceApplicationEntity = new SourceApplicationEntity(); sourceApplicationEntity.AeTitle = " "; Assert.False(sourceApplicationEntity.IsValid(new List <string>(), out _)); sourceApplicationEntity.AeTitle = "ABCDEFGHIJKLMNOPQRSTUVW"; Assert.False(sourceApplicationEntity.IsValid(new List <string>(), out _)); }
public void SourceApplicationEntity_InvalidIfAlreadyExists() { var sourceApplicationEntity = new SourceApplicationEntity(); sourceApplicationEntity.AeTitle = "AET"; sourceApplicationEntity.HostIp = "HOST"; Assert.False(sourceApplicationEntity.IsValid(new List <string>() { "AET" }, out _)); }
public async void Delete_Returns404IfNotFound() { var value = "AET"; var entity = new SourceApplicationEntity { AeTitle = value, HostIp = "host" }; _repository.Setup(p => p.FindAsync(It.IsAny <string>())).Returns(Task.FromResult(default(SourceApplicationEntity))); var result = await _controller.Delete(value); Assert.IsType <NotFoundResult>(result.Result); _repository.Verify(p => p.FindAsync(value), Times.Once()); }
public static bool IsValid(this SourceApplicationEntity sourceApplicationEntity, IEnumerable <string> existingAeTitles, out IList <string> validationErrors) { Guard.Against.Null(sourceApplicationEntity, nameof(sourceApplicationEntity)); Guard.Against.Null(existingAeTitles, nameof(existingAeTitles)); validationErrors = new List <string>(); var valid = true; valid &= IsAeTitleValid(sourceApplicationEntity.GetType().Name, sourceApplicationEntity.AeTitle, validationErrors); valid &= IsValidHostNameIp(sourceApplicationEntity.AeTitle, sourceApplicationEntity.HostIp, validationErrors); if (existingAeTitles.Any(p => p.Equals(sourceApplicationEntity.AeTitle, StringComparison.Ordinal))) { validationErrors.Add($"Source with AE Title {sourceApplicationEntity.AeTitle} already exists."); valid = false; } return(valid); }
public async void Create_ShallReturnBadRequestWHenCrdIsDisabled(string aeTitle, string hostIp) { var claraAeTitle = new SourceApplicationEntity { AeTitle = aeTitle, HostIp = hostIp }; var result = await _controller.Create(claraAeTitle); Assert.NotNull(result); var objectResult = result.Result as ObjectResult; Assert.NotNull(objectResult); var problem = objectResult.Value as ProblemDetails; Assert.NotNull(problem); Assert.Equal("Invalid source AE Title specs provided", problem.Title); Assert.Equal((int)HttpStatusCode.InternalServerError, problem.Status); }
public async void Create_ShallReturnCreatedAtAction() { var aeTitle = "AET"; var claraAeTitle = new SourceApplicationEntity { AeTitle = aeTitle, HostIp = "host", }; _repository.Setup(p => p.AddAsync(It.IsAny <SourceApplicationEntity>(), It.IsAny <CancellationToken>())); _repository.Setup(p => p.SaveChangesAsync(It.IsAny <CancellationToken>())); var result = await _controller.Create(claraAeTitle); Assert.IsType <CreatedAtActionResult>(result.Result); _repository.Verify(p => p.AddAsync(It.IsAny <SourceApplicationEntity>(), It.IsAny <CancellationToken>()), Times.Once()); _repository.Verify(p => p.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once()); }
public async void Create_ShallReturnBadRequestWithBadJobProcessType() { var aeTitle = "TOOOOOOOOOOOOOOOOOOOOOOOLONG"; var claraAeTitle = new SourceApplicationEntity { AeTitle = aeTitle, HostIp = "host" }; var result = await _controller.Create(claraAeTitle); var objectResult = result.Result as ObjectResult; Assert.NotNull(objectResult); var problem = objectResult.Value as ProblemDetails; Assert.NotNull(problem); Assert.Equal("Validation error.", problem.Title); Assert.Equal($"'{aeTitle}' is not a valid AE Title (source: SourceApplicationEntity).", problem.Detail); Assert.Equal((int)HttpStatusCode.BadRequest, problem.Status); }
public async void Create_ShallReturnServiceUnavailableWHenCrdIsDisabled() { var claraAeTitle = new SourceApplicationEntity { AeTitle = "AET", HostIp = "localhost" }; _configuration.Value.ReadAeTitlesFromCrd = false; var result = await _controller.Create(claraAeTitle); Assert.NotNull(result); var objectResult = result.Result as ObjectResult; Assert.NotNull(objectResult); var problem = objectResult.Value as ProblemDetails; Assert.NotNull(problem); Assert.Equal("Reading AE Titles from Kubernetes CRD is not enabled.", problem.Title); Assert.Equal(503, problem.Status); }
public async void Delete_ReturnsDeleted() { var value = "AET"; var entity = new SourceApplicationEntity { AeTitle = value, HostIp = "host" }; _repository.Setup(p => p.FindAsync(It.IsAny <string>())).Returns(Task.FromResult(entity)); _repository.Setup(p => p.Remove(It.IsAny <SourceApplicationEntity>())); _repository.Setup(p => p.SaveChangesAsync(It.IsAny <CancellationToken>())); var result = await _controller.Delete(value); Assert.NotNull(result.Value); Assert.Equal(value, result.Value.AeTitle); _repository.Verify(p => p.FindAsync(value), Times.Once()); _repository.Verify(p => p.Remove(entity), Times.Once()); _repository.Verify(p => p.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once()); }