@@ -1,84 +1,141 | |||||
1 | #include "FuzzingValidators.h" |
|
1 | #include "FuzzingValidators.h" | |
|
2 | #include <Variable/Variable.h> | |||
2 |
|
3 | |||
3 | #include <QTest> |
|
4 | #include <QTest> | |
4 |
|
5 | |||
5 | #include <functional> |
|
6 | #include <functional> | |
6 |
|
7 | |||
7 | Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators") |
|
8 | Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators") | |
8 |
|
9 | |||
9 | namespace { |
|
10 | namespace { | |
10 |
|
11 | |||
|
12 | // ////////////// // | |||
|
13 | // DATA VALIDATOR // | |||
|
14 | // ////////////// // | |||
|
15 | ||||
|
16 | /// Singleton used to validate data of a variable | |||
|
17 | class DataValidatorHelper { | |||
|
18 | public: | |||
|
19 | /// @return the single instance of the helper | |||
|
20 | static DataValidatorHelper &instance(); | |||
|
21 | virtual ~DataValidatorHelper() noexcept = default; | |||
|
22 | ||||
|
23 | virtual void validate(const VariableState &variableState) const = 0; | |||
|
24 | }; | |||
|
25 | ||||
|
26 | /** | |||
|
27 | * Default implementation of @sa DataValidatorHelper | |||
|
28 | */ | |||
|
29 | class DefaultDataValidatorHelper : public DataValidatorHelper { | |||
|
30 | public: | |||
|
31 | void validate(const VariableState &variableState) const override | |||
|
32 | { | |||
|
33 | Q_UNUSED(variableState); | |||
|
34 | qCWarning(LOG_FuzzingValidators()).noquote() << "Checking variable's data... WARN: no data " | |||
|
35 | "verification is available for this server"; | |||
|
36 | } | |||
|
37 | }; | |||
|
38 | ||||
|
39 | /** | |||
|
40 | * Implementation of @sa DataValidatorHelper for the local AMDA server | |||
|
41 | */ | |||
|
42 | class LocalhostServerDataValidatorHelper : public DataValidatorHelper { | |||
|
43 | public: | |||
|
44 | void validate(const VariableState &variableState) const override | |||
|
45 | { | |||
|
46 | /// @todo: complete | |||
|
47 | } | |||
|
48 | }; | |||
|
49 | ||||
|
50 | /// Creates the @sa DataValidatorHelper according to the server passed in parameter | |||
|
51 | std::unique_ptr<DataValidatorHelper> createDataValidatorInstance(const QString &server) | |||
|
52 | { | |||
|
53 | if (server == QString{"localhost"}) { | |||
|
54 | return std::make_unique<LocalhostServerDataValidatorHelper>(); | |||
|
55 | } | |||
|
56 | else { | |||
|
57 | return std::make_unique<DefaultDataValidatorHelper>(); | |||
|
58 | } | |||
|
59 | } | |||
|
60 | ||||
|
61 | DataValidatorHelper &DataValidatorHelper::instance() | |||
|
62 | { | |||
|
63 | // Creates instance depending on the SCIQLOP_AMDA_SERVER value at compile time | |||
|
64 | static auto instance = createDataValidatorInstance(SCIQLOP_AMDA_SERVER); | |||
|
65 | return *instance; | |||
|
66 | } | |||
|
67 | ||||
11 | // /////////////// // |
|
68 | // /////////////// // | |
12 | // RANGE VALIDATOR // |
|
69 | // RANGE VALIDATOR // | |
13 | // /////////////// // |
|
70 | // /////////////// // | |
14 |
|
71 | |||
15 | /** |
|
72 | /** | |
16 | * Checks that a range of a variable matches the expected range passed as a parameter |
|
73 | * Checks that a range of a variable matches the expected range passed as a parameter | |
17 | * @param variable the variable for which to check the range |
|
74 | * @param variable the variable for which to check the range | |
18 | * @param expectedRange the expected range |
|
75 | * @param expectedRange the expected range | |
19 | * @param getVariableRangeFun the function to retrieve the range from the variable |
|
76 | * @param getVariableRangeFun the function to retrieve the range from the variable | |
20 | * @remarks if the variable is null, checks that the expected range is the invalid range |
|
77 | * @remarks if the variable is null, checks that the expected range is the invalid range | |
21 | */ |
|
78 | */ | |
22 | void validateRange(std::shared_ptr<Variable> variable, const SqpRange &expectedRange, |
|
79 | void validateRange(std::shared_ptr<Variable> variable, const SqpRange &expectedRange, | |
23 | std::function<SqpRange(const Variable &)> getVariableRangeFun) |
|
80 | std::function<SqpRange(const Variable &)> getVariableRangeFun) | |
24 | { |
|
81 | { | |
25 | auto compare = [](const auto &range, const auto &expectedRange, const auto &message) { |
|
82 | auto compare = [](const auto &range, const auto &expectedRange, const auto &message) { | |
26 | if (range == expectedRange) { |
|
83 | if (range == expectedRange) { | |
27 | qCInfo(LOG_FuzzingValidators()).noquote() << message << "OK"; |
|
84 | qCInfo(LOG_FuzzingValidators()).noquote() << message << "OK"; | |
28 | } |
|
85 | } | |
29 | else { |
|
86 | else { | |
30 | qCInfo(LOG_FuzzingValidators()).noquote() |
|
87 | qCInfo(LOG_FuzzingValidators()).noquote() | |
31 | << message << "FAIL (current range:" << range |
|
88 | << message << "FAIL (current range:" << range | |
32 | << ", expected range:" << expectedRange << ")"; |
|
89 | << ", expected range:" << expectedRange << ")"; | |
33 | QFAIL(""); |
|
90 | QFAIL(""); | |
34 | } |
|
91 | } | |
35 | }; |
|
92 | }; | |
36 |
|
93 | |||
37 | if (variable) { |
|
94 | if (variable) { | |
38 | compare(getVariableRangeFun(*variable), expectedRange, "Checking variable's range..."); |
|
95 | compare(getVariableRangeFun(*variable), expectedRange, "Checking variable's range..."); | |
39 | } |
|
96 | } | |
40 | else { |
|
97 | else { | |
41 | compare(INVALID_RANGE, expectedRange, "Checking that there is no range set..."); |
|
98 | compare(INVALID_RANGE, expectedRange, "Checking that there is no range set..."); | |
42 | } |
|
99 | } | |
43 | } |
|
100 | } | |
44 |
|
101 | |||
45 | /** |
|
102 | /** | |
46 | * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its |
|
103 | * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its | |
47 | * construction a function of validation which is called in the validate() method |
|
104 | * construction a function of validation which is called in the validate() method | |
48 | */ |
|
105 | */ | |
49 | class FuzzingValidator : public IFuzzingValidator { |
|
106 | class FuzzingValidator : public IFuzzingValidator { | |
50 | public: |
|
107 | public: | |
51 | /// Signature of a validation function |
|
108 | /// Signature of a validation function | |
52 | using ValidationFunction = std::function<void(const VariableState &variableState)>; |
|
109 | using ValidationFunction = std::function<void(const VariableState &variableState)>; | |
53 |
|
110 | |||
54 | explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {} |
|
111 | explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {} | |
55 |
|
112 | |||
56 | void validate(const VariableState &variableState) const override { m_Fun(variableState); } |
|
113 | void validate(const VariableState &variableState) const override { m_Fun(variableState); } | |
57 |
|
114 | |||
58 | private: |
|
115 | private: | |
59 | ValidationFunction m_Fun; |
|
116 | ValidationFunction m_Fun; | |
60 | }; |
|
117 | }; | |
61 |
|
118 | |||
62 | } // namespace |
|
119 | } // namespace | |
63 |
|
120 | |||
64 | std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type) |
|
121 | std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type) | |
65 | { |
|
122 | { | |
66 | switch (type) { |
|
123 | switch (type) { | |
67 | case FuzzingValidatorType::DATA: |
|
124 | case FuzzingValidatorType::DATA: | |
68 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { |
|
125 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { | |
69 | /// @todo: complete |
|
126 | DataValidatorHelper::instance().validate(variableState); | |
70 | }); |
|
127 | }); | |
71 | case FuzzingValidatorType::RANGE: |
|
128 | case FuzzingValidatorType::RANGE: | |
72 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { |
|
129 | return std::make_unique<FuzzingValidator>([](const VariableState &variableState) { | |
73 | auto getVariableRange = [](const Variable &variable) { return variable.range(); }; |
|
130 | auto getVariableRange = [](const Variable &variable) { return variable.range(); }; | |
74 | validateRange(variableState.m_Variable, variableState.m_Range, getVariableRange); |
|
131 | validateRange(variableState.m_Variable, variableState.m_Range, getVariableRange); | |
75 | }); |
|
132 | }); | |
76 | default: |
|
133 | default: | |
77 | // Default case returns invalid validator |
|
134 | // Default case returns invalid validator | |
78 | break; |
|
135 | break; | |
79 | } |
|
136 | } | |
80 |
|
137 | |||
81 | // Invalid validator |
|
138 | // Invalid validator | |
82 | return std::make_unique<FuzzingValidator>( |
|
139 | return std::make_unique<FuzzingValidator>( | |
83 | [](const VariableState &) { QFAIL("Invalid validator"); }); |
|
140 | [](const VariableState &) { QFAIL("Invalid validator"); }); | |
84 | } |
|
141 | } |
General Comments 0
You need to be logged in to leave comments.
Login now