You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

This article explains how to implement handling and validation of common parameter into the Policy Framework Components using Spring boot framework.

A component can use org.springframework.core.env.Environment component directly.

Environment component is not a good approach because there is not type conversion and error checking, but it could be useful when the name of the property you need to access changes dynamically.

@Component
@RequiredArgsConstructor
public class Example {

private Environment env;
....

public void method(String pathPropertyName) {
    .....  
    String path = env.getProperty(pathPropertyName);
    .....
}


A component can use org.springframework.beans.factory.annotation.Value, which reads from properties, performs a type conversion and injects the value into the filed. There is not error checking, but it can assign default value if the property is not defined.

    @Value("${security.enable-csrf:true}")
    private boolean csrfEnabled = true;


Other approach using property configuration.

    @Scheduled(
            fixedRateString = "${runtime.participantParameters.heartBeatMs}",
            initialDelayString = "${runtime.participantParameters.heartBeatMs}")
    public void schedule() { 
}


@ConfigurationProperties can be used to map values from .properties( .yml also supported) to a POJO. It performs all type conversion and error checking using validation javax.validation.constraints

@Validated
@Getter
@Setter
@ConfigurationProperties(prefix = "runtime")
public class ClRuntimeParameterGroup {
    @Min(100)
    private long heartBeatMs;

    @Valid
    @Positive
    private long reportingTimeIntervalMs;

    @Valid
    @NotNull
    private ParticipantUpdateParameters updateParameters;

    @NotBlank
    private String description; 
}


Convert org.onap.policy.common.parameters.BeanValidationResult to Spring validator using org.onap.policy.common.parameters.validation.ParameterGroupConstraint

    @NotNull
    @ParameterGroupConstraint
    private TopicParameterGroup topicParameterGroup;


ClRuntimeParameterGroup defined before could be injected into components.

@Component
@RequiredArgsConstructor
public class Example {
 
private ClRuntimeParameterGroup parameters;
....
 
public void method() {
    ..... 
    long heartBeatMs = parameters.getHeartBeatMs();
    .....
}


The below code shows an example of validation of the POJO ClRuntimeParameterGroup which contains the values mapped from properties file.

    private ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();

    @Test
    void testParameters_NullTopicParameterGroup() {
        final ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup();
        parameters.setTopicParameterGroup(null);
        assertThat(validatorFactory.getValidator().validate(parameters)).isNotEmpty();
    }



This page is Work in Progress


  • No labels