diff --git a/src/main/kotlin/com/example/demo/config/AppConfig.kt b/src/main/kotlin/com/example/demo/config/AppConfig.kt index 0f28ae6..23bbe3c 100644 --- a/src/main/kotlin/com/example/demo/config/AppConfig.kt +++ b/src/main/kotlin/com/example/demo/config/AppConfig.kt @@ -9,7 +9,7 @@ import com.example.demo.services.database.city.CityServiceImpl import com.example.demo.services.database.product.ProductService import com.example.demo.services.database.product.ProductServiceImpl import com.example.demo.services.kafka.Producer -import com.example.demo.services.kafka.SchemaValidator +import com.example.demo.services.validation.SchemaValidator import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule diff --git a/src/main/kotlin/com/example/demo/config/KafkaProducerConfig.kt b/src/main/kotlin/com/example/demo/config/KafkaProducerConfig.kt index 91a6eba..59a2f31 100644 --- a/src/main/kotlin/com/example/demo/config/KafkaProducerConfig.kt +++ b/src/main/kotlin/com/example/demo/config/KafkaProducerConfig.kt @@ -2,7 +2,7 @@ package com.example.demo.config import com.example.demo.services.kafka.Producer import com.example.demo.services.kafka.ProducerImpl -import com.example.demo.services.kafka.SchemaValidator +import com.example.demo.services.validation.SchemaValidator import org.apache.kafka.clients.producer.ProducerConfig import org.apache.kafka.common.serialization.StringSerializer import org.springframework.beans.factory.annotation.Autowired diff --git a/src/main/kotlin/com/example/demo/services/kafka/ProducerImpl.kt b/src/main/kotlin/com/example/demo/services/kafka/ProducerImpl.kt index 0da67f0..8a9bbcb 100644 --- a/src/main/kotlin/com/example/demo/services/kafka/ProducerImpl.kt +++ b/src/main/kotlin/com/example/demo/services/kafka/ProducerImpl.kt @@ -2,6 +2,7 @@ package com.example.demo.services.kafka import com.example.demo.models.Product import com.example.demo.services.kafka.dto.ProductDto +import com.example.demo.services.validation.SchemaValidator import kotlinx.serialization.json.Json import kotlinx.serialization.json.encodeToJsonElement import org.springframework.kafka.core.KafkaTemplate diff --git a/src/main/kotlin/com/example/demo/services/kafka/SchemaValidator.kt b/src/main/kotlin/com/example/demo/services/validation/SchemaValidator.kt similarity index 77% rename from src/main/kotlin/com/example/demo/services/kafka/SchemaValidator.kt rename to src/main/kotlin/com/example/demo/services/validation/SchemaValidator.kt index 968e52c..6123616 100644 --- a/src/main/kotlin/com/example/demo/services/kafka/SchemaValidator.kt +++ b/src/main/kotlin/com/example/demo/services/validation/SchemaValidator.kt @@ -1,5 +1,7 @@ -package com.example.demo.services.kafka +package com.example.demo.services.validation +import com.example.demo.services.validation.exceptions.ElementNotValidException +import com.example.demo.services.validation.exceptions.SchemaNotFoundException import io.github.optimumcode.json.schema.JsonSchema import io.github.optimumcode.json.schema.ValidationError import kotlinx.serialization.json.JsonElement @@ -20,10 +22,7 @@ class SchemaValidator( val valid = schema.validate(value, errors::add) if (!valid) { - // todo throw another exception - println(errors.toString()) - - throw RuntimeException("invalid schema") + throw ElementNotValidException(errors) } } @@ -34,8 +33,7 @@ class SchemaValidator( } val schemaFile = schemaMap[schemaName] - ?: // todo throw another exception - throw RuntimeException("unknown json-schema") + ?: throw SchemaNotFoundException() val schema = ResourceUtils.getFile("classpath:json-schemas/$schemaFile") .readText(Charsets.UTF_8) diff --git a/src/main/kotlin/com/example/demo/services/validation/exceptions/ElementNotValidException.kt b/src/main/kotlin/com/example/demo/services/validation/exceptions/ElementNotValidException.kt new file mode 100644 index 0000000..94df391 --- /dev/null +++ b/src/main/kotlin/com/example/demo/services/validation/exceptions/ElementNotValidException.kt @@ -0,0 +1,7 @@ +package com.example.demo.services.validation.exceptions + +import io.github.optimumcode.json.schema.ValidationError + +class ElementNotValidException( + val validationErrors: List, +): RuntimeException() \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/services/validation/exceptions/SchemaNotFoundException.kt b/src/main/kotlin/com/example/demo/services/validation/exceptions/SchemaNotFoundException.kt new file mode 100644 index 0000000..f13db10 --- /dev/null +++ b/src/main/kotlin/com/example/demo/services/validation/exceptions/SchemaNotFoundException.kt @@ -0,0 +1,3 @@ +package com.example.demo.services.validation.exceptions + +class SchemaNotFoundException: RuntimeException() \ No newline at end of file diff --git a/src/test/kotlin/com/example/demo/services/kafka/ProducerImplTest.kt b/src/test/kotlin/com/example/demo/services/kafka/ProducerImplTest.kt index 36c8d99..60c67b7 100644 --- a/src/test/kotlin/com/example/demo/services/kafka/ProducerImplTest.kt +++ b/src/test/kotlin/com/example/demo/services/kafka/ProducerImplTest.kt @@ -3,6 +3,7 @@ package com.example.demo.services.kafka import com.example.demo.BaseUnitTest import com.example.demo.models.Product import com.example.demo.services.kafka.dto.ProductDto +import com.example.demo.services.validation.SchemaValidator import kotlinx.serialization.json.Json import kotlinx.serialization.json.encodeToJsonElement import org.junit.runner.RunWith