mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
move SchemaValidator to separate package
add new exception classes
This commit is contained in:
@@ -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.ProductService
|
||||||
import com.example.demo.services.database.product.ProductServiceImpl
|
import com.example.demo.services.database.product.ProductServiceImpl
|
||||||
import com.example.demo.services.kafka.Producer
|
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.ObjectMapper
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature
|
import com.fasterxml.jackson.databind.SerializationFeature
|
||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.example.demo.config
|
|||||||
|
|
||||||
import com.example.demo.services.kafka.Producer
|
import com.example.demo.services.kafka.Producer
|
||||||
import com.example.demo.services.kafka.ProducerImpl
|
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.clients.producer.ProducerConfig
|
||||||
import org.apache.kafka.common.serialization.StringSerializer
|
import org.apache.kafka.common.serialization.StringSerializer
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.example.demo.services.kafka
|
|||||||
|
|
||||||
import com.example.demo.models.Product
|
import com.example.demo.models.Product
|
||||||
import com.example.demo.services.kafka.dto.ProductDto
|
import com.example.demo.services.kafka.dto.ProductDto
|
||||||
|
import com.example.demo.services.validation.SchemaValidator
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.encodeToJsonElement
|
import kotlinx.serialization.json.encodeToJsonElement
|
||||||
import org.springframework.kafka.core.KafkaTemplate
|
import org.springframework.kafka.core.KafkaTemplate
|
||||||
|
|||||||
@@ -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.JsonSchema
|
||||||
import io.github.optimumcode.json.schema.ValidationError
|
import io.github.optimumcode.json.schema.ValidationError
|
||||||
import kotlinx.serialization.json.JsonElement
|
import kotlinx.serialization.json.JsonElement
|
||||||
@@ -20,10 +22,7 @@ class SchemaValidator(
|
|||||||
|
|
||||||
val valid = schema.validate(value, errors::add)
|
val valid = schema.validate(value, errors::add)
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
// todo throw another exception
|
throw ElementNotValidException(errors)
|
||||||
println(errors.toString())
|
|
||||||
|
|
||||||
throw RuntimeException("invalid schema")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,8 +33,7 @@ class SchemaValidator(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val schemaFile = schemaMap[schemaName]
|
val schemaFile = schemaMap[schemaName]
|
||||||
?: // todo throw another exception
|
?: throw SchemaNotFoundException()
|
||||||
throw RuntimeException("unknown json-schema")
|
|
||||||
|
|
||||||
val schema = ResourceUtils.getFile("classpath:json-schemas/$schemaFile")
|
val schema = ResourceUtils.getFile("classpath:json-schemas/$schemaFile")
|
||||||
.readText(Charsets.UTF_8)
|
.readText(Charsets.UTF_8)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.demo.services.validation.exceptions
|
||||||
|
|
||||||
|
import io.github.optimumcode.json.schema.ValidationError
|
||||||
|
|
||||||
|
class ElementNotValidException(
|
||||||
|
val validationErrors: List<ValidationError>,
|
||||||
|
): RuntimeException()
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package com.example.demo.services.validation.exceptions
|
||||||
|
|
||||||
|
class SchemaNotFoundException: RuntimeException()
|
||||||
@@ -3,6 +3,7 @@ package com.example.demo.services.kafka
|
|||||||
import com.example.demo.BaseUnitTest
|
import com.example.demo.BaseUnitTest
|
||||||
import com.example.demo.models.Product
|
import com.example.demo.models.Product
|
||||||
import com.example.demo.services.kafka.dto.ProductDto
|
import com.example.demo.services.kafka.dto.ProductDto
|
||||||
|
import com.example.demo.services.validation.SchemaValidator
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.encodeToJsonElement
|
import kotlinx.serialization.json.encodeToJsonElement
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
|||||||
Reference in New Issue
Block a user