-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #185 material services tests part 1
- Loading branch information
1 parent
a075a59
commit 50b0563
Showing
8 changed files
with
289 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
kodemy-backend/src/test/groovy/factory/MaterialFactory.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
...backend/src/test/groovy/unit/infrastructure/module/material/MaterialControllerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...kend/src/test/groovy/unit/infrastructure/module/material/MaterialCreateServiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...end/src/test/groovy/unit/infrastructure/module/material/MaterialGetByIdServiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...groovy/unit/infrastructure/module/material_by_user/MaterialGetByUserControllerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters