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());
    }
}

Using Dao

There is a way to use the whole Policy Framework implementation. It needs to create a new DefaultPfDao class that uses the Entity Manger with no "begin transaction" and no "commit".

Code Block
languagejava
titleDefaultPfDao
linenumberstrue
public class DefaultPfDao implements PfDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);

    private final EntityManager entityManager;

    /**
     * Constructor.
     *
     * @param entityManager EntityManager
     */
    public DefaultPfDao(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public void init(final DaoParameters daoParameters) throws PfModelException {
        // Not need
    }

    @Override
    public <T extends PfConcept> void create(final T obj) {
        if (obj == null) {
            return;
        }
        entityManager.merge(obj);
    }

    @Override
    public <T extends PfConcept> void delete(final T obj) {
        entityManager.remove(entityManager.contains(obj) ? obj : entityManager.merge(obj));
    }


EntityManager is not thread safe, and it need to use @PersistenceContext annotation, so Spring will inject a thread-safe proxy for the actual transactional EntityManager.

Code Block
languagejava
titlePolicyModelsProviderImpl
linenumberstrue
@Service
@Transactional
public class PolicyModelsProviderImpl implements PolicyModelsProvider {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public ToscaServiceTemplate createServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate) {
        LOGGER.debug("->createServiceTemplate: serviceTemplate={}", serviceTemplate);
        try {

            ToscaServiceTemplate createdServiceTemplate =
                    new SimpleToscaProvider().appendToServiceTemplate(new DefaultPfDao(entityManager),
                            new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative();

            LOGGER.debug("<-createServiceTemplate: createdServiceTemplate={}", createdServiceTemplate);
            return createdServiceTemplate;
        } catch (Exception pfme) {
            throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, pfme.getMessage(), pfme);
        }
    }

Spring Service

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

...