public async Task HeadFile_QueryReturnsEmpty_ReturnsNull()
        {
            // Arrange
            var configRepoMock = new Mock <IConfigRepository>();

            configRepoMock.Setup((m) => m.CommandTimeout).Returns(60);
            var cxn        = new SqlConnectionWrapperMock();
            var repository = new SqlFilesRepository(cxn.Object, configRepoMock.Object);
            var guid       = new Guid("88888888888888888888888888888888");

            File[] result = { };
            cxn.SetupQueryAsync(
                "[FileStore].ReadFileHead",
                new Dictionary <string, object> {
                { "FileId", guid }
            },
                result);

            // Act
            File file = await repository.GetFileHead(guid);

            // Assert
            cxn.Verify();
            Assert.IsNull(file);
        }
        public async Task GetHeadFile_QueryReturnsFile_ReturnsFirst()
        {
            // Arrange
            var configRepoMock = new Mock <IConfigRepository>();

            configRepoMock.Setup((m) => m.CommandTimeout).Returns(60);
            var cxn        = new SqlConnectionWrapperMock();
            var repository = new SqlFilesRepository(cxn.Object, configRepoMock.Object);
            var guid       = new Guid("99999999999999999999999999999999");

            File[] result = { new File {
                                  FileName = "name", FileType = "type"
                              } };
            cxn.SetupQueryAsync(
                "[FileStore].ReadFileHead",
                new Dictionary <string, object> {
                { "FileId", guid }
            },
                result);

            // Act
            File file = await repository.GetFileHead(guid);

            // Assert
            cxn.Verify();
            Assert.AreEqual(result.First(), file);
        }
        public async Task PostFile_QueryReturnsId_ReturnsId()
        {
            // Arrange
            var configRepoMock = new Mock <IConfigRepository>();

            configRepoMock.Setup((m) => m.CommandTimeout).Returns(60);
            var  cxn        = new SqlConnectionWrapperMock();
            var  repository = new SqlFilesRepository(cxn.Object, configRepoMock.Object);
            File file       = new File {
                FileName = "name", FileType = "type"
            };
            Guid?result = new Guid("12345678901234567890123456789012");

            cxn.SetupExecuteAsync(
                "[FileStore].InsertFileHead",
                new Dictionary <string, object> {
                { "FileName", file.FileName }, { "FileType", file.FileType }, { "FileId", null }
            },
                1,
                new Dictionary <string, object> {
                { "FileId", result }
            });

            // Act
            Guid?id = await repository.PostFileHead(file);

            // Assert
            cxn.Verify();
            Assert.AreEqual(result, id);
        }
Exemple #4
0
        // Return a File object with the file info or null if the file is not found in the legacy database
        public File GetFileHead(Guid fileId)
        {
            if (fileId == Guid.Empty)
            {
                throw new ArgumentException("fileId param is empty.", nameof(fileId));
            }

            File file = new File();

            using (var sqlConnection = CreateConnection())
            {
                sqlConnection.Open();

                // get file length from the FileStream
                file.FileSize = GetFileSize((SqlConnection)sqlConnection, fileId);

                // get file name either from AttachmentVersions table or Templates table
                file.FileName = GetFileName((SqlConnection)sqlConnection, fileId);

                // get file type from AttachmentVersions table
                file.FileType = GetFileTypeFromAttachmentVersions((SqlConnection)sqlConnection, fileId, file.FileName);
            }

            file.FileId       = fileId;
            file.IsLegacyFile = true;

            // if there is no file content assume that the file does not exist in the legacy db
            return(file);
        }
        public async Task Read_File_Chunks_Success()
        {
            // This tests reading file chunks and pushing them to the output stream

            // Arrange
            var moqFilesRepo  = new Mock <IFilesRepository>();
            var moqConfigRepo = new Mock <IConfigRepository>();

            int fileChunkSize = 125;
            var contentString = GetRandomString(125);

            // set the size of the content to force two loops to retrieve total of 250 bytes
            byte[] fileStreamContent = Encoding.UTF8.GetBytes(contentString + contentString);

            var file = new File
            {
                FileId     = new Guid("22222222-2222-2222-2222-222222222222"),
                FileName   = "Test2.txt",
                StoredTime = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"),
                FileType   = "application/octet-stream",
                FileSize   = fileStreamContent.Length,
                ChunkCount = 2
            };

            var moqDbConnection = new Mock <DbConnection>();

            moqFilesRepo.Setup(t => t.CreateConnection()).Returns(moqDbConnection.Object);

            moqFilesRepo.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync(file);

            moqFilesRepo.Setup(t => t.ReadChunkContent(moqDbConnection.Object, It.IsAny <Guid>(), It.IsAny <int>())).Returns(fileStreamContent.Take <byte>(125).ToArray <byte>());

            moqConfigRepo.Setup(t => t.LegacyFileChunkSize).Returns(fileChunkSize);

            moqFilesRepo.Setup(t => t.GetFileInfo(It.IsAny <Guid>())).Returns(file);

            // Act
            SqlPushStream sqlPushStream = new SqlPushStream();

            sqlPushStream.Initialize(moqFilesRepo.Object, file.FileId);

            HttpContent responseContent = new PushStreamContent(sqlPushStream.WriteToStream, new MediaTypeHeaderValue(file.ContentType));

            Stream response = await responseContent.ReadAsStreamAsync();

            string originalContent = Encoding.UTF8.GetString(fileStreamContent);
            string resultContent   = string.Empty;

            using (var memoryStream = new MemoryStream())
            {
                response.CopyTo(memoryStream);
                resultContent = Encoding.UTF8.GetString(memoryStream.ToArray());
            }

            // Assert
            Assert.IsTrue(originalContent.Equals(resultContent));
        }
Exemple #6
0
        public async Task HeadFile_GetHeadForExistentFileStreamFile_Success()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            var file = new File
            {
                FileId       = new Guid("33333333-3333-3333-3333-333333333333"),
                FileName     = "Test3.txt",
                StoredTime   = I18NHelper.DateTimeParseExactInvariant("2015-09-05T22:57:31.7824054-04:00", "o"),
                FileType     = "text/html",
                ChunkCount   = 1,
                IsLegacyFile = true
            };

            moq.Setup(t => t.GetFileHead(It.IsAny <Guid>())).ReturnsAsync((File)null);

            moqFileStreamRepo.Setup(t => t.FileExists(file.FileId)).Returns(true);
            moqFileStreamRepo.Setup(t => t.GetFileHead(file.FileId)).Returns(file);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Method     = HttpMethod.Head
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            var actionResult = await controller.GetFileHead("33333333333333333333333333333333");

            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            HttpResponseMessage response = await actionResult.ExecuteAsync(cancellationToken);

            var content     = response.Content;
            var contentType = content.Headers.ContentType;
            var fileName    = content.Headers.ContentDisposition.FileName;
            var storedTime  = response.Headers.GetValues("Stored-Date");

            // Assert
            Assert.IsTrue(response.IsSuccessStatusCode);
            Assert.AreEqual("Test3.txt", fileName);
            Assert.AreEqual("application/octet-stream", contentType.MediaType, string.Format("Returned content type {0} does not match expected {1}", contentType, "text/html"));
            Assert.AreEqual(DateTime.Parse("2015-09-05T22:57:31.7824054-04:00"), DateTime.Parse(storedTime.First()));
        }