diff options
Diffstat (limited to 'models-tosca/src')
48 files changed, 1779 insertions, 350 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java index 284e39c9b..27d4eb2e7 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java @@ -23,6 +23,8 @@ package org.onap.policy.models.tosca.authorative.concepts; +import com.google.gson.annotations.SerializedName; +import io.swagger.annotations.ApiModelProperty; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -44,6 +46,8 @@ import lombok.ToString; public class ToscaPolicy extends ToscaEntity implements Comparable<ToscaPolicy> { private String type; + @ApiModelProperty(name = "type_version") + @SerializedName("type_version") private String typeVersion; private Map<String, Object> properties; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java index bb0026e9a..012f7de34 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,16 +40,19 @@ import org.onap.policy.models.base.PfObjectFilter; public class ToscaPolicyFilter implements PfObjectFilter<ToscaPolicy> { public static final String LATEST_VERSION = "LATEST"; - // Regular expression + // Exact expression private String name; - // Regular Expression, set to LATEST_VERRSION to get the latest version + // Exact match, set to LATEST_VERSION to get the latest version private String version; - // Regular expression + // version prefix + private String versionPrefix; + + // Exact expression private String type; - // Regular Expression, set to LATEST_VERRSION to get the latest version + // Exact Expression, set to LATEST_VERSION to get the latest version private String typeVersion; @Override @@ -56,11 +60,11 @@ public class ToscaPolicyFilter implements PfObjectFilter<ToscaPolicy> { // @formatter:off List<ToscaPolicy> returnList = originalList.stream() - .filter(p -> filterString(p.getName(), name)) - .filter(p -> LATEST_VERSION.equals(version) - || filterString(p.getVersion(), version)) - .filter(p -> filterString(p.getType(), type)) - .filter(p -> filterString(p.getTypeVersion(), typeVersion)) + .filter(filterStringPred(name, ToscaPolicy::getName)) + .filter(filterStringPred((LATEST_VERSION.equals(version) ? null : version), ToscaPolicy::getVersion)) + .filter(filterPrefixPred(versionPrefix, ToscaPolicy::getVersion)) + .filter(filterStringPred(type, ToscaPolicy::getType)) + .filter(filterStringPred(typeVersion, ToscaPolicy::getTypeVersion)) .collect(Collectors.toList()); // @formatter:off diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifier.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifier.java index e55c6bd4d..f98a238ff 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifier.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifier.java @@ -23,13 +23,16 @@ package org.onap.policy.models.tosca.authorative.concepts; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationResult; /** * Identifies a policy. Both the name and version must be non-null. */ @Data @NoArgsConstructor -public class ToscaPolicyIdentifier { +public class ToscaPolicyIdentifier implements Comparable<ToscaPolicyIdentifier> { @NonNull private String name; @@ -47,4 +50,37 @@ public class ToscaPolicyIdentifier { this.name = source.name; this.version = source.version; } + + /** + * Validates that appropriate fields are populated for an incoming call to the PAP + * REST API. + * + * @return the validation result + */ + public ValidationResult validatePapRest() { + BeanValidationResult result = new BeanValidationResult("group", this); + + result.validateNotNull("name", name); + result.validateNotNull("version", version); + + return result; + } + + @Override + public int compareTo(ToscaPolicyIdentifier other) { + if (this == other) { + return 0; + } + + if (other == null) { + return 1; + } + + int result = ObjectUtils.compare(getName(), other.getName()); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(getVersion(), other.getVersion()); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersion.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersion.java index 9350d1738..a6aec0858 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersion.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersion.java @@ -20,20 +20,25 @@ package org.onap.policy.models.tosca.authorative.concepts; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.google.gson.annotations.SerializedName; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; /** * Policy identifier with an optional version; only the "name" is required. */ @Data @NoArgsConstructor -public class ToscaPolicyIdentifierOptVersion { +public class ToscaPolicyIdentifierOptVersion implements Comparable<ToscaPolicyIdentifierOptVersion> { @NonNull + @SerializedName("policy-id") private String name; + @SerializedName("policy-version") private String version; @@ -47,12 +52,36 @@ public class ToscaPolicyIdentifierOptVersion { this.version = source.version; } + public ToscaPolicyIdentifierOptVersion(ToscaPolicyIdentifier source) { + this.name = source.getName(); + this.version = source.getVersion(); + } + /** * Determines if the version is null/missing. * * @return {@code true} if the version is null/missing, {@code false} */ + @JsonIgnore public boolean isNullVersion() { return (version == null); } + + @Override + public int compareTo(ToscaPolicyIdentifierOptVersion other) { + if (this == other) { + return 0; + } + + if (other == null) { + return 1; + } + + int result = ObjectUtils.compare(getName(), other.getName()); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(getVersion(), other.getVersion()); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifier.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifier.java index a10c3eb9c..4cd1764de 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifier.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifier.java @@ -23,13 +23,16 @@ package org.onap.policy.models.tosca.authorative.concepts; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationResult; /** * Identifies a policy type. Both the name and version must be non-null. */ @Data @NoArgsConstructor -public class ToscaPolicyTypeIdentifier { +public class ToscaPolicyTypeIdentifier implements Comparable<ToscaPolicyTypeIdentifier> { @NonNull private String name; @@ -47,4 +50,37 @@ public class ToscaPolicyTypeIdentifier { this.name = source.name; this.version = source.version; } + + /** + * Validates that appropriate fields are populated for an incoming call to the PAP + * REST API. + * + * @return the validation result + */ + public ValidationResult validatePapRest() { + BeanValidationResult result = new BeanValidationResult("group", this); + + result.validateNotNull("name", name); + result.validateNotNull("version", version); + + return result; + } + + @Override + public int compareTo(ToscaPolicyTypeIdentifier other) { + if (this == other) { + return 0; + } + + if (other == null) { + return 1; + } + + int result = ObjectUtils.compare(getName(), other.getName()); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(getVersion(), other.getVersion()); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java index 4bf014644..5f8729e08 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -200,7 +201,10 @@ public class AuthorativeToscaProvider { public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) throws PfModelException { - ToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicies(dao, null, null).toAuthorative(); + String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion(); + + ToscaServiceTemplate serviceTemplate = + new SimpleToscaProvider().getPolicies(dao, filter.getName(), version).toAuthorative(); List<ToscaPolicy> filteredPolicies = asConceptList(serviceTemplate.getToscaTopologyTemplate().getPolicies()); filteredPolicies = filter.filter(filteredPolicies); @@ -221,7 +225,8 @@ public class AuthorativeToscaProvider { public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) throws PfModelException { - return filter.filter(getPolicyList(dao, null, null)); + String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion(); + return filter.filter(getPolicyList(dao, filter.getName(), version)); } /** @@ -276,10 +281,6 @@ public class AuthorativeToscaProvider { * @return the plain list */ private <T> List<T> asConceptList(final List<Map<String, T>> listOfMaps) { - if (listOfMaps == null) { - return Collections.emptyList(); - } - List<T> returnList = new ArrayList<>(); for (Map<String, T> conceptMap : listOfMaps) { for (T concept : conceptMap.values()) { diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java index cc37338e4..0d04cb9d1 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java @@ -22,6 +22,7 @@ package org.onap.policy.models.tosca.legacy.mapping; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; import javax.ws.rs.core.Response; @@ -46,6 +47,11 @@ import org.slf4j.LoggerFactory; */ public class LegacyGuardPolicyMapper implements JpaToscaServiceTemplateMapper<LegacyGuardPolicyInput, Map<String, LegacyGuardPolicyOutput>> { + + // Tag for metadata fields + private static final String POLICY_ID = "policy-id"; + private static final String POLICY_VERSION = "policy-version"; + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class); private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>(); @@ -55,8 +61,6 @@ public class LegacyGuardPolicyMapper new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0")); GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); - GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout", - new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); GUARD_POLICY_TYPE_MAP.put("guard.blacklist", new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0")); } @@ -84,8 +88,13 @@ public class LegacyGuardPolicyMapper toscaPolicy.setType(guardPolicyType); toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap()); + final Map<String, String> metadata = new LinkedHashMap<>(); + metadata.put(POLICY_ID, toscaPolicy.getKey().getName()); + metadata.put(POLICY_VERSION, Integer.toString(toscaPolicy.getKey().getMajorVersion())); + toscaPolicy.setMetadata(metadata); + final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setToscaDefinitionsVersion("tosca_simimport java.util.HashMap;\n" + "ple_yaml_1_0"); + serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0"); serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); @@ -109,9 +118,20 @@ public class LegacyGuardPolicyMapper legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName()); legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion()); + if (toscaPolicy.getMetadata() == null) { + String errorMessage = "no metadata defined on TOSCA policy"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + final Map<String, Object> metadata = new LinkedHashMap<>(); - metadata.put("policy-id", toscaPolicy.getKey().getName()); - metadata.put("policy-version", toscaPolicy.getKey().getMajorVersion()); + for (Entry<String, String> metadataEntry : toscaPolicy.getMetadata().entrySet()) { + if (POLICY_VERSION.equals(metadataEntry.getKey())) { + metadata.put(POLICY_VERSION, Integer.parseInt(metadataEntry.getValue())); + } else { + metadata.put(metadataEntry.getKey(), metadataEntry.getValue()); + } + } legacyGuardPolicyOutput.setMetadata(metadata); if (toscaPolicy.getProperties() == null) { @@ -139,12 +159,6 @@ public class LegacyGuardPolicyMapper propertiesMap.put("content", content); legacyGuardPolicyOutput.setProperties(propertiesMap); - if (toscaPolicy.getProperties() == null) { - String errorMessage = "property \"Content\" not defined on TOSCA policy"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput); } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java index b6c5d3bba..7caba98d8 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java @@ -106,8 +106,8 @@ public class LegacyOperationalPolicyMapper } final String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY); - if (toscaPolicy.getProperties() == null) { - String errorMessage = "property \"Content\" not defined on TOSCA policy"; + if (content == null) { + String errorMessage = "property \"content\" not defined on TOSCA policy"; LOGGER.warn(errorMessage); throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java index fad227c34..6369f7996 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java @@ -53,9 +53,7 @@ public abstract class JpaToscaConstraint } @Override - public int compareTo(JpaToscaConstraint otherConstraint) { - return 0; - } + public abstract int compareTo(JpaToscaConstraint otherConstraint); /** * Create instances of constraints of various types. diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java index 9841cbe82..632f84add 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java @@ -73,30 +73,33 @@ public class JpaToscaConstraintLogical extends JpaToscaConstraint { public ToscaConstraint toAuthorative() { ToscaConstraint toscaConstraint = new ToscaConstraint(); + if (operation == null) { + return null; + } + switch (operation) { - case EQ: { + case EQ: toscaConstraint.setEqual(compareTo); break; - } - case GT: { + + case GT: toscaConstraint.setGreaterThan(compareTo); break; - } - case GE: { + + case GE: toscaConstraint.setGreaterOrEqual(compareTo); break; - } - case LT: { + + case LT: toscaConstraint.setLessThan(compareTo); break; - } - case LE: { + + case LE: toscaConstraint.setLessOrEqual(compareTo); break; - } - default: { + + default: // Can't happen - } } return toscaConstraint; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java index a322c167f..204ef5b9d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModel.java @@ -24,6 +24,7 @@ import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.OneToOne; @@ -57,7 +58,7 @@ import org.onap.policy.models.base.PfValidationResult; public class JpaToscaModel extends PfModel { private static final long serialVersionUID = 8800599637708309945L; - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) private JpaToscaServiceTemplates serviceTemplates; /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java index 671b5ccac..3e049ea17 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java @@ -38,17 +38,21 @@ import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Lob; import javax.persistence.Table; +import javax.ws.rs.core.Response; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.common.utils.validation.ParameterValidationUtils; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; @@ -69,6 +73,10 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements PfAuthorative<ToscaPolicy> { private static final long serialVersionUID = 3265174757061982805L; + // Tags for metadata + private static final String METADATA_POLICY_ID_TAG = "policy-id"; + private static final String METADATA_POLICY_VERSION_TAG = "policy-version"; + // @formatter:off @Column @AttributeOverrides({ @@ -129,6 +137,8 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P * @param authorativeConcept the authorative concept to copy from */ public JpaToscaPolicy(final ToscaPolicy authorativeConcept) { + super(new PfConceptKey()); + type = new PfConceptKey(); this.fromAuthorative(authorativeConcept); } @@ -142,8 +152,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) { toscaPolicy.setTypeVersion(type.getVersion()); - } - else { + } else { toscaPolicy.setTypeVersion(null); } @@ -151,7 +160,18 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P Map<String, Object> propertyMap = new LinkedHashMap<>(); for (Entry<String, String> entry : properties.entrySet()) { - propertyMap.put(entry.getKey(), entry.getValue()); + try { + // TODO: This is a HACK, we need to validate the properties against their + // TODO: their data type in their policy type definition in TOSCA, which means reading + // TODO: the policy type from the database and parsing the property value object correctly + // TODO: Here we are simply reading a JSON string from the database and deserializing the + // TODO: property value from JSON + propertyMap.put(entry.getKey(), new StandardCoder().decode(entry.getValue(), Object.class)); + } catch (CoderException ce) { + String errorMessage = "error decoding property JSON value read from database: key=" + entry.getKey() + + ", value=" + entry.getValue(); + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } } toscaPolicy.setProperties(propertyMap); @@ -179,9 +199,24 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P // TODO: the policy type from the database and parsing the property value object correctly // TODO: Here we are simply serializing the property value into a string and storing it // TODO: unvalidated into the database - properties.put(propertyEntry.getKey(), propertyEntry.getValue().toString()); + try { + properties.put(propertyEntry.getKey(), new StandardCoder().encode(propertyEntry.getValue())); + } catch (CoderException ce) { + String errorMessage = "error encoding property JSON value for database: key=" + + propertyEntry.getKey() + ", value=" + propertyEntry.getValue(); + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } } } + + // Add the property metadata if it doesn't exist already + if (toscaPolicy.getMetadata() == null) { + setMetadata(new LinkedHashMap<>()); + } + + // Add the policy name and version fields to the metadata + getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName()); + getMetadata().put(METADATA_POLICY_VERSION_TAG, Integer.toString(getKey().getMajorVersion())); } @Override @@ -215,7 +250,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P PfValidationResult result = super.validate(resultIn); if (type == null || type.isNullKey()) { - result.addValidationMessage(new PfValidationMessage(type, this.getClass(), ValidationResult.INVALID, + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, "type is null or a null key")); } else { result = type.validate(result); @@ -238,7 +273,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P * @param result The result of validations up to now * @return the validation result */ - private PfValidationResult validateProperties(@NonNull final PfValidationResult resultIn) { + private PfValidationResult validateProperties(final PfValidationResult resultIn) { PfValidationResult result = resultIn; for (Entry<String, String> propertyEntry : properties.entrySet()) { diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java index fda0c8014..f9e388b04 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java @@ -27,6 +27,7 @@ import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.OneToOne; @@ -63,6 +64,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp implements PfAuthorative<ToscaServiceTemplate> { private static final long serialVersionUID = 8084846046148349401L; + public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0"; public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple"; public static final String DEFAULT_VERSION = "1.0.0"; @@ -70,14 +72,15 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp @SerializedName("tosca_definitions_version") private String toscaDefinitionsVersion; - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @SerializedName("data_types") private JpaToscaDataTypes dataTypes; - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @SerializedName("policy_types") private JpaToscaPolicyTypes policyTypes; + @Column @SerializedName("topology_template") private JpaToscaTopologyTemplate topologyTemplate; @@ -95,7 +98,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp * @param key the key */ public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) { - this(key, ""); + this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON); } /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java index 3476258cf..b1c3f209d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java @@ -26,6 +26,7 @@ import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.OneToOne; @@ -68,7 +69,7 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative @Column(name = "description") private String description; - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) private JpaToscaPolicies policies; /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index a207c4267..ef8ac05ec 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -24,14 +24,11 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import javax.ws.rs.core.Response; - import lombok.NonNull; import org.onap.policy.models.base.PfConcept; 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.dao.PfDao; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; @@ -40,8 +37,6 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; import org.onap.policy.models.tosca.utils.ToscaUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * This class provides the provision of information on TOSCA concepts in the database to callers. @@ -49,8 +44,6 @@ import org.slf4j.LoggerFactory; * @author Liam Fallon (liam.fallon@est.tech) */ public class SimpleToscaProvider { - private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class); - /** * Get policy types. * @@ -69,14 +62,9 @@ public class SimpleToscaProvider { // Add the policy type to the TOSCA service template List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version); - if (jpaPolicyTypeList != null) { - serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); - return serviceTemplate; - } else { - String errorMessage = "policy type not found: " + name + ":" + version; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } + serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); + + return serviceTemplate; } /** @@ -178,14 +166,8 @@ public class SimpleToscaProvider { // Add the policy type to the TOSCA service template List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version); - if (jpaPolicyList != null) { - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); - return serviceTemplate; - } else { - String errorMessage = "policy not found: " + name + ":" + version; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); + return serviceTemplate; } /** diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java index 5c935394b..d7bca2808 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaIdentifierTestBase.java @@ -20,6 +20,9 @@ package org.onap.policy.models.tosca.authorative.concepts; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -29,19 +32,49 @@ import org.onap.policy.common.utils.coder.StandardCoder; * * @param <T> type of key being tested */ -public class ToscaIdentifierTestBase<T> { +public class ToscaIdentifierTestBase<T extends Comparable<T>> { + public static final String NAME = "my-name"; + public static final String VERSION = "1.2.3"; private static final Coder coder = new StandardCoder(); private final Class<T> clazz; + private final String nameField; + private final String versionField; /** * Constructs the object. + * * @param clazz the type of class being tested + * @param nameField name of the field containing the "name" + * @param versionField name of the field containing the "version" */ - public ToscaIdentifierTestBase(Class<T> clazz) { + public ToscaIdentifierTestBase(Class<T> clazz, String nameField, String versionField) { this.clazz = clazz; + this.nameField = nameField; + this.versionField = versionField; + } + + /** + * Tests the compareTo() method. + * + * @throws Exception if an error occurs + */ + public void testCompareTo() throws Exception { + T ident = makeIdent(NAME, VERSION); + assertEquals(0, ident.compareTo(ident)); + + assertTrue(ident.compareTo(null) > 0); + + assertTrue(ident.compareTo(makeIdent(NAME, VERSION)) == 0); + assertTrue(ident.compareTo(makeIdent(NAME, null)) > 0); + assertTrue(ident.compareTo(makeIdent(null, VERSION)) > 0); + assertTrue(ident.compareTo(makeIdent(NAME, VERSION + "a")) < 0); + assertTrue(ident.compareTo(makeIdent(NAME + "a", VERSION)) < 0); + + // name takes precedence over version + assertTrue(makeIdent(NAME, VERSION + "a").compareTo(makeIdent(NAME + "a", VERSION)) < 0); } /** @@ -57,7 +90,9 @@ public class ToscaIdentifierTestBase<T> { bldr.append("{"); if (name != null) { - bldr.append("'name':'"); + bldr.append("'"); + bldr.append(nameField); + bldr.append("':'"); bldr.append(name); bldr.append("'"); } @@ -67,7 +102,9 @@ public class ToscaIdentifierTestBase<T> { bldr.append(','); } - bldr.append("'version':'"); + bldr.append("'"); + bldr.append(versionField); + bldr.append("':'"); bldr.append(version); bldr.append("'"); } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java index 4653296b7..f7c9c7ef0 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -95,6 +96,7 @@ public class ToscaPolicyFilterTest { if (policy.getName() == null) { policy.setName(policyEntry.getKey()); } + if (policy.getVersion() == null) { policy.setVersion(PfKey.NULL_KEY_VERSION); } @@ -186,6 +188,22 @@ public class ToscaPolicyFilterTest { } @Test + public void testFilterVersionPrefix() { + // null pattern + ToscaPolicyFilter filter = ToscaPolicyFilter.builder().versionPrefix(null).build(); + List<ToscaPolicy> filteredList = filter.filter(policyList); + assertEquals(17, filteredList.size()); + + filter = ToscaPolicyFilter.builder().versionPrefix("1.").build(); + filteredList = filter.filter(policyList); + assertEquals(17, filteredList.size()); + + filter = ToscaPolicyFilter.builder().versionPrefix("100.").build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + } + + @Test public void testFilterTypeVersion() { ToscaPolicyFilter filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.Operational").build(); List<ToscaPolicy> filteredList = filter.filter(policyList); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java index 561b4fb21..1d393c1d5 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java @@ -31,17 +31,15 @@ import org.junit.Test; * Test the other constructors, as {@link PojosTest} tests the other methods. */ public class ToscaPolicyIdentifierOptVersionTest extends ToscaIdentifierTestBase<ToscaPolicyIdentifierOptVersion> { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; public ToscaPolicyIdentifierOptVersionTest() { - super(ToscaPolicyIdentifierOptVersion.class); + super(ToscaPolicyIdentifierOptVersion.class, "policy-id", "policy-version"); } @Test public void testAllArgsConstructor_testIsNullVersion() { assertThatThrownBy(() -> new ToscaPolicyIdentifierOptVersion(null, VERSION)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); // with null version ToscaPolicyIdentifierOptVersion orig = new ToscaPolicyIdentifierOptVersion(NAME, null); @@ -57,7 +55,8 @@ public class ToscaPolicyIdentifierOptVersionTest extends ToscaIdentifierTestBase @Test public void testCopyConstructor() throws Exception { - assertThatThrownBy(() -> new ToscaPolicyIdentifierOptVersion(null)).isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new ToscaPolicyIdentifierOptVersion((ToscaPolicyIdentifierOptVersion) null)) + .isInstanceOf(NullPointerException.class); ToscaPolicyIdentifierOptVersion orig = new ToscaPolicyIdentifierOptVersion(); @@ -68,4 +67,29 @@ public class ToscaPolicyIdentifierOptVersionTest extends ToscaIdentifierTestBase orig = makeIdent(NAME, VERSION); assertEquals(orig.toString(), new ToscaPolicyIdentifierOptVersion(orig).toString()); } + + @Test + public void testCopyToscaPolicyIdentifierConstructor() throws Exception { + assertThatThrownBy(() -> new ToscaPolicyIdentifierOptVersion((ToscaPolicyIdentifier) null)) + .isInstanceOf(NullPointerException.class); + + ToscaPolicyIdentifier orig = new ToscaPolicyIdentifier(); + + // verify with null values + ToscaPolicyIdentifierOptVersion newIdent = new ToscaPolicyIdentifierOptVersion(orig); + assertEquals(null, newIdent.getName()); + assertEquals(null, newIdent.getVersion()); + + // verify with all values + orig.setName(NAME); + orig.setVersion(VERSION); + newIdent = new ToscaPolicyIdentifierOptVersion(orig); + assertEquals(NAME, newIdent.getName()); + assertEquals(VERSION, newIdent.getVersion()); + } + + @Test + public void testCompareTo() throws Exception { + super.testCompareTo(); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java index a53af7b1f..f31abf837 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java @@ -22,18 +22,21 @@ package org.onap.policy.models.tosca.authorative.concepts; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; /** - * Test the other constructors, as {@link PojosTest} tests the other methods. + * Test methods not tested by {@link PojosTest}. */ public class ToscaPolicyIdentifierTest extends ToscaIdentifierTestBase<ToscaPolicyIdentifier> { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; public ToscaPolicyIdentifierTest() { - super(ToscaPolicyIdentifier.class); + super(ToscaPolicyIdentifier.class, "name", "version"); } @Test @@ -59,4 +62,30 @@ public class ToscaPolicyIdentifierTest extends ToscaIdentifierTestBase<ToscaPoli orig = new ToscaPolicyIdentifier(NAME, VERSION); assertEquals(orig.toString(), new ToscaPolicyIdentifier(orig).toString()); } + + @Test + public void testValidatePapRest() throws Exception { + ToscaPolicyIdentifier ident = new ToscaPolicyIdentifier(NAME, VERSION); + ValidationResult result = ident.validatePapRest(); + assertNotNull(result); + assertTrue(result.isValid()); + assertNull(result.getResult()); + + ident = makeIdent(NAME, null); + result = ident.validatePapRest(); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + + ident = makeIdent(null, VERSION); + result = ident.validatePapRest(); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + } + + @Test + public void testCompareTo() throws Exception { + super.testCompareTo(); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java index 8388f1061..e440dd6da 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java @@ -22,18 +22,21 @@ package org.onap.policy.models.tosca.authorative.concepts; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; /** - * Test the other constructors, as {@link PojosTest} tests the other methods. + * Test methods not tested by {@link PojosTest}. */ public class ToscaPolicyTypeIdentifierTest extends ToscaIdentifierTestBase<ToscaPolicyTypeIdentifier> { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; public ToscaPolicyTypeIdentifierTest() { - super(ToscaPolicyTypeIdentifier.class); + super(ToscaPolicyTypeIdentifier.class, "name", "version"); } @Test @@ -60,4 +63,29 @@ public class ToscaPolicyTypeIdentifierTest extends ToscaIdentifierTestBase<Tosca assertEquals(orig.toString(), new ToscaPolicyTypeIdentifier(orig).toString()); } + @Test + public void testValidatePapRest() throws Exception { + ToscaPolicyTypeIdentifier ident = new ToscaPolicyTypeIdentifier(NAME, VERSION); + ValidationResult result = ident.validatePapRest(); + assertNotNull(result); + assertTrue(result.isValid()); + assertNull(result.getResult()); + + ident = makeIdent(NAME, null); + result = ident.validatePapRest(); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + + ident = makeIdent(null, VERSION); + result = ident.validatePapRest(); + assertNotNull(result); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + } + + @Test + public void testCompareTo() throws Exception { + super.testCompareTo(); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java new file mode 100644 index 000000000..5ad314ae6 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java @@ -0,0 +1,399 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.provider; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +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.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +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.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; + +/** + * Test of the {@link AuthorativeToscaProvider} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class AuthorativeToscaProviderPolicyTest { + private PfDao pfDao; + private StandardCoder standardCoder; + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); + + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); + + daoParameters.setJdbcProperties(jdbcProperties ); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + /** + * Set up GSON. + */ + @Before + public void setupGson() { + standardCoder = new StandardCoder(); + } + + @After + public void teardown() throws Exception { + pfDao.close(); + } + + @Test + public void testPoliciesGet() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getPolicies(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getPolicyList(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + + ToscaPolicy gotPolicy = + gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + List<ToscaPolicy> gotPolicyList = + new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", null); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, null); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "Nonexistant", "1.0.0"); + assertEquals(0, gotPolicyList.size()); + } + + @Test + public void testPoliciesGetFiltered() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicies(pfDao, null); + }).hasMessage("filter is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null); + }).hasMessage("filter is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getFilteredPolicies(pfDao, ToscaPolicyFilter.builder().build()); + + ToscaPolicy gotPolicy = + gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).build()); + + gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); + + gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); + assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + + List<ToscaPolicy> gotPolicyList = + new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = + new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, ToscaPolicyFilter.builder().build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + + gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, + ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); + assertEquals(1, gotPolicyList.size()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + } + + @Test + public void testPolicyCreate() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, new ToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + } + + + @Test + public void testPolicyUpdate() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(null, new ToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate updatedServiceTemplate = + new AuthorativeToscaProvider().updatePolicies(pfDao, toscaServiceTemplate); + + ToscaPolicy updatedPolicy = + updatedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, updatedPolicy)); + assertTrue(beforePolicy.getType().equals(updatedPolicy.getType())); + } + + @Test + public void testPoliciesDelete() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, null, "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, "name", null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(null, "name", "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, null, null); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, null, "version"); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); + }).hasMessage("version is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + + PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + + ToscaPolicy beforePolicy = + toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + ToscaPolicy createdPolicy = + createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + + ToscaServiceTemplate deletedServiceTemplate = + new AuthorativeToscaProvider().deletePolicy(pfDao, policyKey.getName(), policyKey.getVersion()); + + ToscaPolicy deletedPolicy = + deletedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); + assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); + assertTrue(beforePolicy.getType().equals(deletedPolicy.getType())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + + assertEquals(0, gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).size()); + } + + @Test + public void testAssertPoliciesExist() throws PfModelException { + ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate(); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); + }).hasMessage("version is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("topology template not specified on service template"); + + testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("no policies specified on topology template of service template"); + + testServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + }).hasMessage("An incoming list of concepts must have at least one entry"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java new file mode 100644 index 000000000..6a925bcf3 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java @@ -0,0 +1,408 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.provider; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import com.google.gson.GsonBuilder; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.lang3.ObjectUtils; +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +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.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +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.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; +import org.yaml.snakeyaml.Yaml; + +/** + * Test of the {@link AuthorativeToscaProvider} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class AuthorativeToscaProviderPolicyTypeTest { + private static String yamlAsJsonString; + private PfDao pfDao; + private StandardCoder standardCoder; + + + /** + * Read the policy type definition. + * + * @throws Exception on errors + */ + @BeforeClass + public static void readPolicyDefinition() { + String yamlString = + ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.yaml"); + + Object yamlObject = new Yaml().load(yamlString); + yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + } + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); + + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); + + daoParameters.setJdbcProperties(jdbcProperties ); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + /** + * Set up GSON. + */ + @Before + public void setupGson() { + standardCoder = new StandardCoder(); + } + + @After + public void teardown() throws Exception { + pfDao.close(); + } + + @Test + public void testPolicyTypesGet() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getPolicyTypes(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getPolicyList(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); + + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); + + ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao, + policyTypeKey.getName(), policyTypeKey.getVersion()); + + ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); + + List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, + "onap.policies.optimization.AffinityPolicy", "0.0.0"); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, + "onap.policies.optimization.AffinityPolicy", null); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null); + assertEquals(2, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, "0.0.0"); + assertEquals(2, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + } + + + @Test + public void testPolicyTypesGetFiltered() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null); + }).hasMessage("filter is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null); + }).hasMessage("filter is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); + + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); + + ToscaServiceTemplate gotServiceTemplate = + new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build()); + + ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, + ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build()); + + gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, + ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build()); + + gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); + + List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, + "onap.policies.optimization.AffinityPolicy", "0.0.0"); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaPolicyTypeFilter.builder().build()); + assertEquals(2, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build()); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build()); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaPolicyTypeFilter.builder().version("1.0.0").build()); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + } + + @Test + public void testPolicyTypesCreate() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate(); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate); + }).hasMessage("no policy types specified on service template"); + + testToscaServiceTemplate.setPolicyTypes(new ArrayList<>()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate); + }).hasMessage("An incoming list of concepts must have at least one entry"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); + + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); + } + + @Test + public void testPolicyTypesUpdate() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicyTypes(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); + + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); + + ToscaServiceTemplate updatedServiceTemplate = + new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate); + + ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription())); + } + + @Test + public void testPolicyTypesDelete() throws Exception { + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicyType(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicyType(null, null, "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicyType(null, "name", null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicyType(null, "name", "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version"); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null); + }).hasMessage("version is marked @NonNull but is null"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); + + assertNotNull(toscaServiceTemplate); + ToscaServiceTemplate createdServiceTemplate = + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); + + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); + + ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao, + policyTypeKey.getName(), policyTypeKey.getVersion()); + + ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription())); + + ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao, + policyTypeKey.getName(), policyTypeKey.getVersion()); + + assertEquals(0, gotServiceTemplate.getPolicyTypes().get(0).size()); + } + + @Test + public void testAssertPoliciesExist() throws PfModelException { + ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate(); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null); + }).hasMessage("version is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); + }).hasMessage("no policy types specified on service template"); + + testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); + }).hasMessage("no policy types specified on service template"); + + testServiceTemplate.setPolicyTypes(new ArrayList<>()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); + }).hasMessage("An incoming list of concepts must have at least one entry"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/ToscaServiceTemplateMappingTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java index 1bac0b973..a4458a874 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/ToscaServiceTemplateMappingTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/ToscaServiceTemplateMappingTest.java @@ -21,7 +21,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.tosca.authorative.mapping; +package org.onap.policy.models.tosca.authorative.provider; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java index 5720b5594..9c2344080 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java @@ -20,6 +20,7 @@ package org.onap.policy.models.tosca.legacy.concepts; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -28,6 +29,7 @@ import java.util.Map; import org.junit.Test; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.testconcepts.DummyBadLegacyGuardPolicyContent; public class LegacyGuardPolicyTest { @@ -45,6 +47,10 @@ public class LegacyGuardPolicyTest { content.setActor("SO"); guard.setContent(content); assertEquals("SO", guard.getContent().getActor()); - } + DummyBadLegacyGuardPolicyContent dblgpc = new DummyBadLegacyGuardPolicyContent(); + assertThatThrownBy(() -> { + dblgpc.getAsPropertyMap(); + }).hasMessage("could not convert content to a property map"); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java index f35a4e668..26077189e 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java @@ -44,9 +44,19 @@ public class TestPojos { @Test public void testPojos() { - final Validator validator = ValidatorBuilder.create().with(new ToStringTester()) - .with(new SetterMustExistRule()).with(new GetterMustExistRule()).with(new SetterTester()) - .with(new GetterTester()).build(); - validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + // @formatter:off + final Validator validator = ValidatorBuilder + .create() + .with(new ToStringTester()) + .with(new SetterMustExistRule()) + .with(new GetterMustExistRule()) + .with(new SetterTester()) + .with(new GetterTester()) + .build(); + validator.validate(POJO_PACKAGE, + new FilterPackageInfo() + ); + + // @formatter:on } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java new file mode 100644 index 000000000..1b149416c --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.concepts.testconcepts; + +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent; + +/** + * Dummy {@link LegacyGuardPolicyContent} class to force exception for testing. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyBadLegacyGuardPolicyContent extends LegacyGuardPolicyContent { + public static final String TO_TRIGGER_EXCEPTION = "Dummy"; + @SuppressWarnings("unused") + private final transient String aaa = "aaa"; +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java new file mode 100644 index 000000000..f2fb53d60 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.mapping; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.LinkedHashMap; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +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.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; + +/** + * Test the {@link LegacyGuardPolicyMapper} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyGuardPolicyMapperTest { + + @Test + public void testLegacyGuardPolicyMapper() { + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + + JpaToscaPolicy policy = new JpaToscaPolicy(new PfConceptKey("PolicyName", "0.0.1")); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy.getKey(), policy); + + assertThatThrownBy(() -> { + new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessageContaining("no metadata defined on TOSCA policy"); + + policy.setMetadata(new LinkedHashMap<>()); + assertThatThrownBy(() -> { + new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessageContaining("no properties defined on TOSCA policy"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java index 26242611b..4df62aff0 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java @@ -18,20 +18,27 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.tosca.legacy.serialization; +package org.onap.policy.models.tosca.legacy.mapping; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.util.LinkedHashMap; + import org.junit.Before; import org.junit.Test; 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.PfValidationResult; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper; +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.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,9 +47,9 @@ import org.slf4j.LoggerFactory; * * @author Liam Fallon (liam.fallon@est.tech) */ -public class LegacyOperationalPolicySerializationTest { +public class LegacyOperationalPolicyMapperTest { // Logger for this class - private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicySerializationTest.class); + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapperTest.class); private StandardCoder standardCoder; @@ -68,4 +75,31 @@ public class LegacyOperationalPolicySerializationTest { assertEquals("operational.restart:1.0.0", serviceTemplate.getTopologyTemplate().getPolicies().get("operational.restart").getId()); } + + @Test + public void testOperationalPolicyMapper() throws Exception { + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + + JpaToscaPolicy policy0 = new JpaToscaPolicy(new PfConceptKey("PolicyName0", "0.0.1")); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy0.getKey(), policy0); + JpaToscaPolicy policy1 = new JpaToscaPolicy(new PfConceptKey("PolicyName1", "0.0.1")); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy1.getKey(), policy1); + + assertThatThrownBy(() -> { + new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessage("more than one policy found in service template"); + + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().remove(policy1.getKey()); + + assertThatThrownBy(() -> { + new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessage("no properties defined on TOSCA policy"); + + policy0.setProperties(new LinkedHashMap<>()); + assertThatThrownBy(() -> { + new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessage("property \"content\" not defined on TOSCA policy"); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java index 2fdc3aae7..71254ec6f 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java @@ -24,10 +24,10 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.sql.Connection; -import java.sql.DriverManager; import java.util.Map; +import java.util.Properties; +import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -47,7 +47,6 @@ import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; * @author Liam Fallon (liam.fallon@est.tech) */ public class LegacyProvider4LegacyGuardTest { - private Connection connection; private PfDao pfDao; private StandardCoder standardCoder; @@ -59,17 +58,21 @@ public class LegacyProvider4LegacyGuardTest { */ @Before public void setupDao() throws Exception { - // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database - // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance - connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); - final DaoParameters daoParameters = new DaoParameters(); daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); - // Use the persistence unit ToscaConceptTest to test towards the h2 database - // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance daoParameters.setPersistenceUnit("ToscaConceptTest"); + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); + + daoParameters.setJdbcProperties(jdbcProperties); + pfDao = new PfDaoFactory().createPfDao(daoParameters); pfDao.init(daoParameters); } @@ -85,7 +88,6 @@ public class LegacyProvider4LegacyGuardTest { @After public void teardown() throws Exception { pfDao.close(); - connection.close(); } @Test diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java index d198bd4f5..c018aae7b 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java @@ -24,9 +24,9 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.sql.Connection; -import java.sql.DriverManager; +import java.util.Properties; +import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -44,7 +44,6 @@ import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; * @author Liam Fallon (liam.fallon@est.tech) */ public class LegacyProvider4LegacyOperationalTest { - private Connection connection; private PfDao pfDao; private StandardCoder standardCoder; @@ -55,17 +54,21 @@ public class LegacyProvider4LegacyOperationalTest { */ @Before public void setupDao() throws Exception { - // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database - // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance - connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); - final DaoParameters daoParameters = new DaoParameters(); daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); - // Use the persistence unit ToscaConceptTest to test towards the h2 database - // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance daoParameters.setPersistenceUnit("ToscaConceptTest"); + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); + + daoParameters.setJdbcProperties(jdbcProperties ); + pfDao = new PfDaoFactory().createPfDao(daoParameters); pfDao.init(daoParameters); } @@ -81,7 +84,6 @@ public class LegacyProvider4LegacyOperationalTest { @After public void teardown() throws Exception { pfDao.close(); - connection.close(); } @Test diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalTest.java index de0c813e2..d3239da73 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalTest.java @@ -21,44 +21,55 @@ package org.onap.policy.models.tosca.simple.concepts; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; import org.junit.Test; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; /** - * DAO test for ToscaConstraintLogicalString. + * Test the {@link JpaToscaConstraintLogical} class. * * @author Liam Fallon (liam.fallon@est.tech) */ public class JpaToscaConstraintLogicalTest { @Test - public void testConstraintLogicalStringPojo() { - assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")); + public void testLogicalConstraint() { + ToscaConstraint c0 = new ToscaConstraint(); + c0.setEqual("Hello"); + JpaToscaConstraintLogical jc0 = new JpaToscaConstraintLogical(c0); + assertEquals(c0, jc0.toAuthorative()); + + ToscaConstraint c1 = new ToscaConstraint(); + c1.setGreaterOrEqual("Hello"); + JpaToscaConstraintLogical jc1 = new JpaToscaConstraintLogical(c1); + assertEquals(c1, jc1.toAuthorative()); + + ToscaConstraint c2 = new ToscaConstraint(); + c2.setGreaterThan("Hello"); + JpaToscaConstraintLogical jc2 = new JpaToscaConstraintLogical(c2); + assertEquals(c2, jc2.toAuthorative()); - try { - new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("operation is marked @NonNull but is null", exc.getMessage()); - } + ToscaConstraint c3 = new ToscaConstraint(); + c3.setLessOrEqual("Hello"); + JpaToscaConstraintLogical jc3 = new JpaToscaConstraintLogical(c3); + assertEquals(c3, jc3.toAuthorative()); - try { - new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, "Hello"); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("operation is marked @NonNull but is null", exc.getMessage()); - } + ToscaConstraint c4 = new ToscaConstraint(); + c4.setLessThan("Hello"); + JpaToscaConstraintLogical jc4 = new JpaToscaConstraintLogical(c4); + assertEquals(c4, jc4.toAuthorative()); - try { - new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("compareTo is marked @NonNull but is null", exc.getMessage()); - } + ToscaConstraint c5 = new ToscaConstraint(); + JpaToscaConstraintLogical jc5 = new JpaToscaConstraintLogical(c5); + assertNull(jc5.toAuthorative()); - assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")); + assertEquals(-1, jc0.compareTo(null)); + assertEquals(0, jc0.compareTo(jc0)); + assertNotEquals(0, jc0.compareTo(new JpaToscaConstraintValidValues(new ArrayList<>()))); + assertEquals(-2, jc0.compareTo(jc1)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintTest.java new file mode 100644 index 000000000..ff4187a47 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintTest.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.simple.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical; + +/** + * DAO test for ToscaConstraintLogicalString. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class JpaToscaConstraintTest { + + @Test + public void testConstraintLogicalStringPojo() { + assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")); + + try { + new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("operation is marked @NonNull but is null", exc.getMessage()); + } + + try { + new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, "Hello"); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("operation is marked @NonNull but is null", exc.getMessage()); + } + + try { + new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("compareTo is marked @NonNull but is null", exc.getMessage()); + } + + assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")); + + assertEquals(0, new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "") + .compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, ""))); + + JpaToscaConstraintOperation op = JpaToscaConstraintOperation.EQ; + assertEquals("equal_to", op.getToscaToken()); + + List<String> validValues = new ArrayList<>(); + validValues.add("hello"); + validValues.add("goodbye"); + JpaToscaConstraintValidValues cvv0 = new JpaToscaConstraintValidValues(validValues); + assertEquals(-1, cvv0.compareTo(null)); + assertEquals(0, cvv0.compareTo(cvv0)); + assertNotEquals(0, cvv0.compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint"))); + JpaToscaConstraintValidValues cvv1 = new JpaToscaConstraintValidValues(validValues); + assertEquals(0, cvv0.compareTo(cvv1)); + + cvv1.fromAuthorative(new ToscaConstraint()); + assertNotNull(cvv1.getValidValues()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java index efdddccb0..66cde51fc 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java @@ -20,11 +20,11 @@ package org.onap.policy.models.tosca.simple.concepts; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -35,6 +35,8 @@ import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; @@ -51,20 +53,15 @@ public class JpaToscaDataTypeTest { assertNotNull(new JpaToscaDataType()); assertNotNull(new JpaToscaDataType(new PfConceptKey())); assertNotNull(new JpaToscaDataType(new JpaToscaDataType())); + assertNotNull(new JpaToscaDataType(new ToscaDataType())); - try { + assertThatThrownBy(() -> { new JpaToscaDataType((PfConceptKey) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaDataType((JpaToscaDataType) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("copyConcept is marked @NonNull but is null"); PfConceptKey dtKey = new PfConceptKey("tdt", "0.0.1"); JpaToscaDataType tdt = new JpaToscaDataType(dtKey); @@ -105,12 +102,9 @@ public class JpaToscaDataTypeTest { otherDt.setProperties(properties); assertEquals(0, tdt.compareTo(otherDt)); - try { + assertThatThrownBy(() -> { tdt.copyTo(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("target is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("target is marked @NonNull but is null"); assertEquals(3, tdt.getKeys().size()); assertEquals(1, new JpaToscaDataType().getKeys().size()); @@ -132,12 +126,23 @@ public class JpaToscaDataTypeTest { tdt.getProperties().remove(null); assertTrue(tdt.validate(new PfValidationResult()).isValid()); - try { + assertThatThrownBy(() -> { tdt.validate(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); - } - + }).hasMessage("resultIn is marked @NonNull but is null"); + + ToscaDataType dat = new ToscaDataType(); + dat.setName("name"); + dat.setVersion("1.2.3"); + dat.setConstraints(new ArrayList<>()); + ToscaConstraint constraint = new ToscaConstraint(); + constraint.setEqual("EqualTo"); + dat.getConstraints().add(constraint); + + JpaToscaDataType tdta = new JpaToscaDataType(); + tdta.fromAuthorative(dat); + assertEquals("name", tdta.getKey().getName()); + + ToscaDataType datOut = tdta.toAuthorative(); + assertNotNull(datOut); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java index ce90e372c..c732fa604 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java @@ -24,10 +24,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.TreeMap; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; @@ -74,5 +79,10 @@ public class JpaToscaDataTypesTest { } catch (Exception exc) { assertEquals("key is marked @NonNull but is null", exc.getMessage()); } + + List<Map<String, ToscaDataType>> dtMapList = new ArrayList<>(); + dtMapList.add(new LinkedHashMap<>()); + dtMapList.get(0).put("policyType", new ToscaDataType()); + assertNotNull(new JpaToscaDataTypes(dtMapList)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPoliciesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPoliciesTest.java index 6f6141c5b..db3635ecb 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPoliciesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPoliciesTest.java @@ -24,10 +24,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.TreeMap; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; @@ -74,5 +79,10 @@ public class JpaToscaPoliciesTest { } catch (Exception exc) { assertEquals("key is marked @NonNull but is null", exc.getMessage()); } + + List<Map<String, ToscaPolicy>> polMapList = new ArrayList<>(); + polMapList.add(new LinkedHashMap<>()); + polMapList.get(0).put("policyType", new ToscaPolicy()); + assertNotNull(new JpaToscaPolicies(polMapList)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java index ec8e6da8f..ae38ab916 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java @@ -20,11 +20,11 @@ package org.onap.policy.models.tosca.simple.concepts; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.HashMap; @@ -33,7 +33,9 @@ import java.util.Map; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; /** @@ -50,47 +52,36 @@ public class JpaToscaPolicyTest { assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey())); assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy())); - try { + ToscaPolicy pol = new ToscaPolicy(); + pol.setType("type"); + assertNotNull(new JpaToscaPolicy(pol)); + + assertThatThrownBy(() -> { new JpaToscaPolicy((PfConceptKey) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaPolicy(null, null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaPolicy(new PfConceptKey(), null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("type is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("type is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaPolicy(null, new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaPolicy((JpaToscaPolicy) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("copyConcept is marked @NonNull but is null"); PfConceptKey tpKey = new PfConceptKey("tdt", "0.0.1"); PfConceptKey ptKey = new PfConceptKey("policyType", "0.0.1"); JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey); Map<String, String> propertyMap = new HashMap<>(); - propertyMap.put("Property", "Property Value"); + propertyMap.put("Property", "\"Property Value\""); tp.setProperties(propertyMap); assertEquals(propertyMap, tp.getProperties()); @@ -126,12 +117,9 @@ public class JpaToscaPolicyTest { otherDt.setTargets(targets); assertEquals(0, tp.compareTo(otherDt)); - try { + assertThatThrownBy(() -> { tp.copyTo(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("target is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("target is marked @NonNull but is null"); assertEquals(3, tp.getKeys().size()); assertEquals(2, new JpaToscaPolicy().getKeys().size()); @@ -164,11 +152,35 @@ public class JpaToscaPolicyTest { tp.getTargets().remove(null); assertTrue(tp.validate(new PfValidationResult()).isValid()); - try { + PfConceptKey tpTypeKey = tp.getKey(); + assertNotNull(tpTypeKey); + tp.setType(null); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.setType(PfConceptKey.getNullKey()); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.setType(tpTypeKey); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + assertThatThrownBy(() -> { tp.validate(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("resultIn is marked @NonNull but is null"); + + assertNotNull(tp.toAuthorative()); + tp.getType().setVersion(PfKey.NULL_KEY_VERSION); + assertNotNull(tp.toAuthorative()); + tp.setProperties(null); + assertNotNull(tp.toAuthorative()); + + assertThatThrownBy(() -> { + tp.fromAuthorative(null); + }).hasMessage("toscaPolicy is marked @NonNull but is null"); + + pol = new ToscaPolicy(); + pol.setName("policy"); + pol.setVersion("1.2.3"); + pol.setType("poltype"); + pol.setTypeVersion("2.2.3"); + tp.fromAuthorative(pol); + assertEquals("2.2.3", tp.getType().getVersion()); } -}
\ No newline at end of file +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java index 870640e75..3cdcd9552 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java @@ -37,6 +37,7 @@ import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; @@ -209,5 +210,9 @@ public class JpaToscaPolicyTypeTest { assertEquals(-1, tet.compareTo(null)); assertEquals(0, tet.compareTo(tet)); assertFalse(tet.compareTo(tet.getKey()) == 0); + + assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType())); + + assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType())); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypesTest.java index d0ea1782f..e02df235f 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypesTest.java @@ -24,10 +24,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.TreeMap; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; @@ -74,5 +79,10 @@ public class JpaToscaPolicyTypesTest { } catch (Exception exc) { assertEquals("key is marked @NonNull but is null", exc.getMessage()); } + + List<Map<String, ToscaPolicyType>> ptMapList = new ArrayList<>(); + ptMapList.add(new LinkedHashMap<>()); + ptMapList.get(0).put("policyType", new ToscaPolicyType()); + assertNotNull(new JpaToscaPolicyTypes(ptMapList)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java index b8766601b..706011bcf 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java @@ -191,6 +191,8 @@ public class JpaToscaPropertyTest { tp.setDefaultValue(null); assertTrue(tp.validate(new PfValidationResult()).isValid()); + tp.setDefaultValue(""); + assertFalse(tp.validate(new PfValidationResult()).isValid()); tp.setDefaultValue("defaultKey"); assertTrue(tp.validate(new PfValidationResult()).isValid()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java index 4569d42ff..a2a418ef9 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java @@ -157,7 +157,7 @@ public class JpaToscaServiceTemplateTest { tst.clean(); assertEquals(tttClone0, tst); - assertFalse(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid()); + assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid()); assertTrue(tst.validate(new PfValidationResult()).isValid()); tst.setDescription(null); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplatesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplatesTest.java index 91d6d0bd3..354fe8b78 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplatesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplatesTest.java @@ -20,14 +20,18 @@ package org.onap.policy.models.tosca.simple.concepts; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.TreeMap; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplates; @@ -41,39 +45,29 @@ public class JpaToscaServiceTemplatesTest { new JpaToscaServiceTemplates(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaServiceTemplate>())); assertNotNull(new JpaToscaServiceTemplates(new JpaToscaServiceTemplates())); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates((PfConceptKey) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates((JpaToscaServiceTemplates) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("copyConcept is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates(null, null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates(new PfConceptKey(), null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("conceptMap is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates(null, new TreeMap<PfConceptKey, JpaToscaServiceTemplate>()); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); + + List<Map<String, ToscaServiceTemplate>> tsMapList = new ArrayList<>(); + tsMapList.add(new LinkedHashMap<>()); + tsMapList.get(0).put("serviceTemplate", new ToscaServiceTemplate()); + assertNotNull(new JpaToscaServiceTemplates(tsMapList)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java index 417f202f5..61ce3d077 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java @@ -33,6 +33,7 @@ import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; 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.JpaToscaTopologyTemplate; @@ -49,6 +50,7 @@ public class JpaToscaTopologyTemplateTest { assertNotNull(new JpaToscaTopologyTemplate()); assertNotNull(new JpaToscaTopologyTemplate(new PfReferenceKey())); assertNotNull(new JpaToscaTopologyTemplate(new JpaToscaTopologyTemplate())); + assertNotNull(new JpaToscaTopologyTemplate(new ToscaTopologyTemplate())); try { new JpaToscaTopologyTemplate((PfReferenceKey) null); @@ -121,6 +123,11 @@ public class JpaToscaTopologyTemplateTest { assertTrue(new JpaToscaTopologyTemplate().validate(new PfValidationResult()).isValid()); assertTrue(ttt.validate(new PfValidationResult()).isValid()); + ttt.setKey(PfReferenceKey.getNullKey()); + assertFalse(ttt.validate(new PfValidationResult()).isValid()); + ttt.setKey(tttKey); + assertTrue(ttt.validate(new PfValidationResult()).isValid()); + ttt.setDescription(null); assertTrue(ttt.validate(new PfValidationResult()).isValid()); ttt.setDescription(""); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java index e5be49860..edb6e2e58 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java @@ -40,7 +40,6 @@ import org.onap.policy.common.utils.validation.ToStringTester; * */ public class TestPojos { - private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.simple.concepts"; @Test diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/testconcepts/DummyToscaConstraint.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/testconcepts/DummyToscaConstraint.java index b13cb4b0b..f8e422160 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/testconcepts/DummyToscaConstraint.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/testconcepts/DummyToscaConstraint.java @@ -45,4 +45,9 @@ public class DummyToscaConstraint extends JpaToscaConstraint { @Override public void fromAuthorative(ToscaConstraint authorativeConcept) { } + + @Override + public int compareTo(JpaToscaConstraint otherConstraint) { + return 0; + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java index dca34b08e..1f582cf84 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java @@ -20,14 +20,15 @@ package org.onap.policy.models.tosca.simple.provider; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.sql.Connection; -import java.sql.DriverManager; +import java.util.Properties; +import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -50,11 +51,9 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; * @author Liam Fallon (liam.fallon@est.tech) */ public class SimpleToscaProviderTest { - private Connection connection; private PfDao pfDao; private StandardCoder standardCoder; - /** * Set up the DAO towards the database. * @@ -62,17 +61,21 @@ public class SimpleToscaProviderTest { */ @Before public void setupDao() throws Exception { - // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database - // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance - connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); - final DaoParameters daoParameters = new DaoParameters(); daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); - // Use the persistence unit ToscaConceptTest to test towards the h2 database - // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance daoParameters.setPersistenceUnit("ToscaConceptTest"); + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); + + daoParameters.setJdbcProperties(jdbcProperties); + pfDao = new PfDaoFactory().createPfDao(daoParameters); pfDao.init(daoParameters); } @@ -88,18 +91,10 @@ public class SimpleToscaProviderTest { @After public void teardown() throws Exception { pfDao.close(); - connection.close(); } @Test public void testPoliciesGet() throws Exception { - try { - new SimpleToscaProvider().getPolicies(null, null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -125,27 +120,6 @@ public class SimpleToscaProviderTest { @Test public void testPolicyCreate() throws Exception { - try { - new SimpleToscaProvider().createPolicies(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().createPolicies(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -162,27 +136,6 @@ public class SimpleToscaProviderTest { @Test public void testPolicyUpdate() throws Exception { - try { - new SimpleToscaProvider().updatePolicies(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().updatePolicies(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -199,27 +152,6 @@ public class SimpleToscaProviderTest { @Test public void testPoliciesDelete() throws Exception { - try { - new SimpleToscaProvider().deletePolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().deletePolicy(null, new PfConceptKey()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().deletePolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -272,6 +204,88 @@ public class SimpleToscaProviderTest { assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); } + } + + @Test + public void testNonNulls() { + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicyTypes(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicyTypes(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicyTypes(null, new JpaToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicyTypes(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicyTypes(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicyTypes(null, new JpaToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicyTypes(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicyType(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicyType(null, new PfConceptKey()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicyType(pfDao, null); + }).hasMessage("policyTypeKey is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicies(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicy(null, new PfConceptKey()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicy(pfDao, null); + }).hasMessage("policyKey is marked @NonNull but is null"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java index 5f0cbb355..9d9ee608d 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java @@ -171,7 +171,7 @@ public class MonitoringPolicySerializationTest { JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next(); // Check metadata - assertTrue(policyVal.getMetadata().size() == 1); + assertTrue(policyVal.getMetadata().size() == 2); assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey()); assertEquals("onap.restart.tca", policyVal.getMetadata().entrySet().iterator().next().getValue()); @@ -204,7 +204,7 @@ public class MonitoringPolicySerializationTest { JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next(); // Check metadata - assertTrue(policyVal.getMetadata().size() == 1); + assertTrue(policyVal.getMetadata().size() == 2); assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey()); assertEquals("onap.scaleout.tca", policyVal.getMetadata().entrySet().iterator().next().getValue()); @@ -237,7 +237,7 @@ public class MonitoringPolicySerializationTest { JpaToscaPolicy policyVal = policiesConceptMap.values().iterator().next(); // Check metadata - assertTrue(policyVal.getMetadata().size() == 1); + assertTrue(policyVal.getMetadata().size() == 2); assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey()); assertEquals("onap.vfirewall.tca", policyVal.getMetadata().entrySet().iterator().next().getValue()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java new file mode 100644 index 000000000..40a2fd29a --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.utils; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; + +/** + * Import the {@link ToscaUtils} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaUtilsTest { + + @Test + public void test() { + JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(); + jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + + assertThatThrownBy(() -> { + ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate); + }).hasMessage("list of policy types specified on service template is empty"); + } +} diff --git a/models-tosca/src/test/resources/META-INF/persistence.xml b/models-tosca/src/test/resources/META-INF/persistence.xml index 23e8567f1..936e5a11c 100644 --- a/models-tosca/src/test/resources/META-INF/persistence.xml +++ b/models-tosca/src/test/resources/META-INF/persistence.xml @@ -26,34 +26,13 @@ <class>org.onap.policy.models.dao.converters.CDataConditioner</class> <class>org.onap.policy.models.dao.converters.Uuid2String</class> <class>org.onap.policy.models.base.PfConceptKey</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class> <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class> <properties> - <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> - <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb" /> - <property name="javax.persistence.jdbc.user" value="policy" /> - <property name="javax.persistence.jdbc.password" value="P01icY" /> <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> <property name="eclipselink.logging.level" value="INFO" /> - </properties> - </persistence-unit> - - <persistence-unit name="ToscaConceptMariaDBTest" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - - <class>org.onap.policy.models.dao.converters.CDataConditioner</class> - <class>org.onap.policy.models.dao.converters.Uuid2String</class> - <class>org.onap.policy.models.base.PfConceptKey</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class> - - <properties> - <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" /> - <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/policy" /> - <property name="javax.persistence.jdbc.user" value="policy" /> - <property name="javax.persistence.jdbc.password" value="P01icY" /> - <property name="javax.persistence.schema-generation.database.action" value="create" /> <!-- property name="eclipselink.logging.level" value="ALL" /> <property name="eclipselink.logging.level.jpa" value="ALL" /> @@ -65,10 +44,6 @@ <property name="eclipselink.logging.level.server" value="ALL" /> <property name="eclipselink.logging.level.query" value="ALL" /> <property name="eclipselink.logging.level.properties" value="ALL" /--> - - <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> - <property name="eclipselink.ddl-generation.output-mode" value="database" /> - <property name="eclipselink.logging.level" value="INFO" /> </properties> </persistence-unit> </persistence> |