summaryrefslogtreecommitdiffstats
path: root/models/src
diff options
context:
space:
mode:
authorFrancescoFioraEst <francesco.fiora@est.tech>2022-12-07 16:22:45 +0000
committerFrancesco Fiora <francesco.fiora@est.tech>2022-12-08 10:08:43 +0000
commita50038a6ed673819b45e9af35eb203e6e24dec99 (patch)
tree7cd5cf908e5367f03d938bef91fe0778bf0ec436 /models/src
parenta0ccd552fa1c6ddbef3a1e8d455ce256395ee97a (diff)
Add instanceId support in AutomationCompositionProvider
As part of the endpoint refactoring of InstantiationController, AutomationCompositionProvider have to support instanceId in CRUD operations. InstanceId will be temporary add as normal field and it will became primary key in next Jira task. Issue-ID: POLICY-4478 Change-Id: I0f7a1ab7683671403831cffdfe6db37eea569c74 Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'models/src')
-rw-r--r--models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java4
-rw-r--r--models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java2
-rw-r--r--models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java45
-rw-r--r--models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java60
-rw-r--r--models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java5
-rw-r--r--models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionTest.java66
-rw-r--r--models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java64
-rw-r--r--models/src/test/resources/providers/TestAutomationCompositions.json2
8 files changed, 154 insertions, 94 deletions
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java
index c3245d27c..b19f54c3b 100644
--- a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java
@@ -38,6 +38,9 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
@EqualsAndHashCode(callSuper = true)
public class AutomationComposition extends ToscaEntity implements Comparable<AutomationComposition> {
@NonNull
+ private UUID instanceId;
+
+ @NonNull
private UUID compositionId;
@NonNull
@@ -58,6 +61,7 @@ public class AutomationComposition extends ToscaEntity implements Comparable<Aut
*/
public AutomationComposition(final AutomationComposition otherAutomationComposition) {
super(otherAutomationComposition);
+ this.instanceId = otherAutomationComposition.instanceId;
this.compositionId = otherAutomationComposition.compositionId;
this.state = otherAutomationComposition.state;
this.orderedState = otherAutomationComposition.orderedState;
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java
index 4ecb8ce32..534c909aa 100644
--- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java
@@ -20,6 +20,7 @@
package org.onap.policy.clamp.models.acm.messages.rest.instantiation;
+import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@@ -33,5 +34,6 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
@Setter
@ToString(callSuper = true)
public class InstantiationResponse extends SimpleResponse {
+ private UUID instanceId;
ToscaConceptIdentifier affectedAutomationComposition;
}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java
index 8268e9066..b87bad4e0 100644
--- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java
@@ -23,7 +23,6 @@ package org.onap.policy.clamp.models.acm.persistence.concepts;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@@ -40,7 +39,6 @@ import lombok.EqualsAndHashCode;
import lombok.NonNull;
import org.apache.commons.lang3.ObjectUtils;
import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
-import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
import org.onap.policy.common.parameters.annotations.NotNull;
@@ -66,11 +64,16 @@ import org.onap.policy.models.base.validation.annotations.VerifyKey;
public class JpaAutomationComposition extends PfConcept implements PfAuthorative<AutomationComposition> {
private static final long serialVersionUID = -4725410933242154805L;
+ @Column
+ @NotNull
+ private String instanceId;
+
@EmbeddedId
@VerifyKey
@NotNull
private PfConceptKey key;
+ @Column
@NotNull
private String compositionId;
@@ -97,29 +100,23 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative
* The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
*/
public JpaAutomationComposition() {
- this(new PfConceptKey());
- }
-
- /**
- * The Key Constructor creates a {@link JpaAutomationComposition} object with the given concept key.
- *
- * @param key the key
- */
- public JpaAutomationComposition(@NonNull final PfConceptKey key) {
- this(key, UUID.randomUUID().toString(), AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
+ this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(),
+ AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
}
/**
* The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
*
+ * @param instanceId The UUID of the automation composition instance
* @param key the key
* @param compositionId the TOSCA compositionId of the automation composition definition
* @param state the state of the automation composition
* @param elements the elements of the automation composition in participants
*/
- public JpaAutomationComposition(@NonNull final PfConceptKey key, @NonNull final String compositionId,
- @NonNull final AutomationCompositionState state,
+ public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
+ @NonNull final String compositionId, @NonNull final AutomationCompositionState state,
@NonNull final Map<UUID, JpaAutomationCompositionElement> elements) {
+ this.instanceId = instanceId;
this.key = key;
this.compositionId = compositionId;
this.state = state;
@@ -133,6 +130,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative
*/
public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
super(copyConcept);
+ this.instanceId = copyConcept.instanceId;
this.key = new PfConceptKey(copyConcept.key);
this.compositionId = copyConcept.compositionId;
this.state = copyConcept.state;
@@ -156,6 +154,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative
public AutomationComposition toAuthorative() {
var automationComposition = new AutomationComposition();
+ automationComposition.setInstanceId(UUID.fromString(instanceId));
automationComposition.setName(getKey().getName());
automationComposition.setVersion(getKey().getVersion());
automationComposition.setCompositionId(UUID.fromString(compositionId));
@@ -171,6 +170,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative
@Override
public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
+ this.instanceId = automationComposition.getInstanceId().toString();
if (this.key == null || this.getKey().isNullKey()) {
this.setKey(new PfConceptKey(automationComposition.getName(), automationComposition.getVersion()));
}
@@ -182,7 +182,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative
this.primed = automationComposition.getPrimed();
this.elements = new LinkedHashMap<>(automationComposition.getElements().size());
- for (Entry<UUID, AutomationCompositionElement> elementEntry : automationComposition.getElements().entrySet()) {
+ for (var elementEntry : automationComposition.getElements().entrySet()) {
var jpaAutomationCompositionElement = new JpaAutomationCompositionElement();
jpaAutomationCompositionElement
.setKey(new PfReferenceKey(getKey(), elementEntry.getValue().getId().toString()));
@@ -193,9 +193,9 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative
@Override
public List<PfKey> getKeys() {
- List<PfKey> keyList = getKey().getKeys();
+ var keyList = getKey().getKeys();
- for (JpaAutomationCompositionElement element : elements.values()) {
+ for (var element : elements.values()) {
keyList.addAll(element.getKeys());
}
@@ -207,7 +207,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative
key.clean();
description = (description == null ? null : description.trim());
- for (JpaAutomationCompositionElement element : elements.values()) {
+ for (var element : elements.values()) {
element.clean();
}
}
@@ -224,8 +224,13 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative
return this.getClass().getName().compareTo(otherConcept.getClass().getName());
}
- final JpaAutomationComposition other = (JpaAutomationComposition) otherConcept;
- int result = key.compareTo(other.key);
+ final var other = (JpaAutomationComposition) otherConcept;
+ var result = ObjectUtils.compare(instanceId, other.instanceId);
+ if (result != 0) {
+ return result;
+ }
+
+ result = key.compareTo(other.key);
if (result != 0) {
return result;
}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java
index 5024785f8..3d2813eb2 100644
--- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java
@@ -52,29 +52,31 @@ public class AutomationCompositionProvider {
/**
* Get automation composition.
*
- * @param automationCompositionId the ID of the automation composition to get
+ * @param instanceId the ID of the automation composition to get
* @return the automation composition found
*/
@Transactional(readOnly = true)
- public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId) {
- try {
- return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
- } catch (EntityNotFoundException e) {
- throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found", e);
+ public AutomationComposition getAutomationComposition(final UUID instanceId) {
+ var result = automationCompositionRepository.findByInstanceId(instanceId.toString());
+ if (result.isEmpty()) {
+ throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found");
}
+ return result.get().toAuthorative();
}
/**
- * Find automation composition by automationCompositionId.
+ * Get automation composition.
*
- * @param name the name of the automation composition to get, null to get all automation compositions
- * @param version the version of the automation composition to get, null to get all automation compositions
+ * @param automationCompositionId the ID of the automation composition to get
* @return the automation composition found
*/
@Transactional(readOnly = true)
- public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name,
- @NonNull final String version) {
- return findAutomationComposition(new PfConceptKey(name, version));
+ public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId) {
+ try {
+ return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative();
+ } catch (EntityNotFoundException e) {
+ throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found", e);
+ }
}
/**
@@ -94,12 +96,27 @@ public class AutomationCompositionProvider {
}
/**
- * Save automation composition.
+ * Create automation composition.
+ *
+ * @param automationComposition the automation composition to create
+ * @return the create automation composition
+ */
+ public AutomationComposition createAutomationComposition(final AutomationComposition automationComposition) {
+ automationComposition.setInstanceId(UUID.randomUUID());
+ var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
+ JpaAutomationComposition::new, "automation composition"));
+
+ // Return the saved automation composition
+ return result.toAuthorative();
+ }
+
+ /**
+ * Update automation composition.
*
* @param automationComposition the automation composition to update
* @return the updated automation composition
*/
- public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition) {
+ public AutomationComposition updateAutomationComposition(final AutomationComposition automationComposition) {
var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition,
JpaAutomationComposition::new, "automation composition"));
@@ -136,23 +153,18 @@ public class AutomationCompositionProvider {
/**
* Delete a automation composition.
*
- * @param name the name of the automation composition to delete
- * @param version the version of the automation composition to delete
+ * @param instanceId the ID of the automation composition to get
* @return the automation composition deleted
*/
- public AutomationComposition deleteAutomationComposition(@NonNull final String name,
- @NonNull final String version) {
-
- var automationCompositionKey = new PfConceptKey(name, version);
- var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey);
-
+ public AutomationComposition deleteAutomationComposition(@NonNull final UUID instanceId) {
+ var jpaDeleteAutomationComposition = automationCompositionRepository.findByInstanceId(instanceId.toString());
if (jpaDeleteAutomationComposition.isEmpty()) {
- String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId()
+ var errorMessage = "delete of automation composition \"" + instanceId
+ "\" failed, automation composition does not exist";
throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage);
}
- automationCompositionRepository.deleteById(automationCompositionKey);
+ automationCompositionRepository.deleteById(jpaDeleteAutomationComposition.get().getKey());
return jpaDeleteAutomationComposition.get().toAuthorative();
}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java
index 273b99a63..aba752667 100644
--- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java
@@ -21,6 +21,7 @@
package org.onap.policy.clamp.models.acm.persistence.repository;
import java.util.List;
+import java.util.Optional;
import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
import org.onap.policy.models.base.PfConceptKey;
import org.springframework.data.jpa.repository.JpaRepository;
@@ -28,7 +29,9 @@ import org.springframework.stereotype.Repository;
@Repository
public interface AutomationCompositionRepository
- extends JpaRepository<JpaAutomationComposition, PfConceptKey>, FilterRepository {
+ extends JpaRepository<JpaAutomationComposition, PfConceptKey>, FilterRepository {
+
+ Optional<JpaAutomationComposition> findByInstanceId(String instanceId);
List<JpaAutomationComposition> findByCompositionId(String compositionId);
}
diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionTest.java
index 733ba3807..f5a2149b4 100644
--- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionTest.java
+++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionTest.java
@@ -46,7 +46,8 @@ import org.onap.policy.models.base.PfConceptKey;
*/
class JpaAutomationCompositionTest {
- private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
+ private static final String NULL_KEY_ERROR = "instanceId is marked .*ull but is null";
+ private static final UUID INSTANCE_ID = UUID.fromString("709c62b3-8918-41b9-a747-d21eb79c6c20");
private static final String COMPOSITION_ID = "709c62b3-8918-41b9-a747-e21eb79c6c41";
@Test
@@ -56,74 +57,70 @@ class JpaAutomationCompositionTest {
}).hasMessageMatching("copyConcept is marked .*ull but is null");
assertThatThrownBy(() -> {
- new JpaAutomationComposition((PfConceptKey) null);
+ new JpaAutomationComposition(null, null, null, null, null);
}).hasMessageMatching(NULL_KEY_ERROR);
assertThatThrownBy(() -> {
- new JpaAutomationComposition(null, null, null, null);
+ new JpaAutomationComposition(null, null, null, null, new LinkedHashMap<>());
}).hasMessageMatching(NULL_KEY_ERROR);
assertThatThrownBy(() -> {
- new JpaAutomationComposition(null, null, null, new LinkedHashMap<>());
+ new JpaAutomationComposition(null, null, null, AutomationCompositionState.UNINITIALISED, null);
}).hasMessageMatching(NULL_KEY_ERROR);
assertThatThrownBy(() -> {
- new JpaAutomationComposition(null, null, AutomationCompositionState.UNINITIALISED, null);
- }).hasMessageMatching(NULL_KEY_ERROR);
-
- assertThatThrownBy(() -> {
- new JpaAutomationComposition(null, null, AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
- }).hasMessageMatching(NULL_KEY_ERROR);
-
- assertThatThrownBy(() -> {
- new JpaAutomationComposition(null, "key", null, null);
+ new JpaAutomationComposition(null, null, null, AutomationCompositionState.UNINITIALISED,
+ new LinkedHashMap<>());
}).hasMessageMatching(NULL_KEY_ERROR);
assertThatThrownBy(() -> {
- new JpaAutomationComposition(null, "key", null, new LinkedHashMap<>());
+ new JpaAutomationComposition(null, null, "key", null, new LinkedHashMap<>());
}).hasMessageMatching(NULL_KEY_ERROR);
assertThatThrownBy(() -> {
- new JpaAutomationComposition(null, "key", AutomationCompositionState.UNINITIALISED, null);
+ new JpaAutomationComposition(null, null, "key", AutomationCompositionState.UNINITIALISED, null);
}).hasMessageMatching(NULL_KEY_ERROR);
assertThatThrownBy(() -> {
- new JpaAutomationComposition(null, "key", AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
+ new JpaAutomationComposition(null, null, "key", AutomationCompositionState.UNINITIALISED,
+ new LinkedHashMap<>());
}).hasMessageMatching(NULL_KEY_ERROR);
assertThatThrownBy(() -> {
- new JpaAutomationComposition(new PfConceptKey(), null, null, null);
+ new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), null, null, null);
}).hasMessageMatching("compositionId is marked .*ull but is null");
assertThatThrownBy(() -> {
- new JpaAutomationComposition(new PfConceptKey(), null, null, new LinkedHashMap<>());
+ new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), null, null, new LinkedHashMap<>());
}).hasMessageMatching("compositionId is marked .*ull but is null");
assertThatThrownBy(() -> {
- new JpaAutomationComposition(new PfConceptKey(), null, AutomationCompositionState.UNINITIALISED, null);
+ new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), null,
+ AutomationCompositionState.UNINITIALISED, null);
}).hasMessageMatching("compositionId is marked .*ull but is null");
assertThatThrownBy(() -> {
- new JpaAutomationComposition(new PfConceptKey(), null, AutomationCompositionState.UNINITIALISED,
- new LinkedHashMap<>());
+ new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), null,
+ AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
}).hasMessageMatching("compositionId is marked .*ull but is null");
assertThatThrownBy(() -> {
- new JpaAutomationComposition(new PfConceptKey(), "key", null, null);
+ new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), "key", null, null);
}).hasMessageMatching("state is marked .*ull but is null");
assertThatThrownBy(() -> {
- new JpaAutomationComposition(new PfConceptKey(), "key", null, new LinkedHashMap<>());
+ new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), "key", null,
+ new LinkedHashMap<>());
}).hasMessageMatching("state is marked .*ull but is null");
assertThatThrownBy(() -> {
- new JpaAutomationComposition(new PfConceptKey(), "key", AutomationCompositionState.UNINITIALISED, null);
+ new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), "key",
+ AutomationCompositionState.UNINITIALISED, null);
}).hasMessageMatching("elements is marked .*ull but is null");
assertNotNull(new JpaAutomationComposition());
- assertNotNull(new JpaAutomationComposition((new PfConceptKey())));
- assertNotNull(new JpaAutomationComposition(new PfConceptKey(), "key", AutomationCompositionState.UNINITIALISED,
- new LinkedHashMap<>()));
+ assertNotNull(new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), "key",
+ AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>()));
}
@Test
@@ -182,23 +179,24 @@ class JpaAutomationCompositionTest {
new StandardCoder().decode(new File("src/test/resources/json/AutomationCompositionNoOrderedState.json"),
AutomationComposition.class);
+ noOrderedStateAc.setInstanceId(INSTANCE_ID);
var noOrderedStateJpaAc = new JpaAutomationComposition(noOrderedStateAc);
assertNull(noOrderedStateJpaAc.getOrderedState());
noOrderedStateAc.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
noOrderedStateJpaAc = new JpaAutomationComposition(noOrderedStateAc);
assertEquals(testJpaAutomationComposition, noOrderedStateJpaAc);
- var automationCompositionsWithElements = new StandardCoder().decode(
- new File("src/test/resources/providers/TestAutomationCompositions.json"), AutomationCompositions.class);
+ var acWithElements =
+ new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
+ AutomationCompositions.class).getAutomationCompositionList().get(0);
- var jpaAutomationCompositionWithElements =
- new JpaAutomationComposition(automationCompositionsWithElements.getAutomationCompositionList().get(0));
+ acWithElements.setInstanceId(INSTANCE_ID);
+ var jpaAutomationCompositionWithElements = new JpaAutomationComposition(acWithElements);
assertEquals(4, jpaAutomationCompositionWithElements.getElements().size());
assertEquals(17, jpaAutomationCompositionWithElements.getKeys().size());
assertThatCode(jpaAutomationCompositionWithElements::clean).doesNotThrowAnyException();
- assertEquals(automationCompositionsWithElements.getAutomationCompositionList().get(0),
- jpaAutomationCompositionWithElements.toAuthorative());
+ assertEquals(acWithElements, jpaAutomationCompositionWithElements.toAuthorative());
}
@Test
@@ -282,6 +280,7 @@ class JpaAutomationCompositionTest {
var ac2 = new JpaAutomationComposition();
ac2.setCompositionId(COMPOSITION_ID);
+ ac2.setInstanceId(ac0.getInstanceId());
assertEquals(ac2, ac0);
}
@@ -299,6 +298,7 @@ class JpaAutomationCompositionTest {
private AutomationComposition createAutomationCompositionInstance() {
var testAutomationComposition = new AutomationComposition();
testAutomationComposition.setName("automation-composition");
+ testAutomationComposition.setInstanceId(INSTANCE_ID);
testAutomationComposition.setVersion("0.0.1");
testAutomationComposition.setCompositionId(UUID.fromString(COMPOSITION_ID));
testAutomationComposition.setElements(new LinkedHashMap<>());
diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java
index 5b12eee17..d7d96e9bd 100644
--- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java
+++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Optional;
+import java.util.UUID;
import javax.persistence.EntityNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -40,7 +41,6 @@ import org.onap.policy.clamp.models.acm.persistence.repository.AutomationComposi
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
-import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
class AutomationCompositionProviderTest {
@@ -68,18 +68,32 @@ class AutomationCompositionProviderTest {
}
@Test
- void testAutomationCompositionSave() {
+ void testAutomationCompositionCreate() {
var automationCompositionRepository = mock(AutomationCompositionRepository.class);
var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
- assertThatThrownBy(() -> automationCompositionProvider.saveAutomationComposition(null))
+ when(automationCompositionRepository.save(any(JpaAutomationComposition.class)))
+ .thenReturn(inputAutomationCompositionsJpa.get(0));
+ var inputAc = inputAutomationCompositions.getAutomationCompositionList().get(0);
+
+ var createdAutomationComposition = automationCompositionProvider.createAutomationComposition(inputAc);
+ inputAc.setInstanceId(createdAutomationComposition.getInstanceId());
+ assertEquals(inputAc, createdAutomationComposition);
+ }
+
+ @Test
+ void testAutomationCompositionUpdate() {
+ var automationCompositionRepository = mock(AutomationCompositionRepository.class);
+ var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
+
+ assertThatThrownBy(() -> automationCompositionProvider.updateAutomationComposition(null))
.hasMessageMatching(OBJECT_IS_NULL);
when(automationCompositionRepository.save(inputAutomationCompositionsJpa.get(0)))
.thenReturn(inputAutomationCompositionsJpa.get(0));
var createdAutomationComposition = automationCompositionProvider
- .saveAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0));
+ .updateAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0));
assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(0), createdAutomationComposition);
}
@@ -121,31 +135,49 @@ class AutomationCompositionProviderTest {
.getAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION)))
.hasMessageMatching("AutomationComposition not found");
- ac = automationCompositionProvider.findAutomationComposition(ID_NAME, ID_VERSION)
- .orElse(new AutomationComposition());
- assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
-
assertThat(automationCompositionProvider
.findAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION))).isEmpty();
}
@Test
- void testDeleteAutomationComposition() {
+ void testGetAutomationComposition() {
var automationCompositionRepository = mock(AutomationCompositionRepository.class);
var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
- assertThatThrownBy(
- () -> automationCompositionProvider.deleteAutomationComposition(ID_NAME_NOT_EXTST, ID_VERSION))
- .hasMessageMatching(".*.failed, automation composition does not exist");
+ var automationComposition = inputAutomationCompositions.getAutomationCompositionList().get(0);
+ when(automationCompositionRepository.findByInstanceId(automationComposition.getInstanceId().toString()))
+ .thenReturn(Optional.of(inputAutomationCompositionsJpa.get(0)));
+ var ac = automationCompositionProvider.getAutomationComposition(automationComposition.getInstanceId());
+ assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(0), ac);
+ }
+
+ @Test
+ void testGetAcInstancesByCompositionId() {
+ var automationCompositionRepository = mock(AutomationCompositionRepository.class);
+ var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
var automationComposition = inputAutomationCompositions.getAutomationCompositionList().get(0);
- var name = automationComposition.getName();
- var version = automationComposition.getVersion();
+ when(automationCompositionRepository.findByCompositionId(automationComposition.getCompositionId().toString()))
+ .thenReturn(inputAutomationCompositionsJpa);
+ var acList =
+ automationCompositionProvider.getAcInstancesByCompositionId(automationComposition.getCompositionId());
+ assertEquals(inputAutomationCompositions.getAutomationCompositionList(), acList);
+ }
- when(automationCompositionRepository.findById(new PfConceptKey(name, version)))
+ @Test
+ void testDeleteAutomationComposition() {
+ var automationCompositionRepository = mock(AutomationCompositionRepository.class);
+ var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
+
+ assertThatThrownBy(() -> automationCompositionProvider.deleteAutomationComposition(UUID.randomUUID()))
+ .hasMessageMatching(".*.failed, automation composition does not exist");
+
+ var automationComposition = inputAutomationCompositions.getAutomationCompositionList().get(0);
+ when(automationCompositionRepository.findByInstanceId(automationComposition.getInstanceId().toString()))
.thenReturn(Optional.of(inputAutomationCompositionsJpa.get(0)));
- AutomationComposition deletedAc = automationCompositionProvider.deleteAutomationComposition(name, version);
+ var deletedAc =
+ automationCompositionProvider.deleteAutomationComposition(automationComposition.getInstanceId());
assertEquals(automationComposition, deletedAc);
}
}
diff --git a/models/src/test/resources/providers/TestAutomationCompositions.json b/models/src/test/resources/providers/TestAutomationCompositions.json
index 171ce07f6..286759988 100644
--- a/models/src/test/resources/providers/TestAutomationCompositions.json
+++ b/models/src/test/resources/providers/TestAutomationCompositions.json
@@ -2,6 +2,7 @@
"automationCompositionList": [
{
"compositionId": "709c62b3-8918-41b9-a747-e21eb79c6c40",
+ "instanceId": "809c62b3-8918-41b9-a748-e21eb79c6c89",
"state": "UNINITIALISED",
"orderedState": "UNINITIALISED",
"elements": {
@@ -68,6 +69,7 @@
},
{
"compositionId": "709c62b3-8918-41b9-a747-e21eb79c6c40",
+ "instanceId": "809c62b3-8918-41b9-a748-e21eb79c6c90",
"state": "UNINITIALISED",
"orderedState": "UNINITIALISED",
"elements": {