Merge "APPC Actor"
authorJim Hahn <jrh3@att.com>
Tue, 18 Feb 2020 19:37:52 +0000 (19:37 +0000)
committerGerrit Code Review <gerrit@onap.org>
Tue, 18 Feb 2020 19:37:52 +0000 (19:37 +0000)
20 files changed:
models-dao/src/test/java/org/onap/policy/models/dao/DummyTimestampEntity.java
models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java
models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java
models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java
models-sim/policy-models-sim-pdp/src/main/java/org/onap/policy/models/sim/pdp/handler/PdpMessageHandler.java
models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java

index d18a915..0dee8b4 100644 (file)
@@ -32,7 +32,6 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 
-import org.onap.policy.common.utils.validation.Assertions;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfTimestampKey;
@@ -93,7 +92,6 @@ public class DummyTimestampEntity extends PfConcept {
         key.clean();
     }
 
-
     @Override
     public int compareTo(@NonNull final PfConcept otherObj) {
         if (this == otherObj) {
index 0b22e1b..edef297 100644 (file)
@@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
 import java.util.Date;
+
 import org.junit.Test;
 import org.onap.policy.models.base.PfTimestampKey;
 import org.onap.policy.models.base.PfValidationResult;
@@ -43,11 +44,9 @@ public class JpaPdpStatisticsTest {
     public void testConstructor() {
         assertThatThrownBy(() -> new JpaPdpStatistics((PfTimestampKey) null)).hasMessageContaining("key");
 
-        assertThatThrownBy(() -> new JpaPdpStatistics((JpaPdpStatistics) null))
-            .hasMessageContaining("copyConcept");
+        assertThatThrownBy(() -> new JpaPdpStatistics((JpaPdpStatistics) null)).hasMessageContaining("copyConcept");
 
-        assertThatThrownBy(() -> new JpaPdpStatistics((PdpStatistics) null))
-            .hasMessageContaining("authorativeConcept");
+        assertThatThrownBy(() -> new JpaPdpStatistics((PdpStatistics) null)).hasMessageContaining("authorativeConcept");
 
         assertNotNull(new JpaPdpStatistics());
         assertNotNull(new JpaPdpStatistics(new PfTimestampKey()));
index 08c01b6..3cae650 100644 (file)
@@ -49,8 +49,10 @@ import org.onap.policy.models.provider.PolicyModelsProvider;
 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
@@ -101,7 +103,7 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
         daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
 
         // Decode the password using Base64
-        String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
+        String decodedPassword = new String(Base64.getDecoder().decode(getValue(parameters.getDatabasePassword())));
 
         // @formatter:off
         Properties jdbcProperties = new Properties();
@@ -125,6 +127,13 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
         }
     }
 
+    private String getValue(final String value) {
+        if (value != null && value.matches("[$][{].*[}]$")) {
+            return System.getenv(value.substring(2, value.length() - 1));
+        }
+        return value;
+    }
+
     @Override
     public void close() throws PfModelException {
         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
@@ -182,6 +191,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
     public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
             throws PfModelException {
         assertInitialized();
+
+        ToscaPolicyTypeIdentifier policyTypeIdentifier = new ToscaPolicyTypeIdentifier(name, version);
+        assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
+
         return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version);
     }
 
@@ -227,6 +240,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
     public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
             throws PfModelException {
         assertInitialized();
+
+        ToscaPolicyIdentifier policyIdentifier = new ToscaPolicyIdentifier(name, version);
+        assertPolicyNotDeployedInPdpGroup(policyIdentifier);
+
         return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version);
     }
 
@@ -255,6 +272,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId,
             @NonNull final String policyVersion) throws PfModelException {
         assertInitialized();
+
+        assertPolicyNotDeployedInPdpGroup(
+                new ToscaPolicyIdentifier(policyId, policyVersion + LegacyProvider.LEGACY_MINOR_PATCH_SUFFIX));
+
         return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId, policyVersion);
     }
 
@@ -283,6 +304,10 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId,
             @NonNull final String policyVersion) throws PfModelException {
         assertInitialized();
+
+        assertPolicyNotDeployedInPdpGroup(
+                new ToscaPolicyIdentifier(policyId, policyVersion + LegacyProvider.LEGACY_MINOR_PATCH_SUFFIX));
+
         return new LegacyProvider().deleteGuardPolicy(pfDao, policyId, policyVersion);
     }
 
@@ -375,4 +400,42 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
         }
     }
