public async Task AddReceivedFile_FileShoudBeAdded()
        {
            File addedFile          = null;
            var  fileRepositoryMock = new Mock <IFileRepository>();

            fileRepositoryMock.Setup(x => x.AddReceivedFile(It.IsAny <File>()))
            .Callback <File>(x => addedFile = x)
            .ReturnsAsync(() => Result.Success());

            var recipientRepositoryMock = new Mock <IRecipientRepository>();

            recipientRepositoryMock.Setup(x => x.AddRecipientToFolder(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>()))
            .ReturnsAsync(() => Result.Success());

            var newFolderGuid        = Guid.NewGuid();
            var folderRepositoryMock = new Mock <IFolderRepository>();

            folderRepositoryMock.Setup(x => x.AddFolder(It.IsAny <string>()))
            .ReturnsAsync(() => Result <Guid> .Success(newFolderGuid));
            var currentTimeStamp = DateTime.Now;

            var fileService = new FileService(fileRepositoryMock.Object, folderRepositoryMock.Object, recipientRepositoryMock.Object);
            var result      = await fileService.AddReceivedFile(Guid.NewGuid(), "TestFolder", "testFile", 1, 2, currentTimeStamp, "TestPath", "Localhost");

            Assert.IsNotNull(addedFile);
            Assert.AreEqual(newFolderGuid, addedFile.FolderId);
            Assert.AreEqual("testFile", addedFile.Name);
            Assert.AreEqual(1, addedFile.Size);
            Assert.AreEqual(2, addedFile.Version);
            Assert.AreEqual(currentTimeStamp, addedFile.LastModifiedDate);
            Assert.AreEqual("TestPath", addedFile.Path);
        }
        public async Task AddReceivedFile_FolderShouldBeCreatedOnFileSystem()
        {
            var folderName         = "TestFileService";
            var fileRepositoryMock = new Mock <IFileRepository>();

            fileRepositoryMock.Setup(x => x.AddReceivedFile(It.IsAny <File>()))
            .ReturnsAsync(() => Result.Success());

            var recipientRepositoryMock = new Mock <IRecipientRepository>();

            recipientRepositoryMock.Setup(x => x.AddRecipientToFolder(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>()))
            .ReturnsAsync(() => Result.Success());

            var isCorrectPassedFolder = false;
            var folderRepositoryMock  = new Mock <IFolderRepository>();

            folderRepositoryMock.Setup(x => x.AddFolder(It.IsAny <string>()))
            .Callback <string>(x =>
            {
                isCorrectPassedFolder = x == folderName;
            })
            .ReturnsAsync(() => Result <Guid> .Success(Guid.NewGuid()));

            var fileService = new FileService(fileRepositoryMock.Object, folderRepositoryMock.Object, recipientRepositoryMock.Object);
            var result      = await fileService.AddReceivedFile(Guid.NewGuid(), folderName, "testFile", 1, 1, DateTime.Now, "TestPath", "Localhost");

            Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.Combine(PathBuilder.BasePath, folderName)));
        }
        public async Task AddReceivedFile_FolderShouldBeAddedToDatabase()
        {
            var folderName         = "TestFileService";
            var fileRepositoryMock = new Mock <IFileRepository>();

            fileRepositoryMock.Setup(x => x.AddReceivedFile(It.IsAny <File>()))
            .ReturnsAsync(() => Result.Success());

            var recipientRepositoryMock = new Mock <IRecipientRepository>();

            recipientRepositoryMock.Setup(x => x.AddRecipientToFolder(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>()))
            .ReturnsAsync(() => Result.Success());

            var isCorrectPassedFolder = false;
            var folderRepositoryMock  = new Mock <IFolderRepository>();

            folderRepositoryMock.Setup(x => x.AddFolder(It.IsAny <string>()))
            .Callback <string>(x =>
            {
                isCorrectPassedFolder = x == folderName;
            })
            .ReturnsAsync(() => Result <Guid> .Success(Guid.NewGuid()));

            var fileService = new FileService(fileRepositoryMock.Object, folderRepositoryMock.Object, recipientRepositoryMock.Object);
            var result      = await fileService.AddReceivedFile(Guid.NewGuid(), folderName, "testFile", 1, 1, DateTime.Now, "TestPath", "Localhost");

            Assert.IsTrue(result.WasSuccessful);
            Assert.IsTrue(isCorrectPassedFolder);
        }
Beispiel #4
0
        public async Task <IActionResult> Post([FromBody] FileTransmissionModel model)
        {
            WorkerLog.Instance.Information($"File data received from {model.Host}");
            var result = await _fileService.AddReceivedFile(model.Id, model.FolderName, model.Name, model.Size, model.Version, model.LastModifiedDate, model.Path, model.Host);

            return(result.WasSuccessful
                        ? NoContent()
                        : StatusCode((int)HttpStatusCode.InternalServerError));
        }
            public async Task <FileTransmissionResponse> SendFile(FileTransmissionRequest request)
            {
                WorkerLog.Instance.Information($"File data received from {request.Host}");
                var result = await _fileService.AddReceivedFile(Guid.Parse(request.Id), request.FolderName, request.Name, request.Size, request.Version, request.LastModifiedDate.ToDateTime(), request.Path, request.Host);

                return(result.WasSuccessful
                                        ? _successFullFileTransmissionResponse
                                        : _failedEFileTransmissionResponse);
            }
        public async Task AddReceivedFile_RecipientShouldBeLinkedToFolder()
        {
            var fileRepositoryMock = new Mock <IFileRepository>();

            fileRepositoryMock.Setup(x => x.AddReceivedFile(It.IsAny <File>()))
            .ReturnsAsync(() => Result.Success());

            string addedHost               = null;
            string addedAddress            = null;
            Guid   addedFolderId           = Guid.Empty;
            var    recipientRepositoryMock = new Mock <IRecipientRepository>();

            recipientRepositoryMock.Setup(x => x.AddRecipientToFolder(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>()))
            .Callback <string, string, Guid>((x, y, z) =>
            {
                addedHost     = x;
                addedAddress  = y;
                addedFolderId = z;
            })
            .ReturnsAsync(() => Result.Success());

            var newFolderGuid        = Guid.NewGuid();
            var folderRepositoryMock = new Mock <IFolderRepository>();

            folderRepositoryMock.Setup(x => x.AddFolder(It.IsAny <string>()))
            .ReturnsAsync(() => Result <Guid> .Success(newFolderGuid));
            var currentTimeStamp = DateTime.Now;

            var fileService = new FileService(fileRepositoryMock.Object, folderRepositoryMock.Object, recipientRepositoryMock.Object);
            var result      = await fileService.AddReceivedFile(Guid.NewGuid(), "TestFolder", "testFile", 1, 2, currentTimeStamp, "TestPath", "Localhost");


            Assert.AreEqual(newFolderGuid, addedFolderId);
            Assert.AreEqual("Localhost", addedHost);
            Assert.AreEqual(DummyAdress.Create("Localhost"), addedAddress);
        }