aboutsummaryrefslogtreecommitdiffstats
path: root/models/src
diff options
context:
space:
mode:
authorLiam Fallon <liam.fallon@est.tech>2021-12-02 14:50:10 +0000
committerGerrit Code Review <gerrit@onap.org>2021-12-02 14:50:10 +0000
commit8f54ee96bb0b86425d9c177eb991b1fd91000ad4 (patch)
tree5420dd96e5e62a572f977acae68583dd46e3acdb /models/src
parent5f28fc449cb304792aaac7fd4e925115e64565bb (diff)
parenta41acc89a8c7177014d1377c4457cd402ed7ee93 (diff)
Merge "Add support transaction in CL and ServiceTemplate Providers"
Diffstat (limited to 'models/src')
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProvider.java238
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java51
-rw-r--r--models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProviderTest.java254
-rw-r--r--models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProviderTest.java115
4 files changed, 395 insertions, 263 deletions
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProvider.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProvider.java
index a61b97fa8..7b6787c5d 100644
--- a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProvider.java
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProvider.java
@@ -22,146 +22,173 @@
package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.stream.Collectors;
+import java.util.Optional;
+import javax.persistence.EntityNotFoundException;
import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
-import org.onap.policy.models.base.PfAuthorative;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ControlLoopRepository;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplateRepository;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplatesRepository;
import org.onap.policy.models.base.PfConceptKey;
-import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.base.PfModelRuntimeException;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.onap.policy.models.provider.impl.AbstractModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates;
import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
/**
* This class provides information on control loop concepts in the database to callers.
*/
@Component
-public class ControlLoopProvider extends AbstractModelsProvider {
+@Transactional
+@AllArgsConstructor
+public class ControlLoopProvider {
+
+ private final ControlLoopRepository controlLoopRepository;
+ private final ToscaNodeTemplateRepository toscaNodeTemplateRepository;
+ private final ToscaNodeTemplatesRepository toscaNodeTemplatesRepository;
/**
- * Create a provider for control loops.
+ * Get Control Loop.
*
- * @param parameters the parameters for database access
- * @throws PfModelException on initiation errors
+ * @param controlLoopId the ID of the control loop to get
+ * @return the control loop found
+ * @throws PfModelException on errors getting the control loop
*/
- public ControlLoopProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
- super(parameters);
- this.init();
+ @Transactional(readOnly = true)
+ public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
+ try {
+ return controlLoopRepository.getById(controlLoopId.asConceptKey()).toAuthorative();
+ } catch (EntityNotFoundException e) {
+ throw new PfModelException(Status.NOT_FOUND, "ControlLoop not found", e);
+ }
}
/**
- * Get Control Loop.
+ * Find Control Loop by controlLoopId.
+ *
+ * @param name the name of the control loop to get, null to get all control loops
+ * @param version the version of the control loop to get, null to get all control loops
+ * @return the control loop found
+ * @throws PfModelException on errors getting the control loop
+ */
+ @Transactional(readOnly = true)
+ public Optional<ControlLoop> findControlLoop(@NonNull final String name, @NonNull final String version)
+ throws PfModelException {
+ return findControlLoop(new PfConceptKey(name, version));
+ }
+
+ /**
+ * Find Control Loop by controlLoopId.
*
* @param controlLoopId the ID of the control loop to get
* @return the control loop found
* @throws PfModelException on errors getting the control loop
*/
- public ControlLoop getControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
- var jpaControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopId.asConceptKey());
+ @Transactional(readOnly = true)
+ public Optional<ControlLoop> findControlLoop(final ToscaConceptIdentifier controlLoopId) throws PfModelException {
+ return findControlLoop(controlLoopId.asConceptKey());
+ }
- return jpaControlLoop == null ? null : jpaControlLoop.toAuthorative();
+ private Optional<ControlLoop> findControlLoop(@NonNull final PfConceptKey key) throws PfModelException {
+ try {
+ return controlLoopRepository.findById(key).map(JpaControlLoop::toAuthorative);
+ } catch (IllegalArgumentException e) {
+ throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e);
+ }
}
/**
- * Update Control Loop.
+ * Save Control Loop.
*
* @param controlLoop the control loop to update
* @return the updated control loop
* @throws PfModelException on errors updating the control loop
*/
- public ControlLoop updateControlLoop(final ControlLoop controlLoop) throws PfModelException {
- return updateControlLoops(Collections.singletonList(controlLoop)).get(0);
+ public ControlLoop saveControlLoop(final ControlLoop controlLoop) throws PfModelException {
+ try {
+ var result = controlLoopRepository
+ .save(ProviderUtils.getJpaAndValidate(controlLoop, JpaControlLoop::new, "control loop"));
+
+ // Return the saved participant
+ return result.toAuthorative();
+ } catch (IllegalArgumentException e) {
+ throw new PfModelException(Status.BAD_REQUEST, "Error in save controlLoop", e);
+ }
}
/**
- * Get Control Loops.
+ * Get All Control Loops.
*
- * @param name the name of the control loop to get, null to get all control loops
- * @param version the version of the control loop to get, null to get all control loops
- * @return the control loops found
+ * @return all control loops found
* @throws PfModelException on errors getting control loops
*/
- public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
+ @Transactional(readOnly = true)
+ public List<ControlLoop> getControlLoops() throws PfModelException {
- return asEntityList(getPfDao().getFiltered(JpaControlLoop.class, name, version));
+ return ProviderUtils.asEntityList(controlLoopRepository.findAll());
}
/**
- * Get filtered control loops.
+ * Get Control Loops.
*
- * @param filter the filter for the control loops to get
+ * @param name the name of the control loop to get, null to get all control loops
+ * @param version the version of the control loop to get, null to get all control loops
* @return the control loops found
* @throws PfModelException on errors getting control loops
*/
- public List<ControlLoop> getFilteredControlLoops(@NonNull final ToscaTypedEntityFilter<ControlLoop> filter) {
+ @Transactional(readOnly = true)
+ public List<ControlLoop> getControlLoops(final String name, final String version) throws PfModelException {
- return filter.filter(
- asEntityList(getPfDao().getFiltered(JpaControlLoop.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
+ return ProviderUtils.asEntityList(controlLoopRepository.getFiltered(JpaControlLoop.class, name, version));
}
/**
- * Creates control loops.
+ * Saves control loops.
*
* @param controlLoops a specification of the control loops to create
* @return the control loops created
* @throws PfModelException on errors creating control loops
*/
- public List<ControlLoop> createControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
-
- List<JpaControlLoop> jpaControlLoopList =
- ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loop");
-
- jpaControlLoopList.forEach(jpaControlLoop -> getPfDao().create(jpaControlLoop));
-
- // Return the created control loops
- List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
-
- for (ControlLoop controlLoop : controlLoops) {
- var jpaControlLoop = getPfDao().get(JpaControlLoop.class,
- new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
- returnControlLoops.add(jpaControlLoop.toAuthorative());
+ public List<ControlLoop> saveControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
+ try {
+ var result = controlLoopRepository
+ .saveAll(ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loops"));
+
+ // Return the saved participant
+ return ProviderUtils.asEntityList(result);
+ } catch (IllegalArgumentException e) {
+ throw new PfModelException(Status.BAD_REQUEST, "Error in save ControlLoops", e);
}
-
- return returnControlLoops;
}
/**
- * Updates control loops.
+ * Saves Instance Properties to the database.
*
- * @param controlLoops a specification of the control loops to update
- * @return the control loops updated
- * @throws PfModelException on errors updating control loops
+ * @param serviceTemplate the service template
+ * @return a Map of tosca node templates
*/
- public List<ControlLoop> updateControlLoops(@NonNull final List<ControlLoop> controlLoops) throws PfModelException {
-
- List<JpaControlLoop> jpaControlLoopList =
- ProviderUtils.getJpaAndValidateList(controlLoops, JpaControlLoop::new, "control loop");
+ public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
+ Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
- // Return the created control loops
- List<ControlLoop> returnControlLoops = new ArrayList<>(controlLoops.size());
+ var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
+ jpaToscaNodeTemplates.fromAuthorative(List.of(serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()));
- jpaControlLoopList.forEach(jpaControlLoop -> {
- var returnJpaControlLoop = getPfDao().update(jpaControlLoop);
- returnControlLoops.add(returnJpaControlLoop.toAuthorative());
- });
+ toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
+ serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach(savedNodeTemplates::put);
- return returnControlLoops;
+ return savedNodeTemplates;
}
/**
@@ -170,42 +197,23 @@ public class ControlLoopProvider extends AbstractModelsProvider {
* @param name the name of the control loop to delete
* @param version the version of the control loop to delete
* @return the control loop deleted
- * @throws PfModelRuntimeException on errors deleting the control loop
+ * @throws PfModelException on errors deleting the control loop
*/
- public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version) {
+ public ControlLoop deleteControlLoop(@NonNull final String name, @NonNull final String version)
+ throws PfModelException {
var controlLoopKey = new PfConceptKey(name, version);
+ var jpaDeleteControlLoop = controlLoopRepository.findById(controlLoopKey);
- var jpaDeleteControlLoop = getPfDao().get(JpaControlLoop.class, controlLoopKey);
-
- if (jpaDeleteControlLoop == null) {
+ if (jpaDeleteControlLoop.isEmpty()) {
String errorMessage =
"delete of control loop \"" + controlLoopKey.getId() + "\" failed, control loop does not exist";
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
}
- getPfDao().delete(jpaDeleteControlLoop);
+ controlLoopRepository.deleteById(controlLoopKey);
- return jpaDeleteControlLoop.toAuthorative();
- }
-
- /**
- * Saves Instance Properties to the database.
- * @param serviceTemplate the service template
- * @return a Map of tosca node templates
- */
- public Map<String, ToscaNodeTemplate> saveInstanceProperties(ToscaServiceTemplate serviceTemplate) {
-
- Map<String, ToscaNodeTemplate> savedNodeTemplates = new HashMap<>();
-
- var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
- jpaToscaNodeTemplates.fromAuthorative(Collections.singletonList(serviceTemplate.getToscaTopologyTemplate()
- .getNodeTemplates()));
-
- getPfDao().create(jpaToscaNodeTemplates);
- serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach(savedNodeTemplates::put);
-
- return savedNodeTemplates;
+ return jpaDeleteControlLoop.get().toAuthorative();
}
/**
@@ -214,23 +222,33 @@ public class ControlLoopProvider extends AbstractModelsProvider {
* @param filteredToscaNodeTemplateMap filtered node templates map to delete
* @param filteredToscaNodeTemplateList filtered node template list to delete
*/
- public void deleteInstanceProperties(
- Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap,
- List<ToscaNodeTemplate> filteredToscaNodeTemplateList) {
+ public void deleteInstanceProperties(Map<String, ToscaNodeTemplate> filteredToscaNodeTemplateMap,
+ List<ToscaNodeTemplate> filteredToscaNodeTemplateList) {
var jpaToscaNodeTemplates = new JpaToscaNodeTemplates();
- jpaToscaNodeTemplates.fromAuthorative(Collections.singletonList(filteredToscaNodeTemplateMap));
+ jpaToscaNodeTemplates.fromAuthorative(List.of(filteredToscaNodeTemplateMap));
- getPfDao().create(jpaToscaNodeTemplates);
+ toscaNodeTemplatesRepository.save(jpaToscaNodeTemplates);
filteredToscaNodeTemplateList.forEach(template -> {
var jpaToscaNodeTemplate = new JpaToscaNodeTemplate(template);
- getPfDao().delete(jpaToscaNodeTemplate);
+ toscaNodeTemplateRepository.delete(jpaToscaNodeTemplate);
});
}
/**
+ * Get All Node Templates.
+ *
+ * @return the list of node templates found
+ * @throws PfModelException on errors getting node templates
+ */
+ @Transactional(readOnly = true)
+ public List<ToscaNodeTemplate> getAllNodeTemplates() {
+ return ProviderUtils.asEntityList(toscaNodeTemplateRepository.findAll());
+ }
+
+ /**
* Get Node Templates.
*
* @param name the name of the node template to get, null to get all node templates
@@ -238,8 +256,10 @@ public class ControlLoopProvider extends AbstractModelsProvider {
* @return the node templates found
* @throws PfModelException on errors getting node templates
*/
+ @Transactional(readOnly = true)
public List<ToscaNodeTemplate> getNodeTemplates(final String name, final String version) {
- return asEntityList(getPfDao().getFiltered(JpaToscaNodeTemplate.class, name, version));
+ return ProviderUtils
+ .asEntityList(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version));
}
/**
@@ -249,23 +269,11 @@ public class ControlLoopProvider extends AbstractModelsProvider {
* @return the node templates found
* @throws PfModelException on errors getting node templates
*/
+ @Transactional(readOnly = true)
public List<ToscaNodeTemplate> getFilteredNodeTemplates(
@NonNull final ToscaTypedEntityFilter<ToscaNodeTemplate> filter) {
- return filter.filter(asEntityList(
- getPfDao().getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
+ return filter.filter(ProviderUtils.asEntityList(toscaNodeTemplateRepository
+ .getFiltered(JpaToscaNodeTemplate.class, filter.getName(), filter.getVersion())));
}
-
- /**
- * Convert JPA control loop list to an authorative control loop list.
- *
- * @param <T> the type of TOSCA entity
- * @param <J> the type of JPA TOSCA entity
- * @param jpaEntityList the list to convert
- * @return the authorative list
- */
- private <T extends ToscaEntity, J extends PfAuthorative<T>> List<T> asEntityList(List<J> jpaEntityList) {
- return jpaEntityList.stream().map(J::toAuthorative).collect(Collectors.toList());
- }
-
}
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java
index 6a6d20fe7..6b3749fc5 100644
--- a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java
@@ -23,21 +23,26 @@ package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provide
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import lombok.RequiredArgsConstructor;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaServiceTemplateRepository;
+import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
@Component
@RequiredArgsConstructor
+@Transactional
public class ServiceTemplateProvider {
- private final PolicyModelsProvider modelsProvider;
+ private final ToscaServiceTemplateRepository serviceTemplateRepository;
/**
* Create service template.
@@ -48,7 +53,13 @@ public class ServiceTemplateProvider {
*/
public ToscaServiceTemplate createServiceTemplate(final ToscaServiceTemplate serviceTemplate)
throws PfModelException {
- return modelsProvider.createServiceTemplate(serviceTemplate);
+ try {
+ var result = serviceTemplateRepository.save(ProviderUtils.getJpaAndValidate(serviceTemplate,
+ JpaToscaServiceTemplate::new, "toscaServiceTemplate"));
+ return result.toAuthorative();
+ } catch (IllegalArgumentException e) {
+ throw new PfModelException(Status.BAD_REQUEST, "Error in save serviceTemplate", e);
+ }
}
/**
@@ -60,7 +71,15 @@ public class ServiceTemplateProvider {
* @throws PfModelException on errors deleting policy types
*/
public ToscaServiceTemplate deleteServiceTemplate(final String name, final String version) throws PfModelException {
- return modelsProvider.deleteServiceTemplate(name, version);
+ var serviceTemplateKey = new PfConceptKey(name, version);
+ var jpaDelete = serviceTemplateRepository.findById(serviceTemplateKey);
+ if (jpaDelete.isEmpty()) {
+ String errorMessage = "delete of serviceTemplate \"" + serviceTemplateKey.getId()
+ + "\" failed, serviceTemplate does not exist";
+ throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+ serviceTemplateRepository.deleteById(serviceTemplateKey);
+ return jpaDelete.get().toAuthorative();
}
/**
@@ -71,12 +90,26 @@ public class ServiceTemplateProvider {
* @return the control loop definitions
* @throws PfModelException on errors getting control loop definitions
*/
+ @Transactional(readOnly = true)
public ToscaServiceTemplate getToscaServiceTemplate(String name, String version) throws PfModelException {
- var serviceTemplates = modelsProvider.getServiceTemplateList(name, version);
- if (serviceTemplates.isEmpty()) {
+ var serviceTemplateKey = new PfConceptKey(name, version);
+ var jpaServiceTemplates = serviceTemplateRepository.findById(serviceTemplateKey);
+ if (jpaServiceTemplates.isEmpty()) {
throw new PfModelException(Status.NOT_FOUND, "Control Loop definitions not found");
}
- return serviceTemplates.get(0);
+ return jpaServiceTemplates.get().toAuthorative();
+ }
+
+ /**
+ * Get service templates.
+ *
+ * @return the topology templates found
+ * @throws PfModelException on errors getting service templates
+ */
+ @Transactional(readOnly = true)
+ public List<ToscaServiceTemplate> getAllServiceTemplates() throws PfModelException {
+ var jpaList = serviceTemplateRepository.findAll();
+ return ProviderUtils.asEntityList(jpaList);
}
/**
@@ -87,9 +120,11 @@ public class ServiceTemplateProvider {
* @return the topology templates found
* @throws PfModelException on errors getting service templates
*/
+ @Transactional(readOnly = true)
public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
throws PfModelException {
- return modelsProvider.getServiceTemplateList(name, version);
+ var jpaList = serviceTemplateRepository.getFiltered(JpaToscaServiceTemplate.class, name, version);
+ return ProviderUtils.asEntityList(jpaList);
}
/**
diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProviderTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProviderTest.java
index 9805edb86..2b472c835 100644
--- a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProviderTest.java
+++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ControlLoopProviderTest.java
@@ -24,174 +24,255 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
-import org.junit.jupiter.api.AfterEach;
+import java.util.Optional;
+import javax.persistence.EntityNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ControlLoopRepository;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplateRepository;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplatesRepository;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.onap.policy.common.utils.resources.ResourceUtils;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
class ControlLoopProviderTest {
private static final String LIST_IS_NULL = "controlLoops is marked .*ull but is null";
+ private static final String OBJECT_IS_NULL = "controlLoop is marked non-null but is null";
+
+ private static final String ID_NAME = "PMSHInstance1";
+ private static final String ID_VERSION = "1.0.1";
+ private static final String ID_NAME_NOT_EXTST = "not_exist";
+ private static final String ID_NAME_NOT_VALID = "not_valid";
+
private static final Coder CODER = new StandardCoder();
private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json";
- private static final String UPDATE_CL_JSON = "src/test/resources/providers/UpdateControlLoops.json";
private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
- private static AtomicInteger dbNameCounter = new AtomicInteger();
- private PolicyModelsProviderParameters parameters;
- private ControlLoopProvider controlLoopProvider;
private ControlLoops inputControlLoops;
- private ControlLoops updateControlLoops;
+ private List<JpaControlLoop> inputControlLoopsJpa;
private String originalJson = ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON);
- private String updateClJson = ResourceUtils.getResourceAsString(UPDATE_CL_JSON);
@BeforeEach
void beforeSetupDao() throws Exception {
-
- parameters = new PolicyModelsProviderParameters();
- parameters.setDatabaseDriver("org.h2.Driver");
- parameters.setName("PolicyProviderParameterGroup");
- parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
- parameters.setDatabaseUrl("jdbc:h2:mem:controlLoopProviderTestDb" + dbNameCounter.getAndDecrement());
- parameters.setDatabaseUser("policy");
- parameters.setDatabasePassword("P01icY");
- parameters.setPersistenceUnit("ToscaConceptTest");
-
- controlLoopProvider = new ControlLoopProvider(parameters);
-
inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
- updateControlLoops = CODER.decode(updateClJson, ControlLoops.class);
- }
-
- @AfterEach
- void teardown() {
- controlLoopProvider.close();
+ inputControlLoopsJpa = ProviderUtils.getJpaAndValidateList(inputControlLoops.getControlLoopList(),
+ JpaControlLoop::new, "control loops");
}
@Test
- void testControlLoopCreate() throws Exception {
+ void testControlLoopsSave() throws Exception {
+ var controlLoopRepository = mock(ControlLoopRepository.class);
+ var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
+ mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
+
assertThatThrownBy(() -> {
- controlLoopProvider.createControlLoops(null);
+ controlLoopProvider.saveControlLoops(null);
}).hasMessageMatching(LIST_IS_NULL);
+ when(controlLoopRepository.saveAll(inputControlLoopsJpa)).thenReturn(inputControlLoopsJpa);
+
var createdControlLoops = new ControlLoops();
createdControlLoops
- .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
+ .setControlLoopList(controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList()));
assertEquals(inputControlLoops, createdControlLoops);
+
+ when(controlLoopRepository.saveAll(any())).thenThrow(IllegalArgumentException.class);
+
+ assertThatThrownBy(() -> {
+ controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList());
+ }).hasMessageMatching("Error in save ControlLoops");
}
@Test
- void testGetControlLoops() throws Exception {
+ void testControlLoopSave() throws Exception {
+ var controlLoopRepository = mock(ControlLoopRepository.class);
+ var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
+ mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
- List<ControlLoop> getResponse;
+ assertThatThrownBy(() -> {
+ controlLoopProvider.saveControlLoop(null);
+ }).hasMessageMatching(OBJECT_IS_NULL);
+
+ when(controlLoopRepository.save(inputControlLoopsJpa.get(0))).thenReturn(inputControlLoopsJpa.get(0));
+
+ var createdControlLoop = controlLoopProvider.saveControlLoop(inputControlLoops.getControlLoopList().get(0));
+
+ assertEquals(inputControlLoops.getControlLoopList().get(0), createdControlLoop);
+
+ when(controlLoopRepository.save(any())).thenThrow(IllegalArgumentException.class);
+
+ assertThatThrownBy(() -> {
+ controlLoopProvider.saveControlLoop(inputControlLoops.getControlLoopList().get(0));
+ }).hasMessageMatching("Error in save controlLoop");
+ }
+
+ @Test
+ void testGetControlLoops() throws Exception {
+ var controlLoopRepository = mock(ControlLoopRepository.class);
+ var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
+ mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
// Return empty list when no data present in db
- getResponse = controlLoopProvider.getControlLoops(null, null);
+ List<ControlLoop> getResponse = controlLoopProvider.getControlLoops();
assertThat(getResponse).isEmpty();
- controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
- var name = inputControlLoops.getControlLoopList().get(0).getName();
- var version = inputControlLoops.getControlLoopList().get(0).getVersion();
+ controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList());
+
+ var controlLoop0 = inputControlLoops.getControlLoopList().get(1);
+ var name = controlLoop0.getName();
+ var version = controlLoop0.getVersion();
+ var controlLoop1 = inputControlLoops.getControlLoopList().get(1);
+
+ when(controlLoopRepository.getFiltered(eq(JpaControlLoop.class), any(), any()))
+ .thenReturn(List.of(new JpaControlLoop(controlLoop0), new JpaControlLoop(controlLoop1)));
+ when(controlLoopRepository.findById(controlLoop0.getKey().asIdentifier().asConceptKey()))
+ .thenReturn(Optional.of(new JpaControlLoop(controlLoop0)));
+ when(controlLoopRepository.getById(controlLoop0.getKey().asIdentifier().asConceptKey()))
+ .thenReturn(new JpaControlLoop(controlLoop0));
+ when(controlLoopRepository.getFiltered(JpaControlLoop.class, name, version))
+ .thenReturn(List.of(new JpaControlLoop(controlLoop0)));
+ when(controlLoopRepository.findById(controlLoop1.getKey().asIdentifier().asConceptKey()))
+ .thenReturn(Optional.of(new JpaControlLoop(controlLoop1)));
+
assertEquals(1, controlLoopProvider.getControlLoops(name, version).size());
- var cl = new ControlLoop();
- cl = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier("PMSHInstance1", "1.0.1"));
+ var cl = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(ID_NAME, ID_VERSION)).get();
+ assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
+
+ cl = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME, ID_VERSION));
assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
- assertNull(controlLoopProvider.getControlLoop(new ToscaConceptIdentifier("invalid_name", "1.0.1")));
+ when(controlLoopRepository.getById(any())).thenThrow(EntityNotFoundException.class);
assertThatThrownBy(() -> {
- controlLoopProvider.getFilteredControlLoops(null);
- }).hasMessageMatching("filter is marked .*ull but is null");
+ controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION));
+ }).hasMessageMatching("ControlLoop not found");
- final ToscaTypedEntityFilter<ControlLoop> filter = ToscaTypedEntityFilter.<ControlLoop>builder()
- .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build();
- assertEquals(2, controlLoopProvider.getFilteredControlLoops(filter).size());
- }
+ cl = controlLoopProvider.findControlLoop(ID_NAME, ID_VERSION).get();
+ assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
- @Test
- void testUpdateControlLoops() throws Exception {
- assertThatThrownBy(() -> {
- controlLoopProvider.updateControlLoops(null);
- }).hasMessageMatching("controlLoops is marked .*ull but is null");
+ assertThat(controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION)))
+ .isEmpty();
- var existingControlLoops = new ControlLoops();
- existingControlLoops
- .setControlLoopList(controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList()));
- var updateResponse = new ControlLoop();
- updateResponse = controlLoopProvider.updateControlLoop(updateControlLoops.getControlLoopList().get(0));
+ when(controlLoopRepository.findById(any())).thenThrow(IllegalArgumentException.class);
- assertEquals(ControlLoopOrderedState.RUNNING, updateResponse.getOrderedState());
+ assertThatThrownBy(() -> {
+ controlLoopProvider.findControlLoop(ID_NAME_NOT_VALID, ID_VERSION);
+ }).hasMessageMatching("Not valid parameter");
}
@Test
void testDeleteControlLoop() throws Exception {
+ var controlLoopRepository = mock(ControlLoopRepository.class);
+ var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
+ mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
+
assertThatThrownBy(() -> {
- controlLoopProvider.deleteControlLoop("Invalid_name", "1.0.1");
+ controlLoopProvider.deleteControlLoop(ID_NAME_NOT_EXTST, ID_VERSION);
}).hasMessageMatching(".*.failed, control loop does not exist");
- ControlLoop deletedCl;
- List<ControlLoop> clList = controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
- var name = inputControlLoops.getControlLoopList().get(0).getName();
- var version = inputControlLoops.getControlLoopList().get(0).getVersion();
+ var controlLoop = inputControlLoops.getControlLoopList().get(0);
+ var name = controlLoop.getName();
+ var version = controlLoop.getVersion();
- deletedCl = controlLoopProvider.deleteControlLoop(name, version);
- assertEquals(clList.get(0), deletedCl);
+ when(controlLoopRepository.findById(new PfConceptKey(name, version)))
+ .thenReturn(Optional.of(inputControlLoopsJpa.get(0)));
+
+ ControlLoop deletedCl = controlLoopProvider.deleteControlLoop(name, version);
+ assertEquals(controlLoop, deletedCl);
}
@Test
void testDeleteAllInstanceProperties() throws Exception {
+ var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
+ mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
var toscaServiceTemplate = testControlLoopRead();
- controlLoopProvider.deleteInstanceProperties(
- controlLoopProvider.saveInstanceProperties(toscaServiceTemplate),
+ controlLoopProvider.deleteInstanceProperties(controlLoopProvider.saveInstanceProperties(toscaServiceTemplate),
controlLoopProvider.getNodeTemplates(null, null));
- assertThat(controlLoopProvider.getControlLoops(null, null)).isEmpty();
+ assertThat(controlLoopProvider.getControlLoops()).isEmpty();
}
@Test
void testSaveAndDeleteInstanceProperties() throws Exception {
+ var toscaNodeTemplatesRepository = mock(ToscaNodeTemplatesRepository.class);
+ var toscaNodeTemplateRepository = mock(ToscaNodeTemplateRepository.class);
+ var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
+ toscaNodeTemplateRepository, toscaNodeTemplatesRepository);
var toscaServiceTest = testControlLoopRead();
- controlLoopProvider.createControlLoops(inputControlLoops.getControlLoopList());
controlLoopProvider.saveInstanceProperties(toscaServiceTest);
- assertThat(controlLoopProvider.getNodeTemplates(
- "org.onap.policy.controlloop.PolicyControlLoopParticipant",
- "2.3.1")).isNotEmpty();
-
- controlLoopProvider.deleteInstanceProperties(
- controlLoopProvider.saveInstanceProperties(toscaServiceTest),
- controlLoopProvider.getNodeTemplates(
- "org.onap.policy.controlloop.PolicyControlLoopParticipant",
- "2.3.1"));
-
- assertThat(controlLoopProvider.getNodeTemplates(
- "org.onap.policy.controlloop.PolicyControlLoopParticipant",
- "2.3.1")).isEmpty();
+ verify(toscaNodeTemplatesRepository).save(any());
+
+ var name = "org.onap.policy.controlloop.PolicyControlLoopParticipant";
+ var version = "2.3.1";
+ var elem = toscaServiceTest.getToscaTopologyTemplate().getNodeTemplates().get(name);
+ when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version))
+ .thenReturn(List.of(new JpaToscaNodeTemplate(elem)));
+
+ var filtered = controlLoopProvider.getNodeTemplates(name, version);
+ verify(toscaNodeTemplateRepository).getFiltered(JpaToscaNodeTemplate.class, name, version);
+
+ controlLoopProvider.deleteInstanceProperties(controlLoopProvider.saveInstanceProperties(toscaServiceTest),
+ filtered);
+
+ verify(toscaNodeTemplateRepository).delete(any());
}
@Test
void testGetNodeTemplates() throws Exception {
- //Getting all nodes
- List<ToscaNodeTemplate> listNodes = controlLoopProvider.getNodeTemplates(null, null);
+ var toscaNodeTemplateRepository = mock(ToscaNodeTemplateRepository.class);
+ var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
+ toscaNodeTemplateRepository, mock(ToscaNodeTemplatesRepository.class));
+
+ var toscaNodeTemplate0 = new JpaToscaNodeTemplate(new PfConceptKey(ID_NAME, ID_VERSION));
+ var toscaNodeTemplate1 = new JpaToscaNodeTemplate(new PfConceptKey("PMSHInstance2", ID_VERSION));
+
+ when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, null, null))
+ .thenReturn(List.of(toscaNodeTemplate0, toscaNodeTemplate1));
+ when(toscaNodeTemplateRepository.findAll()).thenReturn(List.of(toscaNodeTemplate0, toscaNodeTemplate1));
+ when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, ID_NAME, ID_VERSION))
+ .thenReturn(List.of(toscaNodeTemplate0));
+
+ // Getting all nodes
+ var listNodes = controlLoopProvider.getNodeTemplates(null, null);
+ assertNotNull(listNodes);
+ assertThat(listNodes).hasSize(2);
+
+ listNodes = controlLoopProvider.getNodeTemplates(ID_NAME, ID_VERSION);
+ assertNotNull(listNodes);
+ assertThat(listNodes).hasSize(1);
+
+ listNodes = controlLoopProvider.getAllNodeTemplates();
+ assertNotNull(listNodes);
+ assertThat(listNodes).hasSize(2);
+
+ var nodeTemplateFilter =
+ ToscaTypedEntityFilter.<ToscaNodeTemplate>builder().name(ID_NAME).version(ID_VERSION).build();
+
+ listNodes = controlLoopProvider.getFilteredNodeTemplates(nodeTemplateFilter);
assertNotNull(listNodes);
+ assertThat(listNodes).hasSize(1);
assertThatThrownBy(() -> {
controlLoopProvider.getFilteredNodeTemplates(null);
@@ -203,8 +284,7 @@ class ControlLoopProviderTest {
}
private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
- String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
- ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
- return serviceTemplate;
+ var controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
+ return yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
}
}
diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProviderTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProviderTest.java
index 7f2731abf..523a4fb88 100644
--- a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProviderTest.java
+++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProviderTest.java
@@ -29,13 +29,17 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
+import java.util.Optional;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaServiceTemplateRepository;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
class ServiceTemplateProviderTest {
@@ -44,28 +48,33 @@ class ServiceTemplateProviderTest {
private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder();
+ private static ToscaServiceTemplate inputServiceTemplate;
+
+ @BeforeAll
+ static void loadServiceTemplate() {
+ inputServiceTemplate = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
+ }
+
@Test
void testGetCommonOrInstancePropertiesFromNodeTypes() throws PfModelException {
- var serviceTemplateProvider = new ServiceTemplateProvider(mock(PolicyModelsProvider.class));
+ var serviceTemplateRepository = mock(ToscaServiceTemplateRepository.class);
+ var serviceTemplateProvider = new ServiceTemplateProvider(serviceTemplateRepository);
- var serviceTemplate = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
-
- var result = serviceTemplateProvider.getCommonOrInstancePropertiesFromNodeTypes(true, serviceTemplate);
+ var result = serviceTemplateProvider.getCommonOrInstancePropertiesFromNodeTypes(true, inputServiceTemplate);
assertNotNull(result);
assertThat(result).hasSize(8);
}
@Test
void testGetDerivedCommonOrInstanceNodeTemplates() throws PfModelException {
- var serviceTemplateProvider = new ServiceTemplateProvider(mock(PolicyModelsProvider.class));
-
- var serviceTemplate = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
+ var serviceTemplateRepository = mock(ToscaServiceTemplateRepository.class);
+ var serviceTemplateProvider = new ServiceTemplateProvider(serviceTemplateRepository);
var commonOrInstanceNodeTypeProps =
- serviceTemplateProvider.getCommonOrInstancePropertiesFromNodeTypes(true, serviceTemplate);
+ serviceTemplateProvider.getCommonOrInstancePropertiesFromNodeTypes(true, inputServiceTemplate);
var result = serviceTemplateProvider.getDerivedCommonOrInstanceNodeTemplates(
- serviceTemplate.getToscaTopologyTemplate().getNodeTemplates(), commonOrInstanceNodeTypeProps);
+ inputServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(), commonOrInstanceNodeTypeProps);
assertNotNull(result);
assertThat(result).hasSize(8);
@@ -73,74 +82,74 @@ class ServiceTemplateProviderTest {
@Test
void testCreateServiceTemplate() throws PfModelException {
- var modelsProvider = mock(PolicyModelsProvider.class);
- var serviceTemplateProvider = new ServiceTemplateProvider(modelsProvider);
+ var serviceTemplateRepository = mock(ToscaServiceTemplateRepository.class);
+ var serviceTemplateProvider = new ServiceTemplateProvider(serviceTemplateRepository);
- var serviceTemplate = new ToscaServiceTemplate();
- when(modelsProvider.createServiceTemplate(serviceTemplate)).thenReturn(serviceTemplate);
+ var jpaServiceTemplate = ProviderUtils.getJpaAndValidate(inputServiceTemplate, JpaToscaServiceTemplate::new,
+ "toscaServiceTemplate");
+ when(serviceTemplateRepository.save(jpaServiceTemplate)).thenReturn(jpaServiceTemplate);
- var result = serviceTemplateProvider.createServiceTemplate(serviceTemplate);
+ var result = serviceTemplateProvider.createServiceTemplate(inputServiceTemplate);
- assertThat(result).isEqualTo(serviceTemplate);
+ assertThat(result).isEqualTo(jpaServiceTemplate.toAuthorative());
}
@Test
void testDeleteServiceTemplate() throws PfModelException {
- var serviceTemplate = new ToscaServiceTemplate();
- serviceTemplate.setName("Name");
- serviceTemplate.setVersion("1.0.0");
- var modelsProvider = mock(PolicyModelsProvider.class);
- when(modelsProvider.deleteServiceTemplate(serviceTemplate.getName(), serviceTemplate.getVersion()))
- .thenReturn(serviceTemplate);
-
- var serviceTemplateProvider = new ServiceTemplateProvider(modelsProvider);
- var result =
- serviceTemplateProvider.deleteServiceTemplate(serviceTemplate.getName(), serviceTemplate.getVersion());
-
- assertThat(result).isEqualTo(serviceTemplate);
+ var jpaServiceTemplate = ProviderUtils.getJpaAndValidate(inputServiceTemplate, JpaToscaServiceTemplate::new,
+ "toscaServiceTemplate");
+ var serviceTemplateRepository = mock(ToscaServiceTemplateRepository.class);
+ when(serviceTemplateRepository
+ .findById(new PfConceptKey(inputServiceTemplate.getName(), inputServiceTemplate.getVersion())))
+ .thenReturn(Optional.of(jpaServiceTemplate));
+
+ var serviceTemplateProvider = new ServiceTemplateProvider(serviceTemplateRepository);
+ var result = serviceTemplateProvider.deleteServiceTemplate(inputServiceTemplate.getName(),
+ inputServiceTemplate.getVersion());
+
+ assertThat(result).isEqualTo(jpaServiceTemplate.toAuthorative());
}
@Test
void testGetServiceTemplateListEmpty() throws PfModelException {
- var modelsProvider = mock(PolicyModelsProvider.class);
- when(modelsProvider.getServiceTemplateList(any(String.class), any(String.class))).thenReturn(List.of());
+ var serviceTemplateRepository = mock(ToscaServiceTemplateRepository.class);
+ when(serviceTemplateRepository.findById(any())).thenReturn(Optional.empty());
- var serviceTemplateProvider = new ServiceTemplateProvider(modelsProvider);
+ var serviceTemplateProvider = new ServiceTemplateProvider(serviceTemplateRepository);
assertThatThrownBy(() -> serviceTemplateProvider.getToscaServiceTemplate("Name", "1.0.0"))
.hasMessage("Control Loop definitions not found");
}
@Test
void testGetServiceTemplateList() throws PfModelException {
- var serviceTemplate = new ToscaServiceTemplate();
- serviceTemplate.setName("Name");
- serviceTemplate.setVersion("1.0.0");
- var modelsProvider = mock(PolicyModelsProvider.class);
- when(modelsProvider.getServiceTemplateList(serviceTemplate.getName(), serviceTemplate.getVersion()))
- .thenReturn(List.of(serviceTemplate));
-
- var serviceTemplateProvider = new ServiceTemplateProvider(modelsProvider);
- var result = serviceTemplateProvider.getToscaServiceTemplate(serviceTemplate.getName(),
- serviceTemplate.getVersion());
-
- assertThat(result).isEqualTo(serviceTemplate);
+ var jpaServiceTemplate = ProviderUtils.getJpaAndValidate(inputServiceTemplate, JpaToscaServiceTemplate::new,
+ "toscaServiceTemplate");
+ var serviceTemplateRepository = mock(ToscaServiceTemplateRepository.class);
+ when(serviceTemplateRepository
+ .findById(new PfConceptKey(inputServiceTemplate.getName(), inputServiceTemplate.getVersion())))
+ .thenReturn(Optional.of(jpaServiceTemplate));
+
+ var serviceTemplateProvider = new ServiceTemplateProvider(serviceTemplateRepository);
+ var result = serviceTemplateProvider.getToscaServiceTemplate(inputServiceTemplate.getName(),
+ inputServiceTemplate.getVersion());
+
+ assertThat(result).isEqualTo(jpaServiceTemplate.toAuthorative());
}
@Test
void testGetServiceTemplate() throws PfModelException {
- var serviceTemplate = new ToscaServiceTemplate();
- serviceTemplate.setName("Name");
- serviceTemplate.setVersion("1.0.0");
- var modelsProvider = mock(PolicyModelsProvider.class);
- when(modelsProvider.getServiceTemplateList(serviceTemplate.getName(), serviceTemplate.getVersion()))
- .thenReturn(List.of(serviceTemplate));
+ var jpaServiceTemplate = ProviderUtils.getJpaAndValidate(inputServiceTemplate, JpaToscaServiceTemplate::new,
+ "toscaServiceTemplate");
+ var serviceTemplateRepository = mock(ToscaServiceTemplateRepository.class);
+ when(serviceTemplateRepository.getFiltered(JpaToscaServiceTemplate.class, inputServiceTemplate.getName(),
+ inputServiceTemplate.getVersion())).thenReturn(List.of(jpaServiceTemplate));
- var serviceTemplateProvider = new ServiceTemplateProvider(modelsProvider);
- var result =
- serviceTemplateProvider.getServiceTemplateList(serviceTemplate.getName(), serviceTemplate.getVersion());
+ var serviceTemplateProvider = new ServiceTemplateProvider(serviceTemplateRepository);
+ var result = serviceTemplateProvider.getServiceTemplateList(inputServiceTemplate.getName(),
+ inputServiceTemplate.getVersion());
assertThat(result).hasSize(1);
- assertThat(result.get(0)).isEqualTo(serviceTemplate);
+ assertThat(result.get(0)).isEqualTo(jpaServiceTemplate.toAuthorative());
}
/**