diff --git a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialCreateService.java b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialCreateService.java index 7725c96..03d665f 100644 --- a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialCreateService.java +++ b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialCreateService.java @@ -26,6 +26,7 @@ import pl.sknikod.kodemybackend.infrastructure.store.TypeStore; import pl.sknikod.kodemycommons.exception.content.ExceptionUtil; import pl.sknikod.kodemycommons.security.AuthFacade; +import pl.sknikod.kodemycommons.util.PrincipalUtil; import java.util.List; import java.util.Set; @@ -39,9 +40,10 @@ public class MaterialCreateService { private final MaterialStore materialStore; private final TypeStore typeStore; - private final MaterialCreateMapper createMaterialMapper; + private final MaterialCreateMapper materialCreateMapper; private final CategoryStore categoryStore; private final TagStore tagStore; + private final PrincipalUtil principalUtil; public MaterialCreateResponse create(MaterialCreateRequest body) { var category = categoryStore.findById(body.categoryId) @@ -53,7 +55,7 @@ public MaterialCreateResponse create(MaterialCreateRequest body) { return Try.of(() -> this.toMaterial(body, category, type, tags)) .flatMap(materialStore::save) - .map(createMaterialMapper::map) + .map(materialCreateMapper::map) .getOrElseThrow(ExceptionUtil::throwIfFailure); } @@ -71,7 +73,7 @@ private Material toMaterial( material.setCategory(category); material.setType(type); material.setTags(tags); - var user = AuthFacade.getCurrentUserPrincipal().get(); + var user = principalUtil.getPrincipal().get(); material.setUserId(user.getId()); var isApprovedMaterial = user.getAuthorities().contains(new SimpleGrantedAuthority("CAN_AUTO_APPROVED_MATERIAL")) ? APPROVED : PENDING; diff --git a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialGetByIdService.java b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialGetByIdService.java index 3817957..8e580d9 100644 --- a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialGetByIdService.java +++ b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialGetByIdService.java @@ -12,7 +12,6 @@ @Service @RequiredArgsConstructor public class MaterialGetByIdService { - private final GradeStore gradeStore; private final MaterialStore materialStore; public SingleMaterialResponse showDetails(Long materialId) { diff --git a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialStatusService.java b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialStatusService.java index 5f1956d..90b0a49 100644 --- a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialStatusService.java +++ b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialStatusService.java @@ -33,7 +33,7 @@ public Material.MaterialStatus update(Long materialId, Material.MaterialStatus n .getOrElseThrow(ExceptionUtil::throwIfFailure); } - StatusesToChangeResponse showStatusesToChange(Long materialId) { + public StatusesToChangeResponse showStatusesToChange(Long materialId) { return materialStore.findById(materialId, true) .map(MaterialStore.FindByIdObject::material) .map(material -> getPossibleStatuses(material.getStatus())) diff --git a/kodemy-backend/src/test/groovy/factory/MaterialFactory.groovy b/kodemy-backend/src/test/groovy/factory/MaterialFactory.groovy new file mode 100644 index 0000000..5432f70 --- /dev/null +++ b/kodemy-backend/src/test/groovy/factory/MaterialFactory.groovy @@ -0,0 +1,26 @@ +package factory + +import pl.sknikod.kodemybackend.infrastructure.database.Category +import pl.sknikod.kodemybackend.infrastructure.database.Material +import pl.sknikod.kodemybackend.infrastructure.database.Tag +import pl.sknikod.kodemybackend.infrastructure.database.Type + +class MaterialFactory { + static Material create() { + def category = new Category() + category.id = 1L + def type = new Type() + type.id = 1L + def tags = new HashSet(List.of(new Tag("name"))) + def material = new Material() + material.id = 1L + material.title = "title" + material.description = "desc" + material.link = "link" + material.status = Material.MaterialStatus.APPROVED + material.category = category + material.type = type + material.tags = tags + return material + } +} diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialControllerSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialControllerSpec.groovy new file mode 100644 index 0000000..d78dca6 --- /dev/null +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialControllerSpec.groovy @@ -0,0 +1,156 @@ +package unit.infrastructure.module.material + + +import org.spockframework.util.Assert +import org.springframework.data.domain.PageImpl +import org.springframework.data.domain.PageRequest +import org.springframework.data.domain.Sort +import org.springframework.http.ResponseEntity +import pl.sknikod.kodemybackend.infrastructure.database.Material +import pl.sknikod.kodemybackend.infrastructure.module.material.* +import pl.sknikod.kodemybackend.infrastructure.module.material.model.MaterialPageable +import pl.sknikod.kodemybackend.infrastructure.module.material.model.SingleMaterialResponse +import pl.sknikod.kodemybackend.infrastructure.module.material.model.StatusesToChangeResponse +import pl.sknikod.kodemybackend.infrastructure.rest.MaterialControllerDefinition +import spock.lang.Specification + +import java.time.Instant +import java.time.LocalDateTime + +class MaterialControllerSpec extends Specification { + def materialCreateService = Mock(MaterialCreateService) + def materialUpdateService = Mock(MaterialUpdateService) + def materialGetByIdService = Mock(MaterialGetByIdService) + def materialIndexService = Mock(MaterialIndexService) + def materialStatusService = Mock(MaterialStatusService) + def materialManageService = Mock(MaterialManageService) + + def materialController = new MaterialController( + materialCreateService, + materialUpdateService, + materialGetByIdService, + materialIndexService, + materialStatusService, + materialManageService + ) + + def "shouldCreate"() { + given: + def response = new MaterialCreateService.MaterialCreateResponse(1L, "title", + Material.MaterialStatus.APPROVED) + materialCreateService.create(_ as MaterialCreateService.MaterialCreateRequest) + >> response + + when: + def result = materialController.create( + new MaterialCreateService.MaterialCreateRequest("title", "desc", + "link", 1L, 1L, new ArrayList() + )) + + then: + Assert.that(result.body == response) + } + + def "shouldUpdate"() { + given: + def response = new MaterialUpdateService.MaterialUpdateResponse( + 1L, "title", + "desc", "link", + new MaterialUpdateService.MaterialUpdateResponse.BaseDetails(1L, "name"), + new MaterialUpdateService.MaterialUpdateResponse.BaseDetails(1L, "name"), + new HashSet() + ) + materialUpdateService.update(1L, _ as MaterialUpdateService.MaterialUpdateRequest) + >> response + + when: + def result = materialController.update(1L, + new MaterialUpdateService.MaterialUpdateRequest("title", "desc", + "link", 1L, 1L, new ArrayList() + )) + + then: + Assert.that(result.body == response) + } + + def "shouldUpdateStatus"() { + given: + materialStatusService.update(1L, Material.MaterialStatus.APPROVED) + >> Material.MaterialStatus.APPROVED + + when: + def result = materialController.updateStatus(1L, Material.MaterialStatus.APPROVED) + + then: + Assert.that(result.body == Material.MaterialStatus.APPROVED) + } + + def "shouldReindex"() { + given: + materialIndexService.reindex(_ as Instant, _ as Instant) + >> _ + + when: + def result = materialController.reindex(Instant.now(), Instant.now().plusSeconds(60)) + + then: + Assert.that(result instanceof ResponseEntity) + } + + def "shouldShowDetails"() { + given: + def response = new SingleMaterialResponse( + 1L, "title", "description", "link", Material.MaterialStatus.APPROVED, + new SingleMaterialResponse.TypeDetails(1L, "name"), + new ArrayList(), + 3.0D, new ArrayList(), new SingleMaterialResponse.AuthorDetails(1L, "name"), + LocalDateTime.now() + ) + materialGetByIdService.showDetails(1L) + >> response + + when: + def result = materialController.showDetails(1L) + + then: + Assert.that(result.body == response) + } + + def "shouldShowStatusesToChange"() { + given: + def response = new StatusesToChangeResponse(List.of(Material.MaterialStatus.APPROVED)) + materialStatusService.showStatusesToChange(1L) + >> response + + when: + def result = materialController.showStatusesToChange(1L) + + then: + Assert.that(result.body == response) + } + + def "shouldManage"() { + given: + def response = new MaterialPageable( + 1L, "title", "desc", "link", Material.MaterialStatus.APPROVED, + new MaterialPageable.TypeDetails(1L, "name"), + new ArrayList(), + 3.0D, new MaterialPageable.AuthorDetails(1L, "name"), + LocalDateTime.now() + ) + materialManageService.manage( + _ as MaterialControllerDefinition.MaterialFilterSearchParams, + _ as PageRequest + ) >> new PageImpl(List.of(response)) + + when: + def result = materialController.manage( + 1, 1, MaterialControllerDefinition.MaterialSortField.ID, + Sort.Direction.ASC, + new MaterialControllerDefinition.MaterialFilterSearchParams() + ) + + then: + Assert.that(result.body.content == List.of(response)) + } +} diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialCreateServiceSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialCreateServiceSpec.groovy new file mode 100644 index 0000000..ae3903c --- /dev/null +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialCreateServiceSpec.groovy @@ -0,0 +1,59 @@ +package unit.infrastructure.module.material + +import factory.MaterialFactory +import io.vavr.control.Try +import org.spockframework.util.Assert +import org.springframework.security.core.authority.SimpleGrantedAuthority +import pl.sknikod.kodemybackend.infrastructure.database.Material +import pl.sknikod.kodemybackend.infrastructure.module.material.MaterialCreateService +import pl.sknikod.kodemybackend.infrastructure.store.CategoryStore +import pl.sknikod.kodemybackend.infrastructure.store.MaterialStore +import pl.sknikod.kodemybackend.infrastructure.store.TagStore +import pl.sknikod.kodemybackend.infrastructure.store.TypeStore +import pl.sknikod.kodemycommons.security.UserPrincipal +import pl.sknikod.kodemycommons.util.PrincipalUtil +import spock.lang.Specification + +class MaterialCreateServiceSpec extends Specification { + def materialStore = Mock(MaterialStore) + def typeStore = Mock(TypeStore) + def materialCreateMapper = Mock(MaterialCreateService.MaterialCreateMapper) + def categoryStore = Mock(CategoryStore) + def tagStore = Mock(TagStore) + def principalUtil = Mock(PrincipalUtil) + + def materialCreateService = new MaterialCreateService( + materialStore, + typeStore, + materialCreateMapper, + categoryStore, + tagStore, + principalUtil + ) + + def "shouldCreate"() { + given: + def material = MaterialFactory.create() + categoryStore.findById(1L) >> Try.success(material.category) + typeStore.findById(1L) >> Try.success(material.type) + tagStore.findAllByIdIn(_ as List) >> Try.success(material.tags) + materialStore.save(_ as Material) >> Try.success(material) + principalUtil.getPrincipal() >> Optional.of( + new UserPrincipal(1L, "user", new ArrayList()) + ) + def response = new MaterialCreateService.MaterialCreateResponse( + 1L, "title", Material.MaterialStatus.APPROVED + ) + materialCreateMapper.map(material) >> response + + when: + def result = materialCreateService.create( + new MaterialCreateService.MaterialCreateRequest( + "title", "desc", "link", 1L, 1L, List.of(1L) + ) + ) + + then: + Assert.that(result == response) + } +} diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialGetByIdServiceSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialGetByIdServiceSpec.groovy new file mode 100644 index 0000000..c1a92a3 --- /dev/null +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialGetByIdServiceSpec.groovy @@ -0,0 +1,41 @@ +package unit.infrastructure.module.material + +import factory.MaterialFactory +import io.vavr.control.Try +import pl.sknikod.kodemybackend.infrastructure.database.Material +import pl.sknikod.kodemybackend.infrastructure.module.material.MaterialGetByIdService +import pl.sknikod.kodemybackend.infrastructure.store.MaterialStore +import spock.lang.Specification + +import java.time.LocalDateTime + +class MaterialGetByIdServiceSpec extends Specification { + def materialStore = Mock(MaterialStore) + + def materialGetByIdService = new MaterialGetByIdService(materialStore) + + def "shouldShowDetails"() { + given: + def time = LocalDateTime.now() + def material = MaterialFactory.create() + material.createdDate = time + materialStore.findById(1L) + >> Try.success(new MaterialStore.FindByIdObject( + material, "name", 3.0D, + new ArrayList(List.of(3L)) + )) + + when: + def result = materialGetByIdService.showDetails(1L) + + then: + verifyAll { + result.id == 1L + result.title == "title" + result.description == "desc" + result.link == "link" + result.status == Material.MaterialStatus.APPROVED + result.type.id == material.type.id + } + } +} diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material_by_user/MaterialGetByUserControllerSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material_by_user/MaterialGetByUserControllerSpec.groovy index 33387cb..be2c1e2 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material_by_user/MaterialGetByUserControllerSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material_by_user/MaterialGetByUserControllerSpec.groovy @@ -1,6 +1,5 @@ package unit.infrastructure.module.material_by_user - import org.spockframework.util.Assert import org.springframework.data.domain.PageImpl import org.springframework.data.domain.PageRequest @@ -8,8 +7,8 @@ import org.springframework.data.domain.Sort import pl.sknikod.kodemybackend.infrastructure.database.Material import pl.sknikod.kodemybackend.infrastructure.module.material.model.MaterialPageable import pl.sknikod.kodemybackend.infrastructure.module.material.model.MaterialSortField -import pl.sknikod.kodemybackend.infrastructure.module.material_by_user.MaterialGetByUserService import pl.sknikod.kodemybackend.infrastructure.module.material_by_user.MaterialGetByUserController +import pl.sknikod.kodemybackend.infrastructure.module.material_by_user.MaterialGetByUserService import pl.sknikod.kodemybackend.infrastructure.rest.UserControllerDefinition import spock.lang.Specification