+
+    /**
+     * Assert that the policy type is not supported in any PDP group.
+     *
+     * @param policyTypeIdentifier the policy type identifier
+     * @throws PfModelException if the policy type is supported in a PDP group
+     */
+    private void assertPolicyTypeNotSupportedInPdpGroup(ToscaPolicyTypeIdentifier policyTypeIdentifier)
+            throws PfModelException {
+        for (PdpGroup pdpGroup : getPdpGroups(null)) {
+            for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+                if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
+                    throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+                            "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
+                                    + pdpSubGroup.getPdpType());
+                }
+            }
+        }
+    }
+
+    /**
+     * Assert that the policy is not deployed in a PDP group.
+     *
+     * @param policyIdentifier the identifier of the policy
+     * @throws PfModelException thrown if the policy is deployed in a PDP group
+     */
+    private void assertPolicyNotDeployedInPdpGroup(final ToscaPolicyIdentifier policyIdentifier)
+            throws PfModelException {
+        for (PdpGroup pdpGroup : getPdpGroups(null)) {
+            for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+                if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
+                    throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+                            "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
+                                    + pdpSubGroup.getPdpType());
+                }
+            }
+        }
+    }
 }
index 3d1c9f6..c09c6c2 100644 (file)
@@ -258,7 +258,7 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider {
     @Override
     public List<PdpStatistics> deletePdpStatistics(final String name, final Date timestamp) {
         // Not implemented
-        return null;
+        return new ArrayList<>();
     }
 
     /**
index aa6802a..f085605 100644 (file)
@@ -45,6 +45,7 @@ import org.onap.policy.models.provider.PolicyModelsProvider;
 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
@@ -536,4 +537,79 @@ public class DatabasePolicyModelsProviderTest {
 
         databaseProvider.close();
     }
+
+    @Test
+    public void testDeletePolicyDeployedInSubgroup() throws PfModelException {
+        List<ToscaPolicyIdentifier> policies = new ArrayList<>();
+
+        policies.add(new ToscaPolicyIdentifier("p0", "0.0.1"));
+        policies.add(new ToscaPolicyIdentifier("p1", "0.0.1"));
+
+        List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
+        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt2", "0.0.1"));
+
+        PdpSubGroup subGroup = new PdpSubGroup();
+        subGroup.setPdpType("pdpType");
+        subGroup.setSupportedPolicyTypes(supportedPolicyTypes);
+        subGroup.setPolicies(policies);
+
+        List<PdpSubGroup> pdpSubgroups = new ArrayList<>();
+        pdpSubgroups.add(subGroup);
+
+        PdpGroup pdpGroup = new PdpGroup();
+        pdpGroup.setName("pdpGroup");
+        pdpGroup.setPdpGroupState(PdpState.PASSIVE);
+        pdpGroup.setPdpSubgroups(pdpSubgroups);
+
+        List<PdpGroup> pdpGroups = new ArrayList<>();
+        pdpGroups.add(pdpGroup);
+
+        PolicyModelsProvider databaseProvider =
+                new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+        databaseProvider.createPdpGroups(pdpGroups);
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicy("p0", "0.0.1"))
+                .hasMessageContaining("policy is in use, it is deployed in PDP group pdpGroup subgroup pdpType");
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicy("p3", "0.0.1"))
+                .hasMessageContaining("service template not found in database");
+
+        databaseProvider.close();
+    }
+
+    @Test
+    public void testDeletePolicyTypeSupportedInSubgroup() throws PfModelException {
+        List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
+        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt1", "0.0.1"));
+        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt2", "0.0.1"));
+
+        PdpSubGroup subGroup = new PdpSubGroup();
+        subGroup.setPdpType("pdpType");
+        subGroup.setSupportedPolicyTypes(supportedPolicyTypes);
+
+        List<PdpSubGroup> pdpSubgroups = new ArrayList<>();
+        pdpSubgroups.add(subGroup);
+
+        PdpGroup pdpGroup = new PdpGroup();
+        pdpGroup.setName("pdpGroup");
+        pdpGroup.setPdpGroupState(PdpState.PASSIVE);
+        pdpGroup.setPdpSubgroups(pdpSubgroups);
+
+        List<PdpGroup> pdpGroups = new ArrayList<>();
+        pdpGroups.add(pdpGroup);
+
+        PolicyModelsProvider databaseProvider =
+                new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+        databaseProvider.createPdpGroups(pdpGroups);
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicyType("pt2", "0.0.1"))
+                .hasMessageContaining("policy type is in use, it is referenced in PDP group pdpGroup subgroup pdpType");
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicyType("pt0", "0.0.1"))
+                .hasMessageContaining("service template not found in database");
+
+        databaseProvider.close();
+    }
 }
index 500d9d4..3212428 100644 (file)
@@ -28,6 +28,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.Date;
 
 import org.junit.Test;
 import org.onap.policy.models.pdp.concepts.Pdp;
@@ -117,6 +118,12 @@ public class DummyPolicyModelsProviderTest {
         dummyProvider.updatePdp("name", "type", new Pdp());
         dummyProvider.updatePdpStatistics(new ArrayList<>());
         assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty());
+
+        assertTrue(
+                dummyProvider.getFilteredPdpStatistics("name", null, null, new Date(), new Date(), null, 0).isEmpty());
+        assertTrue(dummyProvider.createPdpStatistics(null).isEmpty());
+        assertTrue(dummyProvider.updatePdpStatistics(null).isEmpty());
+        assertTrue(dummyProvider.deletePdpStatistics(null, new Date()).isEmpty());
     }
 
     @Test
index 855919e..de0edef 100644 (file)
@@ -32,10 +32,8 @@ import org.onap.policy.models.pdp.enums.PdpResponseStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
 import org.onap.policy.models.sim.pdp.parameters.PdpStatusParameters;
-import org.onap.policy.models.sim.pdp.parameters.ToscaPolicyTypeIdentifierParameters;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 
 /**
  * This class supports the handling of pdp messages.
@@ -63,24 +61,6 @@ public class PdpMessageHandler {
         return pdpStatus;
     }
 
-    /**
-     * Method to get supported policy types from the parameters.
-     *
-     * @param pdpStatusParameters pdp status parameters
-     * @return supportedPolicyTypes list of PolicyTypeIdent
-     */
-    private List<ToscaPolicyTypeIdentifier> getSupportedPolicyTypesFromParameters(
-            final PdpStatusParameters pdpStatusParameters) {
-        final List<ToscaPolicyTypeIdentifier> supportedPolicyTypes =
-                new ArrayList<>(pdpStatusParameters.getSupportedPolicyTypes().size());
-        for (final ToscaPolicyTypeIdentifierParameters policyTypeIdentParameters : pdpStatusParameters
-                .getSupportedPolicyTypes()) {
-            supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(policyTypeIdentParameters.getName(),
-                    policyTypeIdentParameters.getVersion()));
-        }
-        return supportedPolicyTypes;
-    }
-
     /**
      * Method to create PdpStatus message from the context, which is to be sent by pdp simulator to pap.
      *
index 09cc6c0..dc8affc 100644 (file)
@@ -53,7 +53,7 @@ import org.slf4j.LoggerFactory;
 public class LegacyProvider {
     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
 
-    private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
+    public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
 
     // Recurring constants
     private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
index 2a12157..664855e 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ package org.onap.policy.models.tosca.simple.concepts;
 
 import java.util.ArrayList;
 import java.util.List;
+
 import javax.persistence.ElementCollection;
 
 import lombok.EqualsAndHashCode;
@@ -77,8 +78,8 @@ public class JpaToscaConstraintValidValues extends JpaToscaConstraint {
 
     @Override
     public void fromAuthorative(final ToscaConstraint toscaConstraint) {
+        validValues = new ArrayList<>();
         if (toscaConstraint.getValidValues() != null) {
-            validValues = new ArrayList<>();
             validValues.addAll(toscaConstraint.getValidValues());
         }
     }
index 881d87c..b432486 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP Policy Model
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -47,7 +47,6 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntrySchema;
 
-
 /**
  * Class to represent the EntrySchema of list/map property in TOSCA definition.
  *
@@ -72,7 +71,7 @@ public class JpaToscaEntrySchema
     private String description;
 
     @ElementCollection
-    private List<JpaToscaConstraint> constraints;
+    private List<JpaToscaConstraint> constraints = new ArrayList<>();
 
     /**
      * The full constructor creates a {@link JpaToscaEntrySchema} object with mandatory fields.
@@ -170,7 +169,6 @@ public class JpaToscaEntrySchema
                     this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank"));
         }
 
-
         if (constraints != null) {
             for (JpaToscaConstraint constraint : constraints) {
                 if (constraint == null) {
index b500a8b..b5da497 100644 (file)
@@ -88,7 +88,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
 
     @ElementCollection
     @Lob
-    private Map<String, String> properties;
+    private Map<String, String> properties = new LinkedHashMap<>();
 
     @ElementCollection
     private List<PfConceptKey> targets = new ArrayList<>();
index 0e8201f..a2127f3 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP Policy Model
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+
 import javax.persistence.Column;
 import javax.persistence.ElementCollection;
 import javax.persistence.EmbeddedId;
@@ -35,9 +36,11 @@ import javax.persistence.Entity;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
 import javax.persistence.Table;
+
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
+
 import org.apache.commons.lang3.ObjectUtils;
 import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
@@ -85,13 +88,13 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr
     private Status status = Status.SUPPORTED;
 
     @ElementCollection
-    private List<JpaToscaConstraint> constraints;
+    private List<JpaToscaConstraint> constraints = new ArrayList<>();
 
     @Column
     private JpaToscaEntrySchema entrySchema;
 
     @ElementCollection
-    private Map<String, String> metadata;
+    private Map<String, String> metadata = new LinkedHashMap<>();
 
     /**
      * The Default Constructor creates a {@link JpaToscaProperty} object with a null key.
index c537bbc..a3e18ca 100644 (file)
@@ -37,8 +37,10 @@ import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.base.PfValidationResult;
 import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
@@ -58,8 +60,11 @@ public class SimpleToscaProvider {
     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
 
     // Recurring string constants
+    private static final String DATA_TYPE = "data type ";
+    private static final String POLICY_TYPE = "policy type ";
     private static final String SERVICE_TEMPLATE_NOT_FOUND_IN_DATABASE = "service template not found in database";
     private static final String DO_NOT_EXIST = " do not exist";
+    private static final String NOT_FOUND = " not found";
 
     /**
      * Get Service Template.
@@ -226,7 +231,32 @@ public class SimpleToscaProvider {
             throws PfModelException {
         LOGGER.debug("->deleteDataType: key={}", dataTypeKey);
 
-        JpaToscaServiceTemplate serviceTemplate = getDataTypes(dao, dataTypeKey.getName(), dataTypeKey.getVersion());
+        JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
+
+        if (!ToscaUtils.doDataTypesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no data types found");
+        }
+
+        JpaToscaDataType dataType4Deletion = serviceTemplate.getDataTypes().get(dataTypeKey);
+        if (dataType4Deletion == null) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND, DATA_TYPE + dataTypeKey.getId() + NOT_FOUND);
+        }
+
+        for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) {
+            if (dataType.getReferencedDataTypes().contains(dataTypeKey)) {
+                throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, DATA_TYPE + dataTypeKey.getId()
+                        + " is in use, it is referenced in data type " + dataType.getId());
+            }
+        }
+
+        if (ToscaUtils.doPolicyTypesExist(serviceTemplate)) {
+            for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
+                if (policyType.getReferencedDataTypes().contains(dataTypeKey)) {
+                    throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, DATA_TYPE + dataTypeKey.getId()
+                            + " is in use, it is referenced in policy type " + policyType.getId());
+                }
+            }
+        }
 
         dao.delete(JpaToscaDataType.class, dataTypeKey);
 
@@ -355,8 +385,37 @@ public class SimpleToscaProvider {
             throws PfModelException {
         LOGGER.debug("->deletePolicyType: key={}", policyTypeKey);
 
-        JpaToscaServiceTemplate serviceTemplate =
-                getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
+        JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
+
+        if (!ToscaUtils.doPolicyTypesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no policy types found");
+        }
+
+        JpaToscaEntityType<? extends ToscaEntity> policyType4Deletion =
+                serviceTemplate.getPolicyTypes().get(policyTypeKey);
+        if (policyType4Deletion == null) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+                    POLICY_TYPE + policyTypeKey.getId() + NOT_FOUND);
+        }
+
+        for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
+            Collection<JpaToscaEntityType<ToscaEntity>> ancestorList = ToscaUtils
+                    .getEntityTypeAncestors(serviceTemplate.getPolicyTypes(), policyType, new PfValidationResult());
+
+            if (ancestorList.contains(policyType4Deletion)) {
+                throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, POLICY_TYPE + policyTypeKey.getId()
+                        + " is in use, it is referenced in policy type " + policyType.getId());
+            }
+        }
+
+        if (ToscaUtils.doPoliciesExist(serviceTemplate)) {
+            for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+                if (policyTypeKey.equals(policy.getType())) {
+                    throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, POLICY_TYPE
+                            + policyTypeKey.getId() + " is in use, it is referenced in policy " + policy.getId());
+                }
+            }
+        }
 
         dao.delete(JpaToscaPolicyType.class, policyTypeKey);
 
@@ -476,7 +535,16 @@ public class SimpleToscaProvider {
             throws PfModelException {
         LOGGER.debug("->deletePolicy: key={}", policyKey);
 
-        JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
+        JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao);
+
+        if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no policies found");
+        }
+
+        JpaToscaPolicy policy4Deletion = serviceTemplate.getTopologyTemplate().getPolicies().get(policyKey);
+        if (policy4Deletion == null) {
+            throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "policy " + policyKey.getId() + NOT_FOUND);
+        }
 
         dao.delete(JpaToscaPolicy.class, policyKey);
 
@@ -507,7 +575,7 @@ public class SimpleToscaProvider {
 
         if (policyType == null) {
             String errorMessage =
-                    "policy type " + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist";
+                    POLICY_TYPE + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist";
             throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, errorMessage);
         }
     }
index b75273e..77633bd 100644 (file)
@@ -211,7 +211,7 @@ public final class ToscaUtils {
     }
 
     /**
-     * Find all the ancestors of an entity type.
+     * getLatestPolicyTypeVersion Find all the ancestors of an entity type.
      *
      * @param entityTypes the set of entity types that exist
      * @param entityType the entity type for which to get the parents
@@ -227,6 +227,12 @@ public final class ToscaUtils {
             return CollectionUtils.emptyCollection();
         }
 
+        if (entityType.getKey().equals(parentEntityTypeKey)) {
+            result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class,
+                    ValidationResult.INVALID, "entity cannot be an ancestor of itself"));
+            throw new PfModelRuntimeException(Response.Status.CONFLICT, result.toString());
+        }
+
         @SuppressWarnings("unchecked")
         Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySet = (Set<JpaToscaEntityType<ToscaEntity>>) entityTypes
                 .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
index 62c088c..332552a 100644 (file)
@@ -50,7 +50,13 @@ public class LegacyGuardPolicyMapperTest {
         JpaToscaPolicy policy = new JpaToscaPolicy(new PfConceptKey("PolicyName", "2.0.0"));
         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy.getKey(), policy);
 
+        policy.setMetadata(null);
+        assertThatThrownBy(() -> {
+            new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
+        }).hasMessageContaining("no metadata defined on TOSCA policy");
+
         policy.setMetadata(new LinkedHashMap<>());
+        policy.setProperties(null);
         assertThatThrownBy(() -> {
             new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
         }).hasMessageContaining("no properties defined on TOSCA policy");
index 4dcfeaf..79de6f9 100644 (file)
@@ -106,6 +106,7 @@ public class LegacyOperationalPolicyMapperTest {
 
         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().remove(policy1.getKey());
 
+        policy0.setProperties(null);
         assertThatThrownBy(() -> {
             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
         }).hasMessage("no properties defined on TOSCA policy");
index f34c0c8..4937c5c 100644 (file)
@@ -36,6 +36,7 @@ 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.base.PfModelException;
+import org.onap.policy.models.base.PfReferenceKey;
 import org.onap.policy.models.dao.DaoParameters;
 import org.onap.policy.models.dao.PfDao;
 import org.onap.policy.models.dao.PfDaoFactory;
@@ -45,8 +46,10 @@ import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvide
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
 import org.yaml.snakeyaml.Yaml;
@@ -135,13 +138,64 @@ public class SimpleToscaProviderTest {
         assertEquals(dataType0, gotServiceTemplate.getDataTypes().get(dataType0Key));
         assertEquals("Updated Description", gotServiceTemplate.getDataTypes().get(dataType0Key).getDescription());
 
+        assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, new PfConceptKey("IDontExist:0.0.1")))
+                .hasMessage("data type IDontExist:0.0.1 not found");
+
         JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key);
 
         assertEquals(dataType0, deletedServiceTemplate.getDataTypes().get(dataType0Key));
         assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription());
 
+        // Create the data type again
+        new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate);
+
+        updatedServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
+        JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1"));
+        updatedServiceTemplate.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0);
+        new SimpleToscaProvider().createPolicyTypes(pfDao, updatedServiceTemplate);
+
+        deletedServiceTemplate = new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key);
+
+        assertEquals(dataType0, deletedServiceTemplate.getDataTypes().get(dataType0Key));
+        assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription());
+
         assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key))
-                .hasMessage("data types for DataType0:0.0.1 do not exist");
+                .hasMessage("no data types found");
+
+        // Create the data type again
+        new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate);
+
+        JpaToscaPolicyType pt0v2 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.2"));
+        JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0v2.getKey(), "prop0"));
+        prop0.setType(dataType0Key);
+        pt0v2.getProperties().put(prop0.getKey().getLocalName(), prop0);
+        updatedServiceTemplate.getPolicyTypes().getConceptMap().put(pt0v2.getKey(), pt0v2);
+        new SimpleToscaProvider().createPolicyTypes(pfDao, updatedServiceTemplate);
+
+        assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key))
+                .hasMessage("data type DataType0:0.0.1 is in use, it is referenced in policy type pt0:0.0.2");
+
+        JpaToscaDataType dataType0v2 = new JpaToscaDataType(new PfConceptKey("DataType0:0.0.2"));
+        updatedServiceTemplate.getDataTypes().getConceptMap().put(dataType0v2.getKey(), dataType0v2);
+        new SimpleToscaProvider().createDataTypes(pfDao, updatedServiceTemplate);
+
+        deletedServiceTemplate = new SimpleToscaProvider().deleteDataType(pfDao, dataType0v2.getKey());
+
+        assertEquals(dataType0, deletedServiceTemplate.getDataTypes().get(dataType0Key));
+        assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription());
+
+        assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key))
+                .hasMessage("data type DataType0:0.0.1 is in use, it is referenced in policy type pt0:0.0.2");
+
+        JpaToscaDataType dataType1 = new JpaToscaDataType(new PfConceptKey("DataType1:0.0.3"));
+        JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(dataType1.getKey(), "prop1"));
+        prop1.setType(dataType0v2.getKey());
+        dataType1.getProperties().put(prop1.getKey().getLocalName(), prop1);
+        updatedServiceTemplate.getDataTypes().getConceptMap().put(dataType1.getKey(), dataType1);
+        new SimpleToscaProvider().createDataTypes(pfDao, updatedServiceTemplate);
+
+        assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0v2.getKey()))
+                .hasMessage("data type DataType0:0.0.2 is in use, it is referenced in data type DataType1:0.0.3");
     }
 
     @Test
@@ -182,6 +236,42 @@ public class SimpleToscaProviderTest {
         assertEquals(policyType0, gotServiceTemplate.getPolicyTypes().get(policyType0Key));
         assertEquals("Updated Description", gotServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription());
 
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().deletePolicyType(pfDao, new PfConceptKey("IDontExist:0.0.1"));
+        }).hasMessage("policy type IDontExist:0.0.1 not found");
+
+        JpaToscaPolicyType pt1 = new JpaToscaPolicyType(new PfConceptKey("pt1:0.0.2"));
+        pt1.setDerivedFrom(policyType0Key);
+        serviceTemplate.getPolicyTypes().getConceptMap().put(pt1.getKey(), pt1);
+        new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
+
+        assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key))
+                .hasMessage("policy type PolicyType0:0.0.1 is in use, it is referenced in policy type pt1:0.0.2");
+
+        serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
+        serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
+
+        JpaToscaPolicy p0 = new JpaToscaPolicy(new PfConceptKey("p0:0.0.1"));
+        p0.setType(policyType0Key);
+        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(p0.getKey(), p0);
+
+        JpaToscaPolicy p1 = new JpaToscaPolicy(new PfConceptKey("p1:0.0.1"));
+        p1.setType(pt1.getKey());
+        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(p1.getKey(), p1);
+        new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate);
+
+        assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key))
+                .hasMessage("policy type PolicyType0:0.0.1 is in use, it is referenced in policy type pt1:0.0.2");
+
+        assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, pt1.getKey()))
+                .hasMessage("policy type pt1:0.0.2 is in use, it is referenced in policy p1:0.0.1");
+
+        new SimpleToscaProvider().deletePolicy(pfDao, p1.getKey());
+
+        new SimpleToscaProvider().deletePolicyType(pfDao, pt1.getKey());
+
+        new SimpleToscaProvider().deletePolicy(pfDao, p0.getKey());
+
         JpaToscaServiceTemplate deletedServiceTemplate =
                 new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key);
 
@@ -190,7 +280,7 @@ public class SimpleToscaProviderTest {
                 deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription());
 
         assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key))
-                .hasMessage("policy types for PolicyType0:0.0.1 do not exist");
+                .hasMessage("no policy types found");
     }
 
     @Test
@@ -232,7 +322,7 @@ public class SimpleToscaProviderTest {
                 deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription());
 
         assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key))
-                .hasMessage("policy types for PolicyType0:0.0.1 do not exist");
+                .hasMessage("no policy types found");
     }
 
     @Test
@@ -259,6 +349,9 @@ public class SimpleToscaProviderTest {
 
         assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
                 gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
+
+        JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, policyKey);
+        assertEquals(1, deletedServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size());
     }
 
     @Test
@@ -314,17 +407,20 @@ public class SimpleToscaProviderTest {
 
         PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
 
-        JpaToscaServiceTemplate deletedServiceTemplate =
-                new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey(policyKey));
+        assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey("IDontExist:0.0.1")))
+                .hasMessage("policy IDontExist:0.0.1 not found");
+
+        JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, policyKey);
 
         assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
                 deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
 
-        // @formatter:off
-        assertThatThrownBy(
-            () -> new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()))
-                    .hasMessage("policies for onap.restart.tca:1.0.0 do not exist");
-        // @formatter:on
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion());
+        }).hasMessage("policies for onap.restart.tca:1.0.0 do not exist");
+
+        assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicy(pfDao, policyKey))
+                .hasMessage("no policies found");
     }
 
     @Test
@@ -343,8 +439,219 @@ public class SimpleToscaProviderTest {
                 .hasMessage("list of policies specified on topology template of service template is empty");
     }
 
+    @Test
+    public void testGetServiceTemplate() throws PfModelException {
+        assertThatThrownBy(() -> new SimpleToscaProvider().getServiceTemplate(pfDao))
+                .hasMessage("service template not found in database");
+    }
+
+    @Test
+    public void testAppendToServiceTemplate() throws PfModelException {
+        JpaToscaServiceTemplate serviceTemplateFragment = new JpaToscaServiceTemplate();
+        serviceTemplateFragment.setPolicyTypes(new JpaToscaPolicyTypes());
+        JpaToscaPolicyType badPt = new JpaToscaPolicyType();
+        serviceTemplateFragment.getPolicyTypes().getConceptMap().put(badPt.getKey(), badPt);
+
+        assertThatThrownBy(() -> new SimpleToscaProvider().appendToServiceTemplate(pfDao, serviceTemplateFragment))
+                .hasMessageContaining(
+                        "key on concept entry PfConceptKey(name=NULL, version=0.0.0) may not be the null key");
+    }
+
+    @Test
+    public void testGetDataTypesCornerCases() throws PfModelException {
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getDataTypes(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("service template not found in database");
+
+        JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
+        serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
+        JpaToscaPolicyType p0 = new JpaToscaPolicyType(new PfConceptKey("p0:0.0.1"));
+        serviceTemplate.getPolicyTypes().getConceptMap().put(p0.getKey(), p0);
+
+        new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getDataTypes(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("data types for hello:0.0.1 do not exist");
+
+        serviceTemplate.setDataTypes(new JpaToscaDataTypes());
+
+        JpaToscaDataType p01 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
+        serviceTemplate.getDataTypes().getConceptMap().put(p01.getKey(), p01);
+
+        new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getDataTypes(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("data types for hello:0.0.1 do not exist");
+
+        JpaToscaServiceTemplate gotSt = new SimpleToscaProvider().getDataTypes(pfDao, p01.getName(), p01.getVersion());
+
+        assertEquals(p01, gotSt.getDataTypes().get(p01.getKey()));
+        assertEquals(p01, gotSt.getDataTypes().get(p01.getName()));
+        assertEquals(p01, gotSt.getDataTypes().get(p01.getName(), null));
+        assertEquals(p01, gotSt.getDataTypes().get(p01.getName(), p01.getVersion()));
+        assertEquals(1, gotSt.getDataTypes().getAll(null).size());
+        assertEquals(1, gotSt.getDataTypes().getAll(null, null).size());
+        assertEquals(1, gotSt.getDataTypes().getAll(p01.getName(), null).size());
+        assertEquals(1, gotSt.getDataTypes().getAll(p01.getName(), p01.getVersion()).size());
+
+        JpaToscaDataType p02 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.2"));
+        serviceTemplate.getDataTypes().getConceptMap().put(p02.getKey(), p02);
+
+        new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate);
+        gotSt = new SimpleToscaProvider().getDataTypes(pfDao, p01.getName(), p01.getVersion());
+
+        assertEquals(p01, gotSt.getDataTypes().get(p01.getKey()));
+        assertEquals(p02, gotSt.getDataTypes().get(p01.getName()));
+        assertEquals(p02, gotSt.getDataTypes().get(p01.getName(), null));
+        assertEquals(p01, gotSt.getDataTypes().get(p01.getName(), p01.getVersion()));
+        assertEquals(p02, gotSt.getDataTypes().get(p01.getName(), p02.getVersion()));
+        assertEquals(2, gotSt.getDataTypes().getAll(null).size());
+        assertEquals(2, gotSt.getDataTypes().getAll(null, null).size());
+        assertEquals(2, gotSt.getDataTypes().getAll(p01.getName(), null).size());
+        assertEquals(1, gotSt.getDataTypes().getAll(p01.getName(), p02.getVersion()).size());
+    }
+
+    @Test
+    public void testGetPolicyTypesCornerCases() throws PfModelException {
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getPolicyTypes(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("service template not found in database");
+
+        JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
+        serviceTemplate.setDataTypes(new JpaToscaDataTypes());
+        JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
+        serviceTemplate.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
+
+        new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getPolicyTypes(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("policy types for hello:0.0.1 do not exist");
+
+        serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
+
+        JpaToscaPolicyType pt01 = new JpaToscaPolicyType(new PfConceptKey("p0:0.0.1"));
+        serviceTemplate.getPolicyTypes().getConceptMap().put(pt01.getKey(), pt01);
+
+        new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getPolicyTypes(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("policy types for hello:0.0.1 do not exist");
+
+        JpaToscaServiceTemplate gotSt =
+                new SimpleToscaProvider().getPolicyTypes(pfDao, pt01.getName(), pt01.getVersion());
+
+        assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getKey()));
+        assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName()));
+        assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName(), null));
+        assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName(), pt01.getVersion()));
+        assertEquals(1, gotSt.getPolicyTypes().getAll(null).size());
+        assertEquals(1, gotSt.getPolicyTypes().getAll(null, null).size());
+        assertEquals(1, gotSt.getPolicyTypes().getAll(pt01.getName(), null).size());
+        assertEquals(1, gotSt.getPolicyTypes().getAll(pt01.getName(), pt01.getVersion()).size());
+
+        JpaToscaPolicyType pt02 = new JpaToscaPolicyType(new PfConceptKey("p0:0.0.2"));
+        serviceTemplate.getPolicyTypes().getConceptMap().put(pt02.getKey(), pt02);
+
+        new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
+        gotSt = new SimpleToscaProvider().getPolicyTypes(pfDao, pt01.getName(), pt01.getVersion());
+
+        assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getKey()));
+        assertEquals(pt02, gotSt.getPolicyTypes().get(pt01.getName()));
+        assertEquals(pt02, gotSt.getPolicyTypes().get(pt01.getName(), null));
+        assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName(), pt01.getVersion()));
+        assertEquals(pt02, gotSt.getPolicyTypes().get(pt01.getName(), pt02.getVersion()));
+        assertEquals(2, gotSt.getPolicyTypes().getAll(null).size());
+        assertEquals(2, gotSt.getPolicyTypes().getAll(null, null).size());
+        assertEquals(2, gotSt.getPolicyTypes().getAll(pt01.getName(), null).size());
+        assertEquals(1, gotSt.getPolicyTypes().getAll(pt01.getName(), pt02.getVersion()).size());
+    }
+
+    @Test
+    public void testGetPoliciesCornerCases() throws PfModelException {
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getPolicies(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("service template not found in database");
+
+        JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
+        serviceTemplate.setDataTypes(new JpaToscaDataTypes());
+        JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
+        serviceTemplate.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
+
+        new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getPolicies(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("policies for hello:0.0.1 do not exist");
+
+        serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
+
+        JpaToscaPolicyType pt01 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1"));
+        serviceTemplate.getPolicyTypes().getConceptMap().put(pt01.getKey(), pt01);
+
+        serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
+        serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
+
+        JpaToscaPolicy p01 = new JpaToscaPolicy(new PfConceptKey("p0:0.0.1"));
+        p01.setType(pt01.getKey());
+        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(p01.getKey(), p01);
+
+        new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getPolicies(pfDao, "hello", "0.0.1");
+        }).hasMessageMatching("policies for hello:0.0.1 do not exist");
+
+        JpaToscaServiceTemplate gotSt = new SimpleToscaProvider().getPolicies(pfDao, p01.getName(), p01.getVersion());
+
+        assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getKey()));
+        assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName()));
+        assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), null));
+        assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), p01.getVersion()));
+        assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(null).size());
+        assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(null, null).size());
+        assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), null).size());
+        assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), p01.getVersion()).size());
+
+        JpaToscaPolicy p02 = new JpaToscaPolicy(new PfConceptKey("p0:0.0.2"));
+        p02.setType(pt01.getKey());
+        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(p02.getKey(), p02);
+
+        new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate);
+        gotSt = new SimpleToscaProvider().getPolicies(pfDao, p01.getName(), p01.getVersion());
+
+        assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getKey()));
+        assertEquals(p02, gotSt.getTopologyTemplate().getPolicies().get(p01.getName()));
+        assertEquals(p02, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), null));
+        assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), p01.getVersion()));
+        assertEquals(p02, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), p02.getVersion()));
+        assertEquals(2, gotSt.getTopologyTemplate().getPolicies().getAll(null).size());
+        assertEquals(2, gotSt.getTopologyTemplate().getPolicies().getAll(null, null).size());
+        assertEquals(2, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), null).size());
+        assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), p02.getVersion()).size());
+    }
+
     @Test
     public void testNonNulls() {
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().getServiceTemplate(null);
+        }).hasMessageMatching(DAO_IS_NULL);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().appendToServiceTemplate(null, null);
+        }).hasMessageMatching(DAO_IS_NULL);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().appendToServiceTemplate(null, new JpaToscaServiceTemplate());
+        }).hasMessageMatching(DAO_IS_NULL);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().appendToServiceTemplate(pfDao, null);
+        }).hasMessageMatching("^incomingServiceTemplateFragment is marked .*on.*ull but is null$");
+
         assertThatThrownBy(() -> {
             new SimpleToscaProvider().getDataTypes(null, null, null);
         }).hasMessageMatching(DAO_IS_NULL);
index 0a8283e..270bc6c 100644 (file)
@@ -199,8 +199,9 @@ public class MonitoringPolicyTypeSerializationTest {
         assertTrue(firstDataTypeFirstProperty.getConstraints().size() == 1);
         assertEquals("org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintValidValues",
                 firstDataTypeFirstProperty.getConstraints().iterator().next().getClass().getName());
-        assertTrue(((JpaToscaConstraintValidValues) (firstDataTypeFirstProperty.getConstraints().iterator().next()))
-                .getValidValues().size() == 2);
+        assertEquals(2,
+                ((JpaToscaConstraintValidValues) (firstDataTypeFirstProperty.getConstraints().iterator().next()))
+                        .getValidValues().size());
 
         JpaToscaProperty firstDataTypeSecondProperty = firstDataTypePropertiesIter.next();
         assertEquals(METRICS, firstDataTypeSecondProperty.getKey().getParentKeyName());
index e710faa..fb258a2 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 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,11 +21,11 @@ package org.onap.policy.models.tosca.simple.serialization;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.util.List;
 import java.util.Map;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.common.utils.coder.CoderException;
@@ -115,8 +116,8 @@ public class OptimizationPolicyTypeSerializationTest {
         return coder.encode(auth);
     }
 
-    private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom,
-            String typeName, boolean checkResource, boolean checkService) {
+    private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom, String typeName,
+            boolean checkResource, boolean checkService) {
         JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes();
 
         assertEquals(testnm + " type count", 1, policyTypes.getConceptMap().size());
@@ -194,7 +195,7 @@ public class OptimizationPolicyTypeSerializationTest {
         String testnm = testName + " identity";
 
         assertNotNull(testnm, prop);
-        assertNull(testnm + " metadata", prop.getMetadata());
+        assertEquals(testnm + " metadata", 0, prop.getMetadata().size());
     }
 
     private void validateMatchable(String testName, Map<String, String> metadata) {
index a5d145e..d75e37b 100644 (file)
@@ -231,6 +231,14 @@ public class ToscaUtilsTest {
         assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
         assertTrue(result.isValid());
 
+        dt0.setDerivedFrom(dt0.getKey());
+        assertThatThrownBy(() -> {
+            ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, new PfValidationResult());
+        }).hasMessageContaining("entity cannot be an ancestor of itself");
+
+        dt0.setDerivedFrom(null);
+        assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
+
         dt1.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
         assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());