Skip to content

Commit

Permalink
#10 Added validator on endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
loreV committed Jan 3, 2024
1 parent e1b849f commit 1638d12
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sc.controller.microservice

import io.swagger.v3.oas.annotations.Operation
import org.hikit.common.response.ControllerPagination
import org.openapitools.model.EventResponse
import org.sc.adapter.microservice.ErtEventMicroserviceAdapter
import org.sc.data.validator.GeneralValidator
Expand All @@ -18,7 +17,6 @@ import org.springframework.web.bind.annotation.RestController
class ErtEventsController @Autowired constructor(
private val ertEventMicroserviceAdapter: ErtEventMicroserviceAdapter,
private val generalValidator: GeneralValidator,
private val controllerPagination: ControllerPagination
) {
companion object {
const val PREFIX = "/ert/events"
Expand All @@ -29,6 +27,20 @@ class ErtEventsController @Autowired constructor(
operator fun get(
@PathVariable(required = true) istat: String
): EventResponse? {
val validationErrors = generalValidator.validateIstat(istat)
if(validationErrors.isNotEmpty()) {
composeErrorResponse(validationErrors)
}
return ertEventMicroserviceAdapter.getByIstat1(istat)!!.body
}

private fun composeErrorResponse(validationErrors: Set<String>) {
EventResponse()
.status(EventResponse.StatusEnum.ERROR)
.messages(validationErrors)
.totalCount(0)
.totalPages(0)
.content(listOf())
.currentPage(0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class GeneralValidator @Autowired constructor(
private val poiValidator: PoiValidator,
private val rectangleValidator: RectangleValidator,
private val geoLineValidator: GeoLineValidator,
private val accessibilityReportValidator: AccessibilityReportValidator
private val accessibilityReportValidator: AccessibilityReportValidator,
private val istatValidator: IstatValidator
) {
fun validate(acd: AccessibilityReportDto): Set<String> = accessibilityReportValidator.validate(acd)
fun validate(acd: AccessibilityNotificationDto): Set<String> = accessibilityValidator.validate(acd)
Expand Down Expand Up @@ -69,4 +70,5 @@ class GeneralValidator @Autowired constructor(
fun validateMediaExistence(id: String): Set<String> = mediaExistenceValidator.validate(id)
fun validatePoiExistence(id: String): Set<String> = poiExistenceValidator.validate(id)
fun validate(rectangleDto: RectangleDto): Set<String> = rectangleValidator.validate(rectangleDto)
fun validateIstat(istat: String): Set<String> = istatValidator.validate(istat)
}
28 changes: 28 additions & 0 deletions backend/src/main/java/org/sc/data/validator/IstatValidator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.sc.data.validator

import org.apache.commons.lang3.StringUtils.isEmpty
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.util.regex.Pattern
import java.util.regex.Pattern.compile

@Component
class IstatValidator @Autowired constructor() : Validator<String> {
companion object {
const val noParamSpecifiedError = "Empty istat not accepted"
const val istatNotCorrect = "Istat not in correct format"
val istatRegex: Pattern = compile("^\\d{0,5}\$")
}

override fun validate(request: String): Set<String> {
if (isEmpty(request)) {
return setOf(noParamSpecifiedError)
}

if(!istatRegex.matcher(request).matches()) {
return setOf(istatNotCorrect)
}

return setOf()
}
}
23 changes: 23 additions & 0 deletions backend/src/test/java/org/sc/data/validator/IstatValidatorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sc.data.validator

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

internal class IstatValidatorTest {

@Test
fun `validation shall provide no errors on correct istat `() {
assertThat(IstatValidator().validate("40034")).isEmpty()
}

@Test
fun `validation shall provide error on wrong istat `() {
assertThat(IstatValidator().validate("4003a")).isNotEmpty
}

@Test
fun `validation shall provide error on empty istat `() {
assertThat(IstatValidator().validate("")).isNotEmpty
}

}

0 comments on commit 1638d12

Please sign in to comment.