Skip to content

Commit 4d140fb

Browse files
committed
CategoryServiceImpl.findCategoriesWithParents(): add unit test.
Fix #548
1 parent 676ad28 commit 4d140fb

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import spock.lang.Unroll
2929

3030
import ru.mystamps.web.dao.CategoryDao
3131
import ru.mystamps.web.dao.dto.AddCategoryDbDto
32+
import ru.mystamps.web.dao.dto.CategoryDto
3233
import ru.mystamps.web.controller.dto.AddCategoryForm
3334
import ru.mystamps.web.dao.dto.LinkEntityDto
3435
import ru.mystamps.web.tests.DateUtils
@@ -240,6 +241,26 @@ class CategoryServiceImplTest extends Specification {
240241
null | _
241242
}
242243

244+
//
245+
// Tests for findCategoriesWithParents()
246+
//
247+
248+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
249+
def 'findCategoriesWithParents() should invoke dao and return its result'() {
250+
given:
251+
String expectedLang = nullOr(Random.lang())
252+
List<CategoryDto> expectedResult = Random.listOfCategoryDto()
253+
when:
254+
List<CategoryDto> result = service.findCategoriesWithParents(expectedLang)
255+
then:
256+
1 * categoryDao.findCategoriesWithParents({ String lang ->
257+
assert lang == expectedLang
258+
return true
259+
}) >> expectedResult
260+
and:
261+
result == expectedResult
262+
}
263+
243264
//
244265
// Tests for findOneAsLinkEntity()
245266
//

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,12 @@ public static ParsedDataDto createParsedDataDto() {
201201
);
202202
}
203203

204+
public static CategoryDto createCategoryDto() {
205+
String name = Random.categoryName();
206+
String slug = SlugUtils.slugify(name);
207+
// @todo #548 Introduce name generator for top categories
208+
String parentName = Random.categoryName();
209+
return new CategoryDto(name, slug, parentName);
210+
}
211+
204212
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import io.qala.datagen.RandomShortApi;
2626

27+
import ru.mystamps.web.dao.dto.CategoryDto;
2728
import ru.mystamps.web.dao.dto.EntityWithIdDto;
2829
import ru.mystamps.web.service.TestObjects;
2930
import ru.mystamps.web.validation.ValidationRules;
@@ -132,4 +133,16 @@ public static List<EntityWithIdDto> listOfEntityWithIdDto() {
132133
);
133134
}
134135

136+
public static List<CategoryDto> listOfCategoryDto() {
137+
final int minSize = 1;
138+
final int maxSize = 3;
139+
int size = integer(minSize, maxSize);
140+
return sampleMultiple(
141+
size,
142+
TestObjects.createCategoryDto(),
143+
TestObjects.createCategoryDto(),
144+
TestObjects.createCategoryDto()
145+
);
146+
}
147+
135148
}

0 commit comments

Comments
 (0)