From 8e8f3458646ec97d058d7322917f56021de2dc7f Mon Sep 17 00:00:00 2001 From: liamfallon Date: Sat, 15 Dec 2018 09:47:58 +0000 Subject: Fix sonar test renaming issues Renaming tests introduced Sonar issues: - Tests where coverage was <80% flagged as they are new code - Support test classes with names ending in "Test" flagged becasue they did not contain test cases Issue-ID: POLICY-1263 Change-Id: I17a4b27b2952a4978fab1c76579a013478b60ae1 Signed-off-by: liamfallon --- model/basic-model/pom.xml | 5 + .../model/basicmodel/handling/ApexModelReader.java | 4 +- .../model/basicmodel/handling/ApexModelWriter.java | 8 +- .../apex/model/basicmodel/test/TestApexModel.java | 44 +- .../concepts/AxConceptGetterImplTest.java | 96 +++++ .../model/basicmodel/concepts/AxKeyInfoTest.java | 1 + .../apex/model/basicmodel/concepts/AxKeyTest.java | 40 ++ .../basicmodel/concepts/AxReferenceKeyTest.java | 101 ++++- .../basicmodel/handling/ApexModelReaderTest.java | 55 +++ .../basicmodel/handling/ApexModelWriterTest.java | 111 +++++ .../handling/SupportApexModelCreator0.java | 64 +++ .../handling/SupportApexModelCreator1.java | 69 ++++ .../handling/SupportApexModelCreator2.java | 64 +++ .../handling/SupportBasicModelTester.java | 28 +- .../handling/TestApexModelCreator0Test.java | 64 --- .../handling/TestApexModelCreator1Test.java | 69 ---- .../handling/TestApexModelCreator2Test.java | 64 --- .../model/policymodel/concepts/PoliciesTest.java | 4 +- .../policymodel/concepts/PolicyModelTest.java | 4 +- .../handling/ApexPolicyModelCreatorTest.java | 455 --------------------- .../policymodel/handling/ApexPolicyModelTest.java | 2 +- .../policymodel/handling/PolicyAnalyserTest.java | 2 +- .../handling/PolicyModelComparerTest.java | 2 +- .../handling/PolicyModelMergerTest.java | 6 +- .../handling/PolicyModelSplitterTest.java | 2 +- .../handling/SupportApexPolicyModelCreator.java | 455 +++++++++++++++++++++ 26 files changed, 1123 insertions(+), 696 deletions(-) create mode 100644 model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxConceptGetterImplTest.java create mode 100644 model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator0.java create mode 100644 model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator1.java create mode 100644 model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator2.java delete mode 100644 model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator0Test.java delete mode 100644 model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator1Test.java delete mode 100644 model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator2Test.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelCreatorTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/SupportApexPolicyModelCreator.java (limited to 'model') diff --git a/model/basic-model/pom.xml b/model/basic-model/pom.xml index 3f337590c..ca90e25dd 100644 --- a/model/basic-model/pom.xml +++ b/model/basic-model/pom.xml @@ -45,6 +45,11 @@ org.eclipse.persistence eclipselink + + org.mockito + mockito-all + test + 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 e76d00e12..f340f73d4 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 @@ -260,7 +260,7 @@ public class ApexModelReader { } catch (final Exception e) { LOGGER.warn("JAXB error setting marshaller for JSON Input", e); - throw new ApexModelException("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()) { @@ -270,7 +270,7 @@ public class ApexModelReader { } catch (final Exception e) { LOGGER.warn("JAXB error setting marshaller for XML Input", e); - throw new ApexModelException("JAXB error setting marshaller for XML Input", e); + throw new ApexModelException("JAXB error setting unmarshaller for XML input", e); } } else { 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 6cc542270..eded9fdec 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 @@ -217,8 +217,8 @@ public class ApexModelWriter { cdataFieldSet.toString().replaceAll("[\\[\\]\\,]", " ")); domTransformer.transform(new DOMSource(document), new StreamResult(apexConceptWriter)); } catch (JAXBException | TransformerException | ParserConfigurationException e) { - LOGGER.warn("Unable to marshal Apex concept XML", e); - throw new ApexModelException("Unable to marshal Apex concept XML", e); + LOGGER.warn("Unable to marshal Apex concept to XML", e); + throw new ApexModelException("Unable to marshal Apex concept to XML", e); } LOGGER.debug("wrote Apex concept XML"); } @@ -254,8 +254,8 @@ public class ApexModelWriter { try { marshaller.marshal(concept, apexConceptWriter); } catch (final JAXBException e) { - LOGGER.warn("Unable to marshal Apex concept JSON", e); - throw new ApexModelException("Unable to marshal Apex concept JSON", e); + LOGGER.warn("Unable to marshal Apex concept to JSON", e); + throw new ApexModelException("Unable to marshal Apex concept to JSON", e); } LOGGER.debug("wrote Apex concept JSON"); } 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 24c682de4..6bab169a8 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 @@ -108,10 +108,7 @@ public class TestApexModel { try { final URL apexModelUrl = ResourceUtils.getLocalFile(xmlFile.getAbsolutePath()); final M fileModel = modelReader.read(apexModelUrl.openStream()); - if (!model.equals(fileModel)) { - LOGGER.warn(TEST_MODEL_UNEQUAL_STR + xmlFile.getAbsolutePath()); - throw new ApexException(TEST_MODEL_UNEQUAL_STR + xmlFile.getAbsolutePath()); - } + 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); @@ -126,10 +123,8 @@ public class TestApexModel { modelWriter.write(model, baOutputStream); final ByteArrayInputStream baInputStream = new ByteArrayInputStream(baOutputStream.toByteArray()); final M byteArrayModel = modelReader.read(baInputStream); - if (!model.equals(byteArrayModel)) { - LOGGER.warn("test model does not equal XML marshalled and unmarshalled model"); - throw new ApexException("test model does not equal XML marshalled and unmarshalled model"); - } + + checkModelEquality(model, byteArrayModel, "test model does not equal XML marshalled and unmarshalled model"); LOGGER.debug("ran testApexModelWriteReadXML"); } @@ -161,11 +156,7 @@ public class TestApexModel { try { final URL apexModelUrl = ResourceUtils.getLocalFile(jsonFile.getAbsolutePath()); final M fileModel = modelReader.read(apexModelUrl.openStream()); - if (!model.equals(fileModel)) { - LOGGER.warn(TEST_MODEL_UNEQUAL_STR + jsonFile.getAbsolutePath()); - throw new ApexException( - TEST_MODEL_UNEQUAL_STR + jsonFile.getAbsolutePath()); - } + checkModelEquality(model, fileModel, TEST_MODEL_UNEQUAL_STR + jsonFile.getAbsolutePath()); } catch (final Exception e) { LOGGER.warn(ERROR_PROCESSING_FILE + jsonFile.getAbsolutePath(), e); throw new ApexException(ERROR_PROCESSING_FILE + jsonFile.getAbsolutePath(), e); @@ -178,10 +169,8 @@ public class TestApexModel { modelWriter.write(model, baOutputStream); final ByteArrayInputStream baInputStream = new ByteArrayInputStream(baOutputStream.toByteArray()); final M byteArrayModel = modelReader.read(baInputStream); - if (!model.equals(byteArrayModel)) { - LOGGER.warn("test model does not equal JSON marshalled and unmarshalled model"); - throw new ApexException("test model does not equal JSON marshalled and unmarshalled model"); - } + + checkModelEquality(model, byteArrayModel, "test model does not equal JSON marshalled and unmarshalled model"); LOGGER.debug("ran testApexModelWriteReadJSON"); } @@ -204,10 +193,7 @@ public class TestApexModel { final M dbJpaModel = apexDao.get(rootModelClass, model.getKey()); apexDao.close(); - if (!model.equals(dbJpaModel)) { - LOGGER.warn("test model does not equal model written and read using generic JPA"); - throw new ApexException("test model does not equal model written and read using generic JPA"); - } + checkModelEquality(model, dbJpaModel, "test model does not equal model written and read using generic JPA"); LOGGER.debug("ran testApexModelWriteReadJPA"); } @@ -331,4 +317,20 @@ public class TestApexModel { LOGGER.debug("ran testApexModelVaidateInvalidModel"); return result; } + + /** + * Check if two models are equal. + * + * @param leftModel the left model + * @param rightModel the right model + * @param errorMessage the error message to output on inequality + * @throws ApexException the exception to throw on inequality + */ + public void checkModelEquality(final M leftModel, final M rightModel, final String errorMessage) + throws ApexException { + if (!leftModel.equals(rightModel)) { + LOGGER.warn(errorMessage); + throw new ApexException(errorMessage); + } + } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxConceptGetterImplTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxConceptGetterImplTest.java new file mode 100644 index 000000000..b00e7db9f --- /dev/null +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxConceptGetterImplTest.java @@ -0,0 +1,96 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.TreeSet; + +import org.junit.Test; + +/** + * Test the AxConceptGetterImpl class. + */ +public class AxConceptGetterImplTest { + + @Test + public void testAxConceptGetterImpl() { + NavigableMap keyMap = new TreeMap<>(); + + AxConceptGetterImpl getter = new AxConceptGetterImpl<>(keyMap); + assertNotNull(getter); + + AxArtifactKey keyA = new AxArtifactKey("A", "0.0.1"); + assertNull(getter.get(keyA)); + + try { + getter.get((String)null); + fail("test should throw an exception here"); + } + catch (Exception getException) { + assertEquals("conceptKeyName may not be null", getException.getMessage()); + } + + assertNull(getter.get("W")); + + AxArtifactKey keyZ = new AxArtifactKey("Z", "0.0.1"); + keyMap.put(keyZ, keyZ); + assertNull(getter.get("W")); + + AxArtifactKey keyW001 = new AxArtifactKey("W", "0.0.1"); + keyMap.put(keyW001, keyW001); + assertEquals(keyW001, getter.get("W")); + + AxArtifactKey keyW002 = new AxArtifactKey("W", "0.0.2"); + keyMap.put(keyW002, keyW002); + assertEquals(keyW002, getter.get("W")); + + keyMap.remove(keyZ); + assertEquals(keyW002, getter.get("W")); + + try { + getter.get((String)null, "0.0.1"); + fail("test should throw an exception here"); + } + catch (Exception getException) { + assertEquals("conceptKeyName may not be null", getException.getMessage()); + } + + assertEquals(keyW002, getter.get("W", "0.0.2")); + assertEquals(keyW002, getter.get("W", (String)null)); + + assertEquals(new TreeSet(keyMap.values()), getter.getAll(null)); + assertEquals(new TreeSet(keyMap.values()), getter.getAll(null, null)); + + assertEquals(keyW001, getter.getAll("W", null).iterator().next()); + assertEquals(keyW002, getter.getAll("W", "0.0.2").iterator().next()); + assertEquals(0, getter.getAll("A", null).size()); + assertEquals(0, getter.getAll("Z", null).size()); + + keyMap.put(keyZ, keyZ); + assertEquals(keyW002, getter.getAll("W", "0.0.2").iterator().next()); + } +} diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfoTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfoTest.java index 3f1635cce..b00be2fd1 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfoTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfoTest.java @@ -44,6 +44,7 @@ public class AxKeyInfoTest { AxKeyInfo testKeyInfo = new AxKeyInfo(); testKeyInfo.setKey((new AxArtifactKey("PN", "0.0.1"))); assertEquals("PN:0.0.1", testKeyInfo.getKey().getId()); + assertTrue(testKeyInfo.matchesId("PN:0.0.1")); AxArtifactKey key = new AxArtifactKey("key", "0.0.1"); testKeyInfo.setKey(key); diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java index ea09cc69d..c3c87b965 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java @@ -26,6 +26,8 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.lang.reflect.Field; + import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxKey; @@ -124,4 +126,42 @@ public class AxKeyTest { assertFalse(((AxKey) someKey0).equals(new AxReferenceKey())); } + + @Test + public void testValidation() { + AxArtifactKey testKey = new AxArtifactKey("TheKey", "0.0.1"); + assertEquals("TheKey:0.0.1", testKey.getId()); + + try { + Field nameField = testKey.getClass().getDeclaredField("name"); + nameField.setAccessible(true); + nameField.set(testKey, "Key Name"); + AxValidationResult validationResult = new AxValidationResult(); + testKey.validate(validationResult); + nameField.set(testKey, "TheKey"); + nameField.setAccessible(false); + assertEquals( + "name invalid-parameter name with value Key Name " + + "does not match regular expression [A-Za-z0-9\\-_\\.]+", + validationResult.getMessageList().get(0).getMessage()); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + try { + Field versionField = testKey.getClass().getDeclaredField("version"); + versionField.setAccessible(true); + versionField.set(testKey, "Key Version"); + AxValidationResult validationResult = new AxValidationResult(); + testKey.validate(validationResult); + versionField.set(testKey, "0.0.1"); + versionField.setAccessible(false); + assertEquals( + "version invalid-parameter version with value Key Version " + + "does not match regular expression [A-Za-z0-9.]+", + validationResult.getMessageList().get(0).getMessage()); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKeyTest.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKeyTest.java index 68be4cdbb..27726a9b4 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKeyTest.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxReferenceKeyTest.java @@ -25,6 +25,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; import org.junit.Test; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; @@ -83,7 +86,7 @@ public class AxReferenceKeyTest { AxReferenceKey clonedReferenceKey = new AxReferenceKey(testReferenceKey); assertEquals("AxReferenceKey:(parentKeyName=NPKN,parentKeyVersion=0.0.1,parentLocalName=NPKLN,localName=NLN)", - clonedReferenceKey.toString()); + clonedReferenceKey.toString()); assertFalse(testReferenceKey.hashCode() == 0); @@ -106,5 +109,101 @@ public class AxReferenceKeyTest { assertEquals(0, testReferenceKey.compareTo(new AxReferenceKey("NPKN", "0.0.1", "NPKLN", "NLN"))); assertNotNull(testReferenceKey.getKeys()); + + try { + testReferenceKey.equals(null); + fail("test should throw an exception here"); + } catch (Exception iae) { + assertEquals("comparison object may not be null", iae.getMessage()); + } + + try { + testReferenceKey.copyTo(null); + fail("test should throw an exception here"); + } catch (Exception iae) { + assertEquals("target may not be null", iae.getMessage()); + } + + try { + testReferenceKey.copyTo(new AxArtifactKey("Key", "0.0.1")); + fail("test should throw an exception here"); + } catch (Exception iae) { + assertEquals("org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey is not an instance of " + + "org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey", iae.getMessage()); + } + + AxReferenceKey targetRefKey = new AxReferenceKey(); + assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey)); + } + + @Test + public void testValidation() { + AxReferenceKey testReferenceKey = new AxReferenceKey(); + testReferenceKey.setParentArtifactKey(new AxArtifactKey("PN", "0.0.1")); + assertEquals("PN:0.0.1", testReferenceKey.getParentArtifactKey().getId()); + + try { + Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName"); + parentNameField.setAccessible(true); + parentNameField.set(testReferenceKey, "Parent Name"); + AxValidationResult validationResult = new AxValidationResult(); + testReferenceKey.validate(validationResult); + parentNameField.set(testReferenceKey, "ParentName"); + parentNameField.setAccessible(false); + assertEquals( + "parentKeyName invalid-parameter parentKeyName with value Parent Name " + + "does not match regular expression [A-Za-z0-9\\-_\\.]+", + validationResult.getMessageList().get(0).getMessage()); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + try { + Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion"); + parentVersionField.setAccessible(true); + parentVersionField.set(testReferenceKey, "Parent Version"); + AxValidationResult validationResult = new AxValidationResult(); + testReferenceKey.validate(validationResult); + parentVersionField.set(testReferenceKey, "0.0.1"); + parentVersionField.setAccessible(false); + assertEquals( + "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version " + + "does not match regular expression [A-Za-z0-9.]+", + validationResult.getMessageList().get(0).getMessage()); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + try { + Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName"); + parentLocalNameField.setAccessible(true); + parentLocalNameField.set(testReferenceKey, "Parent Local Name"); + AxValidationResult validationResult = new AxValidationResult(); + testReferenceKey.validate(validationResult); + parentLocalNameField.set(testReferenceKey, "ParentLocalName"); + parentLocalNameField.setAccessible(false); + assertEquals( + "parentLocalName invalid-parameter parentLocalName with value " + + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$", + validationResult.getMessageList().get(0).getMessage()); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + try { + Field localNameField = testReferenceKey.getClass().getDeclaredField("localName"); + localNameField.setAccessible(true); + localNameField.set(testReferenceKey, "Local Name"); + AxValidationResult validationResult = new AxValidationResult(); + testReferenceKey.validate(validationResult); + localNameField.set(testReferenceKey, "LocalName"); + localNameField.setAccessible(false); + assertEquals( + "localName invalid-parameter localName with value Local Name " + + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$", + validationResult.getMessageList().get(0).getMessage()); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } } } 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 20815ae72..d560f62fe 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 @@ -32,14 +32,25 @@ 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.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxModel; import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter; public class ApexModelReaderTest { + @Mock + private Unmarshaller unmarshallerMock; @Test public void testModelReader() throws IOException, ApexException { @@ -149,4 +160,48 @@ public class ApexModelReaderTest { modelReader.setSchema("xml/example.xsd"); } + + @Test + public void testSetInputTypeError() throws ApexModelException { + MockitoAnnotations.initMocks(this); + + ApexModelReader modelReader = new ApexModelReader(AxModel.class, true); + + try { + Field marshallerField = modelReader.getClass().getDeclaredField("unmarshaller"); + marshallerField.setAccessible(true); + marshallerField.set(modelReader, unmarshallerMock); + marshallerField.setAccessible(false); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + try { + Mockito.doThrow(new JAXBException("Exception marshalling to JSON")).when(unmarshallerMock) + .unmarshal((StreamSource)Mockito.anyObject(), Mockito.anyObject()); + + modelReader.read("{Hello}"); + fail("Test should throw an exception here"); + } catch (Exception jaxbe) { + assertEquals("Unable to unmarshal Apex concept ", jaxbe.getMessage()); + } + + try { + Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock) + .setProperty(Mockito.anyString(), Mockito.anyString()); + modelReader.read("{Hello}"); + fail("Test should throw an exception here"); + } catch (Exception jaxbe) { + assertEquals("JAXB error setting unmarshaller for JSON input", jaxbe.getMessage()); + } + + try { + Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(unmarshallerMock) + .setProperty(Mockito.anyString(), Mockito.anyString()); + modelReader.read(""); + fail("Test should throw an exception here"); + } catch (Exception jaxbe) { + assertEquals("JAXB error setting unmarshaller for XML input", jaxbe.getMessage()); + } + } } 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 d7ff4c08b..d25e191a8 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 @@ -27,13 +27,25 @@ import static org.junit.Assert.fail; 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.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxModel; import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter; +import org.w3c.dom.Document; public class ApexModelWriterTest { + @Mock + private Marshaller marshallerMock; @Test public void testModelWriter() throws IOException, ApexException { @@ -89,4 +101,103 @@ public class ApexModelWriterTest { assertEquals("concept stream may not be null", e.getMessage()); } } + + @Test + public void testSetOutputTypeError() throws ApexModelException { + MockitoAnnotations.initMocks(this); + + ApexModelWriter modelWriter = new ApexModelWriter(AxModel.class); + + try { + Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); + marshallerField.setAccessible(true); + marshallerField.set(modelWriter, marshallerMock); + marshallerField.setAccessible(false); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + try { + Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock) + .setProperty(Mockito.anyString(), Mockito.anyString()); + modelWriter.setJsonOutput(true); + fail("Test should throw an exception here"); + } catch (Exception jaxbe) { + assertEquals("JAXB error setting marshaller for JSON output", jaxbe.getMessage()); + } + + try { + Mockito.doThrow(new PropertyException("Exception setting JAXB property")).when(marshallerMock) + .setProperty(Mockito.anyString(), Mockito.anyString()); + modelWriter.setJsonOutput(false); + fail("Test should throw an exception here"); + } catch (Exception jaxbe) { + assertEquals("JAXB error setting marshaller for XML output", jaxbe.getMessage()); + } + } + + @Test + public void testOutputJsonError() throws ApexModelException { + MockitoAnnotations.initMocks(this); + + ApexModelWriter modelWriter = new ApexModelWriter(AxModel.class); + + try { + Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); + marshallerField.setAccessible(true); + marshallerField.set(modelWriter, marshallerMock); + marshallerField.setAccessible(false); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + modelWriter.setValidateFlag(false); + modelWriter.setJsonOutput(true); + + try { + 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()); + + modelWriter.write(model, baos); + fail("Test should throw an exception here"); + } catch (Exception jaxbe) { + assertEquals("Unable to marshal Apex concept to JSON", jaxbe.getMessage()); + } + } + + @Test + public void testOutputXmlError() throws ApexModelException { + MockitoAnnotations.initMocks(this); + + ApexModelWriter modelWriter = new ApexModelWriter(AxModel.class); + modelWriter.setJsonOutput(false); + + try { + Field marshallerField = modelWriter.getClass().getDeclaredField("marshaller"); + marshallerField.setAccessible(true); + marshallerField.set(modelWriter, marshallerMock); + marshallerField.setAccessible(false); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + modelWriter.setValidateFlag(false); + modelWriter.setJsonOutput(false); + + try { + 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()); + + modelWriter.write(model, baos); + fail("Test should throw an exception here"); + } catch (Exception jaxbe) { + assertEquals("Unable to marshal Apex concept to XML", jaxbe.getMessage()); + } + } } diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator0.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator0.java new file mode 100644 index 000000000..e835bc0fc --- /dev/null +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator0.java @@ -0,0 +1,64 @@ +/* + * ============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 org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; +import org.onap.policy.apex.model.basicmodel.concepts.AxModel; +import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; + +public class SupportApexModelCreator0 implements TestApexModelCreator { + + @Override + public AxModel getModel() { + AxModel basicModel = new AxModel(); + + basicModel.setKey(new AxArtifactKey("BasicModel", "0.0.1")); + basicModel.setKeyInformation(new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1"))); + + basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKey(), new AxKeyInfo(basicModel.getKey())); + basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKeyInformation().getKey(), + new AxKeyInfo(basicModel.getKeyInformation().getKey())); + + return basicModel; + } + + @Override + public final AxModel getMalstructuredModel() { + return getModel(); + } + + @Override + public final AxModel getObservationModel() { + return getModel(); + } + + @Override + public final AxModel getWarningModel() { + return getModel(); + } + + @Override + public final AxModel getInvalidModel() { + return getModel(); + } +} diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator1.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator1.java new file mode 100644 index 000000000..123125bb1 --- /dev/null +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator1.java @@ -0,0 +1,69 @@ +/* + * ============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 java.util.UUID; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; +import org.onap.policy.apex.model.basicmodel.concepts.AxModel; +import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; + +public class SupportApexModelCreator1 implements TestApexModelCreator { + + @Override + public AxModel getModel() { + return getInvalidModel(); + } + + @Override + public final AxModel getMalstructuredModel() { + return getInvalidModel(); + } + + @Override + public final AxModel getObservationModel() { + return getInvalidModel(); + } + + @Override + public final AxModel getWarningModel() { + return getInvalidModel(); + } + + @Override + public final AxModel getInvalidModel() { + AxModel basicModel = new AxModel(); + + basicModel.setKey(new AxArtifactKey("BasicModelKey", "0.0.1")); + basicModel.setKeyInformation(new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1"))); + + basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKey(), new AxKeyInfo(basicModel.getKey(), + UUID.fromString("00000000-0000-0000-0000-000000000000"), + "nbasic model description\nThis is a multi line description\nwith another line of text.")); + basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKeyInformation().getKey(), + new AxKeyInfo(basicModel.getKeyInformation().getKey(), + UUID.fromString("00000000-0000-0000-0000-000000000000"), "")); + + return basicModel; + } +} diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator2.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator2.java new file mode 100644 index 000000000..0ab7aab7f --- /dev/null +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportApexModelCreator2.java @@ -0,0 +1,64 @@ +/* + * ============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 org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; +import org.onap.policy.apex.model.basicmodel.concepts.AxModel; +import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; + +public class SupportApexModelCreator2 implements TestApexModelCreator { + + @Override + public AxModel getModel() { + AxModel basicModel = new AxModel(); + + basicModel.setKey(new AxArtifactKey("BasicModel", "0.0.1")); + basicModel.setKeyInformation(new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1"))); + + basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKey(), new AxKeyInfo(basicModel.getKey())); + basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKeyInformation().getKey(), + new AxKeyInfo(basicModel.getKeyInformation().getKey())); + basicModel.getKeyInformation().get("BasicModel").setDescription(""); + return basicModel; + } + + @Override + public final AxModel getMalstructuredModel() { + return getModel(); + } + + @Override + public final AxModel getObservationModel() { + return getModel(); + } + + @Override + public final AxModel getWarningModel() { + return getModel(); + } + + @Override + public final AxModel getInvalidModel() { + return getModel(); + } +} diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTester.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTester.java index e632a521e..57df96089 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTester.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/SupportBasicModelTester.java @@ -34,7 +34,7 @@ public class SupportBasicModelTester { @Test public void testNormalModelCreator() throws ApexException { final TestApexModel testApexModel = new TestApexModel(AxModel.class, - new DummyApexBasicModelCreator()); + new DummyApexBasicModelCreator()); testApexModel.testApexModelValid(); try { @@ -51,10 +51,28 @@ public class SupportBasicModelTester { testApexModel.testApexModelWriteReadXml(); } + @Test + public void testModelsUnequal() throws ApexException { + final TestApexModel testApexModel0 = new TestApexModel(AxModel.class, + new DummyApexBasicModelCreator()); + final TestApexModel testApexModel1 = new TestApexModel(AxModel.class, + new DummyApexBasicModelCreator()); + + testApexModel1.getModel().getKey().setVersion("0.0.2"); + + try { + testApexModel0.checkModelEquality(testApexModel0.getModel(), testApexModel1.getModel(), + "Models are not equal"); + fail("test should throw an exception here"); + } catch (ApexException ae) { + assertEquals("Models are not equal", ae.getMessage()); + } + } + @Test public void testModelCreator0() throws ApexException { final TestApexModel testApexModel = new TestApexModel(AxModel.class, - new TestApexModelCreator0Test()); + new SupportApexModelCreator0()); testApexModel.testApexModelValid(); try { @@ -86,7 +104,7 @@ public class SupportBasicModelTester { @Test public void testModelCreator1() throws ApexException { final TestApexModel testApexModel = new TestApexModel(AxModel.class, - new TestApexModelCreator1Test()); + new SupportApexModelCreator1()); try { testApexModel.testApexModelValid(); @@ -113,7 +131,7 @@ public class SupportBasicModelTester { @Test public void testModelCreator2() throws ApexException { final TestApexModel testApexModel = new TestApexModel(AxModel.class, - new TestApexModelCreator2Test()); + new SupportApexModelCreator2()); testApexModel.testApexModelValid(); testApexModel.testApexModelVaidateObservation(); @@ -128,7 +146,7 @@ public class SupportBasicModelTester { @Test public void testModelCreator1XmlJson() throws ApexException { final TestApexModel testApexModel = new TestApexModel(AxModel.class, - new TestApexModelCreator1Test()); + new SupportApexModelCreator1()); try { testApexModel.testApexModelWriteReadJson(); diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator0Test.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator0Test.java deleted file mode 100644 index d0e8807e1..000000000 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator0Test.java +++ /dev/null @@ -1,64 +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 org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; -import org.onap.policy.apex.model.basicmodel.concepts.AxModel; -import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; - -public class TestApexModelCreator0Test implements TestApexModelCreator { - - @Override - public AxModel getModel() { - AxModel basicModel = new AxModel(); - - basicModel.setKey(new AxArtifactKey("BasicModel", "0.0.1")); - basicModel.setKeyInformation(new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1"))); - - basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKey(), new AxKeyInfo(basicModel.getKey())); - basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKeyInformation().getKey(), - new AxKeyInfo(basicModel.getKeyInformation().getKey())); - - return basicModel; - } - - @Override - public final AxModel getMalstructuredModel() { - return getModel(); - } - - @Override - public final AxModel getObservationModel() { - return getModel(); - } - - @Override - public final AxModel getWarningModel() { - return getModel(); - } - - @Override - public final AxModel getInvalidModel() { - return getModel(); - } -} diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator1Test.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator1Test.java deleted file mode 100644 index f785a47ba..000000000 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator1Test.java +++ /dev/null @@ -1,69 +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 java.util.UUID; - -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; -import org.onap.policy.apex.model.basicmodel.concepts.AxModel; -import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; - -public class TestApexModelCreator1Test implements TestApexModelCreator { - - @Override - public AxModel getModel() { - return getInvalidModel(); - } - - @Override - public final AxModel getMalstructuredModel() { - return getInvalidModel(); - } - - @Override - public final AxModel getObservationModel() { - return getInvalidModel(); - } - - @Override - public final AxModel getWarningModel() { - return getInvalidModel(); - } - - @Override - public final AxModel getInvalidModel() { - AxModel basicModel = new AxModel(); - - basicModel.setKey(new AxArtifactKey("BasicModelKey", "0.0.1")); - basicModel.setKeyInformation(new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1"))); - - basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKey(), new AxKeyInfo(basicModel.getKey(), - UUID.fromString("00000000-0000-0000-0000-000000000000"), - "nbasic model description\nThis is a multi line description\nwith another line of text.")); - basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKeyInformation().getKey(), - new AxKeyInfo(basicModel.getKeyInformation().getKey(), - UUID.fromString("00000000-0000-0000-0000-000000000000"), "")); - - return basicModel; - } -} diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator2Test.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator2Test.java deleted file mode 100644 index 9be21f522..000000000 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/handling/TestApexModelCreator2Test.java +++ /dev/null @@ -1,64 +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 org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; -import org.onap.policy.apex.model.basicmodel.concepts.AxModel; -import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; - -public class TestApexModelCreator2Test implements TestApexModelCreator { - - @Override - public AxModel getModel() { - AxModel basicModel = new AxModel(); - - basicModel.setKey(new AxArtifactKey("BasicModel", "0.0.1")); - basicModel.setKeyInformation(new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1"))); - - basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKey(), new AxKeyInfo(basicModel.getKey())); - basicModel.getKeyInformation().getKeyInfoMap().put(basicModel.getKeyInformation().getKey(), - new AxKeyInfo(basicModel.getKeyInformation().getKey())); - basicModel.getKeyInformation().get("BasicModel").setDescription(""); - return basicModel; - } - - @Override - public final AxModel getMalstructuredModel() { - return getModel(); - } - - @Override - public final AxModel getObservationModel() { - return getModel(); - } - - @Override - public final AxModel getWarningModel() { - return getModel(); - } - - @Override - public final AxModel getInvalidModel() { - return getModel(); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java index edf045a06..e1264e887 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java @@ -41,7 +41,7 @@ import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; import org.onap.policy.apex.model.policymodel.concepts.AxState; import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; import org.onap.policy.apex.model.policymodel.concepts.AxStateTree; -import org.onap.policy.apex.model.policymodel.handling.ApexPolicyModelCreatorTest; +import org.onap.policy.apex.model.policymodel.handling.SupportApexPolicyModelCreator; /** * Test apex policies. @@ -113,7 +113,7 @@ public class PoliciesTest { assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId()); - policy = new ApexPolicyModelCreatorTest().getModel().getPolicies().get("policy"); + policy = new SupportApexPolicyModelCreator().getModel().getPolicies().get("policy"); AxValidationResult result = new AxValidationResult(); result = policy.validate(result); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PolicyModelTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PolicyModelTest.java index e1f1f90c3..6c93efde0 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PolicyModelTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PolicyModelTest.java @@ -46,7 +46,7 @@ import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; import org.onap.policy.apex.model.policymodel.concepts.AxTasks; -import org.onap.policy.apex.model.policymodel.handling.ApexPolicyModelCreatorTest; +import org.onap.policy.apex.model.policymodel.handling.SupportApexPolicyModelCreator; /** * Test policy models. @@ -148,7 +148,7 @@ public class PolicyModelTest { new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - model = new ApexPolicyModelCreatorTest().getModel(); + model = new SupportApexPolicyModelCreator().getModel(); AxValidationResult result = new AxValidationResult(); result = model.validate(result); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelCreatorTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelCreatorTest.java deleted file mode 100644 index 604d8c3bc..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelCreatorTest.java +++ /dev/null @@ -1,455 +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.policymodel.handling; - -import java.util.UUID; - -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; -import org.onap.policy.apex.model.eventmodel.concepts.AxField; -import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; -import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicies; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.concepts.AxState; -import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; -import org.onap.policy.apex.model.policymodel.concepts.AxTask; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTasks; - -/** - * Model creator for model tests. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class ApexPolicyModelCreatorTest implements TestApexModelCreator { - - @Override - public AxPolicyModel getModel() { - final AxContextSchema schema0 = new AxContextSchema(new AxArtifactKey("eventContextItem0", "0.0.1"), "Java", - "java.lang.String"); - final AxContextSchema schema1 = new AxContextSchema(new AxArtifactKey("eventContextItem1", "0.0.1"), "Java", - "java.lang.Long"); - final AxContextSchema schema2 = new AxContextSchema(new AxArtifactKey("StringType", "0.0.1"), "Java", - "org.onap.policy.apex.model.policymodel.concepts.TestContextItem000"); - final AxContextSchema schema3 = new AxContextSchema(new AxArtifactKey("MapType", "0.0.1"), "Java", - "org.onap.policy.apex.model.policymodel.concepts.TestContextItem00A"); - - final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("ContextSchemas", "0.0.1")); - schemas.getSchemasMap().put(schema0.getKey(), schema0); - schemas.getSchemasMap().put(schema1.getKey(), schema1); - schemas.getSchemasMap().put(schema2.getKey(), schema2); - schemas.getSchemasMap().put(schema3.getKey(), schema3); - - final AxContextAlbum album0 = new AxContextAlbum(new AxArtifactKey("contextAlbum0", "0.0.1"), "APPLICATION", - true, schema3.getKey()); - final AxContextAlbum album1 = new AxContextAlbum(new AxArtifactKey("contextAlbum1", "0.0.1"), "GLOBAL", false, - schema2.getKey()); - - final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); - albums.getAlbumsMap().put(album0.getKey(), album0); - albums.getAlbumsMap().put(album1.getKey(), album1); - - final AxEvent inEvent = new AxEvent(new AxArtifactKey("inEvent", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - inEvent.getParameterMap().put("IEPAR0", - new AxField(new AxReferenceKey(inEvent.getKey(), "IEPAR0"), schema0.getKey())); - inEvent.getParameterMap().put("IEPAR1", - new AxField(new AxReferenceKey(inEvent.getKey(), "IEPAR1"), schema1.getKey())); - - final AxEvent outEvent0 = new AxEvent(new AxArtifactKey("outEvent0", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - outEvent0.getParameterMap().put("OE0PAR0", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PAR0"), schema0.getKey())); - outEvent0.getParameterMap().put("OE0PAR1", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PAR1"), schema1.getKey())); - outEvent0.getParameterMap().put("OE1PAR0", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PAR0"), schema0.getKey())); - outEvent0.getParameterMap().put("OE1PAR1", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PAR1"), schema1.getKey())); - - final AxEvent outEvent1 = new AxEvent(new AxArtifactKey("outEvent1", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - outEvent1.getParameterMap().put("OE1PAR0", - new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PAR0"), schema0.getKey())); - outEvent1.getParameterMap().put("OE1PAR1", - new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PAR1"), schema1.getKey())); - - final AxEvents events = new AxEvents(new AxArtifactKey("events", "0.0.1")); - events.getEventMap().put(inEvent.getKey(), inEvent); - events.getEventMap().put(outEvent0.getKey(), outEvent0); - events.getEventMap().put(outEvent1.getKey(), outEvent1); - - final AxTask task = new AxTask(new AxArtifactKey("task", "0.0.1")); - - for (final AxField field : inEvent.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "inputFields", field.getKey().getLocalName()); - final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); - task.getInputFields().put(inputField.getKey().getLocalName(), inputField); - } - - for (final AxField field : outEvent0.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "outputFields", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - for (final AxField field : outEvent1.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "outputFields", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameter0"), - "Task parameter 0 value"); - final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameter1"), - "Task parameter 1 value"); - - task.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); - task.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); - task.getContextAlbumReferences().add(album0.getKey()); - task.getContextAlbumReferences().add(album1.getKey()); - - final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(task.getKey(), "taskLogic"), "MVEL", - "Some task logic"); - task.setTaskLogic(taskLogic); - - final AxTasks tasks = new AxTasks(new AxArtifactKey("tasks", "0.0.1")); - tasks.getTaskMap().put(task.getKey(), task); - - final AxPolicy policy = new AxPolicy(new AxArtifactKey("policy", "0.0.1")); - policy.setTemplate("FREEFORM"); - - final AxState state = new AxState(new AxReferenceKey(policy.getKey(), "state")); - final AxTaskSelectionLogic taskSelectionLogic = new AxTaskSelectionLogic( - new AxReferenceKey(state.getKey(), "taskSelectionLogic"), "MVEL", "Some TS logic "); - - state.setTrigger(inEvent.getKey()); - state.getContextAlbumReferences().add(album0.getKey()); - state.getContextAlbumReferences().add(album1.getKey()); - state.setTaskSelectionLogic(taskSelectionLogic); - state.setDefaultTask(task.getKey()); - - final AxStateOutput stateOutput0 = new AxStateOutput(new AxReferenceKey(state.getKey(), "stateOutput0"), - outEvent0.getKey(), AxReferenceKey.getNullKey()); - state.getStateOutputs().put(stateOutput0.getKey().getLocalName(), stateOutput0); - - final AxStateTaskReference stateTaskReference = new AxStateTaskReference( - new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.DIRECT, - stateOutput0.getKey()); - - state.getTaskReferences().put(task.getKey(), stateTaskReference); - - policy.getStateMap().put(state.getKey().getLocalName(), state); - policy.setFirstState(state.getKey().getLocalName()); - - final AxPolicies policies = new AxPolicies(new AxArtifactKey("policies", "0.0.1")); - policies.getPolicyMap().put(policy.getKey(), policy); - - final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); - final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("PolicyModel", "0.0.1")); - policyModel.setKeyInformation(keyInformation); - policyModel.setSchemas(schemas); - policyModel.setAlbums(albums); - policyModel.setEvents(events); - policyModel.setTasks(tasks); - policyModel.setPolicies(policies); - policyModel.getKeyInformation().generateKeyInfo(policyModel); - - int uuidIncrementer = 0; - for (final AxKeyInfo keyInfo : policyModel.getKeyInformation().getKeyInfoMap().values()) { - final String uuidString = String.format("ce9168c-e6df-414f-9646-6da464b6e%03d", uuidIncrementer++); - keyInfo.setUuid(UUID.fromString(uuidString)); - } - - final AxValidationResult result = new AxValidationResult(); - policyModel.validate(result); - - return policyModel; - } - - /** - * Gets another policy model. - * - * @return the model - */ - public AxPolicyModel getAnotherModel() { - final AxContextSchema schema0 = new AxContextSchema(new AxArtifactKey("eventContextItemA0", "0.0.1"), "Java", - "java.lang.String"); - final AxContextSchema schema1 = new AxContextSchema(new AxArtifactKey("eventContextItemA1", "0.0.1"), "Java", - "java.lang.Long"); - final AxContextSchema schema2 = new AxContextSchema(new AxArtifactKey("StringTypeA", "0.0.1"), "Java", - "org.onap.policy.apex.model.policymodel.concepts.TestContextItem000"); - final AxContextSchema schema3 = new AxContextSchema(new AxArtifactKey("MapTypeA", "0.0.1"), "Java", - "org.onap.policy.apex.model.policymodel.concepts.TestContextItem00A"); - - final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("ContextSchemasA", "0.0.1")); - schemas.getSchemasMap().put(schema0.getKey(), schema0); - schemas.getSchemasMap().put(schema1.getKey(), schema1); - schemas.getSchemasMap().put(schema2.getKey(), schema2); - schemas.getSchemasMap().put(schema3.getKey(), schema3); - - final AxContextAlbum album0 = new AxContextAlbum(new AxArtifactKey("contextAlbumA0", "0.0.1"), "APPLICATION", - true, schema3.getKey()); - final AxContextAlbum album1 = new AxContextAlbum(new AxArtifactKey("contextAlbumA1", "0.0.1"), "GLOBAL", false, - schema2.getKey()); - - final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("contextA", "0.0.1")); - albums.getAlbumsMap().put(album0.getKey(), album0); - albums.getAlbumsMap().put(album1.getKey(), album1); - - final AxEvent inEvent = new AxEvent(new AxArtifactKey("inEventA", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - inEvent.getParameterMap().put("IEPARA0", - new AxField(new AxReferenceKey(inEvent.getKey(), "IEPARA0"), schema0.getKey())); - inEvent.getParameterMap().put("IEPARA1", - new AxField(new AxReferenceKey(inEvent.getKey(), "IEPARA1"), schema1.getKey())); - - final AxEvent outEvent0 = new AxEvent(new AxArtifactKey("outEventA0", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - outEvent0.getParameterMap().put("OE0PARA0", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PARA0"), schema0.getKey())); - outEvent0.getParameterMap().put("OE0PARA1", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PARA1"), schema1.getKey())); - outEvent0.getParameterMap().put("OE1PARA0", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PARA0"), schema0.getKey())); - outEvent0.getParameterMap().put("OE1PARA1", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PARA1"), schema1.getKey())); - - final AxEvent outEvent1 = new AxEvent(new AxArtifactKey("outEventA1", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - outEvent1.getParameterMap().put("OE1PARA0", - new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PARA0"), schema0.getKey())); - outEvent1.getParameterMap().put("OE1PARA1", - new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PARA1"), schema1.getKey())); - - final AxEvents events = new AxEvents(new AxArtifactKey("eventsA", "0.0.1")); - events.getEventMap().put(inEvent.getKey(), inEvent); - events.getEventMap().put(outEvent0.getKey(), outEvent0); - events.getEventMap().put(outEvent1.getKey(), outEvent1); - - final AxTask task = new AxTask(new AxArtifactKey("taskA", "0.0.1")); - - for (final AxField field : inEvent.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "inputFieldsA", field.getKey().getLocalName()); - final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); - task.getInputFields().put(inputField.getKey().getLocalName(), inputField); - } - - for (final AxField field : outEvent0.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "outputFieldsA", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - for (final AxField field : outEvent1.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "outputFieldsA", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameterA0"), - "Task parameter 0 value"); - final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameterA1"), - "Task parameter 1 value"); - - task.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); - task.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); - task.getContextAlbumReferences().add(album0.getKey()); - task.getContextAlbumReferences().add(album1.getKey()); - - final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(task.getKey(), "taskLogicA"), "MVEL", - "Some task logic"); - task.setTaskLogic(taskLogic); - - final AxTasks tasks = new AxTasks(new AxArtifactKey("tasksA", "0.0.1")); - tasks.getTaskMap().put(task.getKey(), task); - - final AxPolicy policy = new AxPolicy(new AxArtifactKey("policyA", "0.0.1")); - policy.setTemplate("FREEFORM"); - - final AxState state = new AxState(new AxReferenceKey(policy.getKey(), "stateA")); - final AxTaskSelectionLogic taskSelectionLogic = new AxTaskSelectionLogic( - new AxReferenceKey(state.getKey(), "taskSelectionLogicA"), "MVEL", "Some TS logic "); - - state.setTrigger(inEvent.getKey()); - state.getContextAlbumReferences().add(album0.getKey()); - state.getContextAlbumReferences().add(album1.getKey()); - state.setTaskSelectionLogic(taskSelectionLogic); - state.setDefaultTask(task.getKey()); - - final AxStateOutput stateOutput0 = new AxStateOutput(new AxReferenceKey(state.getKey(), "stateOutputA0"), - outEvent0.getKey(), AxReferenceKey.getNullKey()); - state.getStateOutputs().put(stateOutput0.getKey().getLocalName(), stateOutput0); - - final AxStateTaskReference stateTaskReference = new AxStateTaskReference( - new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.DIRECT, - stateOutput0.getKey()); - - state.getTaskReferences().put(task.getKey(), stateTaskReference); - - policy.getStateMap().put(state.getKey().getLocalName(), state); - policy.setFirstState(state.getKey().getLocalName()); - - final AxPolicies policies = new AxPolicies(new AxArtifactKey("policiesA", "0.0.1")); - policies.getPolicyMap().put(policy.getKey(), policy); - - final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKeyA", "0.0.1")); - final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("PolicyModelA", "0.0.1")); - policyModel.setKeyInformation(keyInformation); - policyModel.setSchemas(schemas); - policyModel.setAlbums(albums); - policyModel.setEvents(events); - policyModel.setTasks(tasks); - policyModel.setPolicies(policies); - policyModel.getKeyInformation().generateKeyInfo(policyModel); - - int uuidIncrementer = 0; - for (final AxKeyInfo keyInfo : policyModel.getKeyInformation().getKeyInfoMap().values()) { - final String uuidString = String.format("ce9168c-e6df-414f-9646-6da464b6e%03d", uuidIncrementer++); - keyInfo.setUuid(UUID.fromString(uuidString)); - } - - final AxValidationResult result = new AxValidationResult(); - policyModel.validate(result); - - return policyModel; - } - - @Override - public AxPolicyModel getMalstructuredModel() { - final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("policyModel", "0.0.1")); - return policyModel; - } - - @Override - public AxPolicyModel getObservationModel() { - final AxPolicyModel policyModel = getModel(); - - final AxState state = policyModel.getPolicies().get("policy").getStateMap().get("state"); - final AxTask task = policyModel.getTasks().get("task"); - - final AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic( - new AxReferenceKey(state.getKey(), "SFL"), "MVEL", "Some SF logic "); - state.getStateFinalizerLogicMap().put(stateFinalizerLogic.getKey().getLocalName(), stateFinalizerLogic); - final AxStateTaskReference stateTaskReference = new AxStateTaskReference( - new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.LOGIC, - stateFinalizerLogic.getKey()); - - state.getTaskReferences().put(task.getKey(), stateTaskReference); - - return policyModel; - } - - @Override - public AxPolicyModel getWarningModel() { - final AxPolicyModel policyModel = getModel(); - - final AxState anotherState = new AxState( - new AxReferenceKey(new AxArtifactKey("policy", "0.0.1"), "anotherState")); - - final AxEvent inEvent = policyModel.getEvents().getEventMap().get(new AxArtifactKey("inEvent", "0.0.1")); - final AxEvent outEvent0 = policyModel.getEvents().getEventMap().get(new AxArtifactKey("outEvent0", "0.0.1")); - - final AxTask anotherTask = new AxTask(new AxArtifactKey("anotherTask", "0.0.1")); - - for (final AxField field : inEvent.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(anotherTask.getKey().getName(), - anotherTask.getKey().getVersion(), "inputFields", field.getKey().getLocalName()); - final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); - anotherTask.getInputFields().put(inputField.getKey().getLocalName(), inputField); - } - - for (final AxField field : outEvent0.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(anotherTask.getKey().getName(), - anotherTask.getKey().getVersion(), "outputFields", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - anotherTask.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(anotherTask.getKey(), "taskParameter0"), - "Task parameter 0 value"); - final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(anotherTask.getKey(), "taskParameter1"), - "Task parameter 1 value"); - - anotherTask.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); - anotherTask.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); - - final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(anotherTask.getKey(), "taskLogic"), "MVEL", - "Some task logic"); - anotherTask.setTaskLogic(taskLogic); - policyModel.getTasks().getTaskMap().put(anotherTask.getKey(), anotherTask); - - final AxStateOutput anotherStateOutput0 = new AxStateOutput( - new AxReferenceKey(anotherState.getKey(), "stateOutput0"), outEvent0.getKey(), - AxReferenceKey.getNullKey()); - anotherState.setTrigger(inEvent.getKey()); - anotherState.getStateOutputs().put(anotherStateOutput0.getKey().getLocalName(), anotherStateOutput0); - anotherState.setDefaultTask(anotherTask.getKey()); - final AxStateTaskReference anotherStateTaskReference = new AxStateTaskReference( - new AxReferenceKey(anotherState.getKey(), anotherTask.getKey().getName()), - AxStateTaskOutputType.DIRECT, anotherStateOutput0.getKey()); - anotherState.getTaskReferences().put(anotherTask.getKey(), anotherStateTaskReference); - - policyModel.getPolicies().getPolicyMap().get(new AxArtifactKey("policy", "0.0.1")).getStateMap() - .put(anotherState.getKey().getLocalName(), anotherState); - - policyModel.getKeyInformation().generateKeyInfo(policyModel); - - return policyModel; - } - - @Override - public AxPolicyModel getInvalidModel() { - final AxPolicyModel policyModel = getModel(); - - policyModel.getAlbums().get(new AxArtifactKey("contextAlbum0", "0.0.1")).setScope("UNDEFINED"); - policyModel.getAlbums().get(new AxArtifactKey("contextAlbum1", "0.0.1")).setScope("UNDEFINED"); - - final AxEvent outEvent0 = policyModel.getEvents().get("outEvent0"); - outEvent0.getParameterMap().remove("OE1PAR0"); - outEvent0.getParameterMap().remove("OE1PAR1"); - - return policyModel; - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelTest.java index 997664b13..7a240e18b 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelTest.java @@ -49,7 +49,7 @@ public class ApexPolicyModelTest { Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); - testApexModel = new TestApexModel(AxPolicyModel.class, new ApexPolicyModelCreatorTest()); + testApexModel = new TestApexModel(AxPolicyModel.class, new SupportApexPolicyModelCreator()); } @After diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyAnalyserTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyAnalyserTest.java index 425509d3a..d4ab6bd7b 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyAnalyserTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyAnalyserTest.java @@ -31,7 +31,7 @@ import org.onap.policy.apex.model.policymodel.handling.PolicyAnalysisResult; public class PolicyAnalyserTest { @Test public void test() { - final AxPolicyModel apexModel = new ApexPolicyModelCreatorTest().getModel(); + final AxPolicyModel apexModel = new SupportApexPolicyModelCreator().getModel(); final PolicyAnalyser policyAnalyser = new PolicyAnalyser(); final PolicyAnalysisResult analysisResult = policyAnalyser.analyse(apexModel); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelComparerTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelComparerTest.java index 08d00e628..8b0396bc2 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelComparerTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelComparerTest.java @@ -39,7 +39,7 @@ public class PolicyModelComparerTest { @Test public void testPolicyComparer() throws IOException { - final AxPolicyModel leftApexModel = new ApexPolicyModelCreatorTest().getModel(); + final AxPolicyModel leftApexModel = new SupportApexPolicyModelCreator().getModel(); final AxPolicyModel rightApexModel = new AxPolicyModel(leftApexModel); PolicyModelComparer policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java index 7d5ba6a40..b477c47cd 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java @@ -39,8 +39,8 @@ public class PolicyModelMergerTest { @Test public void testPolicyModelMerger() { - final AxPolicyModel leftPolicyModel = new ApexPolicyModelCreatorTest().getModel(); - AxPolicyModel rightPolicyModel = new ApexPolicyModelCreatorTest().getModel(); + final AxPolicyModel leftPolicyModel = new SupportApexPolicyModelCreator().getModel(); + AxPolicyModel rightPolicyModel = new SupportApexPolicyModelCreator().getModel(); try { final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, @@ -99,7 +99,7 @@ public class PolicyModelMergerTest { fail("test should not throw an exception"); } - rightPolicyModel = new ApexPolicyModelCreatorTest().getAnotherModel(); + rightPolicyModel = new SupportApexPolicyModelCreator().getAnotherModel(); try { final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, true); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java index 29e7307f4..c20a40736 100644 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java @@ -37,7 +37,7 @@ import org.onap.policy.apex.model.policymodel.handling.PolicyModelSplitter; public class PolicyModelSplitterTest { @Test public void test() { - final AxPolicyModel apexModel = new ApexPolicyModelCreatorTest().getModel(); + final AxPolicyModel apexModel = new SupportApexPolicyModelCreator().getModel(); final Set requiredPolicySet = new TreeSet(); requiredPolicySet.add(new AxArtifactKey("policy", "0.0.1")); diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/SupportApexPolicyModelCreator.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/SupportApexPolicyModelCreator.java new file mode 100644 index 000000000..b2c49b6b4 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/SupportApexPolicyModelCreator.java @@ -0,0 +1,455 @@ +/*- + * ============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.policymodel.handling; + +import java.util.UUID; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; +import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicies; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxState; +import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTasks; + +/** + * Model creator for model tests. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SupportApexPolicyModelCreator implements TestApexModelCreator { + + @Override + public AxPolicyModel getModel() { + final AxContextSchema schema0 = new AxContextSchema(new AxArtifactKey("eventContextItem0", "0.0.1"), "Java", + "java.lang.String"); + final AxContextSchema schema1 = new AxContextSchema(new AxArtifactKey("eventContextItem1", "0.0.1"), "Java", + "java.lang.Long"); + final AxContextSchema schema2 = new AxContextSchema(new AxArtifactKey("StringType", "0.0.1"), "Java", + "org.onap.policy.apex.model.policymodel.concepts.TestContextItem000"); + final AxContextSchema schema3 = new AxContextSchema(new AxArtifactKey("MapType", "0.0.1"), "Java", + "org.onap.policy.apex.model.policymodel.concepts.TestContextItem00A"); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("ContextSchemas", "0.0.1")); + schemas.getSchemasMap().put(schema0.getKey(), schema0); + schemas.getSchemasMap().put(schema1.getKey(), schema1); + schemas.getSchemasMap().put(schema2.getKey(), schema2); + schemas.getSchemasMap().put(schema3.getKey(), schema3); + + final AxContextAlbum album0 = new AxContextAlbum(new AxArtifactKey("contextAlbum0", "0.0.1"), "APPLICATION", + true, schema3.getKey()); + final AxContextAlbum album1 = new AxContextAlbum(new AxArtifactKey("contextAlbum1", "0.0.1"), "GLOBAL", false, + schema2.getKey()); + + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); + albums.getAlbumsMap().put(album0.getKey(), album0); + albums.getAlbumsMap().put(album1.getKey(), album1); + + final AxEvent inEvent = new AxEvent(new AxArtifactKey("inEvent", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + inEvent.getParameterMap().put("IEPAR0", + new AxField(new AxReferenceKey(inEvent.getKey(), "IEPAR0"), schema0.getKey())); + inEvent.getParameterMap().put("IEPAR1", + new AxField(new AxReferenceKey(inEvent.getKey(), "IEPAR1"), schema1.getKey())); + + final AxEvent outEvent0 = new AxEvent(new AxArtifactKey("outEvent0", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + outEvent0.getParameterMap().put("OE0PAR0", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PAR0"), schema0.getKey())); + outEvent0.getParameterMap().put("OE0PAR1", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PAR1"), schema1.getKey())); + outEvent0.getParameterMap().put("OE1PAR0", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PAR0"), schema0.getKey())); + outEvent0.getParameterMap().put("OE1PAR1", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PAR1"), schema1.getKey())); + + final AxEvent outEvent1 = new AxEvent(new AxArtifactKey("outEvent1", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + outEvent1.getParameterMap().put("OE1PAR0", + new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PAR0"), schema0.getKey())); + outEvent1.getParameterMap().put("OE1PAR1", + new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PAR1"), schema1.getKey())); + + final AxEvents events = new AxEvents(new AxArtifactKey("events", "0.0.1")); + events.getEventMap().put(inEvent.getKey(), inEvent); + events.getEventMap().put(outEvent0.getKey(), outEvent0); + events.getEventMap().put(outEvent1.getKey(), outEvent1); + + final AxTask task = new AxTask(new AxArtifactKey("task", "0.0.1")); + + for (final AxField field : inEvent.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "inputFields", field.getKey().getLocalName()); + final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); + task.getInputFields().put(inputField.getKey().getLocalName(), inputField); + } + + for (final AxField field : outEvent0.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "outputFields", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + for (final AxField field : outEvent1.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "outputFields", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameter0"), + "Task parameter 0 value"); + final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameter1"), + "Task parameter 1 value"); + + task.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); + task.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); + task.getContextAlbumReferences().add(album0.getKey()); + task.getContextAlbumReferences().add(album1.getKey()); + + final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(task.getKey(), "taskLogic"), "MVEL", + "Some task logic"); + task.setTaskLogic(taskLogic); + + final AxTasks tasks = new AxTasks(new AxArtifactKey("tasks", "0.0.1")); + tasks.getTaskMap().put(task.getKey(), task); + + final AxPolicy policy = new AxPolicy(new AxArtifactKey("policy", "0.0.1")); + policy.setTemplate("FREEFORM"); + + final AxState state = new AxState(new AxReferenceKey(policy.getKey(), "state")); + final AxTaskSelectionLogic taskSelectionLogic = new AxTaskSelectionLogic( + new AxReferenceKey(state.getKey(), "taskSelectionLogic"), "MVEL", "Some TS logic "); + + state.setTrigger(inEvent.getKey()); + state.getContextAlbumReferences().add(album0.getKey()); + state.getContextAlbumReferences().add(album1.getKey()); + state.setTaskSelectionLogic(taskSelectionLogic); + state.setDefaultTask(task.getKey()); + + final AxStateOutput stateOutput0 = new AxStateOutput(new AxReferenceKey(state.getKey(), "stateOutput0"), + outEvent0.getKey(), AxReferenceKey.getNullKey()); + state.getStateOutputs().put(stateOutput0.getKey().getLocalName(), stateOutput0); + + final AxStateTaskReference stateTaskReference = new AxStateTaskReference( + new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.DIRECT, + stateOutput0.getKey()); + + state.getTaskReferences().put(task.getKey(), stateTaskReference); + + policy.getStateMap().put(state.getKey().getLocalName(), state); + policy.setFirstState(state.getKey().getLocalName()); + + final AxPolicies policies = new AxPolicies(new AxArtifactKey("policies", "0.0.1")); + policies.getPolicyMap().put(policy.getKey(), policy); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); + final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("PolicyModel", "0.0.1")); + policyModel.setKeyInformation(keyInformation); + policyModel.setSchemas(schemas); + policyModel.setAlbums(albums); + policyModel.setEvents(events); + policyModel.setTasks(tasks); + policyModel.setPolicies(policies); + policyModel.getKeyInformation().generateKeyInfo(policyModel); + + int uuidIncrementer = 0; + for (final AxKeyInfo keyInfo : policyModel.getKeyInformation().getKeyInfoMap().values()) { + final String uuidString = String.format("ce9168c-e6df-414f-9646-6da464b6e%03d", uuidIncrementer++); + keyInfo.setUuid(UUID.fromString(uuidString)); + } + + final AxValidationResult result = new AxValidationResult(); + policyModel.validate(result); + + return policyModel; + } + + /** + * Gets another policy model. + * + * @return the model + */ + public AxPolicyModel getAnotherModel() { + final AxContextSchema schema0 = new AxContextSchema(new AxArtifactKey("eventContextItemA0", "0.0.1"), "Java", + "java.lang.String"); + final AxContextSchema schema1 = new AxContextSchema(new AxArtifactKey("eventContextItemA1", "0.0.1"), "Java", + "java.lang.Long"); + final AxContextSchema schema2 = new AxContextSchema(new AxArtifactKey("StringTypeA", "0.0.1"), "Java", + "org.onap.policy.apex.model.policymodel.concepts.TestContextItem000"); + final AxContextSchema schema3 = new AxContextSchema(new AxArtifactKey("MapTypeA", "0.0.1"), "Java", + "org.onap.policy.apex.model.policymodel.concepts.TestContextItem00A"); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("ContextSchemasA", "0.0.1")); + schemas.getSchemasMap().put(schema0.getKey(), schema0); + schemas.getSchemasMap().put(schema1.getKey(), schema1); + schemas.getSchemasMap().put(schema2.getKey(), schema2); + schemas.getSchemasMap().put(schema3.getKey(), schema3); + + final AxContextAlbum album0 = new AxContextAlbum(new AxArtifactKey("contextAlbumA0", "0.0.1"), "APPLICATION", + true, schema3.getKey()); + final AxContextAlbum album1 = new AxContextAlbum(new AxArtifactKey("contextAlbumA1", "0.0.1"), "GLOBAL", false, + schema2.getKey()); + + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("contextA", "0.0.1")); + albums.getAlbumsMap().put(album0.getKey(), album0); + albums.getAlbumsMap().put(album1.getKey(), album1); + + final AxEvent inEvent = new AxEvent(new AxArtifactKey("inEventA", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + inEvent.getParameterMap().put("IEPARA0", + new AxField(new AxReferenceKey(inEvent.getKey(), "IEPARA0"), schema0.getKey())); + inEvent.getParameterMap().put("IEPARA1", + new AxField(new AxReferenceKey(inEvent.getKey(), "IEPARA1"), schema1.getKey())); + + final AxEvent outEvent0 = new AxEvent(new AxArtifactKey("outEventA0", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + outEvent0.getParameterMap().put("OE0PARA0", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PARA0"), schema0.getKey())); + outEvent0.getParameterMap().put("OE0PARA1", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PARA1"), schema1.getKey())); + outEvent0.getParameterMap().put("OE1PARA0", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PARA0"), schema0.getKey())); + outEvent0.getParameterMap().put("OE1PARA1", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PARA1"), schema1.getKey())); + + final AxEvent outEvent1 = new AxEvent(new AxArtifactKey("outEventA1", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + outEvent1.getParameterMap().put("OE1PARA0", + new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PARA0"), schema0.getKey())); + outEvent1.getParameterMap().put("OE1PARA1", + new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PARA1"), schema1.getKey())); + + final AxEvents events = new AxEvents(new AxArtifactKey("eventsA", "0.0.1")); + events.getEventMap().put(inEvent.getKey(), inEvent); + events.getEventMap().put(outEvent0.getKey(), outEvent0); + events.getEventMap().put(outEvent1.getKey(), outEvent1); + + final AxTask task = new AxTask(new AxArtifactKey("taskA", "0.0.1")); + + for (final AxField field : inEvent.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "inputFieldsA", field.getKey().getLocalName()); + final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); + task.getInputFields().put(inputField.getKey().getLocalName(), inputField); + } + + for (final AxField field : outEvent0.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "outputFieldsA", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + for (final AxField field : outEvent1.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "outputFieldsA", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameterA0"), + "Task parameter 0 value"); + final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameterA1"), + "Task parameter 1 value"); + + task.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); + task.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); + task.getContextAlbumReferences().add(album0.getKey()); + task.getContextAlbumReferences().add(album1.getKey()); + + final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(task.getKey(), "taskLogicA"), "MVEL", + "Some task logic"); + task.setTaskLogic(taskLogic); + + final AxTasks tasks = new AxTasks(new AxArtifactKey("tasksA", "0.0.1")); + tasks.getTaskMap().put(task.getKey(), task); + + final AxPolicy policy = new AxPolicy(new AxArtifactKey("policyA", "0.0.1")); + policy.setTemplate("FREEFORM"); + + final AxState state = new AxState(new AxReferenceKey(policy.getKey(), "stateA")); + final AxTaskSelectionLogic taskSelectionLogic = new AxTaskSelectionLogic( + new AxReferenceKey(state.getKey(), "taskSelectionLogicA"), "MVEL", "Some TS logic "); + + state.setTrigger(inEvent.getKey()); + state.getContextAlbumReferences().add(album0.getKey()); + state.getContextAlbumReferences().add(album1.getKey()); + state.setTaskSelectionLogic(taskSelectionLogic); + state.setDefaultTask(task.getKey()); + + final AxStateOutput stateOutput0 = new AxStateOutput(new AxReferenceKey(state.getKey(), "stateOutputA0"), + outEvent0.getKey(), AxReferenceKey.getNullKey()); + state.getStateOutputs().put(stateOutput0.getKey().getLocalName(), stateOutput0); + + final AxStateTaskReference stateTaskReference = new AxStateTaskReference( + new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.DIRECT, + stateOutput0.getKey()); + + state.getTaskReferences().put(task.getKey(), stateTaskReference); + + policy.getStateMap().put(state.getKey().getLocalName(), state); + policy.setFirstState(state.getKey().getLocalName()); + + final AxPolicies policies = new AxPolicies(new AxArtifactKey("policiesA", "0.0.1")); + policies.getPolicyMap().put(policy.getKey(), policy); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKeyA", "0.0.1")); + final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("PolicyModelA", "0.0.1")); + policyModel.setKeyInformation(keyInformation); + policyModel.setSchemas(schemas); + policyModel.setAlbums(albums); + policyModel.setEvents(events); + policyModel.setTasks(tasks); + policyModel.setPolicies(policies); + policyModel.getKeyInformation().generateKeyInfo(policyModel); + + int uuidIncrementer = 0; + for (final AxKeyInfo keyInfo : policyModel.getKeyInformation().getKeyInfoMap().values()) { + final String uuidString = String.format("ce9168c-e6df-414f-9646-6da464b6e%03d", uuidIncrementer++); + keyInfo.setUuid(UUID.fromString(uuidString)); + } + + final AxValidationResult result = new AxValidationResult(); + policyModel.validate(result); + + return policyModel; + } + + @Override + public AxPolicyModel getMalstructuredModel() { + final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("policyModel", "0.0.1")); + return policyModel; + } + + @Override + public AxPolicyModel getObservationModel() { + final AxPolicyModel policyModel = getModel(); + + final AxState state = policyModel.getPolicies().get("policy").getStateMap().get("state"); + final AxTask task = policyModel.getTasks().get("task"); + + final AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic( + new AxReferenceKey(state.getKey(), "SFL"), "MVEL", "Some SF logic "); + state.getStateFinalizerLogicMap().put(stateFinalizerLogic.getKey().getLocalName(), stateFinalizerLogic); + final AxStateTaskReference stateTaskReference = new AxStateTaskReference( + new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.LOGIC, + stateFinalizerLogic.getKey()); + + state.getTaskReferences().put(task.getKey(), stateTaskReference); + + return policyModel; + } + + @Override + public AxPolicyModel getWarningModel() { + final AxPolicyModel policyModel = getModel(); + + final AxState anotherState = new AxState( + new AxReferenceKey(new AxArtifactKey("policy", "0.0.1"), "anotherState")); + + final AxEvent inEvent = policyModel.getEvents().getEventMap().get(new AxArtifactKey("inEvent", "0.0.1")); + final AxEvent outEvent0 = policyModel.getEvents().getEventMap().get(new AxArtifactKey("outEvent0", "0.0.1")); + + final AxTask anotherTask = new AxTask(new AxArtifactKey("anotherTask", "0.0.1")); + + for (final AxField field : inEvent.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(anotherTask.getKey().getName(), + anotherTask.getKey().getVersion(), "inputFields", field.getKey().getLocalName()); + final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); + anotherTask.getInputFields().put(inputField.getKey().getLocalName(), inputField); + } + + for (final AxField field : outEvent0.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(anotherTask.getKey().getName(), + anotherTask.getKey().getVersion(), "outputFields", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + anotherTask.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(anotherTask.getKey(), "taskParameter0"), + "Task parameter 0 value"); + final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(anotherTask.getKey(), "taskParameter1"), + "Task parameter 1 value"); + + anotherTask.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); + anotherTask.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); + + final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(anotherTask.getKey(), "taskLogic"), "MVEL", + "Some task logic"); + anotherTask.setTaskLogic(taskLogic); + policyModel.getTasks().getTaskMap().put(anotherTask.getKey(), anotherTask); + + final AxStateOutput anotherStateOutput0 = new AxStateOutput( + new AxReferenceKey(anotherState.getKey(), "stateOutput0"), outEvent0.getKey(), + AxReferenceKey.getNullKey()); + anotherState.setTrigger(inEvent.getKey()); + anotherState.getStateOutputs().put(anotherStateOutput0.getKey().getLocalName(), anotherStateOutput0); + anotherState.setDefaultTask(anotherTask.getKey()); + final AxStateTaskReference anotherStateTaskReference = new AxStateTaskReference( + new AxReferenceKey(anotherState.getKey(), anotherTask.getKey().getName()), + AxStateTaskOutputType.DIRECT, anotherStateOutput0.getKey()); + anotherState.getTaskReferences().put(anotherTask.getKey(), anotherStateTaskReference); + + policyModel.getPolicies().getPolicyMap().get(new AxArtifactKey("policy", "0.0.1")).getStateMap() + .put(anotherState.getKey().getLocalName(), anotherState); + + policyModel.getKeyInformation().generateKeyInfo(policyModel); + + return policyModel; + } + + @Override + public AxPolicyModel getInvalidModel() { + final AxPolicyModel policyModel = getModel(); + + policyModel.getAlbums().get(new AxArtifactKey("contextAlbum0", "0.0.1")).setScope("UNDEFINED"); + policyModel.getAlbums().get(new AxArtifactKey("contextAlbum1", "0.0.1")).setScope("UNDEFINED"); + + final AxEvent outEvent0 = policyModel.getEvents().get("outEvent0"); + outEvent0.getParameterMap().remove("OE1PAR0"); + outEvent0.getParameterMap().remove("OE1PAR1"); + + return policyModel; + } +} -- cgit 1.2.3-korg