move validation config to root-level key

some refactoring
This commit is contained in:
Denis Savosin
2024-10-11 13:28:04 +07:00
parent 7cd50456a0
commit 3fbc94553d
11 changed files with 39 additions and 19 deletions

View File

@@ -1,5 +1,7 @@
package com.example.demo.config package com.example.demo.config
import com.example.demo.config.properties.KafkaProperties
import com.example.demo.config.properties.ValidationProperties
import com.example.demo.providers.CityRepository import com.example.demo.providers.CityRepository
import com.example.demo.providers.MockedShopProvider import com.example.demo.providers.MockedShopProvider
import com.example.demo.providers.ProductRepository import com.example.demo.providers.ProductRepository
@@ -24,7 +26,7 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
@Configuration @Configuration
@EnableConfigurationProperties(KafkaProperties::class) @EnableConfigurationProperties(KafkaProperties::class, ValidationProperties::class)
class AppConfig( class AppConfig(
@Autowired private val kafkaProperties: KafkaProperties, @Autowired private val kafkaProperties: KafkaProperties,
) { ) {
@@ -54,7 +56,9 @@ class AppConfig(
fun cityService(@Autowired cityRepository: CityRepository): CityService = CityServiceImpl(cityRepository) fun cityService(@Autowired cityRepository: CityRepository): CityService = CityServiceImpl(cityRepository)
@Bean @Bean
fun schemaValidator(): SchemaValidator = SchemaValidatorImp(kafkaProperties.validation.schema) fun schemaValidator(
@Autowired validationProperties: ValidationProperties,
): SchemaValidator = SchemaValidatorImp(validationProperties.schema)
@Bean @Bean
fun otlpHttpSpanExporter(@Value("\${tracing.url}") url: String): OtlpHttpSpanExporter { fun otlpHttpSpanExporter(@Value("\${tracing.url}") url: String): OtlpHttpSpanExporter {

View File

@@ -1,5 +1,6 @@
package com.example.demo.config package com.example.demo.config
import com.example.demo.config.properties.KafkaProperties
import com.example.demo.services.database.city.CityService import com.example.demo.services.database.city.CityService
import com.example.demo.services.kafka.Consumer import com.example.demo.services.kafka.Consumer
import io.micrometer.core.instrument.MeterRegistry import io.micrometer.core.instrument.MeterRegistry

View File

@@ -1,5 +1,6 @@
package com.example.demo.config package com.example.demo.config
import com.example.demo.config.properties.KafkaProperties
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.validation.SchemaValidator import com.example.demo.services.validation.SchemaValidator

View File

@@ -1,4 +1,4 @@
package com.example.demo.config package com.example.demo.config.properties
import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.bind.ConstructorBinding import org.springframework.boot.context.properties.bind.ConstructorBinding
@@ -8,7 +8,6 @@ data class KafkaProperties @ConstructorBinding constructor(
val bootstrapServers: String, val bootstrapServers: String,
val producer: Producer, val producer: Producer,
val consumer: Consumer, val consumer: Consumer,
val validation: Validation,
) { ) {
data class Producer( data class Producer(
val product: Product, val product: Product,
@@ -24,8 +23,4 @@ data class KafkaProperties @ConstructorBinding constructor(
val autoStartup: Boolean, val autoStartup: Boolean,
val autoOffsetReset: String, val autoOffsetReset: String,
) )
data class Validation(
val schema: Map<String, String>
)
} }

View File

@@ -0,0 +1,8 @@
package com.example.demo.config.properties
import org.springframework.boot.context.properties.ConfigurationProperties
@ConfigurationProperties("validation")
data class ValidationProperties(
val schema: Map<String, String>
)

View File

@@ -3,6 +3,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 com.example.demo.services.validation.SchemaValidator
import com.example.demo.services.validation.SchemaValidator.Companion.SCHEMA_KAFKA_PRODUCT_SYNC
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
@@ -20,7 +21,7 @@ class ProducerImpl(
val serializedProduct = Json.encodeToJsonElement(ProductDto(product)) val serializedProduct = Json.encodeToJsonElement(ProductDto(product))
schemaValidator.validate("product-sync", serializedProduct) schemaValidator.validate(SCHEMA_KAFKA_PRODUCT_SYNC, serializedProduct)
val message: Message<String> = MessageBuilder val message: Message<String> = MessageBuilder
.withPayload(serializedProduct.toString()) .withPayload(serializedProduct.toString())

View File

@@ -7,4 +7,8 @@ import kotlinx.serialization.json.JsonElement
interface SchemaValidator { interface SchemaValidator {
@Throws(ElementNotValidException::class, SchemaNotFoundException::class) @Throws(ElementNotValidException::class, SchemaNotFoundException::class)
fun validate(schemaName: String, value: JsonElement) fun validate(schemaName: String, value: JsonElement)
companion object {
const val SCHEMA_KAFKA_PRODUCT_SYNC = "kafka-product-sync"
}
} }

View File

@@ -31,9 +31,10 @@ kafka:
topics: demo-city-sync topics: demo-city-sync
auto-offset-reset: none auto-offset-reset: none
auto-startup: true auto-startup: true
validation: validation:
schema: schema:
product-sync: product/sync.json kafka-product-sync: kafka/product/sync.json
springdoc: springdoc:
api-docs: api-docs:

View File

@@ -1,7 +1,9 @@
package com.example.demo package com.example.demo
import com.example.demo.config.KafkaProperties import com.example.demo.config.properties.KafkaProperties
import com.example.demo.config.properties.ValidationProperties
import com.example.demo.services.kafka.Consumer import com.example.demo.services.kafka.Consumer
import com.example.demo.services.validation.SchemaValidator.Companion.SCHEMA_KAFKA_PRODUCT_SYNC
import org.springframework.boot.test.context.TestConfiguration import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
@@ -26,9 +28,11 @@ open class BaseUnitTest {
autoStartup = false, autoStartup = false,
autoOffsetReset = "none", autoOffsetReset = "none",
), ),
validation = KafkaProperties.Validation( )
schema = mapOf("product-sync" to "product/sync.json"),
), @Bean
fun validationProperties(): ValidationProperties = ValidationProperties(
schema = mapOf(SCHEMA_KAFKA_PRODUCT_SYNC to "kafka/product/sync.json"),
) )
} }
} }

