Skip to content

Commit e4890f4

Browse files
committed
fix(series import): extract price when a fragment contains other words (for example, currency).
1 parent c7deb47 commit e4890f4

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/main/java/ru/mystamps/web/feature/series/importing/SeriesInfoExtractorServiceImpl.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,23 +366,33 @@ public SeriesExtractedInfo extract(String pageUrl, RawParsedDataDto data) {
366366
return url;
367367
}
368368

369+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
369370
/* default */ BigDecimal extractPrice(String fragment) {
370371
if (StringUtils.isBlank(fragment)) {
371372
return null;
372373
}
373374

374375
log.debug("Determine price from '{}'", fragment);
375376

376-
try {
377-
BigDecimal price = new BigDecimal(fragment);
378-
log.debug("Price is {}", price);
379-
// @todo #695 SeriesInfoExtractorServiceImpl.extractPrice(): filter out values <= 0
380-
return price;
381-
382-
} catch (NumberFormatException ex) {
383-
log.debug("Could not extract price: {}", ex.getMessage());
384-
return null;
377+
String[] candidates = StringUtils.split(fragment, ' ');
378+
for (String candidate : candidates) {
379+
// replace comma with dot to handle 10,5 in the same way as 10.5
380+
if (candidate.contains(",")) {
381+
candidate = StringUtils.replaceChars(candidate, ',', '.');
382+
}
383+
try {
384+
BigDecimal price = new BigDecimal(candidate);
385+
log.debug("Price is {}", price);
386+
// @todo #695 SeriesInfoExtractorServiceImpl.extractPrice(): filter out values <= 0
387+
return price;
388+
389+
} catch (NumberFormatException ignored) {
390+
// continue with the next candidate
391+
}
385392
}
393+
394+
log.debug("Could not extract price from a fragment");
395+
return null;
386396
}
387397

388398
/* default */ String extractCurrency(String fragment) {

src/test/groovy/ru/mystamps/web/feature/series/importing/SeriesInfoExtractorServiceImplTest.groovy

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ class SeriesInfoExtractorServiceImplTest extends Specification {
591591
result == null
592592
}
593593

594-
def 'extractPrice() should extract price from a fragment'() {
594+
def 'extractPrice() should extract exactly specified price'() {
595595
given:
596596
BigDecimal expectedPrice = Random.price()
597597
when:
@@ -600,6 +600,19 @@ class SeriesInfoExtractorServiceImplTest extends Specification {
600600
result == expectedPrice
601601
}
602602

603+
@Unroll
604+
def 'extractPrice() should extract "10" from "#fragment"'(String fragment) {
605+
expect:
606+
service.extractPrice(fragment) == BigDecimal.TEN
607+
where:
608+
fragment | _
609+
'10 EUR' | _
610+
'10.0 EUR' | _
611+
'10.00 EUR' | _
612+
'10,00 EUR' | _
613+
'10 руб 16 коп' | _
614+
}
615+
603616
//
604617
// Tests for extractCurrency()
605618
//

0 commit comments

Comments
 (0)