From c7a238b6e15e90b6c752a282cc604789bdb7ae20 Mon Sep 17 00:00:00 2001 From: akenihan Date: Fri, 16 May 2025 13:06:50 +0100 Subject: [PATCH] Add support to store in/out properties as copy before trigger migrate/update Issue-ID: POLICY-5364 Change-Id: I1851aa5172edfbaf3cd52ec96a92bca2267b685c Signed-off-by: akenihan --- .../concepts/AutomationCompositionRollback.java | 56 ++++++++ .../concepts/JpaAutomationCompositionRollback.java | 146 +++++++++++++++++++++ .../AutomationCompositionRollbackRepository.java | 31 +++++ .../AutomationCompositionRollbackTest.java | 46 +++++++ .../JpaAutomationCompositionRollbackTest.java | 105 +++++++++++++++ 5 files changed, 384 insertions(+) create mode 100644 models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionRollback.java create mode 100644 models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionRollback.java create mode 100644 models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRollbackRepository.java create mode 100644 models/src/test/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionRollbackTest.java create mode 100644 models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionRollbackTest.java diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionRollback.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionRollback.java new file mode 100644 index 000000000..730a08dcb --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionRollback.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.concepts; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.UUID; +import java.util.function.UnaryOperator; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.models.base.PfUtils; + +@NoArgsConstructor +@Data +public class AutomationCompositionRollback { + + @NonNull + private UUID instanceId; + + @NonNull + private UUID compositionId; + + @NonNull + private Map elements = new LinkedHashMap<>(); + + /** + * Copy constructor, does a deep copy. + * + * @param otherAcmRollback the other element to copy from + */ + public AutomationCompositionRollback(final AutomationCompositionRollback otherAcmRollback) { + this.instanceId = otherAcmRollback.instanceId; + this.compositionId = otherAcmRollback.compositionId; + this.elements = PfUtils.mapMap(otherAcmRollback.elements, UnaryOperator.identity()); + } +} diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionRollback.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionRollback.java new file mode 100644 index 000000000..38437e1ee --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionRollback.java @@ -0,0 +1,146 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.persistence.concepts; + +import jakarta.persistence.Column; +import jakarta.persistence.Convert; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.Table; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.UUID; +import java.util.function.UnaryOperator; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionRollback; +import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.Validated; + +@Entity +@Table(name = "AutomationCompositionRollback") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class JpaAutomationCompositionRollback extends Validated + implements PfAuthorative, Comparable { + + @Id + @NotNull + private String instanceId; + + @NotNull + @Column + private String compositionId; + + @NotNull + @Valid + @Convert(converter = StringToMapConverter.class) + @Column(length = 100000) + private Map elements = new LinkedHashMap<>(); + + /** + * The Default Constructor creates a {@link JpaAutomationComposition} object with an empty hashmap. + */ + public JpaAutomationCompositionRollback() { + this(UUID.randomUUID().toString(), UUID.randomUUID().toString(), new LinkedHashMap<>()); + } + + /** + * The Key Constructor creates a {@link JpaAutomationCompositionRollback} object with all mandatory fields. + * + * @param instanceId The UUID of the automation composition rollback instance + * @param compositionId the TOSCA compositionId of the automation composition rollback definition + * @param elements the elements of the automation composition rollback + */ + public JpaAutomationCompositionRollback(@NonNull final String instanceId, @NonNull final String compositionId, + @NonNull final Map elements) { + this.instanceId = instanceId; + this.compositionId = compositionId; + this.elements = PfUtils.mapMap(elements, UnaryOperator.identity()); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public JpaAutomationCompositionRollback(@NonNull final JpaAutomationCompositionRollback copyConcept) { + this.instanceId = copyConcept.instanceId; + this.compositionId = copyConcept.compositionId; + this.elements = PfUtils.mapMap(copyConcept.elements, UnaryOperator.identity()); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaAutomationCompositionRollback(@NonNull final AutomationCompositionRollback authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public AutomationCompositionRollback toAuthorative() { + var acmRollback = new AutomationCompositionRollback(); + acmRollback.setInstanceId(UUID.fromString(this.instanceId)); + acmRollback.setCompositionId(UUID.fromString(this.compositionId)); + acmRollback.setElements(PfUtils.mapMap(this.elements, UnaryOperator.identity())); + return acmRollback; + } + + @Override + public void fromAuthorative(@NonNull final AutomationCompositionRollback acmRollback) { + this.instanceId = acmRollback.getInstanceId().toString(); + this.compositionId = acmRollback.getCompositionId().toString(); + this.elements = PfUtils.mapMap(acmRollback.getElements(), UnaryOperator.identity()); + } + + @Override + public int compareTo(final JpaAutomationCompositionRollback other) { + if (other == null) { + return -1; + } + if (this == other) { + return 0; + } + + var result = ObjectUtils.compare(instanceId, other.instanceId); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(compositionId, other.compositionId); + if (result != 0) { + return result; + } + + return PfUtils.compareObjects(elements, other.elements); + } + +} diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRollbackRepository.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRollbackRepository.java new file mode 100644 index 000000000..7004bb921 --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRollbackRepository.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.persistence.repository; + +import java.util.List; +import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionRollback; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AutomationCompositionRollbackRepository + extends JpaRepository { + + List findByCompositionId(String compositionId); +} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionRollbackTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionRollbackTest.java new file mode 100644 index 000000000..a67639dff --- /dev/null +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionRollbackTest.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.concepts; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.UUID; +import java.util.function.UnaryOperator; +import org.junit.jupiter.api.Test; +import org.onap.policy.models.base.PfUtils; + +class AutomationCompositionRollbackTest { + @Test + void testAutomationCompositionRollback() { + var acr0 = new AutomationCompositionRollback(); + acr0.setInstanceId(UUID.randomUUID()); + acr0.setCompositionId(UUID.randomUUID()); + Map map = new LinkedHashMap<>(); + map.put("test", "test"); + acr0.setElements(PfUtils.mapMap(map, UnaryOperator.identity())); + + var acr1 = new AutomationCompositionRollback(acr0); + assertEquals(acr0, acr1); + } +} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionRollbackTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionRollbackTest.java new file mode 100644 index 000000000..a6273a9e4 --- /dev/null +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionRollbackTest.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.persistence.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.UUID; +import java.util.function.UnaryOperator; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionRollback; +import org.onap.policy.models.base.PfUtils; + + +public class JpaAutomationCompositionRollbackTest { + + private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null"; + private static final String NULL_ERROR = " is marked .*ull but is null"; + private static final String INSTANCE_ID = "709c62b3-8918-41b9-a747-d21eb79c6c20"; + private static final String COMPOSITION_ID = "709c62b3-8918-41b9-a747-e21eb79c6c41"; + + @Test + void testJpaAutomationCompositionRollbackConstructor() { + assertThatThrownBy(() -> { + new JpaAutomationCompositionRollback((JpaAutomationCompositionRollback) null); + }).hasMessageMatching("copyConcept" + NULL_ERROR); + + assertThatThrownBy(() -> { + new JpaAutomationCompositionRollback((AutomationCompositionRollback) null); + }).hasMessageMatching("authorativeConcept" + NULL_ERROR); + + assertThatThrownBy(() -> { + new JpaAutomationCompositionRollback(null, null, null); + }).hasMessageMatching(NULL_INSTANCE_ID_ERROR); + + assertThatThrownBy(() -> { + new JpaAutomationCompositionRollback(INSTANCE_ID, null, null); + }).hasMessageMatching("compositionId" + NULL_ERROR); + + assertThatThrownBy(() -> { + new JpaAutomationCompositionRollback(INSTANCE_ID, COMPOSITION_ID, null); + }).hasMessageMatching("elements" + NULL_ERROR); + + assertDoesNotThrow(() -> new JpaAutomationCompositionRollback()); + Map map = new LinkedHashMap<>(); + map.put("test", "test"); + assertDoesNotThrow(() -> new JpaAutomationCompositionRollback(INSTANCE_ID, COMPOSITION_ID, map)); + } + + @Test + void testJpaAutomationCompositionRollback() { + var automationCompositionRollback = createAutomationCompositionRollbackInstance(); + var jpaAutomationCompositionRollback = new JpaAutomationCompositionRollback(automationCompositionRollback); + + + assertEquals(automationCompositionRollback, jpaAutomationCompositionRollback.toAuthorative()); + assertDoesNotThrow(() -> new JpaAutomationCompositionRollback(jpaAutomationCompositionRollback)); + } + + @Test + void testJpaCompositionRollbackCompareTo() { + var jpaAutomationCompositionRollback = + new JpaAutomationCompositionRollback(createAutomationCompositionRollbackInstance()); + + var otherJpaAutomationComposition = + new JpaAutomationCompositionRollback(jpaAutomationCompositionRollback); + + assertEquals(0, jpaAutomationCompositionRollback.compareTo(otherJpaAutomationComposition)); + assertEquals(-1, jpaAutomationCompositionRollback.compareTo(null)); + assertEquals(0, jpaAutomationCompositionRollback.compareTo(jpaAutomationCompositionRollback)); + } + + private AutomationCompositionRollback createAutomationCompositionRollbackInstance() { + var testAcmRollback = new AutomationCompositionRollback(); + testAcmRollback.setInstanceId(UUID.fromString(INSTANCE_ID)); + testAcmRollback.setCompositionId(UUID.fromString(COMPOSITION_ID)); + Map map = new HashMap<>(); + map.put("test", "test"); + testAcmRollback.setElements(PfUtils.mapMap(map, UnaryOperator.identity())); + + return testAcmRollback; + } +} -- 2.16.6