diff --git a/kodemy-backend/src/test/groovy/unit/TSpec.groovy b/kodemy-backend/src/test/groovy/unit/TSpec.groovy index 4599698..4df0104 100644 --- a/kodemy-backend/src/test/groovy/unit/TSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/TSpec.groovy @@ -5,12 +5,12 @@ import spock.lang.Specification class TSpec extends Specification { def "shouldTrue"() { given: - def email = "" + def email = "" when: - email = "" + email = "" then: - verifyAll { - email == "" - } + verifyAll { + email == "" + } } } diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/store/CategoryStoreSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/store/CategoryStoreSpec.groovy index 91c89f9..dd79e8a 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/store/CategoryStoreSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/store/CategoryStoreSpec.groovy @@ -17,8 +17,10 @@ class CategoryStoreSpec extends Specification { def "shouldFindCategoryById"() { given: categoryRepository.findById(CATEGORY_ID) >> Optional.of(new Category()) + when: def result = categoryStore.findById(CATEGORY_ID) + then: Assert.that(!result.isEmpty()) } @@ -26,8 +28,10 @@ class CategoryStoreSpec extends Specification { def "shouldThrowNotFoundWhenFindCategoryById"() { given: categoryRepository.findById(CATEGORY_ID) >> Optional.empty() + when: categoryStore.findById(CATEGORY_ID).get() + then: thrown(NotFound404Exception) } diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/store/GradeStoreSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/store/GradeStoreSpec.groovy index f96090c..ce0de0a 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/store/GradeStoreSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/store/GradeStoreSpec.groovy @@ -33,8 +33,10 @@ class GradeStoreSpec extends Specification { def "shouldFindAvgGradeByMaterialId"() { given: gradeRepository.findAvgGradeByMaterialId(MATERIAL_ID) >> GRADE + when: def result = gradeStore.findAvgGradeByMaterial(MATERIAL_ID).get() + then: Assert.that(result == 3.0) } @@ -51,8 +53,10 @@ class GradeStoreSpec extends Specification { >> 0L gradeRepository.countAllByMaterialIdAndValue(MATERIAL_ID, 5.0) >> 0L + when: def result = gradeStore.getGradeStats(MATERIAL_ID).get() + then: Assert.that(result == List.of(0L, 0L, 1L, 0L, 0L)) } @@ -61,8 +65,10 @@ class GradeStoreSpec extends Specification { given: def grade = new Grade() gradeRepository.save(_ as Grade) >> grade + when: def result = gradeStore.save(grade).get() + then: Assert.that(grade == result) } @@ -75,7 +81,6 @@ class GradeStoreSpec extends Specification { def user = new UserStore.User() user.id = USER_ID user.username = "name" - gradeRepository.findGradesByMaterialInDateRange( MATERIAL_ID, _ as LocalDateTime, @@ -83,6 +88,7 @@ class GradeStoreSpec extends Specification { _ as PageRequest ) >> new PageImpl(grades) userStore.findUsersById(_ as Set) >> Try.success(new ArrayList<>(List.of(user))) + when: def result = gradeStore.findGradesByMaterialInDateRange( MATERIAL_ID, @@ -90,6 +96,7 @@ class GradeStoreSpec extends Specification { Date.from(LocalDateTime.now().plusDays(1).atZone(ZoneId.systemDefault()).toInstant()), PageRequest.of(1, 1,) ).get() + then: verifyAll(result) { result._1() == new PageImpl(grades) @@ -102,7 +109,6 @@ class GradeStoreSpec extends Specification { def grades = List.of( new Grade(GRADE, USER_ID, MATERIAL_ID) ) - gradeRepository.findGradesByMaterialInDateRange( MATERIAL_ID, _ as LocalDateTime, @@ -110,6 +116,7 @@ class GradeStoreSpec extends Specification { _ as PageRequest ) >> new PageImpl(grades) userStore.findUsersById(_ as Set) >> Try.success(new ArrayList<>()) + when: gradeStore.findGradesByMaterialInDateRange( MATERIAL_ID, @@ -117,6 +124,7 @@ class GradeStoreSpec extends Specification { Date.from(LocalDateTime.now().plusDays(1).atZone(ZoneId.systemDefault()).toInstant()), PageRequest.of(1, 1,) ).get() + then: thrown(IllegalStateException) } @@ -130,8 +138,10 @@ class GradeStoreSpec extends Specification { USER_ID, "", false, false, false, true, new HashSet() )) + when: def result = gradeStore.addGrade(MATERIAL_ID, GRADE).get() + then: Assert.that(grade == result) } @@ -139,8 +149,10 @@ class GradeStoreSpec extends Specification { def "shouldThrowWhenAddGrade"() { given: materialRepository.existsById(MATERIAL_ID) >> false + when: gradeStore.addGrade(MATERIAL_ID, GRADE).get() + then: thrown(NotFound404Exception) } @@ -149,8 +161,10 @@ class GradeStoreSpec extends Specification { given: gradeRepository.findAvgGradeByMaterialsIds(_ as List) >> new HashSet() + when: def result = gradeStore.findAvgGradeByMaterialsIds(List.of(1L)).get() + then: Assert.that(result in List) } diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/store/MaterialStoreSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/store/MaterialStoreSpec.groovy index a9c1c7a..d7f7704 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/store/MaterialStoreSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/store/MaterialStoreSpec.groovy @@ -3,13 +3,9 @@ package unit.infrastructure.store import io.vavr.Tuple import io.vavr.Tuple2 import io.vavr.control.Try -import jakarta.transaction.Status -import org.apache.catalina.User -import org.hibernate.query.Page import org.spockframework.util.Assert import org.springframework.data.domain.PageImpl import org.springframework.data.domain.PageRequest -import pl.sknikod.kodemybackend.infrastructure.database.Grade import pl.sknikod.kodemybackend.infrastructure.database.Material import pl.sknikod.kodemybackend.infrastructure.database.MaterialRepository import pl.sknikod.kodemybackend.infrastructure.store.GradeStore @@ -18,17 +14,19 @@ import pl.sknikod.kodemybackend.infrastructure.store.UserStore import pl.sknikod.kodemycommons.exception.NotFound404Exception import spock.lang.Specification -class MaterialStoreSpec extends Specification{ +import java.time.LocalDateTime + +class MaterialStoreSpec extends Specification { def materialRepository = Mock(MaterialRepository) def userStore = Mock(UserStore) def gradeStore = Mock(GradeStore) - + def material = new Material() def user = new UserStore.User() - - def materialStore = new MaterialStore(materialRepository,userStore,gradeStore) - - def "shouldFindById"(){ + + def materialStore = new MaterialStore(materialRepository, userStore, gradeStore) + + def "shouldFindById"() { given: material.setId(1L) material.setUserId(1L) @@ -37,49 +35,143 @@ class MaterialStoreSpec extends Specification{ materialRepository.findById(1L) >> Optional.of(material) userStore.findById(1L) >> Try.success(user) gradeStore.findAvgGradeByMaterial(1L) >> Try.success(4.0D) - gradeStore.getGradeStats(1L) >> Try.success(new ArrayList<>(List.of(1L,2L,3L,4L,5L))) + gradeStore.getGradeStats(1L) >> Try.success(new ArrayList<>(List.of(1L, 2L, 3L, 4L, 5L))) + when: def result1 = materialStore.findById(1L, false).get() def result2 = materialStore.findById(1L, true).get() def result3 = materialStore.findById(1L).get() + then: Assert.that(result1 instanceof MaterialStore.FindByIdObject) - Assert.that(result2.username()==null) + Assert.that(result2.username() == null) Assert.that(result3 instanceof MaterialStore.FindByIdObject) } - - def "shouldThrowWhenFindById"(){ + + def "shouldThrowWhenFindById"() { given: materialRepository.findById(1L) >> Optional.empty() + when: materialStore.findById(1L).get() + then: thrown(NotFound404Exception) } - - def "shouldSave"(){ + + def "shouldSave"() { given: materialRepository.save(material) >> material + when: def result = materialStore.save(material).get() + then: Assert.that(result == material) } - - def "shouldUpdate"(){ + + def "shouldUpdate"() { given: materialRepository.save(material) >> material + when: def result = materialStore.update(material).get() + then: Assert.that(result == material) } - - def "shouldChangeStatus"(){ + + def "shouldFindAllWithUser"() { + given: + materialRepository.searchMaterialsWithAvgGrades( + _ as Long, + _ as String, + _ as List, + _ as Long, + _ as List, + _ as List, + _ as Long, + null, + null, + _ as Double, + _ as Double, + _ as PageRequest + ) >> new PageImpl(new Object[]{new Object[]{material, 3.0D}} as List) + userStore.findById(1L) >> Try.success(user) + + when: + def result = materialStore.findAll(1L, new MaterialStore.FindAllFilters( + "", 1L, new ArrayList(), 1L, List.of(1L), + List.of(1L), 3.0D, 3.0D, + PageRequest.of(1, 1) + )).get() + + then: + Assert.that(result instanceof Tuple2, UserStore.User>) + } + + def "shouldFindAll"() { + given: + materialRepository.searchMaterialsWithAvgGrades( + _ as Long, + _ as String, + _ as List, + _ as Long, + _ as List, + _ as List, + null, + null, + null, + _ as Double, + _ as Double, + _ as PageRequest + ) >> new PageImpl(new Object[]{new Object[]{material, 3.0D}} as List) + userStore.findUsersById(_ as Set) >> Try.success(new ArrayList<>(List.of(user))) + + when: + def result = materialStore.findAll(new MaterialStore.FindAllFilters( + "", 1L, new ArrayList(), 1L, List.of(1L), + List.of(1L), 3.0D, 3.0D, + PageRequest.of(1, 1) + )).get() + + then: + Assert.that(result instanceof Tuple2, UserStore.User>) + } + + def "shouldFindAllInDataRange"() { + given: + user.username = "name" + user.id = 1L + materialRepository.findMaterialsInDateRangeWithPage( + _ as LocalDateTime, + _ as LocalDateTime, + _ as PageRequest + ) >> new PageImpl(List.of(material)) + gradeStore.findAvgGradeByMaterialsIds(_ as Collection) + >> Try.success(new ArrayList<>(List.of(new GradeStore.FindAvgGradeObject( + new Object[]{1L, 3.0D} + )))) + userStore.findUsersById(_ as Set) >> Try.success(new ArrayList<>(List.of(user))) + + when: + def result = materialStore.findAllInDateRange( + LocalDateTime.now().minusDays(1), + LocalDateTime.now().plusDays(1), + PageRequest.of(1, 1) + ).get() + + then: + Assert.that(result.content.size() == 1) + } + + def "shouldChangeStatus"() { given: materialRepository.updateStatus(1L, Material.MaterialStatus.APPROVED) >> _ + when: def result = materialStore.changeStatus(1L, Material.MaterialStatus.APPROVED).get() + then: Assert.that(result == Tuple.of(1L, Material.MaterialStatus.APPROVED)) } diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/store/SectionStoreSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/store/SectionStoreSpec.groovy index 9c06e01..388f3c1 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/store/SectionStoreSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/store/SectionStoreSpec.groovy @@ -16,8 +16,10 @@ class SectionStoreSpec extends Specification { def section = new Section() sectionRepository.findAllWithFetchCategories() >> new ArrayList
(List.of(section)) + when: def result = sectionStore.findAll().get() + then: Assert.that(result == List.of(section)) } diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/store/TagStoreSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/store/TagStoreSpec.groovy index 9e58211..34428fb 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/store/TagStoreSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/store/TagStoreSpec.groovy @@ -20,8 +20,10 @@ class TagStoreSpec extends Specification { given: tagRepository.findTagsByIdIn(List.of(1L)) >> Set.of(tag) + when: def result = tagStore.findAllByIdIn(List.of(1L)).get() + then: Assert.that(result == Set.of(tag)) } @@ -30,8 +32,10 @@ class TagStoreSpec extends Specification { given: tagRepository.findTagsByIdIn(List.of(1L)) >> new HashSet() + when: tagStore.findAllByIdIn(List.of(1L)).get() + then: thrown(NotFound404Exception) } @@ -40,8 +44,10 @@ class TagStoreSpec extends Specification { given: tagRepository.existsByName(tag.name) >> false tagRepository.save(_ as Tag) >> tag + when: def result = tagStore.save(tag.name).get() + then: Assert.that(result == tag) } @@ -49,8 +55,10 @@ class TagStoreSpec extends Specification { def "shouldThrowWhenSave"() { given: tagRepository.existsByName(tag.name) >> true + when: tagStore.save(tag.name).get() + then: thrown(AlreadyExists409Exception) } @@ -58,8 +66,10 @@ class TagStoreSpec extends Specification { def "shouldFindAll"() { given: tagRepository.findAll() >> new ArrayList(List.of(tag)) + when: def result = tagStore.findAll().get() + then: Assert.that(result == List.of(tag)) } diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/store/TypeStoreSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/store/TypeStoreSpec.groovy index 6d448e6..d24180c 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/store/TypeStoreSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/store/TypeStoreSpec.groovy @@ -17,8 +17,10 @@ class TypeStoreSpec extends Specification { def "shouldFindById"() { given: typeRepository.findById(1L) >> Optional.of(type) + when: def result = typeStore.findById(1L).get() + then: Assert.that(result == type) } @@ -26,8 +28,10 @@ class TypeStoreSpec extends Specification { def "shouldThrowWhenFindById"() { given: typeRepository.findById(1L) >> Optional.empty() + when: typeStore.findById(1L).get() + then: thrown(NotFound404Exception) } @@ -35,8 +39,10 @@ class TypeStoreSpec extends Specification { def "shouldFindAll"() { given: typeRepository.findAll() >> new ArrayList(List.of(type)) + when: def result = typeStore.findAll().get() + then: Assert.that(result == List.of(type)) } diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/store/UserStoreSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/store/UserStoreSpec.groovy index 6d63bf6..8ac2665 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/store/UserStoreSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/store/UserStoreSpec.groovy @@ -23,8 +23,10 @@ class UserStoreSpec extends Specification { given: lanRestTemplate.exchange(_ as String, HttpMethod.GET, null, _ as ParameterizedTypeReference) >> ResponseEntity.of(Optional.of(new ArrayList<>(List.of(user)))) + when: def result = userStore.findUsersById(Stream.of(1L)).get() + then: Assert.that(result == List.of(user)) } @@ -33,8 +35,10 @@ class UserStoreSpec extends Specification { given: lanRestTemplate.exchange(_ as String, HttpMethod.GET, null, _ as ParameterizedTypeReference) >> ResponseEntity.internalServerError() + when: userStore.findUsersById(Stream.of(1L)).get() + then: thrown(InternalError500Exception) } @@ -43,8 +47,10 @@ class UserStoreSpec extends Specification { given: lanRestTemplate.exchange(_ as String, HttpMethod.GET, null, _ as ParameterizedTypeReference) >> ResponseEntity.of(Optional.of(new ArrayList<>(List.of(user)))) + when: def result = userStore.findUsersById(List.of(1L)).get() + then: Assert.that(result == List.of(user)) } @@ -53,8 +59,10 @@ class UserStoreSpec extends Specification { given: lanRestTemplate.exchange(_ as String, HttpMethod.GET, null, _ as ParameterizedTypeReference) >> ResponseEntity.internalServerError() + when: userStore.findUsersById(List.of(1L)).get() + then: thrown(InternalError500Exception) } @@ -63,8 +71,10 @@ class UserStoreSpec extends Specification { given: lanRestTemplate.exchange(_ as String, HttpMethod.GET, null, _ as ParameterizedTypeReference) >> ResponseEntity.of(Optional.of(new ArrayList<>(List.of(user)))) + when: def result = userStore.findById(1L).get() + then: Assert.that(result == user) } @@ -73,8 +83,10 @@ class UserStoreSpec extends Specification { given: lanRestTemplate.exchange(_ as String, HttpMethod.GET, null, _ as ParameterizedTypeReference) >> ResponseEntity.internalServerError() + when: userStore.findById(1L).get() + then: thrown(InternalError500Exception) }