Skip to content

Commit

Permalink
feat: #185 material services tests part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbator committed Dec 10, 2024
1 parent a075a59 commit 50b0563
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@Service
@RequiredArgsConstructor
public class MaterialGetByIdService {
private final GradeStore gradeStore;
private final MaterialStore materialStore;

public SingleMaterialResponse showDetails(Long materialId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
26 changes: 26 additions & 0 deletions kodemy-backend/src/test/groovy/factory/MaterialFactory.groovy
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<Long>()
))

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.MaterialUpdateResponse.BaseDetails>()
)
materialUpdateService.update(1L, _ as MaterialUpdateService.MaterialUpdateRequest)
>> response

when:
def result = materialController.update(1L,
new MaterialUpdateService.MaterialUpdateRequest("title", "desc",
"link", 1L, 1L, new ArrayList<Long>()
))

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<SingleMaterialResponse.TagDetails>(),
3.0D, new ArrayList<Long>(), 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<MaterialPageable.TagDetails>(),
3.0D, new MaterialPageable.AuthorDetails(1L, "name"),
LocalDateTime.now()
)
materialManageService.manage(
_ as MaterialControllerDefinition.MaterialFilterSearchParams,
_ as PageRequest
) >> new PageImpl<MaterialPageable>(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))
}
}
Original file line number Diff line number Diff line change
@@ -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<SimpleGrantedAuthority>())
)
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)
}
}
Original file line number Diff line number Diff line change
@@ -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<Long>(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
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package unit.infrastructure.module.material_by_user


import org.spockframework.util.Assert
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.PageRequest
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

Expand Down

0 comments on commit 50b0563

Please sign in to comment.