Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
@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 In a scenario that we need to include into a POJO shown before, a class that implement org.onap.policy.common.parameters.BeanValidationResult to Spring validator using ParameterGroup interface, we need to add the org.onap.policy.common.parameters.validation.ParameterGroupConstraint annotation. That annotation is configured to use  ParameterGroupValidator that handles the conversion of a org.onap.policy.common.parameters.BeanValidationResult to a Spring validation.

The code below shown how to add TopicParameterGroup parameter into ClRuntimeParameterGroup:

Code Block
languagejava
    @NotNull
    @ParameterGroupConstraint
    private TopicParameterGroup topicParameterGroup;


ClRuntimeParameterGroup defined before A bean configured with ConfigurationProperties, is automatically a Spring component and could be injected into other Spring components. The code below shown an example:

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


The below code shows below, is an example of Unit Test validation of the POJO ClRuntimeParameterGroup which contains the values mapped from properties file.:

Code Block
languagejava
    private ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();

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

...