1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package dev.aherscu.qa.jgiven.commons.steps;
17
18 import static dev.aherscu.qa.testing.utils.StringUtilsExtensions.*;
19 import static org.assertj.core.api.Assertions.assertThat;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.testng.Assert.*;
22
23 import java.io.*;
24 import java.nio.charset.*;
25 import java.util.function.*;
26
27 import javax.annotation.concurrent.*;
28
29 import org.apache.commons.io.*;
30 import org.hamcrest.*;
31
32 import com.google.common.collect.*;
33 import com.tngtech.jgiven.annotation.*;
34
35 import dev.aherscu.qa.jgiven.commons.formatters.*;
36 import dev.aherscu.qa.jgiven.commons.model.*;
37 import dev.aherscu.qa.jgiven.commons.utils.*;
38 import dev.aherscu.qa.testing.utils.assertions.*;
39 import lombok.*;
40 import lombok.extern.slf4j.*;
41 import net.jodah.failsafe.*;
42
43
44
45
46
47
48
49
50
51
52
53
54 @ThreadSafe
55 @Slf4j
56 @SuppressWarnings({ "boxing" })
57 public class GenericVerifications<T extends AnyScenarioType, SELF extends GenericVerifications<T, SELF>>
58 extends StageEx<SELF>
59 implements ScenarioType<T> {
60
61
62
63
64 public GenericVerifications() {
65 log.trace("then stage {} constructed", this);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80 @SafeVarargs
81 public final SELF eventually(
82 final Function<SELF, SELF> step,
83 final Policy<SELF>... additionalRetryPolicies) {
84 return eventually(new StepWithDescription<>(EMPTY, step),
85 additionalRetryPolicies);
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 @SafeVarargs
104 public final SELF eventually(final StepWithDescription<SELF> step,
105 final Policy<SELF>... additionalRetryPolicies) {
106 try {
107 return Failsafe
108 .with(Lists.asList(retryPolicy, additionalRetryPolicies))
109 .get(() -> step.apply(self()));
110 } catch (final Throwable t) {
111 log.error("eventually got {}", t.getMessage());
112 throw t;
113 }
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 @SafeVarargs
137 public final <V> SELF eventually_assert_that(
138 final java.util.function.Supplier<V> objectToBeAsserted,
139 final Matcher<V> matcher,
140 final Policy<SELF>... additionalRetryPolicies) {
141 return eventually(self -> {
142 final V value;
143 try {
144 value = objectToBeAsserted.get();
145 } catch (final Throwable t) {
146 log.trace("while evaluating got {}", t.getMessage());
147 throw t;
148 }
149 log.trace("asserting value {} against {}",
150 prettified(value),
151 matcher);
152 assertThat(value, matcher);
153 return self;
154 }, additionalRetryPolicies);
155 }
156
157
158
159
160
161
162
163
164 public SELF should_succeed(final Matcher<Boolean> matcher) {
165 assertThat(true, matcher);
166 return self();
167 }
168
169
170
171
172
173
174
175
176
177
178 @SneakyThrows(IOException.class)
179 @As("the JSON file $ should contain")
180 public SELF the_JSON_file_$_should_contain(
181 final File jsonFile,
182 @JsonAssertionsFormatter.Annotation final Iterable<? extends JsonAssertion<?>> expectedContents) {
183 try (val jsonInputStream = new FileInputStream(jsonFile)) {
184
185 val jsonVerifier = JsonAssert.with(jsonInputStream);
186 for (val pair : expectedContents) {
187 if (null == pair.getValue()) {
188 jsonVerifier.assertNotDefined(pair.getKey());
189 } else {
190 jsonVerifier.assertEquals(pair.getKey(), pair.getValue());
191 }
192 }
193 }
194 return self();
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public SELF the_file_$_should_contain(
211 final File file,
212 final String contents) {
213 assertThat(file).hasContent(contents);
214 return self();
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 @SneakyThrows(IOException.class)
231 public SELF the_file_$_should_match(
232 final File file,
233 final Matcher<String> expected) {
234 assertTrue(expected.matches(IOUtils
235 .toString(file.toURI(), StandardCharsets.UTF_8)));
236 return self();
237 }
238
239 @Override
240 protected void beforeScenarioConfigurePolling() {
241 super.beforeScenarioConfigurePolling();
242 retryPolicy.handle(AssertionError.class);
243 }
244 }