diff options
author | Liam Fallon <liam.fallon@est.tech> | 2022-12-05 17:36:04 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2022-12-05 17:36:04 +0000 |
commit | 6f3447836b04f2fbea1fa4fca526d549bec42661 (patch) | |
tree | 212c3f61afd3e14d3b1181d70bc58c4aab8c5284 /models | |
parent | e10751382507fa55742a59caf00e42783f33dbe5 (diff) | |
parent | b5d62899bf9cfaef5c9dcc49023428bbb12bcc55 (diff) |
Merge "Refactor POST and PUT Automation Composition"
Diffstat (limited to 'models')
5 files changed, 29 insertions, 109 deletions
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationCommand.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationCommand.java index a3fb70e15..ac5444650 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationCommand.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationCommand.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021-2022 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,7 +20,6 @@ package org.onap.policy.clamp.models.acm.messages.rest.instantiation; -import java.util.List; import lombok.Data; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @@ -30,6 +29,6 @@ public class InstantiationCommand { // The state to which the automation compositions are to be set private AutomationCompositionOrderedState orderedState; - // The list of automation compositions on which the command is to be issued - private List<ToscaConceptIdentifier> automationCompositionIdentifierList; + // The automation composition on which the command is to be issued + private ToscaConceptIdentifier automationCompositionIdentifier; } 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 e7c346498..4ecb8ce32 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021-2022 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,7 +20,6 @@ package org.onap.policy.clamp.models.acm.messages.rest.instantiation; -import java.util.List; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -34,5 +33,5 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @Setter @ToString(callSuper = true) public class InstantiationResponse extends SimpleResponse { - private List<ToscaConceptIdentifier> affectedAutomationCompositions; + ToscaConceptIdentifier affectedAutomationComposition; } 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 272ea422b..5024785f8 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 @@ -34,7 +34,7 @@ import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition; import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository; import org.onap.policy.models.base.PfConceptKey; -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; import org.springframework.transaction.annotation.Transactional; @@ -54,15 +54,13 @@ public class AutomationCompositionProvider { * * @param automationCompositionId the ID of the automation composition to get * @return the automation composition found - * @throws PfModelException on errors getting the automation composition */ @Transactional(readOnly = true) - public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId) - throws PfModelException { + public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId) { try { return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative(); } catch (EntityNotFoundException e) { - throw new PfModelException(Status.NOT_FOUND, "AutomationComposition not found", e); + throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found", e); } } @@ -72,11 +70,10 @@ public class AutomationCompositionProvider { * @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 * @return the automation composition found - * @throws PfModelException on errors getting the automation composition */ @Transactional(readOnly = true) public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name, - @NonNull final String version) throws PfModelException { + @NonNull final String version) { return findAutomationComposition(new PfConceptKey(name, version)); } @@ -85,21 +82,15 @@ public class AutomationCompositionProvider { * * @param automationCompositionId the ID of the automation composition to get * @return the automation composition found - * @throws PfModelException on errors getting the automation composition */ @Transactional(readOnly = true) public Optional<AutomationComposition> findAutomationComposition( - final ToscaConceptIdentifier automationCompositionId) throws PfModelException { + final ToscaConceptIdentifier automationCompositionId) { return findAutomationComposition(automationCompositionId.asConceptKey()); } - private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key) - throws PfModelException { - try { - return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative); - } catch (IllegalArgumentException e) { - throw new PfModelException(Status.BAD_REQUEST, "Not valid parameter", e); - } + private Optional<AutomationComposition> findAutomationComposition(@NonNull final PfConceptKey key) { + return automationCompositionRepository.findById(key).map(JpaAutomationComposition::toAuthorative); } /** @@ -107,19 +98,13 @@ public class AutomationCompositionProvider { * * @param automationComposition the automation composition to update * @return the updated automation composition - * @throws PfModelException on errors updating the automation composition */ - public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition) - throws PfModelException { - try { - var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition, - JpaAutomationComposition::new, "automation composition")); + public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition) { + var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition, + JpaAutomationComposition::new, "automation composition")); - // Return the saved participant - return result.toAuthorative(); - } catch (IllegalArgumentException e) { - throw new PfModelException(Status.BAD_REQUEST, "Error in save automationComposition", e); - } + // Return the saved automation composition + return result.toAuthorative(); } /** @@ -149,36 +134,14 @@ public class AutomationCompositionProvider { } /** - * Saves automation compositions. - * - * @param automationCompositions a specification of the automation compositions to create - * @return the automation compositions created - * @throws PfModelException on errors creating automation compositions - */ - public List<AutomationComposition> saveAutomationCompositions( - @NonNull final List<AutomationComposition> automationCompositions) throws PfModelException { - try { - var result = - automationCompositionRepository.saveAll(ProviderUtils.getJpaAndValidateList(automationCompositions, - JpaAutomationComposition::new, "automation compositions")); - - // Return the saved participant - return ProviderUtils.asEntityList(result); - } catch (IllegalArgumentException e) { - throw new PfModelException(Status.BAD_REQUEST, "Error in save AutomationCompositions", e); - } - } - - /** * Delete a automation composition. * * @param name the name of the automation composition to delete * @param version the version of the automation composition to delete * @return the automation composition deleted - * @throws PfModelException on errors deleting the automation composition */ - public AutomationComposition deleteAutomationComposition(@NonNull final String name, @NonNull final String version) - throws PfModelException { + public AutomationComposition deleteAutomationComposition(@NonNull final String name, + @NonNull final String version) { var automationCompositionKey = new PfConceptKey(name, version); var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey); @@ -186,7 +149,7 @@ public class AutomationCompositionProvider { if (jpaDeleteAutomationComposition.isEmpty()) { String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId() + "\" failed, automation composition does not exist"; - throw new PfModelException(Response.Status.NOT_FOUND, errorMessage); + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage); } automationCompositionRepository.deleteById(automationCompositionKey); diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationCommandTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationCommandTest.java index 579631604..1aa67f425 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationCommandTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationCommandTest.java @@ -25,15 +25,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.util.ArrayList; import org.junit.jupiter.api.Test; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class InstantiationCommandTest { @Test void testInstantiationCommandLombok() { assertNotNull(new InstantiationCommand()); - InstantiationCommand ic0 = new InstantiationCommand(); + var ic0 = new InstantiationCommand(); assertThat(ic0.toString()).contains("InstantiationCommand("); assertNotEquals(0, ic0.hashCode()); @@ -41,9 +41,9 @@ class InstantiationCommandTest { assertNotEquals(null, ic0); - InstantiationCommand ic1 = new InstantiationCommand(); + var ic1 = new InstantiationCommand(); - ic1.setAutomationCompositionIdentifierList(new ArrayList<>()); + ic1.setAutomationCompositionIdentifier(new ToscaConceptIdentifier()); ic1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED); assertThat(ic1.toString()).contains("InstantiationCommand("); @@ -53,7 +53,7 @@ class InstantiationCommandTest { assertNotEquals(ic1, ic0); - InstantiationCommand ic2 = new InstantiationCommand(); + var ic2 = new InstantiationCommand(); assertEquals(ic2, ic0); } 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 5be603d0d..5b12eee17 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 @@ -45,13 +45,11 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class AutomationCompositionProviderTest { - private static final String LIST_IS_NULL = "automationCompositions is marked .*ull but is null"; private static final String OBJECT_IS_NULL = "automationComposition 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 AUTOMATION_COMPOSITION_JSON = @@ -70,31 +68,7 @@ class AutomationCompositionProviderTest { } @Test - void testAutomationCompositionsSave() throws Exception { - var automationCompositionRepository = mock(AutomationCompositionRepository.class); - var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository); - - assertThatThrownBy(() -> automationCompositionProvider.saveAutomationCompositions(null)) - .hasMessageMatching(LIST_IS_NULL); - - when(automationCompositionRepository.saveAll(inputAutomationCompositionsJpa)) - .thenReturn(inputAutomationCompositionsJpa); - - var createdAutomationCompositions = new AutomationCompositions(); - createdAutomationCompositions.setAutomationCompositionList(automationCompositionProvider - .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList())); - - assertEquals(inputAutomationCompositions, createdAutomationCompositions); - - when(automationCompositionRepository.saveAll(any())).thenThrow(IllegalArgumentException.class); - - assertThatThrownBy(() -> automationCompositionProvider - .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList())) - .hasMessageMatching("Error in save AutomationCompositions"); - } - - @Test - void testAutomationCompositionSave() throws Exception { + void testAutomationCompositionSave() { var automationCompositionRepository = mock(AutomationCompositionRepository.class); var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository); @@ -108,27 +82,16 @@ class AutomationCompositionProviderTest { .saveAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0)); assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(0), createdAutomationComposition); - - when(automationCompositionRepository.save(any())).thenThrow(IllegalArgumentException.class); - - assertThatThrownBy(() -> automationCompositionProvider - .saveAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0))) - .hasMessageMatching("Error in save automationComposition"); } @Test void testGetAutomationCompositions() throws Exception { - var automationCompositionRepository = mock(AutomationCompositionRepository.class); - var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository); - - automationCompositionProvider - .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList()); - var automationComposition0 = inputAutomationCompositions.getAutomationCompositionList().get(1); var name = automationComposition0.getName(); var version = automationComposition0.getVersion(); var automationComposition1 = inputAutomationCompositions.getAutomationCompositionList().get(1); + var automationCompositionRepository = mock(AutomationCompositionRepository.class); when(automationCompositionRepository.getFiltered(eq(JpaAutomationComposition.class), any(), any())) .thenReturn(List.of(new JpaAutomationComposition(automationComposition0), new JpaAutomationComposition(automationComposition1))); @@ -141,6 +104,7 @@ class AutomationCompositionProviderTest { when(automationCompositionRepository.findById(automationComposition1.getKey().asIdentifier().asConceptKey())) .thenReturn(Optional.of(new JpaAutomationComposition(automationComposition1))); + var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository); assertEquals(1, automationCompositionProvider.getAutomationCompositions(name, version).size()); var ac = automationCompositionProvider @@ -163,15 +127,10 @@ class AutomationCompositionProviderTest { assertThat(automationCompositionProvider .findAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION))).isEmpty(); - - when(automationCompositionRepository.findById(any())).thenThrow(IllegalArgumentException.class); - - assertThatThrownBy(() -> automationCompositionProvider.findAutomationComposition(ID_NAME_NOT_VALID, ID_VERSION)) - .hasMessageMatching("Not valid parameter"); } @Test - void testDeleteAutomationComposition() throws Exception { + void testDeleteAutomationComposition() { var automationCompositionRepository = mock(AutomationCompositionRepository.class); var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository); |