diff options
Diffstat (limited to 'models')
7 files changed, 126 insertions, 28 deletions
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java index 6a523fa75..c6da3c3d1 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java @@ -20,6 +20,7 @@ package org.onap.policy.clamp.models.acm.concepts; +import java.util.UUID; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @@ -35,6 +36,9 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; @Data @EqualsAndHashCode(callSuper = true) public class Participant extends ToscaEntity implements Comparable<Participant> { + + @NonNull + private UUID participantId; @NonNull private ToscaConceptIdentifier definition = new ToscaConceptIdentifier(PfConceptKey.getNullKey()); @@ -69,5 +73,6 @@ public class Participant extends ToscaEntity implements Comparable<Participant> this.definition = new ToscaConceptIdentifier(otherParticipant.definition); this.participantState = otherParticipant.participantState; this.participantType = otherParticipant.participantType; + this.participantId = otherParticipant.participantId; } } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java index aec2d32d3..398daa245 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java @@ -22,6 +22,7 @@ package org.onap.policy.clamp.models.acm.persistence.concepts; import java.io.Serializable; import java.util.List; +import java.util.UUID; import javax.persistence.AttributeOverride; import javax.persistence.Column; import javax.persistence.EmbeddedId; @@ -56,6 +57,10 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; public class JpaParticipant extends PfConcept implements PfAuthorative<Participant>, Serializable { private static final long serialVersionUID = -4697758484642403483L; + @Column + @NotNull + private String participantId; + @EmbeddedId @VerifyKey @NotNull @@ -94,21 +99,25 @@ public class JpaParticipant extends PfConcept implements PfAuthorative<Participa * @param key the key */ public JpaParticipant(@NonNull final PfConceptKey key) { - this(key, new PfConceptKey(), ParticipantState.ON_LINE); + this(UUID.randomUUID().toString(), key, new PfConceptKey(), ParticipantState.ON_LINE); } /** * The Key Constructor creates a {@link JpaParticipant} object with all mandatory fields. * + * @param participantId the participant id * @param key the key * @param definition the TOSCA definition of the participant * @param participantState the state of the participant */ - public JpaParticipant(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition, - @NonNull final ParticipantState participantState) { + public JpaParticipant(@NotNull String participantId, + @NonNull final PfConceptKey key, + @NonNull final PfConceptKey definition, + @NonNull final ParticipantState participantState) { this.key = key; this.definition = definition; this.participantState = participantState; + this.participantId = participantId; } /** @@ -123,6 +132,7 @@ public class JpaParticipant extends PfConcept implements PfAuthorative<Participa this.participantState = copyConcept.participantState; this.description = copyConcept.description; this.participantType = copyConcept.participantType; + this.participantId = copyConcept.participantId; } /** @@ -144,6 +154,7 @@ public class JpaParticipant extends PfConcept implements PfAuthorative<Participa participant.setParticipantState(participantState); participant.setDescription(description); participant.setParticipantType(new ToscaConceptIdentifier(participantType)); + participant.setParticipantId(UUID.fromString(participantId)); return participant; } @@ -158,6 +169,7 @@ public class JpaParticipant extends PfConcept implements PfAuthorative<Participa this.setParticipantState(participant.getParticipantState()); this.setDescription(participant.getDescription()); this.participantType = participant.getParticipantType().asConceptKey(); + this.participantId = participant.getParticipantId().toString(); } @Override diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java index 3f7e297a4..91766807f 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java @@ -22,12 +22,14 @@ package org.onap.policy.clamp.models.acm.persistence.provider; import java.util.List; import java.util.Optional; +import java.util.UUID; import javax.ws.rs.core.Response.Status; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.onap.policy.clamp.models.acm.concepts.Participant; import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant; import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository; +import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.stereotype.Service; @@ -69,6 +71,24 @@ public class ParticipantProvider { /** * Get participant. * + * @param participantId the id of the participant to get + * @return the participant found + * @throws PfModelException on errors getting participant + */ + @Transactional(readOnly = true) + public Participant getParticipantById(String participantId) { + var participant = participantRepository.findByParticipantId(participantId); + if (participant.isEmpty()) { + throw new PfModelRuntimeException(Status.NOT_FOUND, + "Participant Not Found with ID: " + participantId); + } else { + return participant.get().toAuthorative(); + } + } + + /** + * Get participant. + * * @param participantId the Id of the participant to get * @return the participant found */ @@ -84,8 +104,23 @@ public class ParticipantProvider { * @return the participant created */ public Participant saveParticipant(@NonNull final Participant participant) { + participant.setParticipantId(UUID.randomUUID()); + var result = participantRepository + .save(ProviderUtils.getJpaAndValidate(participant, JpaParticipant::new, "participant")); + + // Return the saved participant + return result.toAuthorative(); + } + + /** + * Updates an existing participant. + * + * @param participant participant to update + * @return the participant updated + */ + public Participant updateParticipant(@NonNull final Participant participant) { var result = participantRepository - .save(ProviderUtils.getJpaAndValidate(participant, JpaParticipant::new, "participant")); + .save(ProviderUtils.getJpaAndValidate(participant, JpaParticipant::new, "participant")); // Return the saved participant return result.toAuthorative(); @@ -102,7 +137,7 @@ public class ParticipantProvider { if (jpaDeleteParticipantOpt.isEmpty()) { String errorMessage = - "delete of participant \"" + participantId + "\" failed, participant does not exist"; + "delete of participant \"" + participantId + "\" failed, participant does not exist"; throw new PfModelRuntimeException(Status.BAD_REQUEST, errorMessage); } participantRepository.delete(jpaDeleteParticipantOpt.get()); diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/ParticipantRepository.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/ParticipantRepository.java index 2279a75cc..67ea18870 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/ParticipantRepository.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/ParticipantRepository.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021-2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ package org.onap.policy.clamp.models.acm.persistence.repository; +import java.util.Optional; import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant; import org.onap.policy.models.base.PfConceptKey; import org.springframework.data.jpa.repository.JpaRepository; @@ -28,4 +29,5 @@ import org.springframework.stereotype.Repository; @Repository public interface ParticipantRepository extends JpaRepository<JpaParticipant, PfConceptKey>, FilterRepository { + Optional<JpaParticipant> findByParticipantId(String compositionId); } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java index 9d1d7ff99..0fbdaa2bc 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java @@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.UUID; import org.junit.jupiter.api.Test; import org.onap.policy.clamp.models.acm.concepts.Participant; import org.onap.policy.clamp.models.acm.concepts.ParticipantState; @@ -47,52 +48,54 @@ class JpaParticipantTest { assertThatThrownBy(() -> new JpaParticipant((PfConceptKey) null)).hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(null, null, null)).hasMessageMatching(NULL_KEY_ERROR); + assertThatThrownBy(() -> new JpaParticipant(null, null, null, null)).hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(null, null, null)) + assertThatThrownBy(() -> new JpaParticipant(null, null, null, null)) .hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(null, null, ParticipantState.ON_LINE)) + assertThatThrownBy(() -> new JpaParticipant(null, null, null, ParticipantState.ON_LINE)) .hasMessageMatching(NULL_KEY_ERROR); assertThatThrownBy( - () -> new JpaParticipant(null, null, ParticipantState.ON_LINE)) + () -> new JpaParticipant(null, null, null, ParticipantState.ON_LINE)) .hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), null)) + assertThatThrownBy(() -> new JpaParticipant(null, null, new PfConceptKey(), null)) .hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), null)) + assertThatThrownBy(() -> new JpaParticipant(null, null, new PfConceptKey(), null)) .hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), ParticipantState.ON_LINE)) + assertThatThrownBy(() -> new JpaParticipant(null, null, new PfConceptKey(), ParticipantState.ON_LINE)) .hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), ParticipantState.ON_LINE)) + assertThatThrownBy(() -> new JpaParticipant(null, null, new PfConceptKey(), ParticipantState.ON_LINE)) .hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(new PfConceptKey(), null, null)) + assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), null, null)) .hasMessageMatching("definition is marked .*ull but is null"); - assertThatThrownBy(() -> new JpaParticipant(new PfConceptKey(), null, null)) + assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), null, null)) .hasMessageMatching("definition is marked .*ull but is null"); - assertThatThrownBy(() -> new JpaParticipant(new PfConceptKey(), null, ParticipantState.ON_LINE)) + assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), null, ParticipantState.ON_LINE)) .hasMessageMatching("definition is marked .*ull but is null"); - assertThatThrownBy(() -> new JpaParticipant(new PfConceptKey(), null, ParticipantState.ON_LINE - )).hasMessageMatching("definition is marked .*ull but is null"); + assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), null, ParticipantState.ON_LINE + )).hasMessageMatching("definition is marked .*ull but is null"); - assertThatThrownBy(() -> new JpaParticipant(new PfConceptKey(), new PfConceptKey(), null)) + assertThatThrownBy(() -> new JpaParticipant(null, new PfConceptKey(), new PfConceptKey(), null)) .hasMessageMatching("participantState is marked .*ull but is null"); assertThatThrownBy( - () -> new JpaParticipant(new PfConceptKey(), new PfConceptKey(), null)) + () -> new JpaParticipant(null, new PfConceptKey(), new PfConceptKey(), null)) .hasMessageMatching("participantState is marked .*ull but is null"); assertNotNull(new JpaParticipant()); assertNotNull(new JpaParticipant((new PfConceptKey()))); - assertNotNull(new JpaParticipant(new PfConceptKey(), new PfConceptKey(), ParticipantState.ON_LINE)); + assertNotNull(new JpaParticipant(null, new PfConceptKey(), new PfConceptKey(), ParticipantState.ON_LINE)); + assertNotNull(new JpaParticipant(UUID.randomUUID().toString(), new PfConceptKey(), + new PfConceptKey(), ParticipantState.ON_LINE)); } @Test @@ -100,6 +103,9 @@ class JpaParticipantTest { JpaParticipant testJpaParticipant = createJpaParticipantInstance(); Participant participant = createParticipantInstance(); + + participant.setParticipantId(testJpaParticipant.toAuthorative().getParticipantId()); + assertEquals(participant, testJpaParticipant.toAuthorative()); assertThatThrownBy(() -> testJpaParticipant.fromAuthorative(null)) @@ -110,6 +116,7 @@ class JpaParticipantTest { JpaParticipant testJpaParticipantFa = new JpaParticipant(); testJpaParticipantFa.setKey(null); testJpaParticipantFa.fromAuthorative(participant); + testJpaParticipantFa.setParticipantId(testJpaParticipant.getParticipantId()); assertEquals(testJpaParticipant, testJpaParticipantFa); testJpaParticipantFa.setKey(PfConceptKey.getNullKey()); testJpaParticipantFa.fromAuthorative(participant); @@ -119,9 +126,9 @@ class JpaParticipantTest { assertEquals(testJpaParticipant, testJpaParticipantFa); assertEquals("participant", testJpaParticipant.getKey().getName()); - assertEquals("participant", new JpaParticipant(createParticipantInstance()).getKey().getName()); + assertEquals("participant", new JpaParticipant(createJpaParticipantInstance()).getKey().getName()); assertEquals("participant", - ((PfConceptKey) new JpaParticipant(createParticipantInstance()).getKeys().get(0)).getName()); + ((PfConceptKey) new JpaParticipant(createJpaParticipantInstance()).getKeys().get(0)).getName()); testJpaParticipant.clean(); assertEquals("participant", testJpaParticipant.getKey().getName()); @@ -131,6 +138,7 @@ class JpaParticipantTest { assertEquals("A Message", testJpaParticipant.getDescription()); JpaParticipant testJpaParticipant2 = new JpaParticipant(testJpaParticipant); + testJpaParticipant2.setParticipantId(testJpaParticipant.getParticipantId()); assertEquals(testJpaParticipant, testJpaParticipant2); } @@ -149,6 +157,7 @@ class JpaParticipantTest { JpaParticipant testJpaParticipant = createJpaParticipantInstance(); JpaParticipant otherJpaParticipant = new JpaParticipant(testJpaParticipant); + otherJpaParticipant.setParticipantId(testJpaParticipant.getParticipantId()); assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant)); assertEquals(-1, testJpaParticipant.compareTo(null)); assertEquals(0, testJpaParticipant.compareTo(testJpaParticipant)); @@ -169,6 +178,10 @@ class JpaParticipantTest { testJpaParticipant.setParticipantState(ParticipantState.ON_LINE); assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant)); assertEquals(testJpaParticipant, new JpaParticipant(testJpaParticipant)); + + JpaParticipant newJpaParticipant = new JpaParticipant(testJpaParticipant); + newJpaParticipant.setParticipantId(testJpaParticipant.getParticipantId()); + assertEquals(testJpaParticipant, newJpaParticipant); } @Test @@ -197,6 +210,7 @@ class JpaParticipantTest { assertNotEquals(p1, p0); JpaParticipant p2 = new JpaParticipant(); + p2.setParticipantId(p0.getParticipantId()); assertEquals(p2, p0); } @@ -204,6 +218,7 @@ class JpaParticipantTest { Participant testParticipant = createParticipantInstance(); JpaParticipant testJpaParticipant = new JpaParticipant(); testJpaParticipant.setKey(null); + testParticipant.setParticipantId(UUID.fromString(testJpaParticipant.getParticipantId())); testJpaParticipant.fromAuthorative(testParticipant); testJpaParticipant.setKey(PfConceptKey.getNullKey()); testJpaParticipant.fromAuthorative(testParticipant); diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProviderTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProviderTest.java index e11541988..6e6637c7e 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProviderTest.java @@ -64,7 +64,7 @@ class ParticipantProviderTest { var participantRepository = mock(ParticipantRepository.class); for (var participant : jpaParticipantList) { when(participantRepository.getById(new PfConceptKey(participant.getName(), participant.getVersion()))) - .thenReturn(participant); + .thenReturn(participant); } var participantProvider = new ParticipantProvider(participantRepository); @@ -73,10 +73,30 @@ class ParticipantProviderTest { when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0)); Participant savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0)); + savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId()); assertEquals(savedParticipant, inputParticipants.get(0)); } @Test + void testParticipantUpdate() throws Exception { + var participantRepository = mock(ParticipantRepository.class); + for (var participant : jpaParticipantList) { + when(participantRepository.getById(new PfConceptKey(participant.getName(), participant.getVersion()))) + .thenReturn(participant); + } + var participantProvider = new ParticipantProvider(participantRepository); + + assertThatThrownBy(() -> participantProvider.updateParticipant(null)) + .hasMessageMatching(LIST_IS_NULL); + + when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0)); + + Participant updatedParticipant = participantProvider.updateParticipant(inputParticipants.get(0)); + updatedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId()); + assertEquals(updatedParticipant, inputParticipants.get(0)); + } + + @Test void testGetAutomationCompositions() throws Exception { var participantRepository = mock(ParticipantRepository.class); var participantProvider = new ParticipantProvider(participantRepository); @@ -88,7 +108,7 @@ class ParticipantProviderTest { String name = inputParticipants.get(0).getName(); String version = inputParticipants.get(0).getVersion(); when(participantRepository.getFiltered(any(), eq(name), eq(version))) - .thenReturn(List.of(jpaParticipantList.get(0))); + .thenReturn(List.of(jpaParticipantList.get(0))); assertEquals(1, participantProvider.getParticipants(name, version).size()); assertThat(participantProvider.getParticipants("invalid_name", "1.0.1")).isEmpty(); @@ -97,6 +117,14 @@ class ParticipantProviderTest { when(participantRepository.findAll()).thenReturn(jpaParticipantList); assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size()); + + when(participantRepository.findByParticipantId(any())).thenReturn( + Optional.ofNullable(jpaParticipantList.get(0))); + + var participant = participantProvider.getParticipantById(inputParticipants.get(0) + .getParticipantId().toString()); + + assertThat(participant).isEqualTo(inputParticipants.get(0)); } @Test @@ -105,12 +133,12 @@ class ParticipantProviderTest { var participantProvider = new ParticipantProvider(participantRepository); assertThatThrownBy(() -> participantProvider.deleteParticipant(INVALID_ID)) - .hasMessageMatching(".*.failed, participant does not exist"); + .hasMessageMatching(".*.failed, participant does not exist"); when(participantRepository.findById(any())).thenReturn(Optional.of(jpaParticipantList.get(0))); Participant deletedParticipant = - participantProvider.deleteParticipant(inputParticipants.get(0).getDefinition()); + participantProvider.deleteParticipant(inputParticipants.get(0).getDefinition()); assertEquals(inputParticipants.get(0), deletedParticipant); } } diff --git a/models/src/test/resources/providers/TestParticipant.json b/models/src/test/resources/providers/TestParticipant.json index 5d8a7ea09..99284cb6a 100644 --- a/models/src/test/resources/providers/TestParticipant.json +++ b/models/src/test/resources/providers/TestParticipant.json @@ -7,6 +7,7 @@ }, "participantState": "ON_LINE", "description": "A dummy PMSH participant1", + "participantId": "82fd8ef9-1d1e-4343-9b28-7f9564ee3de6", "participantType":{ "name": "org.onap.domain.pmsh.PolicyAutomationCompositionDefinition", "version": "1.0.0" |