View File

@@ -1,6 +1,7 @@
package com.example.demo.services.validation package com.example.demo.services.validation
import com.example.demo.BaseUnitTest import com.example.demo.BaseUnitTest
import com.example.demo.services.validation.SchemaValidator.Companion.SCHEMA_KAFKA_PRODUCT_SYNC
import com.example.demo.services.validation.exceptions.ElementNotValidException import com.example.demo.services.validation.exceptions.ElementNotValidException
import com.example.demo.services.validation.exceptions.SchemaNotFoundException import com.example.demo.services.validation.exceptions.SchemaNotFoundException
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@@ -41,7 +42,7 @@ class SchemaValidatorImpTest(
@JvmStatic @JvmStatic
fun validateDataProvider() = listOf( fun validateDataProvider() = listOf(
Arguments.of( Arguments.of(
"product-sync", SCHEMA_KAFKA_PRODUCT_SYNC,
""" """
{ {
"id": 123, "id": 123,
@@ -57,7 +58,7 @@ class SchemaValidatorImpTest(
null, null,
), ),
Arguments.of( // no id Arguments.of( // no id
"product-sync", SCHEMA_KAFKA_PRODUCT_SYNC,
""" """
{ {
"guid": "3a27e322-b5b6-427f-b761-a02284c1cfa4", "guid": "3a27e322-b5b6-427f-b761-a02284c1cfa4",
@@ -72,7 +73,7 @@ class SchemaValidatorImpTest(
ElementNotValidException::class, ElementNotValidException::class,
), ),
Arguments.of( // wrong guid Arguments.of( // wrong guid
"product-sync", SCHEMA_KAFKA_PRODUCT_SYNC,
""" """
{ {
"id": 213, "id": 213,