diff options
author | liamfallon <liam.fallon@est.tech> | 2022-02-07 17:56:44 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2022-02-08 18:39:03 +0000 |
commit | 59b90519eda3b95b0cfc49b6349a591c7d0c78e2 (patch) | |
tree | 810e07f7d5cf270582264c40245372f597c713a2 /model/basic-model/src | |
parent | def44b294fa00bb5470e977eef9a05e5087035f0 (diff) |
Remove JAXB and XML, use GSON for JSON
This review converst apex-pdp to use GSON for JSON handling. In order to
preserve backward compatibility with the JAXB format of JSON, custom
handling of maps was required. Therefore, the policy-common
StandardCoder could not be used.
There are a lot of small changes, removing annotations from concepts and
tweaking of test data. However, this cleans up the code base so it is
worth doing.
Issue-ID: POLICY-1820
Change-Id: I213fa64f6d7f3f1df8d10f111d9fbedbe80f9fe0
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'model/basic-model/src')
33 files changed, 393 insertions, 1508 deletions
diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java index f5d421956..1efe83db8 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxArtifactKey.java @@ -23,11 +23,6 @@ package org.onap.policy.apex.model.basicmodel.concepts; import java.util.ArrayList; import java.util.List; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; import org.onap.policy.common.utils.validation.Assertions; @@ -39,22 +34,13 @@ import org.onap.policy.common.utils.validation.Assertions; * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP * regular expressions respectively. */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlRootElement(name = "apexArtifactKey", namespace = "http://www.onap.org/policy/apex-pdp") - -@XmlType(name = "AxArtifactKey", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = - { "name", "version" }) - public class AxArtifactKey extends AxKey { private static final long serialVersionUID = 8932717618579392561L; private static final String NAME_TOKEN = "name"; private static final String VERSION_TOKEN = "version"; - @XmlElement(required = true) private String name; - - @XmlElement(required = true) private String version; /** diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxConcept.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxConcept.java index 0e2c6bef2..37fe30b33 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxConcept.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxConcept.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019,2022 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,7 +24,6 @@ package org.onap.policy.apex.model.basicmodel.concepts; import java.io.Serializable; import java.util.List; -import javax.xml.bind.annotation.XmlType; import org.onap.policy.common.utils.validation.Assertions; /** @@ -33,9 +32,6 @@ import org.onap.policy.common.utils.validation.Assertions; * * @author Liam Fallon (liam.fallon@ericsson.com) */ - -@XmlType(name = "AxConcept", namespace = "http://www.onap.org/policy/apex-pdp") - public abstract class AxConcept implements Serializable, Comparable<AxConcept> { private static final long serialVersionUID = -7434939557282697490L; @@ -86,6 +82,12 @@ public abstract class AxConcept implements Serializable, Comparable<AxConcept> { public abstract void clean(); /** + * Builds references used by a concept. + */ + public void buildReferences() { + } + + /** * {@inheritDoc}. */ @Override diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java index 9b96d3398..bd3b18ec9 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java @@ -22,14 +22,10 @@ package org.onap.policy.apex.model.basicmodel.concepts; +import com.google.gson.annotations.SerializedName; import java.util.List; import java.util.Random; import java.util.UUID; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; import org.onap.policy.common.utils.validation.Assertions; @@ -41,16 +37,9 @@ import org.onap.policy.common.utils.validation.Assertions; * <p>Validation checks that all fields are defined and that the key is valid. It also observes that descriptions are * blank and warns if the UUID is a zero UUID. */ - -@XmlAccessorType(XmlAccessType.FIELD) -@XmlRootElement(name = "apexKeyInfo", namespace = "http://www.onap.org/policy/apex-pdp") -@XmlType(name = "AxKeyInfo", namespace = "http://www.onap.org/policy/apex-pdp", - propOrder = { "key", "uuid", "description" }) - public class AxKeyInfo extends AxConcept { private static final long serialVersionUID = -4023935924068914308L; - private static final int MAX_DESCRIPTION_LENGTH_8192 = 8192; private static final int UUID_BYTE_LENGTH_16 = 16; /* @@ -58,13 +47,11 @@ public class AxKeyInfo extends AxConcept { */ private static final Random sharedRandom = new Random(); // NOSONAR - @XmlElement(name = "key", required = true) private AxArtifactKey key; - @XmlElement(name = "UUID", required = true) + @SerializedName("UUID") private UUID uuid; - @XmlElement(required = true) private String description; /** diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInformation.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInformation.java index d1cc99a22..439b2960f 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInformation.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInformation.java @@ -30,11 +30,6 @@ import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import java.util.UUID; -import javax.xml.bind.Unmarshaller; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlType; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; import org.onap.policy.common.utils.validation.Assertions; @@ -48,16 +43,10 @@ import org.onap.policy.common.utils.validation.Assertions; * the map is defined, that the key in each map entry matches the key if each entry value, and that no duplicate UUIDs * exist. Each key information entry is then validated individually. */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "AxKeyInformation", namespace = "http://www.onap.org/policy/apex-pdp", - propOrder = { "key", "keyInfoMap" }) - public class AxKeyInformation extends AxConcept implements AxConceptGetter<AxKeyInfo> { private static final long serialVersionUID = -2746380769017043888L; - @XmlElement(name = "key", required = true) private AxArtifactKey key; - private Map<AxArtifactKey, AxKeyInfo> keyInfoMap; /** @@ -102,22 +91,6 @@ public class AxKeyInformation extends AxConcept implements AxConceptGetter<AxKey } /** - * When a model is unmarshalled from disk or from the database, the key information map is returned as a raw Hash - * Map. This method is called by JAXB after unmarshaling and is used to convert the hash map to a - * {@link NavigableMap} so that it will work with the {@link AxConceptGetter} interface. - * - * @param unmarshaler the unmarshaler that is unmarshaling the model - * @param parent the parent object of this object in the unmarshaler - */ - public void afterUnmarshal(final Unmarshaller unmarshaler, final Object parent) { - // The map must be navigable to allow name and version searching, - // unmarshaling returns a hash map - final NavigableMap<AxArtifactKey, AxKeyInfo> navigablekeyInfoMap = new TreeMap<>(); - navigablekeyInfoMap.putAll(keyInfoMap); - keyInfoMap = navigablekeyInfoMap; - } - - /** * This method generates default key information for all keys found in the concept passed in as a parameter that do * not already have key information. * @@ -160,6 +133,14 @@ public class AxKeyInformation extends AxConcept implements AxConceptGetter<AxKey } /** + * {@inheritDoc}. + */ + @Override + public void buildReferences() { + keyInfoMap.values().stream().forEach(keyInfo -> keyInfo.buildReferences()); + } + + /** * Sets the key of this concept. * * @param key the key of this concept @@ -218,7 +199,7 @@ public class AxKeyInformation extends AxConcept implements AxConceptGetter<AxKey } /** - * Validate an key information entry. + * Validate a key information entry. * * @param keyInfoEntry the key information entry * @param uuidSet the set of UUIDs encountered in validation so far, the UUID of this entry is added to the set diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxModel.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxModel.java index 5f63b9a3e..ce52b147f 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxModel.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxModel.java @@ -25,14 +25,7 @@ package org.onap.policy.apex.model.basicmodel.concepts; import java.util.List; import java.util.Set; import java.util.TreeSet; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.basicmodel.handling.KeyInfoMarshalFilter; import org.onap.policy.apex.model.basicmodel.service.ModelService; import org.onap.policy.common.utils.validation.Assertions; @@ -47,25 +40,13 @@ import org.onap.policy.common.utils.validation.Assertions; * {@link AxArtifactKey} and {@link AxReferenceKey} usage references a key that exists. Finally, a check is made to * ensure that an {@link AxArtifactKey} instance exists for every {@link AxKeyInfo} instance. */ - -@XmlRootElement(name = "apexModel", namespace = "http://www.onap.org/policy/apex-pdp") -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "AxModel", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = - { "key", "keyInformation" }) - public class AxModel extends AxConcept { private static final String IS_A_NULL_KEY = " is a null key"; private static final long serialVersionUID = -771659065637205430L; - @XmlElement(name = "key", required = true) private AxArtifactKey key; - - // @formatter:off - @XmlElement(name = "keyInformation", required = true) - @XmlJavaTypeAdapter(KeyInfoMarshalFilter.class) private AxKeyInformation keyInformation; - // @formatter:on /** * The Default Constructor creates this concept with a NULL artifact key. diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKey.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKey.java index 6c671d3e6..b37bc47e4 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKey.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKey.java @@ -23,11 +23,6 @@ package org.onap.policy.apex.model.basicmodel.concepts; import java.util.ArrayList; import java.util.List; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; import org.onap.policy.common.utils.validation.Assertions; @@ -48,11 +43,6 @@ import org.onap.policy.common.utils.validation.Assertions; * VERSION_REGEXP regular expressions respectively and that the local name fields match the * LOCAL_NAME_REGEXP regular expression. */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlRootElement(name = "apexReferenceKey", namespace = "http://www.onap.org/policy/apex-pdp") -@XmlType(name = "AxReferenceKey", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = { "parentKeyName", - "parentKeyVersion", "parentLocalName", "localName" }) - public class AxReferenceKey extends AxKey { private static final String PARENT_KEY_NAME = "parentKeyName"; private static final String PARENT_KEY_VERSION = "parentKeyVersion"; @@ -61,28 +51,25 @@ public class AxReferenceKey extends AxKey { private static final long serialVersionUID = 8932717618579392561L; - /** Regular expression to specify the structure of local names in reference keys. */ + /** + * Regular expression to specify the structure of local names in reference keys. + */ public static final String LOCAL_NAME_REGEXP = "[A-Za-z0-9\\-_\\.]+|^$"; - /** Regular expression to specify the structure of IDs in reference keys. */ + /** + * Regular expression to specify the structure of IDs in reference keys. + */ public static final String REFERENCE_KEY_ID_REGEXP = - "[A-Za-z0-9\\-_]+:[0-9].[0-9].[0-9]:[A-Za-z0-9\\-_]+:[A-Za-z0-9\\-_]+"; + "[A-Za-z0-9\\-_]+:[0-9].[0-9].[0-9]:[A-Za-z0-9\\-_]+:[A-Za-z0-9\\-_]+"; private static final int PARENT_NAME_FIELD = 0; private static final int PARENT_VERSION_FIELD = 1; private static final int PARENT_LOCAL_NAME_FIELD = 2; private static final int LOCAL_NAME_FIELD = 3; - @XmlElement(required = true) private String parentKeyName; - - @XmlElement(required = true) private String parentKeyVersion; - - @XmlElement(required = true) private String parentLocalName; - - @XmlElement(required = true) private String localName; /** @@ -95,19 +82,17 @@ public class AxReferenceKey extends AxKey { /** * The Copy Constructor creates a key by copying another key. * - * @param referenceKey - * the reference key to copy from + * @param referenceKey the reference key to copy from */ public AxReferenceKey(final AxReferenceKey referenceKey) { this(referenceKey.getParentKeyName(), referenceKey.getParentKeyVersion(), referenceKey.getParentLocalName(), - referenceKey.getLocalName()); + referenceKey.getLocalName()); } /** * Constructor to create a null reference key for the specified parent artifact key. * - * @param axArtifactKey - * the parent artifact key of this reference key + * @param axArtifactKey the parent artifact key of this reference key */ public AxReferenceKey(final AxArtifactKey axArtifactKey) { this(axArtifactKey.getName(), axArtifactKey.getVersion(), NULL_KEY_NAME, NULL_KEY_NAME); @@ -116,10 +101,8 @@ public class AxReferenceKey extends AxKey { /** * Constructor to create a reference key for the given parent artifact key with the given local name. * - * @param axArtifactKey - * the parent artifact key of this reference key - * @param localName - * the local name of this reference key + * @param axArtifactKey the parent artifact key of this reference key + * @param localName the local name of this reference key */ public AxReferenceKey(final AxArtifactKey axArtifactKey, final String localName) { this(axArtifactKey, NULL_KEY_NAME, localName); @@ -128,10 +111,8 @@ public class AxReferenceKey extends AxKey { /** * Constructor to create a reference key for the given parent reference key with the given local name. * - * @param parentReferenceKey - * the parent reference key of this reference key - * @param localName - * the local name of this reference key + * @param parentReferenceKey the parent reference key of this reference key + * @param localName the local name of this reference key */ public AxReferenceKey(final AxReferenceKey parentReferenceKey, final String localName) { this(parentReferenceKey.getParentArtifactKey(), parentReferenceKey.getLocalName(), localName); @@ -141,12 +122,9 @@ public class AxReferenceKey extends AxKey { * Constructor to create a reference key for the given parent reference key (specified by the parent reference key's * artifact key and local name) with the given local name. * - * @param axArtifactKey - * the artifact key of the parent reference key of this reference key - * @param parentLocalName - * the local name of the parent reference key of this reference key - * @param localName - * the local name of this reference key + * @param axArtifactKey the artifact key of the parent reference key of this reference key + * @param parentLocalName the local name of the parent reference key of this reference key + * @param localName the local name of this reference key */ public AxReferenceKey(final AxArtifactKey axArtifactKey, final String parentLocalName, final String localName) { this(axArtifactKey.getName(), axArtifactKey.getVersion(), parentLocalName, localName); @@ -156,12 +134,9 @@ public class AxReferenceKey extends AxKey { * Constructor to create a reference key for the given parent artifact key (specified by the parent artifact key's * name and version) with the given local name. * - * @param parentKeyName - * the name of the parent artifact key of this reference key - * @param parentKeyVersion - * the version of the parent artifact key of this reference key - * @param localName - * the local name of this reference key + * @param parentKeyName the name of the parent artifact key of this reference key + * @param parentKeyVersion the version of the parent artifact key of this reference key + * @param localName the local name of this reference key */ public AxReferenceKey(final String parentKeyName, final String parentKeyVersion, final String localName) { this(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName); @@ -171,31 +146,26 @@ public class AxReferenceKey extends AxKey { * Constructor to create a reference key for the given parent key (specified by the parent key's name, version nad * local name) with the given local name. * - * @param parentKeyName - * the parent key name of this reference key - * @param parentKeyVersion - * the parent key version of this reference key - * @param parentLocalName - * the parent local name of this reference key - * @param localName - * the local name of this reference key + * @param parentKeyName the parent key name of this reference key + * @param parentKeyVersion the parent key version of this reference key + * @param parentLocalName the parent local name of this reference key + * @param localName the local name of this reference key */ public AxReferenceKey(final String parentKeyName, final String parentKeyVersion, final String parentLocalName, - final String localName) { + final String localName) { super(); this.parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP); this.parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, - VERSION_REGEXP); + VERSION_REGEXP); this.parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, - LOCAL_NAME_REGEXP); + LOCAL_NAME_REGEXP); this.localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP); } /** * Constructor to create a key from the specified key ID. * - * @param id - * the key ID in a format that respects the KEY_ID_REGEXP + * @param id the key ID in a format that respects the KEY_ID_REGEXP */ public AxReferenceKey(final String id) { final var conditionedId = Assertions.validateStringParameter("id", id, REFERENCE_KEY_ID_REGEXP); @@ -208,13 +178,13 @@ public class AxReferenceKey extends AxKey { // Initiate the new key parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, nameVersionNameArray[PARENT_NAME_FIELD], - NAME_REGEXP); + NAME_REGEXP); parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, - nameVersionNameArray[PARENT_VERSION_FIELD], VERSION_REGEXP); + nameVersionNameArray[PARENT_VERSION_FIELD], VERSION_REGEXP); parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, - nameVersionNameArray[PARENT_LOCAL_NAME_FIELD], LOCAL_NAME_REGEXP); + nameVersionNameArray[PARENT_LOCAL_NAME_FIELD], LOCAL_NAME_REGEXP); localName = Assertions.validateStringParameter(LOCAL_NAME, nameVersionNameArray[LOCAL_NAME_FIELD], - LOCAL_NAME_REGEXP); + LOCAL_NAME_REGEXP); } /** @@ -224,7 +194,7 @@ public class AxReferenceKey extends AxKey { */ public static AxReferenceKey getNullKey() { return new AxReferenceKey(AxKey.NULL_KEY_NAME, AxKey.NULL_KEY_VERSION, AxKey.NULL_KEY_NAME, - AxKey.NULL_KEY_NAME); + AxKey.NULL_KEY_NAME); } /** @@ -274,8 +244,7 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent artifact key of this reference key. * - * @param parentKey - * the parent artifact key of this reference key + * @param parentKey the parent artifact key of this reference key */ public void setParentArtifactKey(final AxArtifactKey parentKey) { Assertions.argumentNotNull(parentKey, "parentKey may not be null"); @@ -288,8 +257,7 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent reference key of this reference key. * - * @param parentKey - * the parent reference key of this reference key + * @param parentKey the parent reference key of this reference key */ public void setParentReferenceKey(final AxReferenceKey parentKey) { Assertions.argumentNotNull(parentKey, "parentKey may not be null"); @@ -311,8 +279,7 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent key name of this reference key. * - * @param parentKeyName - * the parent key name of this reference key + * @param parentKeyName the parent key name of this reference key */ public void setParentKeyName(final String parentKeyName) { this.parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP); @@ -330,12 +297,11 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent key version of this reference key. * - * @param parentKeyVersion - * the parent key version of this reference key + * @param parentKeyVersion the parent key version of this reference key */ public void setParentKeyVersion(final String parentKeyVersion) { this.parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, - VERSION_REGEXP); + VERSION_REGEXP); } /** @@ -350,12 +316,11 @@ public class AxReferenceKey extends AxKey { /** * Sets the parent local name of this reference key. * - * @param parentLocalName - * the parent local name of this reference key + * @param parentLocalName the parent local name of this reference key */ public void setParentLocalName(final String parentLocalName) { this.parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, - LOCAL_NAME_REGEXP); + LOCAL_NAME_REGEXP); } /** @@ -370,8 +335,7 @@ public class AxReferenceKey extends AxKey { /** * Sets the local name of this reference key. * - * @param localName - * the local name of this reference key + * @param localName the local name of this reference key */ public void setLocalName(final String localName) { this.localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP); @@ -409,31 +373,31 @@ public class AxReferenceKey extends AxKey { @Override public AxValidationResult validate(final AxValidationResult result) { final var parentNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(PARENT_KEY_NAME, - parentKeyName, NAME_REGEXP); + parentKeyName, NAME_REGEXP); if (parentNameValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentKeyName invalid-" + parentNameValidationErrorMessage)); + "parentKeyName invalid-" + parentNameValidationErrorMessage)); } final var parentKeyVersionValidationErrorMessage = Assertions - .getStringParameterValidationMessage(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP); + .getStringParameterValidationMessage(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP); if (parentKeyVersionValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentKeyVersion invalid-" + parentKeyVersionValidationErrorMessage)); + "parentKeyVersion invalid-" + parentKeyVersionValidationErrorMessage)); } final var parentLocalNameValidationErrorMessage = Assertions - .getStringParameterValidationMessage(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP); + .getStringParameterValidationMessage(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP); if (parentLocalNameValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentLocalName invalid-" + parentLocalNameValidationErrorMessage)); + "parentLocalName invalid-" + parentLocalNameValidationErrorMessage)); } final var localNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(LOCAL_NAME, - localName, LOCAL_NAME_REGEXP); + localName, LOCAL_NAME_REGEXP); if (localNameValidationErrorMessage != null) { result.addValidationMessage(new AxValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "localName invalid-" + localNameValidationErrorMessage)); + "localName invalid-" + localNameValidationErrorMessage)); } return result; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/package-info.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/package-info.java index 294a66496..347e46a0f 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/package-info.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/package-info.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,12 +24,4 @@ * * @author Liam Fallon (liam.fallon@ericsson.com) */ - -@XmlSchema(namespace = "http://www.onap.org/policy/apex-pdp", elementFormDefault = XmlNsForm.QUALIFIED, - xmlns = { @XmlNs(namespaceURI = "http://www.onap.org/policy/apex-pdp", prefix = "") }) - package org.onap.policy.apex.model.basicmodel.concepts; - -import javax.xml.bind.annotation.XmlNs; -import javax.xml.bind.annotation.XmlNsForm; -import javax.xml.bind.annotation.XmlSchema; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCustomGsonMapAdapter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCustomGsonMapAdapter.java new file mode 100644 index 000000000..cb67590f7 --- /dev/null +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCustomGsonMapAdapter.java @@ -0,0 +1,118 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.apex.model.basicmodel.handling; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.AbstractMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +@SuppressWarnings("rawtypes") +public class ApexModelCustomGsonMapAdapter implements JsonSerializer<Map>, JsonDeserializer<Map> { + private static final String MAP_ENTRY_KEY = "entry"; + + @SuppressWarnings("unchecked") + @Override + public Map deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + + if (!(jsonElement instanceof JsonObject)) { + throw new JsonParseException("could not parse JSON map, map is not a JsonObject"); + } + + JsonObject jsonObject = (JsonObject) jsonElement; + + if (jsonObject.size() != 1) { + throw new JsonParseException("could not parse JSON map, map must be in a JsonObject with a single member"); + } + + JsonArray mapEntryArray = (JsonArray) jsonObject.get(MAP_ENTRY_KEY); + if (mapEntryArray == null) { + throw new JsonParseException("could not parse JSON map, map \"entry\" in JsonObject not found"); + } + + return new TreeMap( + StreamSupport + .stream(mapEntryArray.spliterator(), true) + .map(element -> deserializeMapEntry(element, typeOfT, context)) + .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue())) + ); + } + + @Override + public JsonElement serialize(Map sourceMap, Type typeOfSrc, JsonSerializationContext context) { + + // A map is stored in a JsonArray + JsonArray mapEntryArray = new JsonArray(); + + for (Object mapEntryObject : sourceMap.entrySet()) { + Entry mapEntry = (Entry) mapEntryObject; + mapEntryArray.add(serializeMapEntry(mapEntry, typeOfSrc, context)); + } + + JsonObject returnObject = new JsonObject(); + returnObject.add(MAP_ENTRY_KEY, mapEntryArray); + + return returnObject; + } + + @SuppressWarnings("unchecked") + private static Entry deserializeMapEntry(JsonElement element, Type typeOfT, JsonDeserializationContext context) { + // Get the types of the map + ParameterizedType pt = (ParameterizedType) typeOfT; + + // Type of the key and value of the map + Type keyType = pt.getActualTypeArguments()[0]; + Type valueType = pt.getActualTypeArguments()[1]; + + // Deserialize the key and value + return new AbstractMap.SimpleEntry( + context.deserialize(element.getAsJsonObject().get("key"), keyType), + context.deserialize(element.getAsJsonObject().get("value"), valueType)); + } + + private static JsonElement serializeMapEntry(Entry sourceEntry, Type typeOfSrc, JsonSerializationContext context) { + // Get the types of the map + ParameterizedType pt = (ParameterizedType) typeOfSrc; + + // Type of the key and value of the map + Type keyType = pt.getActualTypeArguments()[0]; + Type valueType = pt.getActualTypeArguments()[1]; + + JsonObject entryObject = new JsonObject(); + entryObject.add("key", context.serialize(sourceEntry.getKey(), keyType)); + entryObject.add("value", context.serialize(sourceEntry.getValue(), valueType)); + + return entryObject; + } +} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCustomGsonRefereceKeyAdapter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCustomGsonRefereceKeyAdapter.java new file mode 100644 index 000000000..d731c8689 --- /dev/null +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelCustomGsonRefereceKeyAdapter.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.apex.model.basicmodel.handling; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import java.lang.reflect.Type; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; + +public class ApexModelCustomGsonRefereceKeyAdapter implements JsonDeserializer<AxReferenceKey> { + + @Override + public AxReferenceKey deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + + if (jsonElement instanceof JsonObject) { + return new AxReferenceKey( + jsonElement.getAsJsonObject().get("parentKeyName").getAsString(), + jsonElement.getAsJsonObject().get("parentKeyVersion").getAsString(), + jsonElement.getAsJsonObject().get("parentLocalName").getAsString(), + jsonElement.getAsJsonObject().get("localName").getAsString() + ); + } else { + AxReferenceKey returnKey = new AxReferenceKey(); + returnKey.setLocalName(jsonElement.getAsString()); + return returnKey; + } + } +} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriter.java index 908ad31aa..54b5651cc 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriter.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriter.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,8 @@ package org.onap.policy.apex.model.basicmodel.handling; import java.io.File; import java.io.FileOutputStream; +import lombok.Getter; +import lombok.Setter; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxModel; import org.slf4j.ext.XLogger; @@ -34,42 +36,21 @@ import org.slf4j.ext.XLoggerFactory; * @author Liam Fallon (liam.fallon@ericsson.com) * @param <M> the type of Apex model to write to file, must be a sub class of {@link AxModel} */ +@Getter +@Setter public class ApexModelFileWriter<M extends AxModel> { private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelFileWriter.class); // Should models being written to files be valid - private boolean validateFlag; + private boolean validate; /** * Constructor, set the validation flag. * - * @param validateFlag indicates if validation be performed prior to output + * @param validate indicates if validation be performed prior to output */ - public ApexModelFileWriter(final boolean validateFlag) { - this.validateFlag = validateFlag; - } - - /** - * Write a model to an XML file. - * - * @param model The model to write - * @param rootModelClass The concept class - * @param modelFileName The name of the file to write to - * @throws ApexException thrown on errors - */ - public void apexModelWriteXmlFile(final M model, final Class<M> rootModelClass, final String modelFileName) - throws ApexException { - LOGGER.debug("running apexModelWriteXMLFile . . ."); - - final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass); - modelWriter.setValidateFlag(validateFlag); - modelWriter.getCDataFieldSet().add("description"); - modelWriter.getCDataFieldSet().add("logic"); - modelWriter.getCDataFieldSet().add("uiLogic"); - - writeModelFile(model, modelWriter, modelFileName); - - LOGGER.debug("ran apexModelWriteXMLFile"); + public ApexModelFileWriter(final boolean validate) { + this.validate = validate; } /** @@ -85,8 +66,7 @@ public class ApexModelFileWriter<M extends AxModel> { LOGGER.debug("running apexModelWriteJSONFile . . ."); final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass); - modelWriter.setJsonOutput(true); - modelWriter.setValidateFlag(validateFlag); + modelWriter.setValidate(validate); writeModelFile(model, modelWriter, modelFileName); @@ -94,24 +74,6 @@ public class ApexModelFileWriter<M extends AxModel> { } /** - * Checks if the validation flag is set. - * - * @return true, the validation flag is set - */ - public boolean isValidateFlag() { - return validateFlag; - } - - /** - * Sets the validate flag. - * - * @param validateFlag the validate flag value - */ - public void setValidateFlag(final boolean validateFlag) { - this.validateFlag = validateFlag; - } - - /** * Write a model to a file using a model writer. * * @param model The model to write diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReader.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReader.java index 7e136f6b3..a9bd2fe60 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReader.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReader.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2022 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,55 +22,47 @@ package org.onap.policy.apex.model.basicmodel.handling; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.StringReader; -import java.util.regex.Pattern; -import javax.xml.XMLConstants; -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.SchemaFactory; -import org.eclipse.persistence.jaxb.JAXBContextFactory; -import org.eclipse.persistence.jaxb.MarshallerProperties; -import org.eclipse.persistence.oxm.MediaType; +import java.util.Map; +import lombok.Getter; +import lombok.Setter; import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.common.utils.resources.TextFileUtils; import org.onap.policy.common.utils.validation.Assertions; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; /** - * This class reads an Apex concept from an XML file into a Java Apex Concept {@link AxConcept}. + * This class reads an Apex concept from a file into a Java Apex Concept {@link AxConcept}. * * @author Liam Fallon (liam.fallon@ericsson.com) * @param <C> the type of Apex concept to read, must be a sub class of {@link AxConcept} */ +@Getter +@Setter public class ApexModelReader<C extends AxConcept> { // Get a reference to the logger private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelReader.class); - // Regular expressions for checking input types - // (starts with <?xml...> - private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*"; - // starts with some kind of bracket [ or ( - private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s\\S]*[\\)\\}\\]]"; - // or {, then has something, then has - // and has a close bracket + // Use GSON to deserialize JSON + private static Gson gson = new GsonBuilder() + .registerTypeAdapter(AxReferenceKey.class, new ApexModelCustomGsonRefereceKeyAdapter()) + .registerTypeAdapter(Map.class, new ApexModelCustomGsonMapAdapter()) + .setPrettyPrinting() + .create(); // Â The root class of the concept we are reading private final Class<C> rootConceptClass; - // The unmarshaller for the Apex concepts - private Unmarshaller unmarshaller = null; - // All read concepts are validated after reading if this flag is set - private boolean validateFlag = true; + private boolean validate = true; /** * Constructor, initiates the reader with validation on. @@ -81,17 +73,6 @@ public class ApexModelReader<C extends AxConcept> { public ApexModelReader(final Class<C> rootConceptClass) throws ApexModelException { // Save the root concept class this.rootConceptClass = rootConceptClass; - - try { - final var jaxbContext = JAXBContextFactory.createContext(new Class[] {rootConceptClass}, null); - - // Set up the unmarshaller to carry out validation - unmarshaller = jaxbContext.createUnmarshaller(); - unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); - } catch (final JAXBException e) { - LOGGER.error("Unable to set JAXB context", e); - throw new ApexModelException("Unable to set JAXB context", e); - } } /** @@ -103,36 +84,11 @@ public class ApexModelReader<C extends AxConcept> { */ public ApexModelReader(final Class<C> rootConceptClass, final boolean validate) throws ApexModelException { this(rootConceptClass); - this.validateFlag = validate; - } - - /** - * Set the schema to use for reading XML files. - * - * @param schemaFileName the schema file to use - * @throws ApexModelException if the schema cannot be set - */ - public void setSchema(final String schemaFileName) throws ApexModelException { - // Has a schema been set - if (schemaFileName != null) { - try { - // Set the concept schema - final var schemaUrl = ResourceUtils.getUrlResource(schemaFileName); - final var apexConceptSchema = - SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaUrl); - unmarshaller.setSchema(apexConceptSchema); - } catch (final Exception e) { - LOGGER.error("Unable to load schema ", e); - throw new ApexModelException("Unable to load schema", e); - } - } else { - // Clear the schema - unmarshaller.setSchema(null); - } + this.validate = validate; } /** - * This method checks the specified Apex concept XML file and reads it into an Apex concept. + * This method checks the specified Apex concept file and reads it into an Apex concept. * * @param apexConceptStream the apex concept stream * @return the Apex concept @@ -179,28 +135,23 @@ public class ApexModelReader<C extends AxConcept> { LOGGER.entry("reading Apex concept from string . . ."); - final var apexString = apexConceptString.trim(); - - // Set the type of input for this stream - setInputType(apexString); - - // The Apex Concept C apexConcept = null; - - // Use JAXB to read and verify the Apex concept XML file try { - // Load the configuration file - final var source = new StreamSource(new StringReader(apexString)); - final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, rootConceptClass); - apexConcept = rootElement.getValue(); - } catch (final JAXBException e) { - throw new ApexModelException("Unable to unmarshal Apex concept ", e); + apexConcept = gson.fromJson(apexConceptString, rootConceptClass); + } catch (final Exception je) { + throw new ApexModelException("Unable to unmarshal Apex concept ", je); + } + + if (apexConcept == null) { + throw new ApexModelException("Unable to unmarshal Apex concept, unmarshaled model is null "); } LOGGER.debug("reading of Apex concept {} completed"); + apexConcept.buildReferences(); + // Check if the concept should be validated - if (validateFlag) { + if (validate) { // Validate the configuration file final AxValidationResult validationResult = apexConcept.validate(new AxValidationResult()); if (validationResult.isValid()) { @@ -215,54 +166,4 @@ public class ApexModelReader<C extends AxConcept> { return apexConcept; } } - - /** - * Gets the value of the validation flag. - * - * @return the validation flag value - */ - public boolean getValidateFlag() { - return validateFlag; - } - - /** - * Sets the validation flag. - * - * @param validateFlag the validation flag value - */ - public void setValidateFlag(final boolean validateFlag) { - this.validateFlag = validateFlag; - } - - /** - * Set the type of input for the concept reader. - * - * @param apexConceptString The stream with - * @throws ApexModelException on errors setting input type - */ - private void setInputType(final String apexConceptString) throws ApexModelException { - // Check the input type - if (Pattern.compile(JSON_INPUT_TYPE_REGEXP).matcher(apexConceptString).find()) { - // is json - try { - unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); - unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); - } catch (final Exception e) { - LOGGER.warn("JAXB error setting marshaller for JSON Input", e); - throw new ApexModelException("JAXB error setting unmarshaller for JSON input", e); - } - } else if (Pattern.compile(XML_INPUT_TYPE_REGEXP).matcher(apexConceptString).find()) { - // is xml - try { - unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML); - } catch (final Exception e) { - LOGGER.warn("JAXB error setting marshaller for XML Input", e); - throw new ApexModelException("JAXB error setting unmarshaller for XML input", e); - } - } else { - LOGGER.warn("format of input for Apex concept is neither JSON nor XML"); - throw new ApexModelException("format of input for Apex concept is neither JSON nor XML"); - } - } - } diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelSaver.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelSaver.java index dea32df49..79d69bdd5 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelSaver.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelSaver.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,7 @@ import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; /** - * This class is used to save Apex models to file in XML or JSON format. + * This class is used to save Apex models to file in JSON format. * * @author Liam Fallon (liam.fallon@ericsson.com) * @param <M> the type of Apex model to save to file, must be a sub class of {@link AxModel} @@ -51,7 +51,7 @@ public class ApexModelSaver<M extends AxModel> { * @param rootModelClass the class of the model, a sub class of {@link AxModel} * @param model the model to write, an instance of a sub class of {@link AxModel} * @param writePath the directory to which models will be written. The name of the written model will be the Model - * Name for its key with the suffix {@code .xml} or {@code .json}. + * Name for its key with the suffix {@code .json}. */ public ApexModelSaver(final Class<M> rootModelClass, final M model, final String writePath) { Assertions.argumentNotNull(rootModelClass, "argument rootModelClass may not be null"); @@ -64,21 +64,6 @@ public class ApexModelSaver<M extends AxModel> { } /** - * Write an Apex model to a file in XML format. The model will be written to {@code <writePath/modelKeyName.xml>} - * - * @throws ApexException on errors writing the Apex model - */ - public void apexModelWriteXml() throws ApexException { - LOGGER.debug("running apexModelWriteXML . . ."); - - // Write the file to disk - final var xmlFile = new File(writePath + File.separatorChar + model.getKey().getName() + ".xml"); - new ApexModelFileWriter<M>(true).apexModelWriteXmlFile(model, rootModelClass, xmlFile.getPath()); - - LOGGER.debug("ran apexModelWriteXML"); - } - - /** * Write an Apex model to a file in JSON format. The model will be written to {@code <writePath/modelKeyName.json>} * * @throws ApexException on errors writing the Apex model diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriter.java index de6a72ffd..a9df23afc 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriter.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriter.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,8 @@ package org.onap.policy.apex.model.basicmodel.handling; import java.io.ByteArrayOutputStream; +import lombok.Getter; +import lombok.Setter; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; import org.onap.policy.common.utils.validation.Assertions; @@ -34,19 +36,21 @@ import org.slf4j.ext.XLoggerFactory; * @author Liam Fallon (liam.fallon@ericsson.com) * @param <C> the type of Apex concept to write to a string, must be a sub class of {@link AxConcept} */ +@Getter +@Setter public class ApexModelStringWriter<C extends AxConcept> { private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelStringWriter.class); // Should concepts being written to files be valid - private boolean validateFlag; + private boolean validate; /** * Constructor, set the validation flag. * - * @param validateFlag Should validation be performed prior to output + * @param validate Should validation be performed prior to output */ - public ApexModelStringWriter(final boolean validateFlag) { - this.validateFlag = validateFlag; + public ApexModelStringWriter(final boolean validate) { + this.validate = validate; } /** @@ -54,46 +58,14 @@ public class ApexModelStringWriter<C extends AxConcept> { * * @param concept The concept to write * @param rootConceptClass The concept class - * @param jsonFlag writes JSON if true, and a generic string if false * @return The string with the concept * @throws ApexException thrown on errors */ - public String writeString(final C concept, final Class<C> rootConceptClass, final boolean jsonFlag) + public String writeString(final C concept, final Class<C> rootConceptClass) throws ApexException { Assertions.argumentNotNull(concept, "concept may not be null"); - if (jsonFlag) { - return writeJsonString(concept, rootConceptClass); - } else { - return concept.toString(); - } - } - - /** - * Write a concept to an XML string. - * - * @param concept The concept to write - * @param rootConceptClass The concept class - * @return The string with the concept - * @throws ApexException thrown on errors - */ - public String writeXmlString(final C concept, final Class<C> rootConceptClass) throws ApexException { - LOGGER.debug("running writeXMLString . . ."); - - final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass); - conceptWriter.setValidateFlag(validateFlag); - conceptWriter.getCDataFieldSet().add("description"); - conceptWriter.getCDataFieldSet().add("logic"); - conceptWriter.getCDataFieldSet().add("uiLogic"); - - try (var baOutputStream = new ByteArrayOutputStream()) { - conceptWriter.write(concept, baOutputStream); - return baOutputStream.toString(); - } catch (final Exception e) { - LOGGER.warn("error writing XML string", e); - throw new ApexException("error writing XML string", e); - } - + return writeJsonString(concept, rootConceptClass); } /** @@ -108,8 +80,7 @@ public class ApexModelStringWriter<C extends AxConcept> { LOGGER.debug("running writeJSONString . . ."); final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass); - conceptWriter.setJsonOutput(true); - conceptWriter.setValidateFlag(validateFlag); + conceptWriter.setValidate(validate); try (var baOutputStream = new ByteArrayOutputStream()) { conceptWriter.write(concept, baOutputStream); @@ -120,22 +91,4 @@ public class ApexModelStringWriter<C extends AxConcept> { } } - - /** - * Checks if is validate flag. - * - * @return true, if checks if is validate flag - */ - public boolean isValidateFlag() { - return validateFlag; - } - - /** - * Sets the validate flag. - * - * @param validateFlag the validate flag - */ - public void setValidateFlag(final boolean validateFlag) { - this.validateFlag = validateFlag; - } } diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java index 9e43f76bd..349622697 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriter.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,129 +21,67 @@ package org.onap.policy.apex.model.basicmodel.handling; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; -import java.util.Set; -import java.util.TreeSet; -import javax.xml.XMLConstants; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import org.eclipse.persistence.jaxb.JAXBContextFactory; -import org.eclipse.persistence.jaxb.MarshallerProperties; -import org.eclipse.persistence.oxm.MediaType; +import java.util.Map; +import lombok.Getter; +import lombok.Setter; import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; import org.onap.policy.common.utils.validation.Assertions; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; /** - * This class writes an Apex concept to an XML file or JSON file from a Java Apex Concept. + * This class writes an Apex concept to a file from a Java Apex Concept. * * @param <C> the type of Apex concept to write, must be a sub class of {@link AxConcept} * @author John Keeney (john.keeney@ericsson.com) */ +@Getter +@Setter public class ApexModelWriter<C extends AxConcept> { private static final String CONCEPT_MAY_NOT_BE_NULL = "concept may not be null"; private static final String CONCEPT_WRITER_MAY_NOT_BE_NULL = "concept writer may not be null"; private static final String CONCEPT_STREAM_MAY_NOT_BE_NULL = "concept stream may not be null"; + // Use GSON to serialize JSON + private static Gson gson = new GsonBuilder() + .registerTypeAdapter(AxReferenceKey.class, new ApexModelCustomGsonRefereceKeyAdapter()) + .registerTypeAdapter(Map.class, new ApexModelCustomGsonMapAdapter()) + .setPrettyPrinting() + .create(); + // Get a reference to the logger private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelWriter.class); - // Writing as JSON or XML - private boolean jsonOutput = false; - - // The list of fields to output as CDATA - private final Set<String> cdataFieldSet = new TreeSet<>(); - - // The Marshaller for the Apex concepts - private Marshaller marshaller = null; + // Â The root class of the concept we are reading + private final Class<C> rootConceptClass; // All written concepts are validated before writing if this flag is set - private boolean validateFlag = true; + private boolean validate = true; /** - * Constructor, initiates the writer. + * Constructor, initiates the writer with validation on. * * @param rootConceptClass the root concept class for concept reading - * @throws ApexModelException the apex concept writer exception + * @throws ApexModelException the apex concept reader exception */ public ApexModelWriter(final Class<C> rootConceptClass) throws ApexModelException { - // Set up Eclipselink for XML and JSON output - System.setProperty("javax.xml.bind.context.factory", "org.eclipse.persistence.jaxb.JAXBContextFactory"); - - try { - final var jaxbContext = JAXBContextFactory.createContext(new Class[]{rootConceptClass}, null); - - // Set up the unmarshaller to carry out validation - marshaller = jaxbContext.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - marshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); - } catch (final JAXBException e) { - throw new ApexModelException("JAXB marshaller creation exception", e); - } - } - - /** - * The set of fields to be output as CDATA. - * - * @return the set of fields - */ - public Set<String> getCDataFieldSet() { - return cdataFieldSet; - } - - /** - * Return true if JSON output enabled, XML output if false. - * - * @return true for JSON output - */ - public boolean isJsonOutput() { - return jsonOutput; - } - - /** - * Set the value of JSON output, true for JSON output, false for XML output. - * - * @param jsonOutput true for JSON output - * @throws ApexModelException on errors setting output type - */ - public void setJsonOutput(final boolean jsonOutput) throws ApexModelException { - this.jsonOutput = jsonOutput; - - // Set up output specific parameters - if (this.jsonOutput) { - try { - marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); - marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); - } catch (final Exception e) { - throw new ApexModelException("JAXB error setting marshaller for JSON output", e); - } - } else { - try { - marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML); - } catch (final Exception e) { - throw new ApexModelException("JAXB error setting marshaller for XML output", e); - } - } + // Save the root concept class + this.rootConceptClass = rootConceptClass; } /** * This method validates the Apex concept then writes it into a stream. * - * @param concept the concept to write + * @param concept the concept to write * @param apexConceptStream the stream to write to * @throws ApexModelException on validation or writing exceptions */ @@ -157,7 +95,7 @@ public class ApexModelWriter<C extends AxConcept> { /** * This method validates the Apex concept then writes it into a writer. * - * @param concept the concept to write + * @param concept the concept to write * @param apexConceptWriter the writer to write to * @throws ApexModelException on validation or writing exceptions */ @@ -166,84 +104,23 @@ public class ApexModelWriter<C extends AxConcept> { Assertions.argumentNotNull(apexConceptWriter, CONCEPT_WRITER_MAY_NOT_BE_NULL); // Check if we should validate the concept - if (validateFlag) { + if (validate) { // Validate the concept first final AxValidationResult validationResult = concept.validate(new AxValidationResult()); if (!validationResult.isValid()) { String message = - "Apex concept xml (" + concept.getKey().getId() + ") validation failed: " + validationResult - .toString(); + "Apex concept (" + concept.getKey().getId() + ") validation failed: " + validationResult.toString(); throw new ApexModelException(message); } } - if (jsonOutput) { - writeJson(concept, apexConceptWriter); - } else { - writeXml(concept, apexConceptWriter); - } - } - - /** - * This method writes the Apex concept into a writer in XML format. - * - * @param concept the concept to write - * @param apexConceptWriter the writer to write to - * @throws ApexModelException on validation or writing exceptions - */ - private void writeXml(final C concept, final Writer apexConceptWriter) throws ApexModelException { - Assertions.argumentNotNull(concept, CONCEPT_MAY_NOT_BE_NULL); - - LOGGER.debug("writing Apex concept XML . . ."); - - try { - // Write the concept into a DOM document, then transform to add CDATA fields and pretty - // print, then write out the result - final var docBuilderFactory = DocumentBuilderFactory.newInstance(); - docBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); - docBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); - - docBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - final var document = docBuilderFactory.newDocumentBuilder().newDocument(); - - // Marshal the concept into the empty document. - marshaller.marshal(concept, document); - - final var domTransformer = getTransformer(); - - // Convert the cDataFieldSet into a space delimited string - domTransformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, - cdataFieldSet.toString().replaceAll("[\\[\\]\\,]", " ")); - domTransformer.transform(new DOMSource(document), new StreamResult(apexConceptWriter)); - } catch (JAXBException | TransformerException | ParserConfigurationException e) { - throw new ApexModelException("Unable to marshal Apex concept to XML", e); - } - LOGGER.debug("wrote Apex concept XML"); - } - - private Transformer getTransformer() throws TransformerConfigurationException { - // Transform the DOM to the output stream - final var transformerFactory = TransformerFactory.newInstance(); - transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); - transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); - - final var domTransformer = transformerFactory.newTransformer(); - - // Pretty print - try { - domTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); - // May fail if not using XALAN XSLT engine. But not in any way vital - domTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); - } catch (final Exception ignore) { - LOGGER.trace("Unable to set indent property", ignore); - } - return domTransformer; + writeJson(concept, apexConceptWriter); } /** * This method writes the Apex concept into a writer in JSON format. * - * @param concept the concept to write + * @param concept the concept to write * @param apexConceptWriter the writer to write to * @throws ApexModelException on validation or writing exceptions */ @@ -252,29 +129,20 @@ public class ApexModelWriter<C extends AxConcept> { LOGGER.debug("writing Apex concept JSON . . ."); + String modelJsonString = null; try { - marshaller.marshal(concept, apexConceptWriter); - } catch (final JAXBException e) { - throw new ApexModelException("Unable to marshal Apex concept to JSON", e); + modelJsonString = gson.toJson(concept, rootConceptClass); + } catch (Exception je) { + throw new ApexModelException("Unable to marshal Apex concept to JSON", je); } - LOGGER.debug("wrote Apex concept JSON"); - } - /** - * Gets the validation flag value. - * - * @return the validation flag value - */ - public boolean getValidateFlag() { - return validateFlag; - } + try { + apexConceptWriter.write(modelJsonString); + apexConceptWriter.close(); + } catch (IOException ioe) { + throw new ApexModelException("Unable to write Apex concept as JSON", ioe); + } - /** - * Sets the validation flag. - * - * @param validateFlag the validation flag value - */ - public void setValidateFlag(final boolean validateFlag) { - this.validateFlag = validateFlag; + LOGGER.debug("wrote Apex concept JSON"); } } diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexSchemaGenerator.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexSchemaGenerator.java deleted file mode 100644 index d5f34785b..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexSchemaGenerator.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020-2021 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.apex.model.basicmodel.handling; - -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.io.StringWriter; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.SchemaOutputResolver; -import javax.xml.transform.Result; -import javax.xml.transform.stream.StreamResult; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * This class generates the XML model schema from the given Apex concept classes. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class ApexSchemaGenerator { - // Get a reference to the logger - private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexSchemaGenerator.class); - - /** - * A Main method to allow schema generation from the command line or from maven or scripts. - * - * @param args the command line arguments, usage is {@code ApexSchemaGenerator apex-root-class [schema-file-name]} - */ - public static void main(final String[] args) { - PrintStream printStream = null; - - if (args.length == 1) { - printStream = System.out; - } else if (args.length == 2) { - final var schemaFile = new File(args[1]); - - try { - schemaFile.getParentFile().mkdirs(); - printStream = new PrintStream(schemaFile); - } catch (final Exception e) { - LOGGER.error("error on Apex schema output", e); - return; - } - } else { - LOGGER.error("usage: ApexSchemaGenerator apex-root-class [schema-file-name]"); - return; - } - - // Get the schema - final String schema = new ApexSchemaGenerator().generate(args[0]); - - // Output the schema - printStream.println(schema); - - printStream.close(); - } - - /** - * Generates the XML schema (XSD) for the Apex model described using JAXB annotations. - * - * @param rootClassName the name of the root class for schema generation - * @return The schema - */ - public String generate(final String rootClassName) { - JAXBContext jaxbContext; - try { - jaxbContext = JAXBContext.newInstance(Class.forName(rootClassName)); - } catch (final ClassNotFoundException e) { - LOGGER.error("could not create JAXB context, root class " + rootClassName + " not found", e); - return null; - } catch (final JAXBException e) { - LOGGER.error("could not create JAXB context", e); - return null; - } - - final var sor = new ApexSchemaOutputResolver(); - try { - jaxbContext.generateSchema(sor); - } catch (final IOException e) { - LOGGER.error("error generating the Apex schema (XSD) file", e); - return null; - } - - var schemaString = sor.getSchema(); - schemaString = fixForUnqualifiedBug(schemaString); - - return schemaString; - } - - /** - * There is a bug in schema generation that does not specify the elements from Java Maps as being unqualified. This - * method "hacks" those elements in the schema to fix this, the elements being {@code entry}, {@code key}, and - * {@code value} - * - * @param schemaString The schema in which elements should be fixed - * @return the string - */ - private String fixForUnqualifiedBug(final String schemaString) { - // Fix the "entry" element - var newSchemaString = schemaString.replace( - "<xs:element name=\"entry\" minOccurs=\"0\" maxOccurs=\"unbounded\">", - "<xs:element name=\"entry\" minOccurs=\"0\" maxOccurs=\"unbounded\" form=\"unqualified\">"); - - // Fix the "key" element - newSchemaString = newSchemaString.replace("<xs:element name=\"key\"", - "<xs:element name=\"key\" form=\"unqualified\""); - - // Fix the "value" element - newSchemaString = newSchemaString.replace("<xs:element name=\"value\"", - "<xs:element name=\"value\" form=\"unqualified\""); - - return newSchemaString; - } - - /** - * This inner class is used to receive the output of schema generation from the JAXB schema generator. - */ - private class ApexSchemaOutputResolver extends SchemaOutputResolver { - private final StringWriter stringWriter = new StringWriter(); - - /** - * {@inheritDoc}. - */ - @Override - public Result createOutput(final String namespaceUri, final String suggestedFileName) throws IOException { - final var result = new StreamResult(stringWriter); - result.setSystemId(suggestedFileName); - return result; - } - - /** - * Get the schema from the string writer. - * - * @return the schema generated by JAXB - */ - public String getSchema() { - return stringWriter.toString(); - } - } -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/KeyInfoMarshalFilter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/KeyInfoMarshalFilter.java deleted file mode 100644 index cfc385d01..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/KeyInfoMarshalFilter.java +++ /dev/null @@ -1,104 +0,0 @@ -/*- - * ============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.apex.model.basicmodel.handling; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; -import javax.xml.bind.annotation.adapters.XmlAdapter; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; - -/** - * This class implements a filter to prevent some keyinfo information being marshalled when a model is serialised. - */ -public class KeyInfoMarshalFilter extends XmlAdapter<AxKeyInformation, AxKeyInformation> { - - private List<AxKey> filterList = new LinkedList<>(); - - /** - * Adds a key to the list to be filtered. - * - * @param key the key to add to the filter list - */ - public void addFilterKey(AxKey key) { - filterList.add(key); - } - - /** - * Remove a key from the list to be filtered. - * - * @param key the key to remove from the filter list - * @return true if the passed key was in the filter list and has been removed. - */ - public boolean removeFilterKey(AxKey key) { - return filterList.remove(key); - } - - /** - * Adds some keys to the list to be filtered. - * - * @param keys the keys to add to the filter list - */ - public void addFilterKeys(Collection<? extends AxKey> keys) { - filterList.addAll(keys); - } - - /** - * Decide whether to unmarshall some keyinfos - Always. - * - * @param val the keyinfo - * @return the keyinfo - * @throws Exception if there is some problem unmarshalling - * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object) - */ - @Override - public AxKeyInformation unmarshal(AxKeyInformation val) throws Exception { - return val; - } - - /** - * Select which keyinfo entries will be marshalled - i.e. those not in the filter list. - * - * @param val the keyinfo - * @return the keyinfo - * @throws Exception if there is some problem with the marshalling - * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(Object) - */ - @Override - public AxKeyInformation marshal(AxKeyInformation val) throws Exception { - if (val == null || val.getKeyInfoMap() == null || val.getKeyInfoMap().isEmpty() || filterList.isEmpty()) { - return val; - } - //create a new keyinfo clone to avoid removing keyinfo entries from the original model - AxKeyInformation ret = new AxKeyInformation(val); - Map<AxArtifactKey, AxKeyInfo> retmap = new TreeMap<>(ret.getKeyInfoMap()); - for (AxKey key : filterList) { - retmap.remove(key); - } - ret.setKeyInfoMap(retmap); - return ret; - } -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/package-info.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/package-info.java index 081cff540..3d6dab3b2 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/package-info.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/package-info.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,20 +18,4 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ - -/** - * Contains a number of utility classes for handling APEX {@link org.onap.policy.apex.model.basicmodel.concepts.AxModel} - * models and {@link org.onap.policy.apex.model.basicmodel.concepts.AxConcept} concepts. Classes to read and write - * models to files, strings, and databases are included, as well as classes to generate XML schemas for models. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ - -@XmlSchema(namespace = "http://www.onap.org/policy/apex-pdp", elementFormDefault = XmlNsForm.QUALIFIED, - xmlns = { @XmlNs(namespaceURI = "http://www.onap.org/policy/apex-pdp", prefix = "") }) - package org.onap.policy.apex.model.basicmodel.handling; - -import javax.xml.bind.annotation.XmlNs; -import javax.xml.bind.annotation.XmlNsForm; -import javax.xml.bind.annotation.XmlSchema; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/package-info.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/package-info.java index e540646d9..8ad405e25 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/package-info.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/package-info.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019,2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,7 @@ * the model for APEX concepts anywhere in the system. * * <p>It also provides handling support to models, allowing them to be read and written to file and databases in JSON - * and XML format. + * format. * * @author Liam Fallon (liam.fallon@ericsson.com) */ diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/test/TestApexModel.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/test/TestApexModel.java index 5dbf69492..9dde47d05 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/test/TestApexModel.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/test/TestApexModel.java @@ -44,7 +44,7 @@ import org.slf4j.ext.XLoggerFactory; public class TestApexModel<M extends AxModel> { private static final String MODEL_IS_INVALID = "model is invalid "; private static final String ERROR_PROCESSING_FILE = "error processing file "; - private static final String TEST_MODEL_UNEQUAL_STR = "test model does not equal model read from XML file "; + private static final String TEST_MODEL_UNEQUAL_STR = "test model does not equal model read from file "; private static final String TEMP_FILE_CREATE_ERR_STR = "error creating temporary file for Apex model"; private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestApexModel.class); @@ -77,55 +77,6 @@ public class TestApexModel<M extends AxModel> { } /** - * Test write and read in XML format. - * - * @throws ApexException on write/read errors - */ - public final void testApexModelWriteReadXml() throws ApexException { - LOGGER.debug("running testApexModelWriteReadXML . . ."); - - final var model = modelCreator.getModel(); - - // Write the file to disk - File xmlFile; - - try { - xmlFile = File.createTempFile("ApexModel", ".xml"); - xmlFile.deleteOnExit(); - } catch (final Exception e) { - LOGGER.warn(TEMP_FILE_CREATE_ERR_STR, e); - throw new ApexException(TEMP_FILE_CREATE_ERR_STR, e); - } - new ApexModelFileWriter<M>(true).apexModelWriteXmlFile(model, rootModelClass, xmlFile.getPath()); - - // Read the file from disk - final ApexModelReader<M> modelReader = new ApexModelReader<>(rootModelClass); - - try { - final var apexModelUrl = ResourceUtils.getLocalFile(xmlFile.getAbsolutePath()); - final var fileModel = modelReader.read(apexModelUrl.openStream()); - checkModelEquality(model, fileModel, TEST_MODEL_UNEQUAL_STR + xmlFile.getAbsolutePath()); - } catch (final Exception e) { - LOGGER.warn(ERROR_PROCESSING_FILE + xmlFile.getAbsolutePath(), e); - throw new ApexException(ERROR_PROCESSING_FILE + xmlFile.getAbsolutePath(), e); - } - - final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass); - modelWriter.getCDataFieldSet().add("description"); - modelWriter.getCDataFieldSet().add("logic"); - modelWriter.getCDataFieldSet().add("uiLogic"); - - final var baOutputStream = new ByteArrayOutputStream(); - modelWriter.write(model, baOutputStream); - final var baInputStream = new ByteArrayInputStream(baOutputStream.toByteArray()); - final var byteArrayModel = modelReader.read(baInputStream); - - checkModelEquality(model, byteArrayModel, "test model does not equal XML marshalled and unmarshalled model"); - - LOGGER.debug("ran testApexModelWriteReadXML"); - } - - /** * Test write and read in JSON format. * * @throws ApexException on write/read errors @@ -138,7 +89,7 @@ public class TestApexModel<M extends AxModel> { // Write the file to disk File jsonFile; try { - jsonFile = File.createTempFile("ApexModel", ".xml"); + jsonFile = File.createTempFile("ApexModel", ".json"); jsonFile.deleteOnExit(); } catch (final Exception e) { LOGGER.warn(TEMP_FILE_CREATE_ERR_STR, e); @@ -159,7 +110,6 @@ public class TestApexModel<M extends AxModel> { } final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass); - modelWriter.setJsonOutput(true); final var baOutputStream = new ByteArrayOutputStream(); modelWriter.write(model, baOutputStream); diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/xml/AxReferenceKeyAdapter.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/xml/AxReferenceKeyAdapter.java deleted file mode 100644 index 8515f5724..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/xml/AxReferenceKeyAdapter.java +++ /dev/null @@ -1,58 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2021 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.apex.model.basicmodel.xml; - -import java.io.Serializable; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlType; -import javax.xml.bind.annotation.adapters.XmlAdapter; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; - -/** - * This class manages marshaling and unmarshaling of Apex {@link AxReferenceKey} concepts using JAXB. The local name in - * reference keys must have specific handling. - */ -@XmlAccessorType(XmlAccessType.PROPERTY) -@XmlType(namespace = "http://www.onap.org/policy/apex-pdp") -public class AxReferenceKeyAdapter extends XmlAdapter<String, AxReferenceKey> implements Serializable { - - private static final long serialVersionUID = -3480405083900107029L; - - /** - * {@inheritDoc}. - */ - @Override - public final String marshal(final AxReferenceKey key) throws Exception { - return key.getLocalName(); - } - - /** - * {@inheritDoc}. - */ - @Override - public final AxReferenceKey unmarshal(final String key) throws Exception { - final var axReferenceKey = new AxReferenceKey(); - axReferenceKey.setLocalName(key); - return axReferenceKey; - } -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/xml/package-info.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/xml/package-info.java deleted file mode 100644 index 290209d89..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/xml/package-info.java +++ /dev/null @@ -1,34 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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========================================================= - */ - -/** - * Contains utility classes for managing marshaling and unmarshaling of APEX models using JAXB. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ - -@XmlSchema(namespace = "http://www.onap.org/policy/apex-pdp", elementFormDefault = XmlNsForm.QUALIFIED, - xmlns = { @XmlNs(namespaceURI = "http://www.onap.org/policy/apex-pdp", prefix = "") }) - -package org.onap.policy.apex.model.basicmodel.xml; - -import javax.xml.bind.annotation.XmlNs; -import javax.xml.bind.annotation.XmlNsForm; -import javax.xml.bind.annotation.XmlSchema; diff --git a/model/basic-model/src/main/resources/xml/example.xsd b/model/basic-model/src/main/resources/xml/example.xsd deleted file mode 100644 index 75ecdc323..000000000 --- a/model/basic-model/src/main/resources/xml/example.xsd +++ /dev/null @@ -1,100 +0,0 @@ -<?xml version="1.0" standalone="yes"?> -<!-- - ============LICENSE_START======================================================= - Copyright (C) 2016-2018 Ericsson. 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. - 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========================================================= ---> -<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://www.onap.org/policy/apex-pdp" xmlns="http://www.onap.org/policy/apex-pdp" xmlns:tns="http://www.onap.org/policy/apex-pdp" xmlns:xs="http://www.w3.org/2001/XMLSchema"> - - <xs:element name="apexArtifactKey" type="AxArtifactKey"/> - - <xs:element name="apexKeyInfo" type="AxKeyInfo"/> - - <xs:element name="apexModel" type="AxModel"/> - - <xs:complexType name="AxModel"> - <xs:complexContent> - <xs:extension base="AxConcept"> - <xs:sequence> - <xs:element name="key" form="unqualified" type="AxArtifactKey"/> - <xs:element name="keyInformation" type="AxKeyInformation"/> - </xs:sequence> - </xs:extension> - </xs:complexContent> - </xs:complexType> - - <xs:complexType name="AxConcept" abstract="true"> - <xs:sequence/> - </xs:complexType> - - <xs:complexType name="AxArtifactKey"> - <xs:complexContent> - <xs:extension base="axKey"> - <xs:sequence> - <xs:element name="name" type="xs:string"/> - <xs:element name="version" type="xs:string"/> - </xs:sequence> - </xs:extension> - </xs:complexContent> - </xs:complexType> - - <xs:complexType name="axKey" abstract="true"> - <xs:complexContent> - <xs:extension base="AxConcept"> - <xs:sequence/> - </xs:extension> - </xs:complexContent> - </xs:complexType> - - <xs:complexType name="AxKeyInformation"> - <xs:complexContent> - <xs:extension base="AxConcept"> - <xs:sequence> - <xs:element name="key" form="unqualified" type="AxArtifactKey"/> - <xs:element name="keyInfoMap"> - <xs:complexType> - <xs:sequence> - <xs:element name="entry" minOccurs="0" maxOccurs="unbounded" form="unqualified"> - <xs:complexType> - <xs:sequence> - <xs:element name="key" form="unqualified" minOccurs="0" type="AxArtifactKey"/> - <xs:element name="value" form="unqualified" minOccurs="0" type="AxKeyInfo"/> - </xs:sequence> - </xs:complexType> - </xs:element> - </xs:sequence> - </xs:complexType> - </xs:element> - </xs:sequence> - </xs:extension> - </xs:complexContent> - </xs:complexType> - - <xs:complexType name="AxKeyInfo"> - <xs:complexContent> - <xs:extension base="AxConcept"> - <xs:sequence> - <xs:element name="key" form="unqualified" type="AxArtifactKey"/> - <xs:element name="UUID" type="xs:string"/> - <xs:element name="description" type="xs:string"/> - </xs:sequence> - </xs:extension> - </xs:complexContent> - </xs:complexType> -</xs:schema> - - diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/DummyEntity.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/DummyEntity.java index cd8b52ce6..2b16e89ed 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/DummyEntity.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/DummyEntity.java @@ -23,17 +23,11 @@ package org.onap.policy.apex.model.basicmodel.concepts; import java.util.Arrays; import java.util.List; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; -import org.onap.policy.apex.model.basicmodel.xml.AxReferenceKeyAdapter; public class DummyEntity extends AxConcept { private static final long serialVersionUID = -2962570563281067894L; - @XmlElement(name = "key", required = true) - @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class) protected AxReferenceKey key; - private double doubleValue; public DummyEntity() { diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriterTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriterTest.java index 331e57f47..0f8f956ab 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriterTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelFileWriterTest.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020,2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,33 +36,27 @@ public class ApexModelFileWriterTest { public void testModelFileWriter() throws IOException, ApexException { ApexModelFileWriter<AxModel> modelFileWriter = new ApexModelFileWriter<>(true); - modelFileWriter.setValidateFlag(true); - assertTrue(modelFileWriter.isValidateFlag()); + modelFileWriter.setValidate(true); + assertTrue(modelFileWriter.isValidate()); File tempFile = File.createTempFile("ApexFileWriterTest", "test"); File tempDir = tempFile.getParentFile(); File jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/ApexFileWriterTest.json"); - File xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ApexFileWriterTest.xml"); AxModel model = new DummyApexBasicModelCreator().getModel(); modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath()); - modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath()); jsonTempFile.delete(); - xmlTempFile.delete(); new File(tempDir.getAbsolutePath() + "/aaa").delete(); new File(tempDir.getAbsolutePath() + "/ccc").delete(); jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json"); - xmlTempFile = new File(tempDir.getAbsolutePath() + "/ccc/ddd/ApexFileWriterTest.xml"); modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile.getAbsolutePath()); - modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, xmlTempFile.getAbsolutePath()); jsonTempFile.delete(); - xmlTempFile.delete(); new File(tempDir.getAbsolutePath() + "/aaa/bbb").delete(); new File(tempDir.getAbsolutePath() + "/aaa").delete(); @@ -70,19 +64,14 @@ public class ApexModelFileWriterTest { new File(tempDir.getAbsolutePath() + "/ccc").delete(); File dirA = new File(tempDir.getAbsolutePath() + "/aaa"); - //File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb"); + // File dirB = new File(tempDir.getAbsolutePath() + "/aaa/bbb"); dirA.createNewFile(); - //dirB.createNewFile(); + // dirB.createNewFile(); jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json"); - jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml"); final File jsonTempFile01 = jsonTempFile; - assertThatThrownBy(() -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, - jsonTempFile01.getAbsolutePath())) - .hasMessageContaining("could not create directory"); - - assertThatThrownBy(() -> modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, - jsonTempFile01.getAbsolutePath())) + assertThatThrownBy( + () -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile01.getAbsolutePath())) .hasMessageContaining("could not create directory"); dirA.delete(); @@ -93,15 +82,10 @@ public class ApexModelFileWriterTest { fileB.createNewFile(); jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.json"); - jsonTempFile = new File(tempDir.getAbsolutePath() + "/aaa/bbb/ApexFileWriterTest.xml"); File jsonTempFile02 = jsonTempFile; - assertThatThrownBy(() -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, - jsonTempFile02.getAbsolutePath())) - .hasMessageContaining("error processing file"); - - assertThatThrownBy(() -> modelFileWriter.apexModelWriteXmlFile(model, AxModel.class, - jsonTempFile02.getAbsolutePath())) + assertThatThrownBy( + () -> modelFileWriter.apexModelWriteJsonFile(model, AxModel.class, jsonTempFile02.getAbsolutePath())) .hasMessageContaining("error processing file"); fileB.delete(); diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReaderTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReaderTest.java index d75f20253..1152771ea 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReaderTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReaderTest.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020,2022 Nordix Foundation * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -34,45 +34,33 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; -import java.lang.reflect.Field; -import javax.xml.bind.JAXBException; -import javax.xml.bind.PropertyException; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; import org.mockito.runners.MockitoJUnitRunner; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxModel; @RunWith(MockitoJUnitRunner.class) public class ApexModelReaderTest { - @Mock - private Unmarshaller unmarshallerMock; - @Test public void testModelReader() throws IOException, ApexException { AxModel model = new DummyApexBasicModelCreator().getModel(); AxModel invalidModel = new DummyApexBasicModelCreator().getInvalidModel(); ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class); - modelWriter.setValidateFlag(true); - modelWriter.setJsonOutput(true); + modelWriter.setValidate(true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); modelWriter.write(model, baos); ByteArrayOutputStream baosInvalid = new ByteArrayOutputStream(); - modelWriter.setValidateFlag(false); + modelWriter.setValidate(false); modelWriter.write(invalidModel, baosInvalid); ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true); - modelReader.setValidateFlag(true); - assertTrue(modelReader.getValidateFlag()); + modelReader.setValidate(true); + assertTrue(modelReader.isValidate()); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); AxModel readModel = modelReader.read(bais); @@ -81,26 +69,24 @@ public class ApexModelReaderTest { ByteArrayInputStream baisInvalid = new ByteArrayInputStream(baosInvalid.toByteArray()); assertThatThrownBy(() -> modelReader.read(baisInvalid)) .hasMessageStartingWith("Apex concept validation failed"); - modelReader.setValidateFlag(false); - assertFalse(modelReader.getValidateFlag()); + modelReader.setValidate(false); + assertFalse(modelReader.isValidate()); ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray()); AxModel readModel2 = modelReader.read(bais2); assertEquals(model, readModel2); - modelWriter.setJsonOutput(false); - - ByteArrayOutputStream baosXml = new ByteArrayOutputStream(); - modelWriter.write(model, baosXml); + ByteArrayOutputStream baosJson = new ByteArrayOutputStream(); + modelWriter.write(model, baosJson); - ByteArrayInputStream baisXml = new ByteArrayInputStream(baosXml.toByteArray()); - AxModel readModelXml = modelReader.read(baisXml); - assertEquals(model, readModelXml); + ByteArrayInputStream baisJson = new ByteArrayInputStream(baosJson.toByteArray()); + AxModel readModelJson = modelReader.read(baisJson); + assertEquals(model, readModelJson); String dummyString = "SomeDummyText"; ByteArrayInputStream baisDummy = new ByteArrayInputStream(dummyString.getBytes()); assertThatThrownBy(() -> modelReader.read(baisDummy)) - .hasMessage("format of input for Apex concept is neither JSON nor XML"); + .hasMessageContaining("Unable to unmarshal Apex concept"); ByteArrayInputStream nullBais = null; assertThatThrownBy(() -> modelReader.read(nullBais)) .hasMessage("concept stream may not be null"); @@ -115,42 +101,5 @@ public class ApexModelReaderTest { assertThatThrownBy(() -> modelReader.read(br)) .hasMessage("Unable to read Apex concept "); tempFile.delete(); - modelReader.setSchema(null); - - final File tempFileA = File.createTempFile("Apex", "Dummy"); - assertThatThrownBy(() -> modelReader.setSchema(tempFileA.getCanonicalPath())) - .hasMessage("Unable to load schema"); - tempFile.delete(); - modelReader.setSchema("xml/example.xsd"); - } - - @Test - public void testSetInputTypeError() throws ApexModelException, - NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - MockitoAnnotations.initMocks(this); - - ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class, true); - - Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller"); - marshallerField.setAccessible(true); - marshallerField.set(modelReader, unmarshallerMock); - marshallerField.setAccessible(false); - - assertThatThrownBy(() -> { - Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock) - .unmarshal((StreamSource) Mockito.anyObject(), Mockito.anyObject()); - - modelReader.read("{Hello}"); - }).hasMessage("Unable to unmarshal Apex concept "); - assertThatThrownBy(() -> { - Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock) - .setProperty(Mockito.anyString(), Mockito.anyString()); - modelReader.read("{Hello}"); - }).hasMessage("JAXB error setting unmarshaller for JSON input"); - assertThatThrownBy(() -> { - Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock) - .setProperty(Mockito.anyString(), Mockito.anyString()); - modelReader.read("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); - }).hasMessage("JAXB error setting unmarshaller for XML input"); } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelSaverTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelSaverTest.java index ad897832e..c95106aa8 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelSaverTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelSaverTest.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020,2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,14 +41,12 @@ public class ApexModelSaverTest { Path tempPath = Files.createTempDirectory("ApexTest"); assertNotNull(tempPath); - ApexModelSaver<AxModel> modelSaver = new ApexModelSaver<AxModel>(AxModel.class, model, - tempPath.toAbsolutePath().toString()); + ApexModelSaver<AxModel> modelSaver = + new ApexModelSaver<AxModel>(AxModel.class, model, tempPath.toAbsolutePath().toString()); assertNotNull(modelSaver); - modelSaver.apexModelWriteXml(); modelSaver.apexModelWriteJson(); Files.deleteIfExists(new File(tempPath.toAbsolutePath() + "/BasicModel.json").toPath()); - Files.deleteIfExists(new File(tempPath.toAbsolutePath() + "/BasicModel.xml").toPath()); Files.deleteIfExists(tempPath); } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriterTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriterTest.java index 0b8d789ad..13e72d3e2 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriterTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelStringWriterTest.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020,2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,33 +38,26 @@ public class ApexModelStringWriterTest { AxModel basicModel = new DummyApexBasicModelCreator().getModel(); assertNotNull(basicModel); - AxKeyInfo intKeyInfo = basicModel.getKeyInformation().get("IntegerKIKey"); + AxKeyInfo intKeyInfo = basicModel.getKeyInformation().get("IntegerKIKey"); AxKeyInfo floatKeyInfo = basicModel.getKeyInformation().get("FloatKIKey"); // Ensure marshalling is OK ApexModelStringWriter<AxKeyInfo> stringWriter = new ApexModelStringWriter<AxKeyInfo>(true); - assertNotNull(stringWriter.writeJsonString(intKeyInfo, AxKeyInfo.class)); + assertNotNull(stringWriter.writeJsonString(intKeyInfo, AxKeyInfo.class)); assertNotNull(stringWriter.writeJsonString(floatKeyInfo, AxKeyInfo.class)); - assertNotNull(stringWriter.writeString(intKeyInfo, AxKeyInfo.class, true)); - assertNotNull(stringWriter.writeString(floatKeyInfo, AxKeyInfo.class, true)); + assertNotNull(stringWriter.writeString(intKeyInfo, AxKeyInfo.class)); + assertNotNull(stringWriter.writeString(floatKeyInfo, AxKeyInfo.class)); - assertNotNull(stringWriter.writeString(intKeyInfo, AxKeyInfo.class, false)); - assertNotNull(stringWriter.writeString(floatKeyInfo, AxKeyInfo.class, false)); + assertNotNull(stringWriter.writeString(intKeyInfo, AxKeyInfo.class)); + assertNotNull(stringWriter.writeString(floatKeyInfo, AxKeyInfo.class)); - assertNotNull(stringWriter.writeXmlString(intKeyInfo, AxKeyInfo.class)); - assertNotNull(stringWriter.writeXmlString(floatKeyInfo, AxKeyInfo.class)); - - assertThatThrownBy(() -> stringWriter.writeString(null, AxKeyInfo.class, true)) - .hasMessage("concept may not be null"); - assertThatThrownBy(() -> stringWriter.writeString(null, AxKeyInfo.class, false)) - .hasMessage("concept may not be null"); + assertThatThrownBy(() -> stringWriter.writeString(null, AxKeyInfo.class)).hasMessage("concept may not be null"); + assertThatThrownBy(() -> stringWriter.writeString(null, AxKeyInfo.class)).hasMessage("concept may not be null"); assertThatThrownBy(() -> stringWriter.writeJsonString(null, AxKeyInfo.class)) .hasMessage("error writing JSON string"); - assertThatThrownBy(() -> stringWriter.writeXmlString(null, AxKeyInfo.class)) - .hasMessage("error writing XML string"); - stringWriter.setValidateFlag(true); - assertTrue(stringWriter.isValidateFlag()); + stringWriter.setValidate(true); + assertTrue(stringWriter.isValidate()); } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriterTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriterTest.java index 147eb206a..063855976 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriterTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelWriterTest.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020,2022 Nordix Foundation * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,65 +23,38 @@ package org.onap.policy.apex.model.basicmodel.handling; 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.assertTrue; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.Writer; -import java.lang.reflect.Field; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.PropertyException; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; import org.mockito.runners.MockitoJUnitRunner; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxModel; -import org.w3c.dom.Document; @RunWith(MockitoJUnitRunner.class) public class ApexModelWriterTest { - @Mock - private Marshaller marshallerMock; - @Test public void testModelWriter() throws IOException, ApexException { ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class); - modelWriter.setValidateFlag(true); - assertTrue(modelWriter.getValidateFlag()); - assertEquals(0, modelWriter.getCDataFieldSet().size()); - - assertFalse(modelWriter.isJsonOutput()); - modelWriter.setJsonOutput(true); - assertTrue(modelWriter.isJsonOutput()); - modelWriter.setJsonOutput(false); - assertFalse(modelWriter.isJsonOutput()); + modelWriter.setValidate(true); + assertTrue(modelWriter.isValidate()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); AxModel model = new DummyApexBasicModelCreator().getModel(); modelWriter.write(model, baos); - modelWriter.setJsonOutput(true); - modelWriter.write(model, baos); - modelWriter.setJsonOutput(false); - modelWriter.setValidateFlag(false); + modelWriter.setValidate(false); modelWriter.write(model, baos); - modelWriter.setJsonOutput(true); - modelWriter.write(model, baos); - modelWriter.setJsonOutput(false); - modelWriter.setValidateFlag(true); + modelWriter.setValidate(true); model.getKeyInformation().getKeyInfoMap().clear(); assertThatThrownBy(() -> modelWriter.write(model, baos)) - .hasMessageContaining("Apex concept xml (BasicModel:0.0.1) validation failed"); + .hasMessageContaining("Apex concept (BasicModel:0.0.1) validation failed"); model.getKeyInformation().generateKeyInfo(model); assertThatThrownBy(() -> modelWriter.write(null, baos)) @@ -91,73 +64,4 @@ public class ApexModelWriterTest { assertThatThrownBy(() -> modelWriter.write(model, nullBaos)) .hasMessage("concept stream may not be null"); } - - @Test - public void testSetOutputTypeError() throws ApexModelException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException, PropertyException { - MockitoAnnotations.initMocks(this); - - ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class); - - Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); - marshallerField.setAccessible(true); - marshallerField.set(modelWriter, marshallerMock); - marshallerField.setAccessible(false); - Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock) - .setProperty(Mockito.anyString(), Mockito.anyString()); - assertThatThrownBy(() -> modelWriter.setJsonOutput(true)) - .hasMessage("JAXB error setting marshaller for JSON output"); - Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock) - .setProperty(Mockito.anyString(), Mockito.anyString()); - assertThatThrownBy(() -> modelWriter.setJsonOutput(false)) - .hasMessage("JAXB error setting marshaller for XML output"); - } - - @Test - public void testOutputJsonError() throws ApexModelException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException, JAXBException { - MockitoAnnotations.initMocks(this); - - ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class); - - Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); - marshallerField.setAccessible(true); - marshallerField.set(modelWriter, marshallerMock); - marshallerField.setAccessible(false); - - modelWriter.setValidateFlag(false); - modelWriter.setJsonOutput(true); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - AxModel model = new DummyApexBasicModelCreator().getModel(); - Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock) - .marshal((AxModel) Mockito.anyObject(), (Writer) Mockito.anyObject()); - assertThatThrownBy(() -> modelWriter.write(model, baos)).hasMessage("Unable to marshal Apex concept to JSON"); - } - - @Test - public void testOutputXmlError() throws ApexModelException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException, JAXBException { - MockitoAnnotations.initMocks(this); - - ApexModelWriter<AxModel> modelWriter = new ApexModelWriter<AxModel>(AxModel.class); - modelWriter.setJsonOutput(false); - - Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); - marshallerField.setAccessible(true); - marshallerField.set(modelWriter, marshallerMock); - marshallerField.setAccessible(false); - - modelWriter.setValidateFlag(false); - modelWriter.setJsonOutput(false); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - AxModel model = new DummyApexBasicModelCreator().getModel(); - - Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(marshallerMock) - .marshal((AxModel) Mockito.anyObject(), (Document) Mockito.anyObject()); - - assertThatThrownBy(() -> modelWriter.write(model, baos)) - .hasMessage("Unable to marshal Apex concept to XML"); - } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexSchemaGeneratorTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexSchemaGeneratorTest.java deleted file mode 100644 index 3385830e2..000000000 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/ApexSchemaGeneratorTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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.apex.model.basicmodel.handling; - -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileDescriptor; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import org.junit.After; -import org.junit.Test; - -public class ApexSchemaGeneratorTest { - private final PrintStream stdout = System.out; - - @After - public void tearDown() throws Exception { - System.setOut(stdout); - } - - @Test - public void test() throws IOException { - final ByteArrayOutputStream baos0 = new ByteArrayOutputStream(); - System.setOut(new PrintStream(baos0)); - - final String[] args0 = {}; - ApexSchemaGenerator.main(args0); - assertTrue(baos0.toString().contains("usage: ApexSchemaGenerator apex-root-class [schema-file-name]")); - System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); - - final ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); - System.setOut(new PrintStream(baos1)); - - final String[] args1 = { "hello", "goodbye", "here" }; - ApexSchemaGenerator.main(args1); - assertTrue(baos1.toString().contains("usage: ApexSchemaGenerator apex-root-class [schema-file-name]")); - System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); - - final ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); - System.setOut(new PrintStream(baos2)); - - final String[] args2 = { "hello", "goodbye" }; - ApexSchemaGenerator.main(args2); - assertTrue(baos2.toString().contains("error on Apex schema output")); - System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); - - final ByteArrayOutputStream baos3 = new ByteArrayOutputStream(); - System.setOut(new PrintStream(baos3)); - - final String[] args3 = { "hello" }; - ApexSchemaGenerator.main(args3); - assertTrue(baos3.toString().contains("could not create JAXB context, root class hello not found")); - System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); - - final ByteArrayOutputStream baos4 = new ByteArrayOutputStream(); - System.setOut(new PrintStream(baos4)); - - final String[] args4 = { "org.onap.policy.apex.model.basicmodel.concepts.AxModel" }; - ApexSchemaGenerator.main(args4); - assertTrue(baos4.toString().contains("targetNamespace=\"http://www.onap.org/policy/apex-pdp\"")); - System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); - - final ByteArrayOutputStream baos5 = new ByteArrayOutputStream(); - System.setOut(new PrintStream(baos5)); - - final File tempFile = File.createTempFile("ApexSchemaGeneratorTest", "xsd"); - tempFile.deleteOnExit(); - final String[] args5 = - { "org.onap.policy.apex.model.basicmodel.concepts.AxModel", tempFile.getCanonicalPath() }; - - ApexSchemaGenerator.main(args5); - assertTrue(tempFile.length() > 100); - } -} diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexBasicModelTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexBasicModelTest.java index bb9776fc7..68f755886 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexBasicModelTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexBasicModelTest.java @@ -110,11 +110,6 @@ public class SupportApexBasicModelTest { } @Test - public void testModelWriteReadXml() throws Exception { - testApexModel.testApexModelWriteReadXml(); - } - - @Test public void testModelWriteReadJson() throws Exception { testApexModel.testApexModelWriteReadJson(); } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTest.java index 5fc678ac0..d55a9da29 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020-2021 Nordix Foundation + * Modifications Copyright (C) 2020-2022 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,6 @@ public class SupportBasicModelTest { testApexModel.testApexModelVaidateMalstructured(); testApexModel.testApexModelWriteReadJson(); - testApexModel.testApexModelWriteReadXml(); } @Test @@ -102,14 +101,11 @@ public class SupportBasicModelTest { } @Test - public void testModelCreator1XmlJson() throws ApexException { + public void testModelCreator1Json() throws ApexException { final TestApexModel<AxModel> testApexModel = new TestApexModel<AxModel>(AxModel.class, new SupportApexModelCreator1()); assertThatThrownBy(() -> testApexModel.testApexModelWriteReadJson()) .hasMessageStartingWith("error processing file"); - - assertThatThrownBy(() -> testApexModel.testApexModelWriteReadXml()) - .hasMessageStartingWith("error processing file"); } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportConceptGetterTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportConceptGetterTest.java index 006c583ec..1783fc00f 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportConceptGetterTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportConceptGetterTest.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020-2021 Nordix Foundation. + * Modifications Copyright (C) 2020-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -156,19 +156,19 @@ public class SupportConceptGetterTest { ApexModelReader<AxModel> modelReader = new ApexModelReader<AxModel>(AxModel.class); ApexModelFileWriter<AxModel> modelWriter = new ApexModelFileWriter<AxModel>(true); - modelReader.setValidateFlag(false); - modelWriter.setValidateFlag(false); + modelReader.setValidate(false); + modelWriter.setValidate(false); - File tempXmlFile = File.createTempFile("ApexModel", "xml"); - modelWriter.apexModelWriteJsonFile(basicModel, AxModel.class, tempXmlFile.getCanonicalPath()); + File tempJsonFile = File.createTempFile("ApexModel", "json"); + modelWriter.apexModelWriteJsonFile(basicModel, AxModel.class, tempJsonFile.getCanonicalPath()); - FileInputStream xmlFileInputStream = new FileInputStream(tempXmlFile); - AxModel readXmlModel = modelReader.read(xmlFileInputStream); - xmlFileInputStream.close(); - assertEquals(basicModel, readXmlModel); - assertEquals(intKI91, readXmlModel.getKeyInformation().get("IntegerKIKey91")); - assertNotNull(readXmlModel.getKeyInformation().get("FloatKIKey")); - tempXmlFile.delete(); + FileInputStream jsonFileInputStream = new FileInputStream(tempJsonFile); + AxModel readJsonModel = modelReader.read(jsonFileInputStream); + jsonFileInputStream.close(); + assertEquals(basicModel, readJsonModel); + assertEquals(intKI91, readJsonModel.getKeyInformation().get("IntegerKIKey91")); + assertNotNull(readJsonModel.getKeyInformation().get("FloatKIKey")); + tempJsonFile.delete(); } private AxModel setTestBasicModel() { diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/xml/AxReferenceKeyAdapterTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/xml/AxReferenceKeyAdapterTest.java deleted file mode 100644 index 12a67c803..000000000 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/xml/AxReferenceKeyAdapterTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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.apex.model.basicmodel.xml; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; - -public class AxReferenceKeyAdapterTest { - - @Test - public void test() throws Exception { - AxReferenceKeyAdapter arka = new AxReferenceKeyAdapter(); - assertNotNull(arka); - - AxReferenceKey rkey = new AxReferenceKey("Name", "0.0.1", "PLN", "LN"); - - String rkeyString = arka.marshal(rkey); - assertEquals("LN", rkeyString); - assertEquals(rkey.getLocalName(), arka.unmarshal(rkeyString).getLocalName()); - } -} |