Versions Compared

Key

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

...

Code Block
languagejava
titleJpaToscaPropertyRepositoryTest
linenumberstrue
@ExtendWith(SpringExtension.class)
@DataJpaTest
@Import(value = ParticipantPolicyParameters.class)
@TestPropertySource(locations = {"classpath:application_test.properties"})
class JpaToscaPropertyRepositoryTest {

    @Autowired
    private JpaToscaPropertyRepository toscaPropertyRepository;

    @Test
    void test() {
        JpaToscaProperty toscaProperty = new JpaToscaProperty();
        PfReferenceKey key = toscaProperty.getKey();

        Map<String, String> metadata = new HashMap<>();
        metadata.put("Key", "Value");
        metadata.put("K", "V");

        List<JpaToscaConstraint> constraints = new ArrayList<>();
        String[] list = new String[] {"First", "Second"};
        constraints.add(new JpaToscaConstraintValidValues(Stream.of(list).collect(Collectors.toList())));

        toscaProperty.setDefaultValue("DefaultValue");
        toscaProperty.setDescription("Description");
        toscaProperty.setRequired(true);
        toscaProperty.setStatus(ToscaProperty.Status.EXPERIMENTAL);
        toscaProperty.setMetadata(metadata);
        toscaProperty.setConstraints(constraints);
        toscaPropertyRepository.save(toscaProperty);

        Optional<JpaToscaProperty> opt = toscaPropertyRepository.findById(key);
        assertThat(opt).isNotEmpty();
        JpaToscaProperty actual = opt.get();
        assertThat(actual.getDefaultValue()).isEqualTo(toscaProperty.getDefaultValue());
        assertThat(actual.getDescription()).isEqualTo(toscaProperty.getDescription());
        assertThat(actual.isRequired()).isEqualTo(toscaProperty.isRequired());
        assertThat(actual.getStatus()).isEqualTo(toscaProperty.getStatus());
        assertThat(actual.getType()).isEqualTo(toscaProperty.getType());
        assertThat(actual.getConstraints()).isEqualTo(toscaProperty.getConstraints());
    }
}

...

Spring Service

Example how to convert ControlLoopInstantiationProvider class in Spring style using "@Services@Service" and "@Transactional"

Code Block
languagejava
titleControlLoopInstantiationProvider
linenumberstrue
@Service
public class ControlLoopInstantiationProvider {
    private final ControlLoopProvider controlLoopProvider;
    private final CommissioningProvider commissioningProvider;

    /**
     * Create a instantiation provider.
     *
     * @param databaseProviderParameters the parameters for database access
     */
    public ControlLoopInstantiationProvider(ControlLoopProvider controlLoopProvider,
            CommissioningProvider commissioningProvider) {
        this.controlLoopProvider = controlLoopProvider;
        this.commissioningProvider = commissioningProvider;
    }

    @Transactional
    public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {
        for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
            ControlLoop checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier());
            if (checkControlLoop != null) {
                throw new PfModelException(Response.Status.BAD_REQUEST,
                        controlLoop.getKey().asIdentifier() + " already defined");
            }
        }
        BeanValidationResult validationResult = validateControlLoops(controlLoops);
        if (!validationResult.isValid()) {
            throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
        }
        controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());

        InstantiationResponse response = new InstantiationResponse();
        response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
                .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));

        return response;
    }

...