Exemple #1
0
        public async Task When_read_file_text_Then_returns_contetn()
        {
            _validatorsMock.Setup(m => m.FileValidator).Returns(new Mock <IFileValidator>().Object);
            _validatorsMock.Setup(m => m.StringValidator).Returns(new Mock <IStringValidator>().Object);

            var contetnt = await _sut.ReadFileText(ValidFilePath);

            Assert.AreEqual(FileContent, contetnt);
        }
        public async Task Write(TemplateModel model, string outputPath, string oroginalTemplatePath)
        {
            _stringValidator.IsNullOrWhitespace(outputPath, nameof(outputPath));
            _stringValidator.IsNullOrWhitespace(oroginalTemplatePath, nameof(oroginalTemplatePath));
            _objectValidator.IsNull(model, nameof(model));

            var orifinalFileContent = await _diskService.ReadFileText(oroginalTemplatePath);

            //This is stupid version of algorithm, should be improved in the future
            foreach (var keyValue in model.Fields)
            {
                var indexOfKey     = orifinalFileContent.IndexOf(keyValue.Key);
                var indexOfEndLine = orifinalFileContent.IndexOf(Environment.NewLine, indexOfKey);
                if (indexOfEndLine == -1)
                {
                    indexOfEndLine = orifinalFileContent.Length;
                }
                var charactersToCheck     = indexOfEndLine - indexOfKey;
                var indexOfAssign         = orifinalFileContent.IndexOf(AssignChar, indexOfKey, charactersToCheck);
                var indexOffirstCharacter = GetFirstNonWhitespace(orifinalFileContent, indexOfAssign, indexOfEndLine);
                var lengthToReplace       = indexOfEndLine - indexOffirstCharacter;
                var partToReplace         = orifinalFileContent.Substring(indexOffirstCharacter, lengthToReplace);
                orifinalFileContent = orifinalFileContent.Replace(partToReplace, keyValue.Value);
            }

            await _diskService.WriteFileText(orifinalFileContent, outputPath);
        }
        public async Task Encrypt(string sourcePath, string outputPath)
        {
            _stringValidator.IsNullOrWhitespace(sourcePath, nameof(sourcePath));
            _stringValidator.IsNullOrWhitespace(outputPath, nameof(outputPath));
            var entropyPath = GetEntropyFilePath(outputPath);

            _fileValidator.IsExist(sourcePath);
            _fileValidator.IsNotExist(outputPath);
            _fileValidator.IsNotExist(entropyPath);

            var content = await _diskService.ReadFileText(sourcePath);

            var cipher = _pbkdF2Service.Encrypt(content);

            await _diskService.WriteFileText(cipher.cipherText, outputPath);

            await _diskService.WriteFileText(cipher.entropy, entropyPath);
        }
        public async Task <ProfileModel> Read(string path)
        {
            _stringValidator.IsNullOrWhitespace(path, nameof(path));
            _fileValidator.IsExist(path);

            var fileContent = await _diskService.ReadFileText(path);

            var fields = ProcessFileHelper.ProcessContent(fileContent);

            var result = new ProfileModel(fields);

            return(result);
        }
        public async Task <ConfigurationModel> Read(string path)
        {
            _stringValidator.IsNullOrWhitespace(path, nameof(path));
            _fileValidator.IsExist(path);

            var fileContent = await _diskService.ReadFileText(path);

            var lines = fileContent.Split(Environment.NewLine).ToList();

            var result = new ConfigurationModel();

            lines.ForEach(line =>
            {
                if (!string.IsNullOrWhiteSpace(line))
                {
                    AssignConfigurationValue(result, line);
                }
            });

            return(result);
        }