Skip to content

Commit 96b173b

Browse files
committed
SiteParser.isFullyInitialized(): add unit tests.
Addressed to #685 No functional changes.
1 parent 2603b18 commit 96b173b

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/main/java/ru/mystamps/web/util/extractor/SiteParser.java

+7
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@
2929
import org.jsoup.nodes.Element;
3030

3131
import lombok.AccessLevel;
32+
import lombok.Getter;
3233
import lombok.Setter;
3334

35+
// Getters/setters are being used in unit tests
36+
@Getter(AccessLevel.PROTECTED)
3437
@Setter(AccessLevel.PROTECTED)
3538
public class SiteParser {
3639
private static final Logger LOG = LoggerFactory.getLogger(SiteParser.class);
3740

41+
// When you're adding a new field don't forget to also update:
42+
// - SiteParser.setField()
43+
// - SiteParser.isFullyInitialized() (optionally)
44+
// - SiteParserTest.describe()
3845
private String name;
3946
private String matchedUrl;
4047
private String categoryLocator;

src/test/java/ru/mystamps/web/tests/Random.java

+5
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,9 @@ public static String jsoupLocator() {
170170
return sample(locators);
171171
}
172172

173+
public static String tagAttributeName() {
174+
List<String> attributes = Arrays.asList("href", "src", "data-dst");
175+
return sample(attributes);
176+
}
177+
173178
}

src/test/java/ru/mystamps/web/util/extractor/SiteParserTest.java

+108
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static io.qala.datagen.RandomValue.between;
3030
import static org.hamcrest.MatcherAssert.assertThat;
3131
import static org.hamcrest.Matchers.equalTo;
32+
import static org.hamcrest.Matchers.is;
3233

3334
public class SiteParserTest {
3435

@@ -65,6 +66,91 @@ public void setFieldShouldRequireNonBlankValue() {
6566
parser.setField(anyValidFieldName, nullOrBlank());
6667
}
6768

69+
//
70+
// Tests for isFullyInitialized()
71+
//
72+
73+
@Test
74+
public void isFullyInitializedMayBeOnlyWhenNameIsSet() {
75+
parser.setMatchedUrl(Random.url());
76+
parser.setCategoryLocator(Random.jsoupLocator());
77+
parser.setCountryLocator(Random.jsoupLocator());
78+
parser.setShortDescriptionLocator(Random.jsoupLocator());
79+
parser.setImageUrlLocator(Random.jsoupLocator());
80+
parser.setImageUrlAttribute(Random.tagAttributeName());
81+
parser.setIssueDateLocator(Random.jsoupLocator());
82+
83+
// ensure that required field is null
84+
parser.setName(null);
85+
86+
String msg = describe(parser) + " expected to be not fully initialized";
87+
assertThat(msg, parser.isFullyInitialized(), is(false));
88+
}
89+
90+
@Test
91+
public void isFullyInitializedMayBeOnlyWhenMatchedUrlIsSet() {
92+
parser.setName(Random.name());
93+
parser.setCategoryLocator(Random.jsoupLocator());
94+
parser.setCountryLocator(Random.jsoupLocator());
95+
parser.setShortDescriptionLocator(Random.jsoupLocator());
96+
parser.setImageUrlLocator(Random.jsoupLocator());
97+
parser.setImageUrlAttribute(Random.tagAttributeName());
98+
parser.setIssueDateLocator(Random.jsoupLocator());
99+
100+
// ensure that required field is null
101+
parser.setMatchedUrl(null);
102+
103+
String msg = describe(parser) + " expected to be not fully initialized";
104+
assertThat(msg, parser.isFullyInitialized(), is(false));
105+
}
106+
107+
@Test
108+
public void isFullyInitializedMayBeOnlyWhenOneOfLocatorIsSet() {
109+
parser.setName(Random.name());
110+
parser.setMatchedUrl(Random.url());
111+
parser.setImageUrlAttribute(Random.tagAttributeName());
112+
113+
// ensure that required fields are null
114+
parser.setCategoryLocator(null);
115+
parser.setCountryLocator(null);
116+
parser.setShortDescriptionLocator(null);
117+
parser.setImageUrlLocator(null);
118+
parser.setIssueDateLocator(null);
119+
120+
String msg = describe(parser) + " expected to be not fully initialized";
121+
assertThat(msg, parser.isFullyInitialized(), is(false));
122+
}
123+
124+
@Test
125+
public void isFullyInitializedWhenAllMandatoryFieldsAreSet() {
126+
parser.setName(Random.name());
127+
parser.setMatchedUrl(Random.url());
128+
129+
final int countOfFieldsWithLocator = 5;
130+
String[] locators = new String[countOfFieldsWithLocator];
131+
132+
for (int i = 0; i < locators.length; i++) {
133+
int guaranteedSetPosition = between(0, locators.length - 1).integer();
134+
if (i == guaranteedSetPosition) {
135+
locators[i] = Random.jsoupLocator();
136+
} else {
137+
locators[i] = nullOr(Random.jsoupLocator());
138+
}
139+
}
140+
141+
parser.setCategoryLocator(locators[0]);
142+
parser.setCountryLocator(locators[1]);
143+
parser.setShortDescriptionLocator(locators[2]);
144+
// CheckStyle: ignore MagicNumber for next 2 lines
145+
parser.setImageUrlLocator(locators[3]);
146+
parser.setIssueDateLocator(locators[4]);
147+
148+
parser.setImageUrlAttribute(nullOr(Random.tagAttributeName()));
149+
150+
String msg = describe(parser) + " expected to be fully initialized";
151+
assertThat(msg, parser.isFullyInitialized(), is(true));
152+
}
153+
68154
//
69155
// Tests for canParse()
70156
//
@@ -112,4 +198,26 @@ public void toStringShouldReturnName() {
112198
assertThat(parser.toString(), equalTo(expectedName));
113199
}
114200

201+
private static String describe(SiteParser parser) {
202+
StringBuilder sb = new StringBuilder();
203+
sb.append("SiteParser[name=")
204+
.append(parser.getName())
205+
.append(", matchedUrl=")
206+
.append(parser.getMatchedUrl())
207+
.append(", categoryLocator=")
208+
.append(parser.getCountryLocator())
209+
.append(", countryLocator=")
210+
.append(parser.getCountryLocator())
211+
.append(", shortDescriptionLocator=")
212+
.append(parser.getShortDescriptionLocator())
213+
.append(", imageUrlLocator=")
214+
.append(parser.getImageUrlLocator())
215+
.append(", imageUrlAttribute=")
216+
.append(parser.getImageUrlAttribute())
217+
.append(", issueDateLocator=")
218+
.append(parser.getIssueDateLocator())
219+
.append(']');
220+
return sb.toString();
221+
}
222+
115223
}

0 commit comments

Comments
 (0)