From b7097d21c25a48c9d209548ac8afdc09b1679457 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 10 Feb 2021 16:17:10 -0500 Subject: More sonars in models Addressed the following: - make constructors protected - check exception type - too many assertions - remove annotation lists - use "<>" - reduce cognitive complexity - extract constant - multiple method calls in one assert - don't use eq() in verify() - indentation Issue-ID: POLICY-2905 Change-Id: I25bb3951f781250e9cdfe8f5f3b80cb63e129184 Signed-off-by: Jim Hahn --- .../authorative/concepts/ToscaEntityFilter.java | 3 +- .../concepts/ToscaTypedEntityFilter.java | 2 +- .../tosca/simple/concepts/JpaToscaConstraint.java | 4 +- .../tosca/simple/concepts/JpaToscaEntityType.java | 9 +- .../simple/concepts/JpaToscaNodeTemplate.java | 9 +- .../tosca/simple/concepts/JpaToscaNodeType.java | 5 +- .../tosca/simple/concepts/JpaToscaPolicy.java | 9 +- .../simple/concepts/JpaToscaServiceTemplate.java | 106 +++++++-------------- .../simple/concepts/JpaToscaTopologyTemplate.java | 17 +--- 9 files changed, 55 insertions(+), 109 deletions(-) (limited to 'models-tosca/src/main/java/org') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityFilter.java index ef0b6b183..d6f3a6815 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityFilter.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2021 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. @@ -55,7 +56,7 @@ public class ToscaEntityFilter implements PfObjectFilter< // @formatter:off if (LATEST_VERSION.equals(version)) { - return this.latestVersionFilter(returnList, new ToscaEntityComparator()); + return this.latestVersionFilter(returnList, new ToscaEntityComparator<>()); } else { return returnList; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTypedEntityFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTypedEntityFilter.java index b6ff86b3c..749873a23 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTypedEntityFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTypedEntityFilter.java @@ -67,7 +67,7 @@ public class ToscaTypedEntityFilter implements PfObjectFi // @formatter:off if (LATEST_VERSION.equals(version)) { - return this.latestVersionFilter(returnList, new ToscaEntityComparator()); + return this.latestVersionFilter(returnList, new ToscaEntityComparator<>()); } else { return returnList; } 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 4dfbae74d..f3dbeb030 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -46,7 +46,7 @@ public abstract class JpaToscaConstraint * * @param authorativeConcept the authorative concept to copy from */ - public JpaToscaConstraint(final ToscaConstraint authorativeConcept) { + protected JpaToscaConstraint(final ToscaConstraint authorativeConcept) { this.fromAuthorative(authorativeConcept); } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java index 893cdbe84..2416bab69 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java @@ -26,7 +26,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; import javax.persistence.AttributeOverride; -import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; @@ -61,12 +60,8 @@ public class JpaToscaEntityType extends PfConcept impleme // @formatter:off @Column - @AttributeOverrides({ - @AttributeOverride(name = "name", - column = @Column(name = "derived_from_name")), - @AttributeOverride(name = "version", - column = @Column(name = "derived_from_version")) - }) + @AttributeOverride(name = "name", column = @Column(name = "derived_from_name")) + @AttributeOverride(name = "version", column = @Column(name = "derived_from_version")) @VerifyKey private PfConceptKey derivedFrom; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java index 9a07ea187..c28a1aefb 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeTemplate.java @@ -33,7 +33,6 @@ import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; -import javax.persistence.JoinColumns; import javax.persistence.Lob; import javax.persistence.OneToOne; import javax.persistence.Table; @@ -81,14 +80,14 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType // formatter:off @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) - @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"), - @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")}) + @JoinColumn(name = "requirementsName", referencedColumnName = "name") + @JoinColumn(name = "requirementsVersion", referencedColumnName = "version") @Valid private JpaToscaRequirements requirements; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) - @JoinColumns({@JoinColumn(name = "capabilitiesName", referencedColumnName = "name"), - @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")}) + @JoinColumn(name = "capabilitiesName", referencedColumnName = "name") + @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version") @Valid private JpaToscaCapabilityAssignments capabilities; // @formatter:on diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java index 5ea21ed77..86be4b318 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaNodeType.java @@ -35,7 +35,6 @@ import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; -import javax.persistence.JoinColumns; import javax.persistence.Lob; import javax.persistence.OneToOne; import javax.persistence.Table; @@ -76,8 +75,8 @@ public class JpaToscaNodeType extends JpaToscaEntityType implemen // formatter:off @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) - @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"), - @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")}) + @JoinColumn(name = "requirementsName", referencedColumnName = "name") + @JoinColumn(name = "requirementsVersion", referencedColumnName = "version") // @formatter:on @Valid private JpaToscaRequirements requirements; 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 0be04353f..11f1199c0 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 @@ -27,7 +27,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.persistence.AttributeOverride; -import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; @@ -76,12 +75,8 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P // @formatter:off @Column - @AttributeOverrides({ - @AttributeOverride(name = "name", - column = @Column(name = "type_name")), - @AttributeOverride(name = "version", - column = @Column(name = "type_version")) - }) + @AttributeOverride(name = "name", column = @Column(name = "type_name")) + @AttributeOverride(name = "version", column = @Column(name = "type_version")) @VerifyKey @NotNull private PfConceptKey type; 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 594a5c348..0d9060d33 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2021 Nordix Foundation. - * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2021 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. @@ -34,7 +34,6 @@ import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; -import javax.persistence.JoinColumns; import javax.persistence.OneToOne; import javax.persistence.Table; import lombok.Data; @@ -49,11 +48,6 @@ 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.tosca.authorative.concepts.ToscaCapabilityType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -84,69 +78,45 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType()); - List> dataTypeMapList = dataTypes.toAuthorative(); - for (Map dataTypeMap : dataTypeMapList) { - toscaServiceTemplate.getDataTypes().putAll(dataTypeMap); - } + toscaServiceTemplate.setDataTypes(flattenMap(dataTypes.toAuthorative())); } if (capabilityTypes != null) { - toscaServiceTemplate.setCapabilityTypes(new LinkedHashMap<>()); - List> capabilityTypeMapList = capabilityTypes.toAuthorative(); - for (Map capabilityTypeMap : capabilityTypeMapList) { - toscaServiceTemplate.getCapabilityTypes().putAll(capabilityTypeMap); - } + toscaServiceTemplate.setCapabilityTypes(flattenMap(capabilityTypes.toAuthorative())); } if (relationshipTypes != null) { - toscaServiceTemplate.setRelationshipTypes(new LinkedHashMap<>()); - List> relationshipTypeMapList = relationshipTypes.toAuthorative(); - for (Map relationshipTypeMap : relationshipTypeMapList) { - toscaServiceTemplate.getRelationshipTypes().putAll(relationshipTypeMap); - } + toscaServiceTemplate.setRelationshipTypes(flattenMap(relationshipTypes.toAuthorative())); } if (nodeTypes != null) { - toscaServiceTemplate.setNodeTypes(new LinkedHashMap<>()); - List> nodeTypeMapList = nodeTypes.toAuthorative(); - for (Map nodeTypeMap : nodeTypeMapList) { - toscaServiceTemplate.getNodeTypes().putAll(nodeTypeMap); - } + toscaServiceTemplate.setNodeTypes(flattenMap(nodeTypes.toAuthorative())); } if (policyTypes != null) { - toscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>()); - List> policyTypeMapList = policyTypes.toAuthorative(); - for (Map policyTypeMap : policyTypeMapList) { - toscaServiceTemplate.getPolicyTypes().putAll(policyTypeMap); - } + toscaServiceTemplate.setPolicyTypes(flattenMap(policyTypes.toAuthorative())); } if (topologyTemplate != null) { @@ -265,6 +215,22 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType Map flattenMap(List> list) { + Map result = new LinkedHashMap<>(); + + for (Map map : list) { + result.putAll(map); + } + + return result; + } + @Override public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) { super.fromAuthorative(toscaServiceTemplate); 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 86add360a..b00b14a9e 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 @@ -35,7 +35,6 @@ import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; -import javax.persistence.JoinColumns; import javax.persistence.Lob; import javax.persistence.OneToOne; import javax.persistence.Table; @@ -87,23 +86,15 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative private Map<@NotNull String, @NotNull @Valid JpaToscaParameter> inputs; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) - @JoinColumns( - { - @JoinColumn(name = "nodeTemplatesName", referencedColumnName = "name"), - @JoinColumn(name = "nodeTemplatessVersion", referencedColumnName = "version") - } - ) + @JoinColumn(name = "nodeTemplatesName", referencedColumnName = "name") + @JoinColumn(name = "nodeTemplatessVersion", referencedColumnName = "version") @SerializedName("data_types") @Valid private JpaToscaNodeTemplates nodeTemplates; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) - @JoinColumns( - { - @JoinColumn(name = "policyName", referencedColumnName = "name"), - @JoinColumn(name = "policyVersion", referencedColumnName = "version") - } - ) + @JoinColumn(name = "policyName", referencedColumnName = "name") + @JoinColumn(name = "policyVersion", referencedColumnName = "version") // @formatter:on @Valid private JpaToscaPolicies policies; -- cgit 1.2.3-korg