Exemplo n.º 1
0
        public void DeleteDocument_ReturnsOK()
        {
            // Arrange
            var id                  = 123;
            var documentTitle       = "dokument";
            var categoryId          = 123;
            var caseId              = 123;
            var documentDescription = "description";
            var documentPath        = "path";
            var documentContent     = "content";

            var document = new CreateDocumentDto()
            {
                DocumentId          = id,
                CaseId              = caseId,
                DocumentTitle       = documentTitle,
                CategoryId          = categoryId,
                DocumentDescription = documentDescription,
                DocumentPath        = documentPath,
                DocumentContent     = documentContent
            };

            var documentRepo = new Mock <IDocumentRepository>();

            documentRepo.Setup(x => x.SaveDocument(document));
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);
            // Act
            var result = controller.Delete(id);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
        }
Exemplo n.º 2
0
 public void Initialize()
 {
     _documentRepositoryMock = DocumentMock.GetDocumentRepositoryMock();
     _documentManipulation   = new DocumentManipulation(
         _documentRepositoryMock.Object
         );
 }
        public void DeleteDocument_ReturnsOK()
        {
            // Arrange
            int    id                  = 123;
            string documentTitle       = "dokument";
            int    categoryId          = 123;
            int    caseId              = 123;
            string documentDescription = "description";
            string documentPath        = "path";
            string documentContent     = "content";

            var document = new CreateDocumentDto()
            {
                DocumentId          = id,
                CaseId              = caseId,
                DocumentTitle       = documentTitle,
                CategoryId          = categoryId,
                DocumentDescription = documentDescription,
                DocumentPath        = documentPath,
                DocumentContent     = documentContent
            };

            var documentRepo = new Mock <IDocumentRepository>();

            documentRepo.Setup(x => x.SaveDocument(document));
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var controller           = new DocumentController(documentManipulation);

            // Act
            var result = controller.Delete(id);

            // Assert
            Assert.IsType <OkObjectResult>(result);
        }
Exemplo n.º 4
0
        public void GetNumberOfDocumentsByCase_InvalidId_ThrowException()
        {
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);

            // Act
            controller.GetNumberOfDocumentsByCase(-1);
        }
Exemplo n.º 5
0
        public void Delete_InvalidId_ThrowException()
        {
            // Arrange & Act
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);

            // Act
            controller.Delete(0);
        }
Exemplo n.º 6
0
        public void Create_BadCategoryId_ThrowsException()
        {
            // Arrange
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);

            // Act
            controller.Post(null);
        }
 public IActionResult GetDocumentsByPage([FromBody] DocumentsPagingQueryModel queryDto)
 {
     try
     {
         return(Ok(DocumentManipulation.GetDocumentsByPage(queryDto)));
     }
     catch (Exception ex)
     {
         Logger.LogError(ex.Message);
         throw new NSIException(ex.Message, DC.Exceptions.Enums.Level.Error, DC.Exceptions.Enums.ErrorType.InvalidParameter);
     }
 }
 public IActionResult Get(int id)
 {
     try
     {
         return(Ok(DocumentManipulation.GetDocumentById(id)));
     }
     catch (Exception ex)
     {
         Logger.LogError(ex.Message);
         throw new NSIException(ex.Message, DC.Exceptions.Enums.Level.Error, DC.Exceptions.Enums.ErrorType.InvalidParameter);
     }
 }
 public IActionResult Put(int id, [FromBody] DocumentDto documentDto)
 {
     try
     {
         return(Ok(DocumentManipulation.EditDocument(id, documentDto)));
     }
     catch (Exception ex)
     {
         Logger.LogError(ex.Message);
         throw new NSIException(ex.Message, DC.Exceptions.Enums.Level.Error, DC.Exceptions.Enums.ErrorType.InvalidParameter);
     }
 }
        public void GetDocumentHistoryReturnsOK()
        {
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var controller           = new DocumentController(documentManipulation);

            // Act
            var result = controller.GetDocumentHistory(1);

            // Assert
            Assert.IsType <OkObjectResult>(result);
        }
        public void GetDocumentById_ReturnsNoContent()
        {
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var controller           = new DocumentController(documentManipulation);

            // Act
            var result = controller.Get(100000);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
        }
        public void GetNumberOfDocumentsByCaseOK()
        {
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var controller           = new DocumentController(documentManipulation);

            // Act
            var result = controller.GetNumberOfDocumentsByCase(1);

            // Assert
            Assert.IsType <OkObjectResult>(result);
        }
