Skip to content

Commit 00c2347

Browse files
committed
Merge remote-tracking branch 'cssru/gh35_collection_service_tests'
Fix #35
2 parents e78d762 + bb1ad92 commit 00c2347

File tree

2 files changed

+246
-0
lines changed

2 files changed

+246
-0
lines changed

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

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import spock.lang.Specification
2121
import spock.lang.Unroll
2222

2323
import ru.mystamps.web.dao.JdbcCollectionDao
24+
import ru.mystamps.web.dao.dto.AddCollectionDbDto
25+
import ru.mystamps.web.service.dto.UrlEntityDto
26+
import ru.mystamps.web.util.SlugUtils
2427

2528
class CollectionServiceImplTest extends Specification {
2629
private JdbcCollectionDao collectionDao = Mock()
@@ -31,6 +34,240 @@ class CollectionServiceImplTest extends Specification {
3134
service = new CollectionServiceImpl(collectionDao)
3235
}
3336

37+
//
38+
// Tests for createCollection()
39+
//
40+
41+
def "createCollection() should throw exception when owner id is null"() {
42+
when:
43+
service.createCollection(null, 'test-owner-login')
44+
then:
45+
thrown IllegalArgumentException
46+
}
47+
48+
def "createCollection() should throw exception when owner login is null"() {
49+
when:
50+
service.createCollection(123, null)
51+
then:
52+
thrown IllegalArgumentException
53+
}
54+
55+
def "createCollection() should throw exception when owner login can't be converted to slug"() {
56+
when:
57+
service.createCollection(123, '')
58+
then:
59+
thrown IllegalArgumentException
60+
}
61+
62+
def "createCollection() should pass owner id to dao"() {
63+
given:
64+
Integer expectedOwnerId = 123
65+
when:
66+
service.createCollection(expectedOwnerId, 'test')
67+
then:
68+
1 * collectionDao.add({ AddCollectionDbDto collection ->
69+
assert collection?.ownerId == expectedOwnerId
70+
return true
71+
}) >> 100
72+
}
73+
74+
def "createCollection() should pass slugified owner login to dao"() {
75+
given:
76+
String ownerLogin = 'Test User'
77+
and:
78+
String expectedSlug = SlugUtils.slugify(ownerLogin)
79+
when:
80+
service.createCollection(123, ownerLogin)
81+
then:
82+
1 * collectionDao.add({ AddCollectionDbDto collection ->
83+
assert collection?.slug == expectedSlug
84+
return true
85+
}) >> 200
86+
}
87+
88+
//
89+
// Tests for addToCollection()
90+
//
91+
92+
def "addToCollection() should throw exception when user id is null"() {
93+
when:
94+
service.addToCollection(null, 456)
95+
then:
96+
thrown IllegalArgumentException
97+
}
98+
99+
def "addToCollection() should throw exception when series id is null"() {
100+
when:
101+
service.addToCollection(123, null)
102+
then:
103+
thrown IllegalArgumentException
104+
}
105+
106+
def "addToCollection() should find collection by user id"() {
107+
given:
108+
Integer expectedUserId = 123
109+
when:
110+
service.addToCollection(expectedUserId, 321)
111+
then:
112+
1 * collectionDao.findCollectionUrlEntityByUserId({ Integer userId ->
113+
assert userId == expectedUserId
114+
return true
115+
}) >> TestObjects.createUrlEntityDto()
116+
}
117+
118+
def "addToCollection() should add series to collection"() {
119+
given:
120+
Integer expectedSeriesId = 456
121+
and:
122+
UrlEntityDto url = TestObjects.createUrlEntityDto()
123+
and:
124+
Integer expectedCollectionId = url.getId()
125+
and:
126+
collectionDao.findCollectionUrlEntityByUserId(_ as Integer) >> url
127+
when:
128+
service.addToCollection(123, expectedSeriesId)
129+
then:
130+
1 * collectionDao.addSeriesToCollection({ Integer collectionId ->
131+
assert collectionId == expectedCollectionId
132+
return true
133+
}, { Integer seriesId ->
134+
assert seriesId == expectedSeriesId
135+
return true
136+
})
137+
}
138+
139+
def "addToCollection() should return result from dao"() {
140+
given:
141+
UrlEntityDto expectedUrl = TestObjects.createUrlEntityDto()
142+
and:
143+
collectionDao.findCollectionUrlEntityByUserId(_ as Integer) >> expectedUrl
144+
when:
145+
UrlEntityDto serviceResult = service.addToCollection(123, 456)
146+
then:
147+
serviceResult == expectedUrl
148+
}
149+
150+
//
151+
// Tests for removeFromCollection()
152+
//
153+
154+
def "removeFromCollection() should throw exception when user id is null"() {
155+
when:
156+
service.removeFromCollection(null, 123)
157+
then:
158+
thrown IllegalArgumentException
159+
}
160+
161+
def "removeFromCollection() should throw exception when series id is null"() {
162+
when:
163+
service.removeFromCollection(456, null)
164+
then:
165+
thrown IllegalArgumentException
166+
}
167+
168+
169+
def "removeFromCollection() should find collection by user id"() {
170+
given:
171+
Integer expectedUserId = 123
172+
when:
173+
service.removeFromCollection(expectedUserId, 321)
174+
then:
175+
1 * collectionDao.findCollectionUrlEntityByUserId({ Integer userId ->
176+
assert userId == expectedUserId
177+
return true
178+
}) >> TestObjects.createUrlEntityDto()
179+
}
180+
181+
def "removeFromCollection() should remove series from collection"() {
182+
given:
183+
Integer expectedSeriesId = 456
184+
and:
185+
UrlEntityDto url = TestObjects.createUrlEntityDto()
186+
and:
187+
Integer expectedCollectionId = url.getId()
188+
and:
189+
collectionDao.findCollectionUrlEntityByUserId(_ as Integer) >> url
190+
when:
191+
service.removeFromCollection(123, expectedSeriesId)
192+
then:
193+
1 * collectionDao.removeSeriesFromCollection({ Integer collectionId ->
194+
assert collectionId == expectedCollectionId
195+
return true
196+
}, { Integer seriesId ->
197+
assert seriesId == expectedSeriesId
198+
return true
199+
})
200+
}
201+
202+
def "removeFromCollection() should return result from dao"() {
203+
given:
204+
UrlEntityDto expectedResult = TestObjects.createUrlEntityDto()
205+
and:
206+
collectionDao.findCollectionUrlEntityByUserId(_ as Integer) >> expectedResult
207+
when:
208+
UrlEntityDto serviceResult = service.removeFromCollection(123, 456)
209+
then:
210+
serviceResult == expectedResult
211+
}
212+
213+
//
214+
// Tests for isSeriesInCollection()
215+
//
216+
217+
def "isSeriesInCollection() should throw exception when series id is null"() {
218+
when:
219+
service.isSeriesInCollection(100, null)
220+
then:
221+
thrown IllegalArgumentException
222+
}
223+
224+
def "isSeriesInCollection() should return false for anonymous"() {
225+
given:
226+
Integer anonymousUserId = null
227+
when:
228+
boolean serviceResult = service.isSeriesInCollection(anonymousUserId, 456)
229+
then:
230+
serviceResult == false
231+
and:
232+
0 * collectionDao.isSeriesInUserCollection(_ as Integer, _ as Integer)
233+
}
234+
235+
def "isSeriesInCollection() should pass arguments to dao"() {
236+
given:
237+
Integer expectedUserId = 123
238+
and:
239+
Integer expectedSeriesId = 456
240+
and:
241+
boolean expectedResult = true
242+
when:
243+
boolean serviceResult = service.isSeriesInCollection(expectedUserId, expectedSeriesId)
244+
then:
245+
1 * collectionDao.isSeriesInUserCollection({ Integer userId ->
246+
assert userId == expectedUserId
247+
return true
248+
}, { Integer seriesId ->
249+
assert seriesId == expectedSeriesId
250+
return true
251+
}) >> expectedResult
252+
and:
253+
serviceResult == expectedResult
254+
}
255+
256+
//
257+
// Tests for countCollectionsOfUsers()
258+
//
259+
260+
def "countCollectionsOfUsers() should call dao and return result"() {
261+
given:
262+
long expectedResult = Long.MAX_VALUE
263+
when:
264+
long serviceResult = service.countCollectionsOfUsers()
265+
then:
266+
1 * collectionDao.countCollectionsOfUsers() >> expectedResult
267+
and:
268+
serviceResult == expectedResult
269+
}
270+
34271
//
35272
// Tests for findRecentlyCreated()
36273
//

src/test/java/ru/mystamps/web/service/TestObjects.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import ru.mystamps.web.entity.UsersActivation;
3535
import ru.mystamps.web.service.dto.DbImageDto;
3636
import ru.mystamps.web.service.dto.SitemapInfoDto;
37+
import ru.mystamps.web.service.dto.UrlEntityDto;
3738
import ru.mystamps.web.util.SlugUtils;
3839
import ru.mystamps.web.validation.ValidationRules;
3940

@@ -50,6 +51,10 @@ final class TestObjects {
5051

5152
private static final String TEST_NAME = "Test Name";
5253
private static final String TEST_LOGIN = "test";
54+
55+
private static final Integer TEST_COLLECTION_ID = 456;
56+
private static final String TEST_SLUG = "test-slug";
57+
5358
// CheckStyle: ignore LineLength for next 1 line
5459
private static final String TEST_HASH = "$2a$10$Oo8A/oaKQYwt4Zi1RWGir.HHziCG267CJaqaNaNUtE/8ceysZn0za";
5560

@@ -78,6 +83,10 @@ public static UsersActivationDto createUsersActivationDto() {
7883
return new UsersActivationDto(TEST_EMAIL, new Date());
7984
}
8085

86+
public static UrlEntityDto createUrlEntityDto() {
87+
return new UrlEntityDto(TEST_COLLECTION_ID, TEST_SLUG);
88+
}
89+
8190
public static User createUser() {
8291
final Integer anyId = 777;
8392
User user = new User();

0 commit comments

Comments
 (0)