24
24
import org .junit .jupiter .api .BeforeEach ;
25
25
import org .junit .jupiter .api .Test ;
26
26
import ru .mystamps .web .tests .Random ;
27
+
28
+ import java .util .Arrays ;
29
+ import java .util .Collections ;
30
+ import java .util .List ;
27
31
import java .util .Locale ;
28
32
29
33
import static io .qala .datagen .RandomShortApi .nullOr ;
@@ -91,7 +95,7 @@ public void parseShouldExtractSeriesInfo() {
91
95
expectedInfo .setCategoryName (expectedCategory );
92
96
expectedInfo .setCountryName (expectedCountry );
93
97
expectedInfo .setIssueDate (expectedIssueDate );
94
- expectedInfo .setImageUrl ( expectedImageUrl );
98
+ expectedInfo .setImageUrls ( Collections . singletonList ( expectedImageUrl ) );
95
99
expectedInfo .setSellerName (expectedSellerName );
96
100
expectedInfo .setSellerUrl (expectedSellerUrl );
97
101
expectedInfo .setPrice (expectedPrice );
@@ -136,14 +140,23 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
136
140
String expectedCategory = Random .categoryName ();
137
141
String expectedCountry = Random .countryName ();
138
142
String expectedIssueDate = Random .issueYear ().toString ();
139
- String imageUrl = String .format (
140
- "/%s-%s-%s.png" ,
143
+ String firstImageUrl = String .format (
144
+ "/%s-%s-%s-1.png" ,
145
+ expectedCountry .toLowerCase (Locale .ENGLISH ),
146
+ expectedCategory .toLowerCase (Locale .ENGLISH ),
147
+ expectedIssueDate
148
+ );
149
+ String secondImageUrl = String .format (
150
+ "/%s-%s-%s-2.png" ,
141
151
expectedCountry .toLowerCase (Locale .ENGLISH ),
142
152
expectedCategory .toLowerCase (Locale .ENGLISH ),
143
153
expectedIssueDate
144
154
);
155
+ List <String > expectedImageUrls = Arrays .asList (
156
+ baseUri + firstImageUrl ,
157
+ baseUri + secondImageUrl
158
+ );
145
159
String sellerUrl = String .format ("/seller/%d/info.htm" , positiveInteger ());
146
- String expectedImageUrl = baseUri + imageUrl ;
147
160
String expectedSellerName = Random .sellerName ();
148
161
String expectedSellerUrl = baseUri + sellerUrl ;
149
162
String expectedPrice = Random .price ().toString ();
@@ -166,7 +179,8 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
166
179
expectedInfo .setCategoryName (expectedCategory );
167
180
expectedInfo .setCountryName (expectedCountry );
168
181
expectedInfo .setIssueDate (expectedIssueDate );
169
- expectedInfo .setImageUrl (expectedImageUrl );
182
+ // in case of image URLs, it should find all of them
183
+ expectedInfo .setImageUrls (expectedImageUrls );
170
184
expectedInfo .setSellerName (expectedSellerName );
171
185
expectedInfo .setSellerUrl (expectedSellerUrl );
172
186
expectedInfo .setPrice (expectedPrice );
@@ -189,7 +203,7 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
189
203
+ "<h1>ignored</h1>"
190
204
+ "<p>ignored</p>"
191
205
+ "<span>ignored</span>"
192
- + "<a class='image' href='none '>look at image</a>"
206
+ + "<a class='image' href='%s '>look at image</a>"
193
207
+ "<a class='seller' href='none'>seller name</a>"
194
208
+ "<b>ignored</b>"
195
209
+ "<div>ignored</div>"
@@ -200,13 +214,14 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
200
214
expectedCategory ,
201
215
expectedCountry ,
202
216
expectedIssueDate ,
203
- expectedImageUrl ,
217
+ firstImageUrl ,
204
218
expectedSellerUrl ,
205
219
expectedSellerName ,
206
220
expectedPrice ,
207
221
expectedCurrency ,
208
222
expectedAltPrice ,
209
- expectedAltCurrency
223
+ expectedAltCurrency ,
224
+ secondImageUrl
210
225
);
211
226
212
227
SeriesInfo info = parser .parse (html );
@@ -335,31 +350,31 @@ public void extractCountryShouldReturnTextOfShortDescriptionLocator() {
335
350
}
336
351
337
352
//
338
- // Tests for extractImageUrl ()
353
+ // Tests for extractImageUrls ()
339
354
//
340
355
341
356
@ Test
342
- public void extractImageUrlShouldReturnNullWhenLocatorIsNotSet () {
357
+ public void extractImageUrlsShouldReturnEmptyResultWhenLocatorIsNotSet () {
343
358
parser .setImageUrlLocator (null );
344
359
Element doc = createEmptyDocument ();
345
360
346
- String imageUrl = parser .extractImageUrl (doc );
361
+ List < String > imageUrls = parser .extractImageUrls (doc );
347
362
348
- assertThat (imageUrl ). isNull ();
363
+ assertThat (imageUrls ). isEmpty ();
349
364
}
350
365
351
366
@ Test
352
- public void extractImageUrlShouldReturnNullWhenElementNotFound () {
367
+ public void extractImageUrlsShouldReturnEmptyResultWhenElementNotFound () {
353
368
parser .setImageUrlLocator (Random .jsoupLocator ());
354
369
Element doc = createEmptyDocument ();
355
370
356
- String imageUrl = parser .extractImageUrl (doc );
371
+ List < String > imageUrls = parser .extractImageUrls (doc );
357
372
358
- assertThat (imageUrl ). isNull ();
373
+ assertThat (imageUrls ). isEmpty ();
359
374
}
360
375
361
376
@ Test
362
- public void extractImageUrlShouldReturnValueOfImageUrlAttribute () {
377
+ public void extractImageUrlsShouldReturnValueOfImageUrlAttribute () {
363
378
parser .setImageUrlLocator ("a" );
364
379
parser .setImageUrlAttribute ("data-full-path" );
365
380
@@ -371,14 +386,16 @@ public void extractImageUrlShouldReturnValueOfImageUrlAttribute() {
371
386
);
372
387
Element doc = createDocumentFromText (html );
373
388
374
- String imageUrl = parser .extractImageUrl (doc );
389
+ List < String > imageUrls = parser .extractImageUrls (doc );
375
390
376
- assertThat (imageUrl ).as ("couldn't extract image url from '%s'" , doc )
377
- .isEqualTo (expectedImageUrl );
391
+ assertThat (imageUrls ).as ("couldn't extract image urls from '%s'" , doc )
392
+ .hasOnlyOneElementSatisfying (
393
+ url -> assertThat (url ).isEqualTo (expectedImageUrl )
394
+ );
378
395
}
379
396
380
397
@ Test
381
- public void extractImageUrlShouldReturnValueOfHrefAttributeByDefault () {
398
+ public void extractImageUrlsShouldReturnValueOfHrefAttributeByDefault () {
382
399
parser .setImageUrlLocator ("a" );
383
400
parser .setImageUrlAttribute (null );
384
401
@@ -390,22 +407,24 @@ public void extractImageUrlShouldReturnValueOfHrefAttributeByDefault() {
390
407
);
391
408
Element doc = createDocumentFromText (html );
392
409
393
- String imageUrl = parser .extractImageUrl (doc );
410
+ List < String > imageUrls = parser .extractImageUrls (doc );
394
411
395
- assertThat (imageUrl ).as ("couldn't extract image url from '%s'" , doc )
396
- .isEqualTo (expectedImageUrl );
412
+ assertThat (imageUrls ).as ("couldn't extract image urls from '%s'" , doc )
413
+ .hasOnlyOneElementSatisfying (
414
+ url -> assertThat (url ).isEqualTo (expectedImageUrl )
415
+ );
397
416
}
398
417
399
418
@ Test
400
- public void extractImageUrlShouldReturnNullInsteadOfEmptyString () {
419
+ public void extractImageUrlsShouldIgnoreEmptyUrls () {
401
420
parser .setImageUrlLocator ("a" );
402
421
403
422
String html = "<a href=''>test</a>" ;
404
423
Element doc = createDocumentFromText (html );
405
424
406
- String imageUrl = parser .extractImageUrl (doc );
425
+ List < String > imageUrls = parser .extractImageUrls (doc );
407
426
408
- assertThat (imageUrl ). isNull ();
427
+ assertThat (imageUrls ). isEmpty ();
409
428
}
410
429
411
430
//
0 commit comments