Skip to content
Snippets Groups Projects
Commit 44ec49b0 authored by Krzysztof Witukiewicz's avatar Krzysztof Witukiewicz
Browse files

OZG-6303 Replace XdomeaPropertiesValidator

There can be only one configurationPropertiesValidator, so there could be conflicts if validation of xdomea-properties was done by XdomeaPropertiesValidator. It's not a problem, if XdomaProperties implements Validator itself (see ConfigurationPropertiesBindingPostProcessor).
parent 63211420
No related branches found
No related tags found
1 merge request!4Replace XdomeaPropertiesValidator
...@@ -29,7 +29,6 @@ import java.util.Map; ...@@ -29,7 +29,6 @@ import java.util.Map;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.validation.Validator;
import jakarta.xml.bind.Marshaller; import jakarta.xml.bind.Marshaller;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
...@@ -44,11 +43,6 @@ class ExportConfiguration { ...@@ -44,11 +43,6 @@ class ExportConfiguration {
private final XdomeaNamespacePrefixMapper prefixMapper; private final XdomeaNamespacePrefixMapper prefixMapper;
@Bean
static Validator configurationPropertiesValidator() {
return new XdomeaPropertiesValidator();
}
@Bean @Bean
Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller() {
var marshaller = new Jaxb2Marshaller(); var marshaller = new Jaxb2Marshaller();
......
...@@ -23,10 +23,14 @@ ...@@ -23,10 +23,14 @@
*/ */
package de.ozgcloud.archive.export; package de.ozgcloud.archive.export;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import de.ozgcloud.common.errorhandling.TechnicalException;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
...@@ -35,15 +39,50 @@ import lombok.Setter; ...@@ -35,15 +39,50 @@ import lombok.Setter;
@Configuration @Configuration
@ConfigurationProperties("ozgcloud.xdomea") @ConfigurationProperties("ozgcloud.xdomea")
@Validated
@Builder @Builder
@Getter @Getter
@Setter @Setter
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class XdomeaProperties { public class XdomeaProperties implements Validator {
private static final String PROP_BEHOERDENSCHLUESSEL_URI = "behoerdenschluesselUri";
private static final String PROP_BEHOERDENSCHLUESSEL_VERSION = "behoerdenschluesselVersion";
private String behoerdenschluessel; private String behoerdenschluessel;
private String behoerdenschluesselUri; private String behoerdenschluesselUri;
private String behoerdenschluesselVersion; private String behoerdenschluesselVersion;
@Override
public boolean supports(Class<?> clazz) {
return XdomeaProperties.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
var properties = (XdomeaProperties) target;
validateBehoerdenschluesselProperties(errors, properties);
}
private void validateBehoerdenschluesselProperties(Errors errors, XdomeaProperties properties) {
if (StringUtils.isNotBlank(properties.getBehoerdenschluessel())) {
validateNotBlank(PROP_BEHOERDENSCHLUESSEL_URI, properties, errors);
validateNotBlank(PROP_BEHOERDENSCHLUESSEL_VERSION, properties, errors);
}
}
private void validateNotBlank(String propertyName, XdomeaProperties properties, Errors errors) {
getPropertyValue(propertyName, properties);
if (StringUtils.isBlank(getPropertyValue(propertyName, properties))) {
errors.rejectValue(propertyName, String.format("ozgcloud.xdomea.%s.empty", propertyName), String.format("%s must be set", propertyName));
}
}
private static String getPropertyValue(String fieldName, XdomeaProperties properties) {
try {
return BeanUtils.getSimpleProperty(properties, fieldName);
} catch (Exception e) {
throw new TechnicalException("Property does not exist", e);
}
}
} }
/*
* Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.archive.export;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import de.ozgcloud.common.errorhandling.TechnicalException;
public class XdomeaPropertiesValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return XdomeaProperties.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
var properties = (XdomeaProperties) target;
validateBehoerdenschluesselProperties(errors, properties);
}
private void validateBehoerdenschluesselProperties(Errors errors, XdomeaProperties properties) {
if (StringUtils.isNotBlank(properties.getBehoerdenschluessel())) {
validateNotBlank("behoerdenschluesselUri", properties, errors);
validateNotBlank("behoerdenschluesselVersion", properties, errors);
}
}
private void validateNotBlank(String propertyName, XdomeaProperties properties, Errors errors) {
getPropertyValue(propertyName, properties);
if (StringUtils.isBlank(getPropertyValue(propertyName, properties))) {
errors.rejectValue(propertyName, String.format("ozgcloud.xdomea.%s.empty", propertyName), String.format("%s must be set", propertyName));
}
}
private static String getPropertyValue(String fieldName, XdomeaProperties properties) {
try {
return BeanUtils.getSimpleProperty(properties, fieldName);
} catch (Exception e) {
throw new TechnicalException("Property does not exist", e);
}
}
}
...@@ -29,20 +29,16 @@ import org.junit.jupiter.api.Nested; ...@@ -29,20 +29,16 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.mockito.Spy;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
class XdomeaPropertiesValidatorTest { class XdomeaPropertiesTest {
@Spy
private XdomeaPropertiesValidator validator;
@Nested @Nested
class TestSupports { class TestSupports {
@Test @Test
void shouldSupportXdomeaProperties() { void shouldSupportXdomeaProperties() {
var supports = validator.supports(XdomeaProperties.class); var supports = XdomeaPropertiesTestFactory.create().supports(XdomeaProperties.class);
assertThat(supports).isTrue(); assertThat(supports).isTrue();
} }
...@@ -155,7 +151,7 @@ class XdomeaPropertiesValidatorTest { ...@@ -155,7 +151,7 @@ class XdomeaPropertiesValidatorTest {
} }
private Errors validate(XdomeaProperties properties) { private Errors validate(XdomeaProperties properties) {
return validator.validateObject(properties); return properties.validateObject(properties);
} }
} }
} }
/*
* Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.archive.export;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import de.ozgcloud.common.test.ITCase;
@ITCase
class XdomeaPropertiesValidatorITCase {
@Test
void shouldExistInApplicationContext(ApplicationContext context) {
assertThat(context.getBean("configurationPropertiesValidator")).isNotNull().isInstanceOf(XdomeaPropertiesValidator.class);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment