diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt index 7ce4036..3738732 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/MaterialController.kt @@ -54,8 +54,12 @@ class MaterialController(materialService: MaterialService) : } } + @GetMapping("/simdut") + fun getAllIdsWithSimdut(): ResponseEntity> = + ResponseEntity.ok(service.getAllIdsWithSimdut()) + @PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) - fun saveTest(@Valid entity: MaterialSaveDto, simdutFile: MultipartFile?): ResponseEntity = + fun save(@Valid entity: MaterialSaveDto, simdutFile: MultipartFile?): ResponseEntity = super.save( materialSaveDto( name = entity.name, @@ -65,7 +69,7 @@ class MaterialController(materialService: MaterialService) : ) ) - @PostMapping("oldsave") + @PostMapping("unused_save") override fun save(entity: MaterialSaveDto): ResponseEntity = ResponseEntity.notFound().build() @@ -81,7 +85,7 @@ class MaterialController(materialService: MaterialService) : ) ) - @PutMapping("oldupdate") + @PutMapping("unused_update") override fun update(entity: MaterialUpdateDto): ResponseEntity = ResponseEntity.notFound().build() } diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt index c507592..b11748c 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialService.kt @@ -27,6 +27,9 @@ interface MaterialService : /** Gets all materials available for updating the mix with the given [mixId], including normal materials and materials from [MixType]s included in the mix recipe, excluding the material of the [MixType] of the said mix. */ fun getAllForMixUpdate(mixId: Long): Collection + /** Gets the identifier of materials for which a SIMDUT exists. */ + fun getAllIdsWithSimdut(): Collection + /** Updates the quantity of the given [material] with the given [factor]. */ fun updateQuantity(material: Material, factor: Float) } @@ -38,9 +41,7 @@ class MaterialServiceImpl( val recipeService: RecipeService, val mixService: MixService ) : - AbstractExternalNamedModelService( - materialRepository - ), + AbstractExternalNamedModelService(materialRepository), MaterialService { override fun existsByMaterialType(materialType: MaterialType): Boolean = repository.existsByMaterialType(materialType) @@ -49,6 +50,11 @@ class MaterialServiceImpl( override fun getSimdut(id: Long): ByteArray = simdutService.read(getById(id)) override fun getAllNotMixType(): Collection = getAll().filter { !it.isMixType } + override fun getAllIdsWithSimdut(): Collection = + getAllNotMixType() + .filter { simdutService.exists(it) } + .map { it.id!! } + override fun save(entity: MaterialSaveDto): Material = save(entity.toMaterial()).apply { if (entity.simdutFile != null && !entity.simdutFile.isEmpty) simdutService.write(this, entity.simdutFile) diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/repository/MaterialRepositoryTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/repository/MaterialRepositoryTest.kt index 09b44b7..7e90f3d 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/repository/MaterialRepositoryTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/repository/MaterialRepositoryTest.kt @@ -2,15 +2,12 @@ package dev.fyloz.trial.colorrecipesexplorer.repository import dev.fyloz.trial.colorrecipesexplorer.model.material import org.junit.jupiter.api.Test -import org.junit.runner.RunWith import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager -import org.springframework.test.context.junit4.SpringRunner import kotlin.test.assertEquals -@RunWith(SpringRunner::class) @DataJpaTest(excludeAutoConfiguration = [LiquibaseAutoConfiguration::class]) class MaterialRepositoryTest @Autowired constructor( private val materialRepository: MaterialRepository, diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt index 530e12b..09d74ef 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialServiceTest.kt @@ -93,6 +93,33 @@ class MaterialServiceTest : assertFalse(found.contains(mixTypeMaterial)) } + // getAllIdsWithSimdut() + + @Test + fun `getAllIdsWithSimdut() returns a list containing the identifier of every material with a SIMDUT file`() { + val materials = listOf( + material(id = 0L), + material(id = 1L), + material(id = 2L), + material(id = 3L) + ) + val hasSimdut = mapOf( + *materials + .map { it.id!! to it.evenId } + .toTypedArray() + ) + val expectedIds = hasSimdut + .filter { it.value } + .map { it.key } + + whenever(simdutService.exists(any())).doAnswer { hasSimdut[(it.arguments[0] as Material).id] } + doReturn(materials).whenever(service).getAllNotMixType() + + val found = service.getAllIdsWithSimdut() + + assertEquals(expectedIds, found) + } + // save() @Test @@ -165,9 +192,9 @@ class MaterialServiceTest : val found = service.getAllForMixCreation(recipe.id!!) - assertTrue(found contains normalMaterial) - assertTrue(found contains mixTypeMaterial) - assertFalse(found contains anotherMixTypeMaterial) + assertTrue(normalMaterial in found) + assertTrue(mixTypeMaterial in found) + assertFalse(anotherMixTypeMaterial in found) } // getAllForMixUpdate() @@ -187,9 +214,9 @@ class MaterialServiceTest : val found = service.getAllForMixUpdate(mix.id!!) - assertTrue(found contains normalMaterial) - assertTrue(found contains mixTypeMaterial) - assertFalse(found contains anotherMixTypeMaterial) + assertTrue(normalMaterial in found) + assertTrue(mixTypeMaterial in found) + assertFalse(anotherMixTypeMaterial in found) } // update() @@ -208,8 +235,6 @@ class MaterialServiceTest : verify(simdutService).update(eq(mockSimdutFile), any()) } - // updateQuantity() - // delete() @@ -217,7 +242,7 @@ class MaterialServiceTest : super.`deleteById() deletes the entity with the given id in the repository`() } - /** Helper function to replace collections.in because the id is not considered in the equals function of Material while Thymeleaf is supported. */ - private infix fun Collection.contains(material: Material): Boolean = - any { it.id == material.id } + /** Utility property to check if the identifier of the given [Material] is even. */ + val Material.evenId: Boolean + get() = this.id!! % 2 == 0L }