Exemplo n.º 13
0
        public void GetNumberOfDocumentsByCaseOk()
        {
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);

            // Act
            var result = controller.GetNumberOfDocumentsByCase(1);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
        }
Exemplo n.º 14
0
        public void GetDocumentById_ReturnsNoContent()
        {
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);

            // Act
            var result = controller.Get(100000);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
        }
 public IActionResult Post([FromBody] CreateDocumentDto document)
 {
     try
     {
         var doc = DocumentManipulation.SaveDocument(document);
         return(Ok(doc));
     }
     catch (Exception ex)
     {
         Logger.LogError(ex.Message);
         throw new NSIException(ex.Message, DC.Exceptions.Enums.Level.Error, DC.Exceptions.Enums.ErrorType.InvalidParameter);
     }
 }
        public async Task <IActionResult> Upload()
        {
            try
            {
                var file = Request.Form.Files.FirstOrDefault();
                var path = await DocumentManipulation.UploadFileAsync(file);

                return(Ok(Path.Combine("localhost:59738", path)));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
                throw new Exception(ex.Message);
            }
        }
Exemplo n.º 17
0
        public void UpdateDocument_ReturnsOK()
        {
            // Arrange
            const int    id                  = 123;
            const int    fileTypeId          = 123;
            const string documentTitle       = "dokument";
            const int    categoryId          = 123;
            const int    caseId              = 123;
            const string documentDescription = "description";
            const string documentPath        = "path";
            const string documentContent     = "content";
            var          categoryName        = "category";

            var document = new DocumentDto()
            {
                DocumentId          = id,
                CaseId              = caseId,
                FileTypeId          = fileTypeId,
                DocumentTitle       = documentTitle,
                CategoryId          = categoryId,
                CategoryName        = categoryName,
                DocumentDescription = documentDescription,
                DocumentPath        = documentPath,
                DocumentContent     = documentContent,
                CreatedAt           = DateTime.Now,
                LastModified        = DateTime.Now
            };

            var documentRepo = new Mock <IDocumentRepository>();

            documentRepo.Setup(x => x.Update(It.Is <DocumentDto>(dto => id == 123))).Returns(1);
            documentRepo.Setup(x => x.GetDocument(It.IsAny <int>())).Returns(new DocumentDetails()
            {
                DocumentId = 123
            });
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);

            // Act
            var result = controller.Put(id, document);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
        }
Exemplo n.º 18
0
        public void GetDocuments_InvalidId_ThrowException()
        {
            // Arrange & Act
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);

            // Act
            var page        = -1;
            var perPage     = 100;
            var description = "";
            var pageM       = new DocumentsPagingQueryModel()
            {
                PageNumber          = page,
                ResultsPerPage      = perPage,
                SearchByDescription = description
            };

            controller.GetDocumentsByPage(pageM);
        }
        public void UpdateDocument_ReturnsOK()
        {
            // Arrange
            int    id                  = 123;
            int    fileTypeId          = 123;
            string documentTitle       = "dokument";
            int    categoryId          = 123;
            int    caseId              = 123;
            string documentDescription = "description";
            string documentPath        = "path";
            string documentContent     = "content";
            string categoryName        = "category";

            var document = new DocumentDto()
            {
                DocumentId          = id,
                CaseId              = caseId,
                FileTypeId          = fileTypeId,
                DocumentTitle       = documentTitle,
                CategoryId          = categoryId,
                CategoryName        = categoryName,
                DocumentDescription = documentDescription,
                DocumentPath        = documentPath,
                DocumentContent     = documentContent,
                CreatedAt           = DateTime.Now,
                LastModified        = DateTime.Now
            };

            var documentRepo = new Mock <IDocumentRepository>();

            documentRepo.Setup(x => x.Update(document));
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var controller           = new DocumentController(documentManipulation);

            // Act
            var result = controller.Put(id, document);

            // Assert
            Assert.IsType <OkObjectResult>(result);
        }
        public void GetDocuments_ReturnsOK()
        {
            // Arrange & Act
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var controller           = new DocumentController(documentManipulation);

            // Act
            int    page        = 1;
            int    perPage     = 100;
            string description = "";
            var    pageM       = new DocumentsPagingQueryModel()
            {
                PageNumber          = page,
                ResultsPerPage      = perPage,
                SearchByDescription = description
            };
            var result = controller.GetDocumentsByPage(pageM);

            // Assert
            Assert.IsType <OkObjectResult>(result);
        }
Exemplo n.º 21
0
        public void GetDocuments_ReturnsOK()
        {
            // Arrange & Act
            var documentRepo         = new Mock <IDocumentRepository>();
            var documentManipulation = new DocumentManipulation(documentRepo.Object);
            var logger     = new Mock <Microsoft.Extensions.Logging.ILogger <DocumentController> >().Object;
            var controller = new DocumentController(documentManipulation, logger);

            // Act
            var page        = 1;
            var perPage     = 100;
            var description = "";
            var pageM       = new DocumentsPagingQueryModel()
            {
                PageNumber          = page,
                ResultsPerPage      = perPage,
                SearchByDescription = description
            };
            var result = controller.GetDocumentsByPage(pageM);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));
        }
Exemplo n.º 22
0
        public async Task <IHttpActionResult> UploadFile(string fileName = "", string description = "")
        {
            //Check if submitted content is of MIME Multi Part Content with Form-data in it?
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                return(BadRequest("Could not find file to upload"));
            }
            //Read the content in a InMemory Muli-Part Form Data format
            var provider = await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider());

            //Get the first file
            var files        = provider.Files;
            var uploadedFile = files[0];

            fileName = Path.GetFileNameWithoutExtension(uploadedFile.Headers.ContentDisposition.FileName.Trim('"'));
            //Extract the file extention
            var extension = ExtractExtension(uploadedFile);
            //Get the file's content type
            var contentType = uploadedFile.Headers.ContentType.ToString();
            //create the full name of the image with the fileName and extension
            var imageName = string.Concat(fileName, extension);
            //Get the reference to the Blob Storage and upload the file there
            var storageConnectionString = "";

            string[] tokens        = extension.Split('.');
            string   extensionName = tokens[tokens.Length - 1];
            var      fileTypeId    = _fileTypeManipulation.GetFileIdByExtension(extensionName);

            if (this._azureActive.Equals("true"))
            {
                storageConnectionString = ConfigurationManager.AppSettings["azurestoragepath"];
                var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
                var blobClient     = storageAccount.CreateCloudBlobClient();
                var container      = blobClient.GetContainerReference("nsicontainer");
                container.CreateIfNotExists();
                var blockBlob = container.GetBlockBlobReference(imageName);
                blockBlob.Properties.ContentType = contentType;
                using (var fileStream = await uploadedFile.ReadAsStreamAsync()) //as Stream is IDisposable
                {
                    blockBlob.UploadFromStream(fileStream);
                }

                var document = new DocumentDomain
                {
                    DocumentId         = 0,
                    Name               = fileName,
                    Path               = blockBlob.Uri.ToString(),
                    FileSize           = blockBlob.StreamWriteSizeInBytes,
                    ExternalId         = Guid.NewGuid(),
                    LocationExternalId = blockBlob.Uri.ToString(),
                    DateCreated        = DateTime.Now,
                    StorageTypeId      = 1,
                    FileTypeId         = fileTypeId,
                    Description        = description
                };
                var result = _documentManipulation.CreateDocument(document);
                return(Ok(result));
            }
            else if (this._azureActive.Equals("false"))
            {
                var localFilePath = ConfigurationManager.AppSettings["localstoragepath"];
                var guid          = Guid.NewGuid();
                var fullPath      = $@"{localFilePath}\{fileName}{extension}";
                var fileSize      = 0;

                using (var fileStream = await uploadedFile.ReadAsStreamAsync())
                    using (var localFileStream = File.Create(fullPath))
                    {
                        fileStream.Seek(0, SeekOrigin.Begin);
                        fileStream.CopyTo(localFileStream);
                        fileSize = (int)fileStream.Length;
                    }

                if (!DocumentManipulation.IsSafe(fullPath))
                {
                    File.Delete(fullPath);
                    return(BadRequest("File contains malware"));
                }

                var document = new DocumentDomain
                {
                    DocumentId         = 0,
                    Name               = fileName,
                    Path               = fullPath,
                    FileSize           = fileSize,
                    ExternalId         = guid,
                    LocationExternalId = "",
                    DateCreated        = DateTime.Now,
                    StorageTypeId      = 2,
                    FileTypeId         = fileTypeId,
                    Description        = description
                };

                var result = _documentManipulation.CreateDocument(document);

                return(Ok(result));
            }
            else
            {
                return(BadRequest(DocumentMessages.UnexpectedProblem));
            }
        }