Skip to content

Commit 4dc0953

Browse files
committed
SeriesInfoExtractorServiceImplTest.extractReleaseYear(): add unit tests.
Addressed to #686
1 parent 9550bbe commit 4dc0953

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

src/main/java/ru/mystamps/web/service/SeriesInfoExtractorServiceImpl.java

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
@RequiredArgsConstructor
3535
public class SeriesInfoExtractorServiceImpl implements SeriesInfoExtractorService {
3636

37+
// Related to RELEASE_YEAR_REGEXP and used in unit tests.
38+
protected static final int MAX_SUPPORTED_RELEASE_YEAR = 2099;
39+
3740
// Regular expression matches release year of the stamps (from 1840 till 2099).
3841
private static final Pattern RELEASE_YEAR_REGEXP =
3942
Pattern.compile("18[4-9][0-9]|19[0-9]{2}|20[0-9]{2}");

src/test/groovy/ru/mystamps/web/service/SeriesInfoExtractorServiceImplTest.groovy

+127
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717
*/
1818
package ru.mystamps.web.service
1919

20+
import static io.qala.datagen.RandomElements.from
2021
import static io.qala.datagen.RandomShortApi.nullOrBlank
22+
import static io.qala.datagen.RandomValue.between
23+
24+
import static ru.mystamps.web.service.SeriesInfoExtractorServiceImpl.MAX_SUPPORTED_RELEASE_YEAR
25+
26+
import java.time.Year
2127

2228
import org.slf4j.helpers.NOPLogger
2329

2430
import spock.lang.Specification
31+
import spock.lang.Unroll
32+
33+
import ru.mystamps.web.validation.ValidationRules
2534

2635
@SuppressWarnings(['ClassJavadoc', 'MethodName', 'NoDef', 'NoTabCharacter', 'TrailingWhitespace'])
2736
class SeriesInfoExtractorServiceImplTest extends Specification {
@@ -73,4 +82,122 @@ class SeriesInfoExtractorServiceImplTest extends Specification {
7382
year == null
7483
}
7584

85+
def 'extractReleaseYear() should extract year from XIX century'() {
86+
given:
87+
Integer expectedYear = between(ValidationRules.MIN_RELEASE_YEAR, 1899).integer()
88+
and:
89+
String fragment = String.valueOf(expectedYear)
90+
when:
91+
Integer year = service.extractReleaseYear(fragment)
92+
then:
93+
year == expectedYear
94+
}
95+
96+
def 'extractReleaseYear() should extract year from XX century'() {
97+
given:
98+
Integer expectedYear = between(1900, 1999).integer()
99+
and:
100+
String fragment = String.valueOf(expectedYear)
101+
when:
102+
Integer year = service.extractReleaseYear(fragment)
103+
then:
104+
year == expectedYear
105+
}
106+
107+
def 'extractReleaseYear() should extract year from XXI century'() {
108+
given:
109+
Integer expectedYear = between(2000, MAX_SUPPORTED_RELEASE_YEAR).integer()
110+
and:
111+
String fragment = String.valueOf(expectedYear)
112+
when:
113+
Integer year = service.extractReleaseYear(fragment)
114+
then:
115+
year == expectedYear
116+
}
117+
118+
@Unroll
119+
def 'extractReleaseYear() should extract date from "#fragment"'(String fragment) {
120+
given:
121+
Integer expectedYear = 2010 // should be in sync with examples below
122+
when:
123+
Integer year = service.extractReleaseYear(fragment)
124+
then:
125+
year == expectedYear
126+
where:
127+
fragment | _
128+
'italy 2010' | _
129+
'2010 brazil' | _
130+
'2010\t\tbrazil' | _
131+
'2010 brazil' | _
132+
'prehistoric animals 2010 congo' | _
133+
}
134+
135+
@SuppressWarnings('UnnecessaryGetter')
136+
def 'extractReleaseYear() should return the first year if there are many'() {
137+
given:
138+
Integer currentYear = Year.now().getValue()
139+
Integer expectedYear = between(ValidationRules.MIN_RELEASE_YEAR, currentYear).integer()
140+
and:
141+
Integer anotherYear = between(ValidationRules.MIN_RELEASE_YEAR, currentYear).integer()
142+
and:
143+
String fragment = String.format('%d %d', expectedYear, anotherYear)
144+
when:
145+
Integer year = service.extractReleaseYear(fragment)
146+
then:
147+
year == expectedYear
148+
}
149+
150+
@SuppressWarnings('UnnecessaryGetter')
151+
def 'extractReleaseYear() should skip invalid date'() {
152+
given:
153+
Integer unsupportedYearInPast = between(0, ValidationRules.MIN_RELEASE_YEAR - 1).integer()
154+
Integer unsupportedYearInFuture = between(MAX_SUPPORTED_RELEASE_YEAR + 1, Integer.MAX_VALUE).integer()
155+
Integer unsupportedYear = from(unsupportedYearInPast, unsupportedYearInFuture).sample()
156+
and:
157+
Integer currentYear = Year.now().getValue()
158+
Integer expectedYear = between(ValidationRules.MIN_RELEASE_YEAR, currentYear).integer()
159+
and:
160+
String fragment = String.format('%d %d', unsupportedYear, expectedYear)
161+
when:
162+
Integer year = service.extractReleaseYear(fragment)
163+
then:
164+
year == expectedYear
165+
}
166+
167+
def 'extractReleaseYear() shouldn\'t extract dates before 1840'() {
168+
given:
169+
Integer unsupportedYear = between(0, ValidationRules.MIN_RELEASE_YEAR - 1).integer()
170+
String fragment = String.valueOf(unsupportedYear)
171+
when:
172+
Integer year = service.extractReleaseYear(fragment)
173+
then:
174+
year == null
175+
}
176+
177+
def 'extractReleaseYear() shouldn\'t extract dates after 2099'() {
178+
given:
179+
Integer unsupportedYear = between(MAX_SUPPORTED_RELEASE_YEAR + 1, Integer.MAX_VALUE).integer()
180+
String fragment = String.valueOf(unsupportedYear)
181+
when:
182+
Integer year = service.extractReleaseYear(fragment)
183+
then:
184+
year == null
185+
}
186+
187+
@Unroll
188+
def 'extractReleaseYear() shouldn\'t extract date from "#fragment"'(String fragment) {
189+
when:
190+
Integer year = service.extractReleaseYear(fragment)
191+
then:
192+
year == null
193+
where:
194+
fragment | _
195+
'-2000' | _
196+
'test2000' | _
197+
'test-2000' | _
198+
'test,2000' | _
199+
'test/2000' | _
200+
'part of word2000' | _
201+
}
202+
76203
}

0 commit comments

Comments
 (0)