Skip to content

Commit a5ba1b6

Browse files
committed
feat(series import): add support for currency-locator.
When specified tries to extract currency from an element identified by a locator. Fallbacks to "currency-value" otherwise. Fix #979
1 parent dfbe2d4 commit a5ba1b6

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/main/java/ru/mystamps/web/feature/series/importing/extractor/JsoupSiteParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class JsoupSiteParser implements SiteParser {
5454
private String issueDateLocator;
5555
private String sellerLocator;
5656
private String priceLocator;
57+
private String currencyLocator;
5758
private String currencyValue;
5859

5960
// @todo #975 SiteParserServiceImpl: add unit tests for constructor
@@ -68,6 +69,7 @@ public JsoupSiteParser(SiteParserConfiguration cfg) {
6869
issueDateLocator = cfg.getIssueDateLocator();
6970
sellerLocator = cfg.getSellerLocator();
7071
priceLocator = cfg.getPriceLocator();
72+
currencyLocator = cfg.getCurrencyLocator();
7173
currencyValue = cfg.getCurrencyValue();
7274
}
7375

@@ -219,6 +221,13 @@ protected String extractPrice(Element body) {
219221
}
220222

221223
protected String extractCurrency(Element body) {
224+
if (currencyLocator != null) {
225+
String currency = getTextOfTheFirstElement(body, currencyLocator);
226+
if (currency != null) {
227+
return currency;
228+
}
229+
}
230+
222231
if (currencyValue == null) {
223232
return null;
224233
}

src/main/java/ru/mystamps/web/feature/series/importing/extractor/SiteParserConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class SiteParserConfiguration {
4444
private String issueDateLocator;
4545
private String sellerLocator;
4646
private String priceLocator;
47+
private String currencyLocator;
4748
private String currencyValue;
4849

4950
/* default */ SiteParserConfiguration(Map<String, String> params) {
@@ -58,6 +59,8 @@ public class SiteParserConfiguration {
5859
issueDateLocator = params.get("issue-date-locator");
5960
sellerLocator = params.get("seller-locator");
6061
priceLocator = params.get("price-locator");
62+
// @todo #979 Add integration test for import of series with currency-locator
63+
currencyLocator = params.get("currency-locator");
6164
currencyValue = params.get("currency-value");
6265
}
6366

src/test/java/ru/mystamps/web/feature/series/importing/extractor/JsoupSiteParserTest.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void parseShouldExtractSeriesInfo() {
8181
parser.setImageUrlLocator("#image-url");
8282
parser.setSellerLocator("#seller-info");
8383
parser.setPriceLocator("#price");
84-
parser.setCurrencyValue(expectedCurrency);
84+
parser.setCurrencyLocator("#currency");
8585

8686
SeriesInfo expectedInfo = new SeriesInfo();
8787
expectedInfo.setCategoryName(expectedCategory);
@@ -102,6 +102,7 @@ public void parseShouldExtractSeriesInfo() {
102102
+ "<a id='image-url' href='%s'>look at image</a>"
103103
+ "<a id='seller-info' href='%s'>%s</a>"
104104
+ "<p id='price'>%s</p>"
105+
+ "<p id='currency'>%s</p>"
105106
+ "</body>"
106107
+ "</html",
107108
expectedCategory,
@@ -110,7 +111,8 @@ public void parseShouldExtractSeriesInfo() {
110111
imageUrl,
111112
expectedSellerUrl,
112113
expectedSellerName,
113-
expectedPrice
114+
expectedPrice,
115+
expectedCurrency
114116
);
115117

116118
SeriesInfo info = parser.parse(html);
@@ -135,6 +137,7 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
135137
String expectedSellerName = Random.sellerName();
136138
String expectedSellerUrl = baseUri + sellerUrl;
137139
String expectedPrice = Random.price().toString();
140+
String expectedCurrency = Random.currency().toString();
138141

139142
parser.setMatchedUrl(baseUri);
140143
parser.setCategoryLocator("h1");
@@ -143,6 +146,7 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
143146
parser.setImageUrlLocator("a.image");
144147
parser.setSellerLocator("a.seller");
145148
parser.setPriceLocator("b");
149+
parser.setCurrencyLocator("div");
146150

147151
SeriesInfo expectedInfo = new SeriesInfo();
148152
expectedInfo.setCategoryName(expectedCategory);
@@ -152,6 +156,7 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
152156
expectedInfo.setSellerName(expectedSellerName);
153157
expectedInfo.setSellerUrl(expectedSellerUrl);
154158
expectedInfo.setPrice(expectedPrice);
159+
expectedInfo.setCurrency(expectedCurrency);
155160

156161
String html = String.format(
157162
"<html>"
@@ -162,12 +167,14 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
162167
+ "<a class='image' href='%s'>look at image</a>"
163168
+ "<a class='seller' href='%s'>%s</a>"
164169
+ "<b>%s</b>"
170+
+ "<div>%s</div>"
165171
+ "<h1>ignored</h1>"
166172
+ "<p>ignored</p>"
167173
+ "<span>ignored</span>"
168174
+ "<a class='image' href='none'>look at image</a>"
169175
+ "<a class='seller' href='none'>seller name</a>"
170176
+ "<b>ignored</b>"
177+
+ "<div>ignored</div>"
171178
+ "</body>"
172179
+ "</html",
173180
expectedCategory,
@@ -176,7 +183,8 @@ public void parseShouldExtractSeriesInfoFromFirstMatchedElements() {
176183
expectedImageUrl,
177184
expectedSellerUrl,
178185
expectedSellerName,
179-
expectedPrice
186+
expectedPrice,
187+
expectedCurrency
180188
);
181189

182190
SeriesInfo info = parser.parse(html);
@@ -645,20 +653,39 @@ public void extractPriceShouldIgnoreTextOfChildrenTags() {
645653
//
646654

647655
@Test
648-
public void extractCurrencyShouldReturnNullWhenCurrencyValueIsNotSet() {
656+
public void extractCurrencyShouldReturnNullWhenCurrencyValuesAreNotSet() {
649657
parser.setCurrencyValue(null);
658+
parser.setCurrencyLocator(null);
650659

651660
String currency = parser.extractCurrency(null);
652661

653662
assertThat(currency).isNull();
654663
}
655664

665+
@Test
666+
public void extractCurrencyShouldReturnValueOfCurrencyLocator() {
667+
String expectedValue = "CZK";
668+
669+
parser.setCurrencyLocator("#currency");
670+
parser.setCurrencyValue("RUB");
671+
672+
String html = String.format("<span id='currency'>%s</span>", expectedValue);
673+
Element doc = createDocumentFromText(html);
674+
675+
String currency = parser.extractCurrency(doc);
676+
677+
assertThat(currency).as("couldn't extract currency from '%s'", doc)
678+
.isEqualTo(expectedValue);
679+
}
680+
656681
@Test
657682
public void extractCurrencyShouldReturnCurrencyValue() {
658683
String expectedCurrency = Random.currency().toString();
659684
parser.setCurrencyValue(expectedCurrency);
685+
parser.setCurrencyLocator(Random.jsoupLocator());
686+
Element doc = createEmptyDocument();
660687

661-
String currency = parser.extractCurrency(null);
688+
String currency = parser.extractCurrency(doc);
662689

663690
assertThat(currency).isEqualTo(expectedCurrency);
664691
}

0 commit comments

Comments
 (0)