1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package dev.dnpm.oshelper.analyzer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
class AnalyzerUtilsTest {
private final Map<String, Object> input = Map.of("value1", 1, "valueA", "A", "valueTrue", true);
private static Set<TestTypeData> testTypeData() {
return Set.of(
new TestTypeData("value1", Integer.class).withExpectedResult(true),
new TestTypeData("valueA", String.class).withExpectedResult(true),
new TestTypeData("valueTrue", Boolean.class).withExpectedResult(true),
new TestTypeData("value1", String.class).withExpectedResult(false),
new TestTypeData("valueA", Boolean.class).withExpectedResult(false),
new TestTypeData("valueTrue", Integer.class).withExpectedResult(false),
new TestTypeData("value1", Boolean.class).withExpectedResult(false),
new TestTypeData("valueA", Integer.class).withExpectedResult(false),
new TestTypeData("valueTrue", String.class).withExpectedResult(false)
);
}
@ParameterizedTest
@MethodSource("testTypeData")
void testShouldReturnExpectedResultForTypedCheck(TestTypeData testData) {
var actual = AnalyzerUtils.requiredValuePresent(input, testData.key, testData.type);
assertThat(actual).isEqualTo(testData.result);
}
private static Set<TestMatchData> testMatchData() {
return Set.of(
new TestMatchData("value1", "[\\d]").withExpectedResult(true),
new TestMatchData("valueA", "[A-Z]").withExpectedResult(true),
new TestMatchData("value1", "[A-Z]").withExpectedResult(false),
new TestMatchData("valueA", "[a-z]").withExpectedResult(false),
new TestMatchData("valueA", "[\\d]").withExpectedResult(false)
);
}
@ParameterizedTest
@MethodSource("testMatchData")
void testShouldReturnExpectedResultForMatchCheck(TestMatchData testData) {
var actual = AnalyzerUtils.requiredValueMatches(input, testData.key, testData.regexp);
assertThat(actual).isEqualTo(testData.result);
}
@Test
void testShouldCheckIfInputValueIsIdNumber() {
assertThat(AnalyzerUtils.requiredValueIsId(Map.of("value", 0), "value")).isFalse();
assertThat(AnalyzerUtils.requiredValueIsId(Map.of("value", "ABC"), "value")).isFalse();
assertThat(AnalyzerUtils.requiredValueIsId(Map.of("value", 1234), "value")).isTrue();
}
@Test
void testShouldReturnInputValueAsOptional() {
assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", 1234), "value", Integer.class)).isEqualTo(Optional.of(1234));
assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", "ABC"), "value", String.class)).isEqualTo(Optional.of("ABC"));
assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", 1234), "value1", Integer.class)).isEmpty();
assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", "ABC"), "value1", String.class)).isEmpty();
assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", 1234), "value", String.class)).isEmpty();
assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", "ABC"), "value", Boolean.class)).isEmpty();
}
@Test
void testShouldReturnInputIdAsOptional() {
assertThat(AnalyzerUtils.getRequiredId(Map.of("value", 1234), "value")).isEqualTo(Optional.of(1234));
assertThat(AnalyzerUtils.getRequiredId(Map.of("value", 1234), "value1")).isEmpty();
assertThat(AnalyzerUtils.getRequiredId(Map.of("value", "ABC"), "value")).isEmpty();
assertThat(AnalyzerUtils.getRequiredId(Map.of("value", 0), "value")).isEmpty();
}
@Test
void testShouldReturnInputValueMatchingAsOptional() {
assertThat(AnalyzerUtils.getRequiredValueMatching(Map.of("value", 1234), "value", "[\\d]+")).isEqualTo(Optional.of("1234"));
assertThat(AnalyzerUtils.getRequiredValueMatching(Map.of("value", "ABC"), "value", "[A-Z]+")).isEqualTo(Optional.of("ABC"));
assertThat(AnalyzerUtils.getRequiredValueMatching(Map.of("value", "ABC"), "value1", "[A-Z]+")).isEmpty();
}
private static class TestTypeData {
public final String key;
public final Class<?> type;
public boolean result;
public TestTypeData(String key, Class<?> type) {
this.key = key;
this.type = type;
}
public TestTypeData withExpectedResult(boolean result) {
this.result = result;
return this;
}
@Override
public String toString() {
return String.format("key: '%s', type: %s, result: %s", key, type.getSimpleName(), result);
}
}
private static class TestMatchData {
public final String key;
public final String regexp;
public boolean result;
public TestMatchData(String key, String regexp) {
this.key = key;
this.regexp = regexp;
}
public TestMatchData withExpectedResult(boolean result) {
this.result = result;
return this;
}
@Override
public String toString() {
return String.format("key: '%s', regexp: '%s', result: %s", key, regexp, result);
}
}
}
|