Projekt

Obecné

Profil

Stáhnout (4.24 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System.IO;
2
using System.Threading.Tasks;
3
using LDClient.detection;
4
using LDClient.utils;
5
using Moq;
6
using NUnit.Framework;
7

    
8
namespace LDClientTests.detection; 
9

    
10
internal class InfoFetcherTests {
11
    private AInfoFetcher _defaultFetcher;
12
    private AInfoFetcher _fetcherWithoutPars;
13
        
14

    
15
    private readonly string[] _defaultArguments = new[] { "argument 1", "argument 2" , "argument 3"};
16
    private const uint DefaultMaxAttempts = 5;
17

    
18
    private Mock<IProcessUtils> _mockProcessUtils;
19
    private Mock<IFileUtils> _mockFileUtils;
20

    
21
    [SetUp]
22
    public void Setup() {
23
        _mockProcessUtils = new Mock<IProcessUtils>(MockBehavior.Strict);
24

    
25
        _mockFileUtils = new Mock<IFileUtils>(MockBehavior.Strict);
26
    
27

    
28
        _defaultFetcher = new T32RemFetcher(DefaultMaxAttempts, 50, "info.txt", "executable.exe",
29
            _defaultArguments, 0, 50) {
30
            FileUtils = _mockFileUtils.Object,
31
            ProcessUtils = _mockProcessUtils.Object
32
        };
33

    
34

    
35
        _fetcherWithoutPars = new T32RemFetcher(DefaultMaxAttempts, 50, "info.txt", "executable.exe",
36
            null, 0, 50) {
37
            FileUtils = _mockFileUtils.Object,
38
            ProcessUtils = _mockProcessUtils.Object
39
        };
40
    }
41

    
42
    [Test]
43
    public async Task FetchDataAsync_ExecuteAll_ExecutedAndFetched() {
44

    
45
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
46
            It.IsAny<int>(), It.IsAny<int>())).Returns(true);
47
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
48
            Returns(DebuggerInfoParserTests.CorrectFileContent.Split("\n"));
49

    
50

    
51
        bool result = await _defaultFetcher.FetchDataAsync();
52

    
53
        Assert.IsTrue(result);
54
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
55
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(_defaultArguments.Length));
56

    
57
        Assert.AreEqual(DebuggerInfoParserTests.BodySerialNumber, _defaultFetcher.BodySerialNumber);
58
        Assert.AreEqual(DebuggerInfoParserTests.HeadSerialNumber, _defaultFetcher.HeadSerialNumber);
59

    
60
    }
61

    
62

    
63

    
64
    [Test]
65
    public async Task FetchDataAsync_ExecuteNonExistentProgram_ExecutionFailed() {
66

    
67
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
68
            It.IsAny<int>(), It.IsAny<int>())).Returns(false);
69
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
70
            Returns(new []{""});
71

    
72

    
73
        bool result = await _defaultFetcher.FetchDataAsync();
74

    
75

    
76
        Assert.IsFalse(result);
77
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
78
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(1));
79
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Never);
80
    }
81

    
82
    [Test]
83
    public async Task FetchDataAsync_ExecuteWithoutParameters_NotExecuted() {
84
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
85
            It.IsAny<int>(), It.IsAny<int>())).Returns(false);
86
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
87
            Returns(new[] { "" });
88

    
89
        bool result = await _fetcherWithoutPars.FetchDataAsync();
90
            
91
        Assert.IsFalse(result);
92
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
93
            It.IsAny<int>(), It.IsAny<int>()), Times.Never);
94
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Never);
95

    
96
    }
97

    
98

    
99
    [Test]
100
    public async Task FetchDataAsync_ExecuteInfoNotCreated_FetchFailed() {
101
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
102
            It.IsAny<int>(), It.IsAny<int>())).Returns(true);
103
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).Throws(new FileNotFoundException());
104

    
105
        bool result = await _defaultFetcher.FetchDataAsync();
106
        Assert.IsFalse(result);
107

    
108
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
109
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(_defaultArguments.Length));
110
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Exactly((int)DefaultMaxAttempts));
111

    
112
    }
113

    
114

    
115

    
116

    
117
}
(2-2/3)