diff options
author | liamfallon <liam.fallon@est.tech> | 2019-03-14 10:01:58 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-03-14 10:01:58 +0000 |
commit | 4c28d2cdbf03be9dfe51caa05d45ba341b4c94cd (patch) | |
tree | 9689b2e6a2d6017c8ab9ece7e94c8dd097f3c66d /models-tosca/src/test/java/org/onap | |
parent | 748e3cd4a9e89b3b87a74b9134d45687f197409b (diff) |
Add DAO Enabled Tosca Model
Add DAO annotations to TOSCA model
Add keying between concepts and define
foreign keys in objects for translation to DB schema
Added provider interface, factory, and stubbed implementation.
Completed unit test for models-base
Completed unit test for models-dao
Completed unit test for models-tosca
Issue-ID: POLICY-1195
Change-Id: I53a0ba8b7a679b6887b38bdab184b60315e0cf5b
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/test/java/org/onap')
20 files changed, 2701 insertions, 2 deletions
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/TestPojos.java index 28cd4cc92..d5b5f55b3 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/TestPojos.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/TestPojos.java @@ -20,7 +20,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.tosca; +package org.onap.policy.models.tosca.concepts; import com.openpojo.reflection.filters.FilterPackageInfo; import com.openpojo.validation.Validator; @@ -41,7 +41,7 @@ import org.onap.policy.common.utils.validation.ToStringTester; */ public class TestPojos { - private static final String POJO_PACKAGE = "org.onap.policy.models.tosca"; + private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.concepts"; @Test public void testPojos() { diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalKeyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalKeyTest.java new file mode 100644 index 000000000..60c6eb976 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalKeyTest.java @@ -0,0 +1,134 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaConstraintLogicalKey. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaConstraintLogicalKeyTest { + + @Test + public void testConstraintLogicalKeyPojo() { + assertNotNull(new ToscaConstraintLogicalKey()); + assertNotNull(new ToscaConstraintLogicalKey(new PfReferenceKey())); + assertNotNull(new ToscaConstraintLogicalKey(new PfReferenceKey(), ToscaConstraintLogicalKey.Operation.EQ, + new PfConceptKey())); + + try { + new ToscaConstraintLogicalKey((PfReferenceKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaConstraintLogicalKey((ToscaConstraintLogicalKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey tclParentKey = new PfConceptKey("tParentKey", "0.0.1"); + PfReferenceKey tclKey = new PfReferenceKey(tclParentKey, "trigger0"); + PfConceptKey constraintKey = new PfConceptKey("tParentKey", "0.0.1"); + ToscaConstraintLogicalKey tcl = + new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, constraintKey); + + try { + new ToscaConstraintLogicalKey(tcl); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("cannot copy an immutable constraint", exc.getMessage()); + } + + ToscaConstraintLogicalKey tclClone1 = new ToscaConstraintLogicalKey(); + try { + tcl.copyTo(tclClone1); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("cannot copy an immutable constraint", exc.getMessage()); + } + tclClone1 = new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, constraintKey); + + assertEquals(tcl, tclClone1); + assertEquals(0, tcl.compareTo(tclClone1)); + + assertEquals(-1, tcl.compareTo(null)); + assertEquals(0, tcl.compareTo(tcl)); + assertFalse(tcl.compareTo(tcl.getKey()) == 0); + + ToscaConstraintLogicalKey differentTcl = new ToscaConstraintLogicalKey(new PfReferenceKey(), + ToscaConstraintLogicalKey.Operation.EQ, constraintKey); + assertFalse(tcl.compareTo(differentTcl) == 0); + + ToscaConstraintLogicalKey otherTc = + new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, constraintKey); + assertEquals(0, tcl.compareTo(otherTc)); + + try { + tcl.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(2, tcl.getKeys().size()); + assertEquals(2, new ToscaConstraintLogicalKey().getKeys().size()); + + new ToscaConstraintLogicalKey().clean(); + tcl.clean(); + assertEquals(tclClone1, tcl); + + assertFalse(new ToscaConstraintLogicalKey().validate(new PfValidationResult()).isValid()); + assertTrue(tcl.validate(new PfValidationResult()).isValid()); + + try { + tcl.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, null) + .validate(new PfValidationResult()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("compareToKey is marked @NonNull but is null", exc.getMessage()); + } + + assertFalse(new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, new PfConceptKey()) + .validate(new PfValidationResult()).isValid()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalStringTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalStringTest.java new file mode 100644 index 000000000..5d0fe9332 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalStringTest.java @@ -0,0 +1,133 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaConstraintLogicalString. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaConstraintLogicalStringTest { + + @Test + public void testConstraintLogicalStringPojo() { + assertNotNull(new ToscaConstraintLogicalString()); + assertNotNull(new ToscaConstraintLogicalString(new PfReferenceKey())); + assertNotNull(new ToscaConstraintLogicalString(new PfReferenceKey(), ToscaConstraintLogicalString.Operation.EQ, + "Constraint")); + + try { + new ToscaConstraintLogicalString((PfReferenceKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaConstraintLogicalString((ToscaConstraintLogicalString) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey tclParentKey = new PfConceptKey("tParentKey", "0.0.1"); + PfReferenceKey tclKey = new PfReferenceKey(tclParentKey, "trigger0"); + ToscaConstraintLogicalString tcl = + new ToscaConstraintLogicalString(tclKey, ToscaConstraintLogicalString.Operation.EQ, "Constraint"); + + try { + new ToscaConstraintLogicalString(tcl); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("cannot copy an immutable constraint", exc.getMessage()); + } + + ToscaConstraintLogicalString tclClone1 = new ToscaConstraintLogicalString(); + try { + tcl.copyTo(tclClone1); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("cannot copy an immutable constraint", exc.getMessage()); + } + tclClone1 = new ToscaConstraintLogicalString(tclKey, ToscaConstraintLogicalString.Operation.EQ, "Constraint"); + + assertEquals(tcl, tclClone1); + assertEquals(0, tcl.compareTo(tclClone1)); + + assertEquals(-1, tcl.compareTo(null)); + assertEquals(0, tcl.compareTo(tcl)); + assertFalse(tcl.compareTo(tcl.getKey()) == 0); + + ToscaConstraintLogicalString differentTcl = new ToscaConstraintLogicalString(new PfReferenceKey(), + ToscaConstraintLogicalString.Operation.EQ, "Constraint"); + assertFalse(tcl.compareTo(differentTcl) == 0); + + ToscaConstraintLogicalString otherTc = + new ToscaConstraintLogicalString(tclKey, ToscaConstraintLogicalString.Operation.EQ, "Constraint"); + assertEquals(0, tcl.compareTo(otherTc)); + + try { + tcl.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(1, tcl.getKeys().size()); + assertEquals(1, new ToscaConstraintLogicalString().getKeys().size()); + + new ToscaConstraintLogicalString().clean(); + tcl.clean(); + assertEquals(tclClone1, tcl); + + assertFalse(new ToscaConstraintLogicalString().validate(new PfValidationResult()).isValid()); + assertTrue(tcl.validate(new PfValidationResult()).isValid()); + + try { + tcl.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaConstraintLogicalString(tclKey, ToscaConstraintLogicalString.Operation.EQ, null) + .validate(new PfValidationResult()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("compareToString is marked @NonNull but is null", exc.getMessage()); + } + + assertFalse(new ToscaConstraintLogicalString(tclKey, ToscaConstraintLogicalString.Operation.EQ, "") + .validate(new PfValidationResult()).isValid()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalTest.java new file mode 100644 index 000000000..fc623f1dd --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalTest.java @@ -0,0 +1,166 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.concepts.testconcepts.DummyToscaConstraint; + +/** + * DAO test for ToscaConstraintLogical. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaConstraintLogicalTest { + + @Test + public void testConstraintLogicalPojo() { + assertNotNull(new ToscaConstraintLogical()); + assertNotNull(new ToscaConstraintLogical(new PfReferenceKey())); + + try { + new ToscaConstraintLogical((PfReferenceKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaConstraintLogical((ToscaConstraintLogical) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey tclParentKey = new PfConceptKey("tParentKey", "0.0.1"); + PfReferenceKey tclKey = new PfReferenceKey(tclParentKey, "trigger0"); + ToscaConstraintLogical tcl = new ToscaConstraintLogical(tclKey, ToscaConstraintLogical.Operation.EQ); + + try { + new ToscaConstraintLogical(tcl); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("cannot copy an immutable constraint", exc.getMessage()); + } + + ToscaConstraintLogical tclClone1 = new ToscaConstraintLogical(); + try { + tcl.copyTo(tclClone1); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("cannot copy an immutable constraint", exc.getMessage()); + } + tclClone1 = new ToscaConstraintLogical(tclKey, ToscaConstraintLogical.Operation.EQ); + + assertEquals(tcl, tclClone1); + assertEquals(0, tcl.compareTo(tclClone1)); + + assertEquals(-1, tcl.compareTo(null)); + assertEquals(0, tcl.compareTo(tcl)); + assertFalse(tcl.compareTo(tcl.getKey()) == 0); + + ToscaConstraintLogical differentTcl = + new ToscaConstraintLogical(new PfReferenceKey(), ToscaConstraintLogical.Operation.EQ); + assertFalse(tcl.compareTo(differentTcl) == 0); + + ToscaConstraintLogical otherTc = new ToscaConstraintLogical(tclKey, ToscaConstraintLogical.Operation.EQ); + assertEquals(0, tcl.compareTo(otherTc)); + + try { + tcl.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(1, tcl.getKeys().size()); + assertEquals(1, new ToscaConstraintLogical().getKeys().size()); + + ToscaConstraintLogical tclClone0 = new ToscaConstraintLogical(); + new ToscaConstraintLogical().clean(); + tcl.clean(); + assertEquals(tclClone0, tcl); + + assertFalse(new ToscaConstraintLogical().validate(new PfValidationResult()).isValid()); + assertTrue(tcl.validate(new PfValidationResult()).isValid()); + + try { + tcl.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + DummyToscaConstraint dtc = new DummyToscaConstraint(); + try { + new DummyToscaConstraint(dtc); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("cannot copy an immutable constraint", exc.getMessage()); + } + + try { + new DummyToscaConstraint((PfReferenceKey)null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new DummyToscaConstraint((DummyToscaConstraint)null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + DummyToscaConstraint dtcClone = new DummyToscaConstraint(); + + assertEquals(dtc, dtcClone); + assertEquals(dtc, dtc); + assertEquals(0, dtc.compareTo(dtcClone)); + assertEquals(0, dtc.compareTo(dtc)); + assertEquals(-1, dtc.compareTo(null)); + assertEquals(0, dtc.compareTo(dtcClone)); + assertFalse(dtc.compareTo(dtcClone.getKey()) == 0); + + try { + dtc.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + try { + dtc.copyTo(dtcClone); + fail("target should throw an exception"); + } catch (Exception exc) { + assertEquals("cannot copy an immutable constraint", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaDataTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaDataTypeTest.java new file mode 100644 index 000000000..d0b9c7d9b --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaDataTypeTest.java @@ -0,0 +1,140 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.concepts.ToscaConstraintLogical.Operation; + +/** + * DAO test for ToscaDatatype. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaDataTypeTest { + + @Test + public void testDataTypePojo() { + assertNotNull(new ToscaDataType()); + assertNotNull(new ToscaDataType(new PfConceptKey())); + assertNotNull(new ToscaDataType(new ToscaDataType())); + + try { + new ToscaDataType((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaDataType((ToscaDataType) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey dtKey = new PfConceptKey("tdt", "0.0.1"); + ToscaDataType tdt = new ToscaDataType(dtKey); + + List<ToscaConstraint> constraints = new ArrayList<>(); + ToscaConstraintLogicalString lsc = + new ToscaConstraintLogicalString(new PfReferenceKey(dtKey, "sc"), Operation.EQ, "hello"); + constraints.add(lsc); + tdt.setConstraints(constraints); + assertEquals(constraints, tdt.getConstraints()); + + List<ToscaProperty> properties = new ArrayList<>(); + ToscaProperty tp = new ToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", "0.0.1")); + properties.add(tp); + tdt.setProperties(properties); + assertEquals(properties, tdt.getProperties()); + + ToscaDataType tdtClone0 = new ToscaDataType(tdt); + assertEquals(tdt, tdtClone0); + assertEquals(0, tdt.compareTo(tdtClone0)); + + ToscaDataType tdtClone1 = new ToscaDataType(); + tdt.copyTo(tdtClone1); + assertEquals(tdt, tdtClone1); + assertEquals(0, tdt.compareTo(tdtClone1)); + + assertEquals(-1, tdt.compareTo(null)); + assertEquals(0, tdt.compareTo(tdt)); + assertFalse(tdt.compareTo(tdt.getKey()) == 0); + + PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1"); + ToscaDataType otherDt = new ToscaDataType(otherDtKey); + + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setKey(dtKey); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setConstraints(constraints); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setProperties(properties); + assertEquals(0, tdt.compareTo(otherDt)); + + try { + tdt.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(4, tdt.getKeys().size()); + assertEquals(1, new ToscaDataType().getKeys().size()); + + new ToscaDataType().clean(); + tdt.clean(); + assertEquals(tdtClone0, tdt); + + assertFalse(new ToscaDataType().validate(new PfValidationResult()).isValid()); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + + tdt.getConstraints().add(null); + assertFalse(tdt.validate(new PfValidationResult()).isValid()); + tdt.getConstraints().remove(null); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + + tdt.getProperties().add(null); + assertFalse(tdt.validate(new PfValidationResult()).isValid()); + tdt.getProperties().remove(null); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + + try { + tdt.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaDataTypesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaDataTypesTest.java new file mode 100644 index 000000000..cd9c53e7c --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaDataTypesTest.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; + +public class ToscaDataTypesTest { + + @Test + public void testDataTypes() { + assertNotNull(new ToscaDataTypes()); + assertNotNull(new ToscaDataTypes(new PfConceptKey())); + assertNotNull(new ToscaDataTypes(new PfConceptKey(), new TreeMap<PfConceptKey, ToscaDataType>())); + assertNotNull(new ToscaDataTypes(new ToscaDataTypes())); + + try { + new ToscaDataTypes((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaDataTypes((ToscaDataTypes) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaDataTypes(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaDataTypes(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaDataTypes(null, new TreeMap<PfConceptKey, ToscaDataType>()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaEntrySchemaTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaEntrySchemaTest.java new file mode 100644 index 000000000..97d71a203 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaEntrySchemaTest.java @@ -0,0 +1,147 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.concepts.ToscaConstraintLogical.Operation; + +/** + * DAO test for ToscaEntrySchema. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaEntrySchemaTest { + + @Test + public void testEntrySchemaPojo() { + assertNotNull(new ToscaEntrySchema()); + assertNotNull(new ToscaEntrySchema(new PfReferenceKey())); + assertNotNull(new ToscaEntrySchema(new PfReferenceKey(), new PfConceptKey())); + assertNotNull(new ToscaEntrySchema(new ToscaEntrySchema())); + + try { + new ToscaEntrySchema((PfReferenceKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaEntrySchema((ToscaEntrySchema) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfReferenceKey esKey = new PfReferenceKey("entrySchemaParent", "0.0.1", "entrySchema"); + PfConceptKey typeKey = new PfConceptKey("type", "0.0.1"); + ToscaEntrySchema tes = new ToscaEntrySchema(esKey, typeKey); + + tes.setDescription("A Description"); + assertEquals("A Description", tes.getDescription()); + + List<ToscaConstraint> constraints = new ArrayList<>(); + ToscaConstraintLogicalString lsc = + new ToscaConstraintLogicalString(new PfReferenceKey(esKey, "sc"), Operation.EQ, "hello"); + constraints.add(lsc); + tes.setConstraints(constraints); + assertEquals(constraints, tes.getConstraints()); + + ToscaEntrySchema tdtClone0 = new ToscaEntrySchema(tes); + assertEquals(tes, tdtClone0); + assertEquals(0, tes.compareTo(tdtClone0)); + + ToscaEntrySchema tdtClone1 = new ToscaEntrySchema(); + tes.copyTo(tdtClone1); + assertEquals(tes, tdtClone1); + assertEquals(0, tes.compareTo(tdtClone1)); + + assertEquals(-1, tes.compareTo(null)); + assertEquals(0, tes.compareTo(tes)); + assertFalse(tes.compareTo(tes.getKey()) == 0); + + PfReferenceKey otherEsKey = new PfReferenceKey("entrySchemaParent", "0.0.1", "otherEntrySchema"); + ToscaEntrySchema otherEs = new ToscaEntrySchema(otherEsKey); + + assertFalse(tes.compareTo(otherEs) == 0); + otherEs.setKey(esKey); + assertFalse(tes.compareTo(otherEs) == 0); + otherEs.setType(typeKey); + assertFalse(tes.compareTo(otherEs) == 0); + otherEs.setDescription("A Description"); + assertFalse(tes.compareTo(otherEs) == 0); + otherEs.setConstraints(constraints); + assertEquals(0, tes.compareTo(otherEs)); + + try { + tes.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(3, tes.getKeys().size()); + assertEquals(2, new ToscaEntrySchema().getKeys().size()); + + new ToscaEntrySchema().clean(); + tes.clean(); + assertEquals(tdtClone0, tes); + + assertFalse(new ToscaEntrySchema().validate(new PfValidationResult()).isValid()); + assertTrue(tes.validate(new PfValidationResult()).isValid()); + + tes.setType(PfConceptKey.getNullKey()); + assertFalse(tes.validate(new PfValidationResult()).isValid()); + tes.setType(null); + assertFalse(tes.validate(new PfValidationResult()).isValid()); + tes.setType(typeKey); + assertTrue(tes.validate(new PfValidationResult()).isValid()); + + tes.setDescription("");; + assertFalse(tes.validate(new PfValidationResult()).isValid()); + tes.setDescription("A Description"); + assertTrue(tes.validate(new PfValidationResult()).isValid()); + + tes.getConstraints().add(null); + assertFalse(tes.validate(new PfValidationResult()).isValid()); + tes.getConstraints().remove(null); + assertTrue(tes.validate(new PfValidationResult()).isValid()); + + try { + tes.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaEventFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaEventFilterTest.java new file mode 100644 index 000000000..a45d65d69 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaEventFilterTest.java @@ -0,0 +1,165 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaEventFilter. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaEventFilterTest { + + @Test + public void testEventFilterPojo() { + assertNotNull(new ToscaEventFilter()); + assertNotNull(new ToscaEventFilter(new PfReferenceKey())); + assertNotNull(new ToscaEventFilter(new PfReferenceKey(), new PfConceptKey())); + assertNotNull(new ToscaEventFilter(new ToscaEventFilter())); + + try { + new ToscaEventFilter((PfReferenceKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaEventFilter(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaEventFilter(null, new PfConceptKey()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaEventFilter(new PfReferenceKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("node is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaEventFilter((ToscaEventFilter) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey efParentKey = new PfConceptKey("tParentKey", "0.0.1"); + PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0"); + PfConceptKey nodeKey = new PfConceptKey("tParentKey", "0.0.1"); + ToscaEventFilter tef = new ToscaEventFilter(efKey, nodeKey); + + tef.setRequirement("A Requrement"); + assertEquals("A Requrement", tef.getRequirement()); + + tef.setCapability("A Capability"); + assertEquals("A Capability", tef.getCapability()); + + ToscaEventFilter tdtClone0 = new ToscaEventFilter(tef); + assertEquals(tef, tdtClone0); + assertEquals(0, tef.compareTo(tdtClone0)); + + ToscaEventFilter tdtClone1 = new ToscaEventFilter(); + tef.copyTo(tdtClone1); + assertEquals(tef, tdtClone1); + assertEquals(0, tef.compareTo(tdtClone1)); + + assertEquals(-1, tef.compareTo(null)); + assertEquals(0, tef.compareTo(tef)); + assertFalse(tef.compareTo(tef.getKey()) == 0); + + PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherEventFilter"); + ToscaEventFilter otherDt = new ToscaEventFilter(otherDtKey); + + assertFalse(tef.compareTo(otherDt) == 0); + otherDt.setKey(efKey); + assertFalse(tef.compareTo(otherDt) == 0); + otherDt.setNode(nodeKey); + assertFalse(tef.compareTo(otherDt) == 0); + otherDt.setRequirement("A Requrement"); + assertFalse(tef.compareTo(otherDt) == 0); + otherDt.setCapability("A Capability"); + assertEquals(0, tef.compareTo(otherDt)); + + try { + tef.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(2, tef.getKeys().size()); + assertEquals(2, new ToscaEventFilter().getKeys().size()); + + new ToscaEventFilter().clean(); + tef.clean(); + assertEquals(tdtClone0, tef); + + assertFalse(new ToscaEventFilter().validate(new PfValidationResult()).isValid()); + assertTrue(tef.validate(new PfValidationResult()).isValid()); + + tef.setRequirement(null); + assertTrue(tef.validate(new PfValidationResult()).isValid()); + tef.setRequirement(""); + assertFalse(tef.validate(new PfValidationResult()).isValid()); + tef.setRequirement("A Requrement"); + assertTrue(tef.validate(new PfValidationResult()).isValid()); + + tef.setCapability(null); + assertTrue(tef.validate(new PfValidationResult()).isValid()); + tef.setCapability(""); + assertFalse(tef.validate(new PfValidationResult()).isValid()); + tef.setCapability("A Capability"); + assertTrue(tef.validate(new PfValidationResult()).isValid()); + + tef.setNode(null); + assertFalse(tef.validate(new PfValidationResult()).isValid()); + tef.setNode(PfConceptKey.getNullKey()); + assertFalse(tef.validate(new PfValidationResult()).isValid()); + tef.setNode(nodeKey); + assertTrue(tef.validate(new PfValidationResult()).isValid()); + + try { + tef.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaModelTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaModelTest.java new file mode 100644 index 000000000..ca6445f13 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaModelTest.java @@ -0,0 +1,142 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Map; +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelService; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaDatatype. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaModelTest { + + @Test + public void testModelPojo() { + assertNotNull(new ToscaModel()); + assertNotNull(new ToscaModel(new PfConceptKey())); + assertNotNull(new ToscaModel(new PfConceptKey(), new ToscaServiceTemplates())); + assertNotNull(new ToscaModel(new ToscaModel())); + + try { + new ToscaModel((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaModel(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaModel(null, new ToscaServiceTemplates()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaModel(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("serviceTemplates is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaModel((ToscaModel) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey tstsKey = new PfConceptKey("tsts", "0.0.1"); + Map<PfConceptKey, ToscaServiceTemplate> tstMap = new TreeMap<>(); + ToscaServiceTemplates tsts = new ToscaServiceTemplates(tstsKey, tstMap); + PfConceptKey tmKey = new PfConceptKey("tst", "0.0.1"); + ToscaModel tm = new ToscaModel(tmKey, tsts); + + ToscaModel tttClone0 = new ToscaModel(tm); + assertEquals(tm, tttClone0); + assertEquals(0, tm.compareTo(tttClone0)); + + ToscaModel tttClone1 = new ToscaModel(); + tm.copyTo(tttClone1); + assertEquals(tm, tttClone1); + assertEquals(0, tm.compareTo(tttClone1)); + + assertEquals(-1, tm.compareTo(null)); + assertEquals(0, tm.compareTo(tm)); + assertFalse(tm.compareTo(tm.getKey()) == 0); + + PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1"); + ToscaModel otherDt = new ToscaModel(otherDtKey); + + assertFalse(tm.compareTo(otherDt) == 0); + otherDt.setKey(tmKey); + assertFalse(tm.compareTo(otherDt) == 0); + otherDt.setServiceTemplates(tsts); + assertEquals(0, tm.compareTo(otherDt)); + + try { + tm.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("targetObject is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(2, tm.getKeys().size()); + assertEquals(2, new ToscaModel().getKeys().size()); + + new ToscaModel().clean(); + tm.clean(); + assertEquals(tttClone0, tm); + + assertFalse(new ToscaModel().validate(new PfValidationResult()).isValid()); + assertFalse(tm.validate(new PfValidationResult()).isValid()); + + tm.register(); + assertTrue(PfModelService.existsModel(tm.getServiceTemplates().getId())); + PfModelService.deregisterModel(tm.getServiceTemplates().getId()); + + try { + tm.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPoliciesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPoliciesTest.java new file mode 100644 index 000000000..76fdd538b --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPoliciesTest.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; + +public class ToscaPoliciesTest { + + @Test + public void testPolicies() { + assertNotNull(new ToscaPolicies()); + assertNotNull(new ToscaPolicies(new PfConceptKey())); + assertNotNull(new ToscaPolicies(new PfConceptKey(), new TreeMap<PfConceptKey, ToscaPolicy>())); + assertNotNull(new ToscaPolicies(new ToscaPolicies())); + + try { + new ToscaPolicies((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicies((ToscaPolicies) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicies(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicies(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicies(null, new TreeMap<PfConceptKey, ToscaPolicy>()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTest.java new file mode 100644 index 000000000..295c0f2fd --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTest.java @@ -0,0 +1,162 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaDatatype. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPolicyTest { + + @Test + public void testPolicyPojo() { + assertNotNull(new ToscaPolicy()); + assertNotNull(new ToscaPolicy(new PfConceptKey())); + assertNotNull(new ToscaPolicy(new PfConceptKey(), new PfConceptKey())); + assertNotNull(new ToscaPolicy(new ToscaPolicy())); + + try { + new ToscaPolicy((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicy(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicy(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("type is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicy(null, new PfConceptKey()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicy((ToscaPolicy) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey tpKey = new PfConceptKey("tdt", "0.0.1"); + PfConceptKey ptKey = new PfConceptKey("policyType", "0.0.1"); + ToscaPolicy tp = new ToscaPolicy(tpKey, ptKey); + + List<ToscaProperty> properties = new ArrayList<>(); + ToscaProperty property = new ToscaProperty(new PfReferenceKey(tpKey, "pr"), new PfConceptKey("type", "0.0.1")); + properties.add(property); + tp.setProperties(properties); + assertEquals(properties, tp.getProperties()); + + List<PfConceptKey> targets = new ArrayList<>(); + PfConceptKey target = new PfConceptKey("target", "0.0.1"); + targets.add(target); + tp.setTargets(targets); + assertEquals(targets, tp.getTargets()); + + ToscaPolicy tdtClone0 = new ToscaPolicy(tp); + assertEquals(tp, tdtClone0); + assertEquals(0, tp.compareTo(tdtClone0)); + + ToscaPolicy tdtClone1 = new ToscaPolicy(); + tp.copyTo(tdtClone1); + assertEquals(tp, tdtClone1); + assertEquals(0, tp.compareTo(tdtClone1)); + + assertEquals(-1, tp.compareTo(null)); + assertEquals(0, tp.compareTo(tp)); + assertFalse(tp.compareTo(tp.getKey()) == 0); + + PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1"); + ToscaPolicy otherDt = new ToscaPolicy(otherDtKey); + + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setKey(tpKey); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setType(ptKey); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setProperties(properties); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setTargets(targets); + assertEquals(0, tp.compareTo(otherDt)); + + try { + tp.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(5, tp.getKeys().size()); + assertEquals(2, new ToscaPolicy().getKeys().size()); + + new ToscaPolicy().clean(); + tp.clean(); + assertEquals(tdtClone0, tp); + + assertFalse(new ToscaPolicy().validate(new PfValidationResult()).isValid()); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + tp.getProperties().add(null); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.getProperties().remove(null); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + tp.getTargets().add(null); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.getTargets().remove(null); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + try { + tp.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypeTest.java new file mode 100644 index 000000000..7926bfe78 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypeTest.java @@ -0,0 +1,208 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaPolicyType. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPolicyTypeTest { + + @Test + public void testPolicyTypePojo() { + assertNotNull(new ToscaPolicyType()); + assertNotNull(new ToscaPolicyType(new PfConceptKey())); + assertNotNull(new ToscaPolicyType(new ToscaPolicyType())); + + try { + new ToscaPolicyType((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicyType((ToscaPolicyType) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey ptKey = new PfConceptKey("tdt", "0.0.1"); + ToscaPolicyType tpt = new ToscaPolicyType(ptKey); + + PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", "0.0.1"); + tpt.setDerivedFrom(derivedFromKey); + + Map<String, String> metadata = new HashMap<>(); + metadata.put("key", "value"); + tpt.setMetadata(metadata); + assertEquals(metadata, tpt.getMetadata()); + + tpt.setDescription("A Description"); + + PfConceptKey propTypeKey = new PfConceptKey("propType", "0.0.1"); + List<ToscaProperty> properties = new ArrayList<>(); + ToscaProperty tp = new ToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey); + properties.add(tp); + tpt.setProperties(properties); + assertEquals(properties, tpt.getProperties()); + + List<PfConceptKey> targets = new ArrayList<>(); + PfConceptKey target = new PfConceptKey("target", "0.0.1"); + targets.add(target); + tpt.setTargets(targets); + assertEquals(targets, tpt.getTargets()); + + List<ToscaTrigger> triggers = new ArrayList<>(); + ToscaTrigger trigger = new ToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action"); + triggers.add(trigger); + tpt.setTriggers(triggers); + assertEquals(triggers, tpt.getTriggers()); + + ToscaPolicyType tdtClone0 = new ToscaPolicyType(tpt); + assertEquals(tpt, tdtClone0); + assertEquals(0, tpt.compareTo(tdtClone0)); + + ToscaPolicyType tdtClone1 = new ToscaPolicyType(); + tpt.copyTo(tdtClone1); + assertEquals(tpt, tdtClone1); + assertEquals(0, tpt.compareTo(tdtClone1)); + + assertEquals(-1, tpt.compareTo(null)); + assertEquals(0, tpt.compareTo(tpt)); + assertFalse(tpt.compareTo(tpt.getKey()) == 0); + + PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1"); + ToscaPolicyType otherDt = new ToscaPolicyType(otherDtKey); + + assertFalse(tpt.compareTo(otherDt) == 0); + otherDt.setKey(ptKey); + assertFalse(tpt.compareTo(otherDt) == 0); + otherDt.setDerivedFrom(derivedFromKey); + assertFalse(tpt.compareTo(otherDt) == 0); + otherDt.setMetadata(metadata); + assertFalse(tpt.compareTo(otherDt) == 0); + otherDt.setDescription("A Description"); + assertFalse(tpt.compareTo(otherDt) == 0); + otherDt.setProperties(properties); + assertFalse(tpt.compareTo(otherDt) == 0); + otherDt.setTargets(targets); + assertFalse(tpt.compareTo(otherDt) == 0); + otherDt.setTriggers(triggers); + assertEquals(0, tpt.compareTo(otherDt)); + + try { + tpt.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(6, tpt.getKeys().size()); + assertEquals(1, new ToscaPolicyType().getKeys().size()); + + new ToscaPolicyType().clean(); + tpt.clean(); + assertEquals(tdtClone0, tpt); + + assertFalse(new ToscaPolicyType().validate(new PfValidationResult()).isValid()); + assertTrue(tpt.validate(new PfValidationResult()).isValid()); + + tpt.getProperties().add(null); + assertFalse(tpt.validate(new PfValidationResult()).isValid()); + tpt.getProperties().remove(null); + assertTrue(tpt.validate(new PfValidationResult()).isValid()); + + tpt.getTargets().add(null); + assertFalse(tpt.validate(new PfValidationResult()).isValid()); + tpt.getTargets().remove(null); + assertTrue(tpt.validate(new PfValidationResult()).isValid()); + + tpt.getTriggers().add(null); + assertFalse(tpt.validate(new PfValidationResult()).isValid()); + tpt.getTriggers().remove(null); + assertTrue(tpt.validate(new PfValidationResult()).isValid()); + + tpt.getMetadata().put(null, null); + assertFalse(tpt.validate(new PfValidationResult()).isValid()); + tpt.getMetadata().remove(null); + assertTrue(tpt.validate(new PfValidationResult()).isValid()); + + tpt.getMetadata().put("nullKey", null); + assertFalse(tpt.validate(new PfValidationResult()).isValid()); + tpt.getMetadata().remove("nullKey"); + assertTrue(tpt.validate(new PfValidationResult()).isValid()); + + tpt.setDescription("");; + assertFalse(tpt.validate(new PfValidationResult()).isValid()); + tpt.setDescription("A Description"); + assertTrue(tpt.validate(new PfValidationResult()).isValid()); + + tpt.setDerivedFrom(PfConceptKey.getNullKey()); + assertFalse(tpt.validate(new PfValidationResult()).isValid()); + tpt.setDerivedFrom(derivedFromKey); + assertTrue(tpt.validate(new PfValidationResult()).isValid()); + + try { + tpt.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaEntityType((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaEntityType((ToscaEntityType) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + ToscaEntityType tet = new ToscaEntityType(tpt.getKey()); + assertEquals(-1, tet.compareTo(null)); + assertEquals(0, tet.compareTo(tet)); + assertFalse(tet.compareTo(tet.getKey()) == 0); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypesTest.java new file mode 100644 index 000000000..069346331 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypesTest.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; + +public class ToscaPolicyTypesTest { + + @Test + public void testPolicyTypes() { + assertNotNull(new ToscaPolicyTypes()); + assertNotNull(new ToscaPolicyTypes(new PfConceptKey())); + assertNotNull(new ToscaPolicyTypes(new PfConceptKey(), new TreeMap<PfConceptKey, ToscaPolicyType>())); + assertNotNull(new ToscaPolicyTypes(new ToscaPolicyTypes())); + + try { + new ToscaPolicyTypes((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicyTypes((ToscaPolicyTypes) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicyTypes(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicyTypes(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaPolicyTypes(null, new TreeMap<PfConceptKey, ToscaPolicyType>()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPropertyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPropertyTest.java new file mode 100644 index 000000000..d0563fd26 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaPropertyTest.java @@ -0,0 +1,223 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.concepts.ToscaConstraintLogical.Operation; + +/** + * DAO test for ToscaProperty. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPropertyTest { + + @Test + public void testPropertyPojo() { + assertNotNull(new ToscaProperty()); + assertNotNull(new ToscaProperty(new PfReferenceKey())); + assertNotNull(new ToscaProperty(new PfReferenceKey(), new PfConceptKey())); + assertNotNull(new ToscaProperty(new ToscaProperty())); + + try { + new ToscaProperty((PfReferenceKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaProperty(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaProperty(null, new PfConceptKey()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaProperty(new PfReferenceKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("type is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaProperty((ToscaProperty) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey pparentKey = new PfConceptKey("tParentKey", "0.0.1"); + PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0"); + PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", "0.0.1"); + ToscaProperty tp = new ToscaProperty(pkey, ptypeKey); + + tp.setDescription("A Description"); + assertEquals("A Description", tp.getDescription()); + + tp.setRequired(false); + assertFalse(tp.isRequired()); + + PfConceptKey tdefaultKey = new PfConceptKey("defaultKey", "0.0.1"); + tp.setDefaultValue(tdefaultKey); + + tp.setStatus(ToscaProperty.Status.SUPPORTED); + + List<ToscaConstraint> constraints = new ArrayList<>(); + ToscaConstraintLogicalString lsc = + new ToscaConstraintLogicalString(new PfReferenceKey(pkey, "sc"), Operation.EQ, "hello"); + constraints.add(lsc); + tp.setConstraints(constraints); + assertEquals(constraints, tp.getConstraints()); + + PfReferenceKey esKey = new PfReferenceKey(pkey, "entrySchema"); + PfConceptKey typeKey = new PfConceptKey("type", "0.0.1"); + ToscaEntrySchema tes = new ToscaEntrySchema(esKey, typeKey); + tp.setEntrySchema(tes); + + ToscaProperty tdtClone0 = new ToscaProperty(tp); + assertEquals(tp, tdtClone0); + assertEquals(0, tp.compareTo(tdtClone0)); + + ToscaProperty tdtClone1 = new ToscaProperty(); + tp.copyTo(tdtClone1); + assertEquals(tp, tdtClone1); + assertEquals(0, tp.compareTo(tdtClone1)); + + assertEquals(-1, tp.compareTo(null)); + assertEquals(0, tp.compareTo(tp)); + assertFalse(tp.compareTo(tp.getKey()) == 0); + + PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherProperty"); + ToscaProperty otherDt = new ToscaProperty(otherDtKey); + + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setKey(pkey); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setType(ptypeKey); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setDescription("A Description"); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setRequired(false); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setDefaultValue(tdefaultKey); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setStatus(ToscaProperty.Status.SUPPORTED); + assertFalse(tp.compareTo(otherDt) == 0); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setConstraints(constraints); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setEntrySchema(tes); + assertEquals(0, tp.compareTo(otherDt)); + + otherDt.setRequired(true); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setRequired(false); + assertEquals(0, tp.compareTo(otherDt)); + + otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED); + assertFalse(tp.compareTo(otherDt) == 0); + otherDt.setStatus(ToscaProperty.Status.SUPPORTED); + assertEquals(0, tp.compareTo(otherDt)); + + try { + tp.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(6, tp.getKeys().size()); + assertEquals(2, new ToscaProperty().getKeys().size()); + + new ToscaProperty().clean(); + tp.clean(); + assertEquals(tdtClone0, tp); + + assertFalse(new ToscaProperty().validate(new PfValidationResult()).isValid()); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + tp.setDescription(null); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + tp.setDescription(""); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.setDescription("A Description"); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + tp.setType(null); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.setType(typeKey); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + tp.setType(PfConceptKey.getNullKey()); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.setType(typeKey); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + tp.setDefaultValue(null); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + tp.setDefaultValue(tdefaultKey); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + tp.setDefaultValue(PfConceptKey.getNullKey()); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.setDefaultValue(tdefaultKey); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + tp.getConstraints().add(null); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.getConstraints().remove(null); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + try { + tp.setStatus(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("status is marked @NonNull but is null", exc.getMessage()); + } + + try { + tp.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplateTest.java new file mode 100644 index 000000000..0fc7a0bf3 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplateTest.java @@ -0,0 +1,170 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Map; +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaDatatype. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaServiceTemplateTest { + + @Test + public void testServiceTemplatePojo() { + assertNotNull(new ToscaServiceTemplate()); + assertNotNull(new ToscaServiceTemplate(new PfConceptKey())); + assertNotNull(new ToscaServiceTemplate(new PfConceptKey(), "")); + assertNotNull(new ToscaServiceTemplate(new ToscaServiceTemplate())); + + try { + new ToscaServiceTemplate((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaServiceTemplate(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaServiceTemplate(null, ""); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaServiceTemplate(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("toscaDefinitionsVersion is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaServiceTemplate((ToscaServiceTemplate) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey tstKey = new PfConceptKey("tst", "0.0.1"); + ToscaServiceTemplate tst = new ToscaServiceTemplate(tstKey, "Tosca Version"); + + PfConceptKey dataTypeKey = new PfConceptKey("DataType", "0.0.1"); + ToscaDataType dataType0 = new ToscaDataType(dataTypeKey); + PfConceptKey dtsKey = new PfConceptKey("dts", "0.0.1"); + Map<PfConceptKey, ToscaDataType> dataTypeMap = new TreeMap<>(); + dataTypeMap.put(dataTypeKey, dataType0); + ToscaDataTypes dataTypes = new ToscaDataTypes(dtsKey, dataTypeMap); + tst.setDataTypes(dataTypes); + assertEquals(dataTypes, tst.getDataTypes()); + + PfConceptKey policyTypeKey = new PfConceptKey("DataType", "0.0.1"); + ToscaPolicyType policyType0 = new ToscaPolicyType(policyTypeKey); + PfConceptKey ptsKey = new PfConceptKey("dts", "0.0.1"); + Map<PfConceptKey, ToscaPolicyType> policyTypeMap = new TreeMap<>(); + policyTypeMap.put(policyTypeKey, policyType0); + ToscaPolicyTypes policyTypes = new ToscaPolicyTypes(ptsKey, policyTypeMap); + tst.setPolicyTypes(policyTypes); + assertEquals(policyTypes, tst.getPolicyTypes()); + + PfConceptKey tttKey = new PfConceptKey("TopologyTemplate", "0.0.1"); + ToscaTopologyTemplate ttt = new ToscaTopologyTemplate(tttKey); + tst.setTopologyTemplate(ttt); + assertEquals(ttt, tst.getTopologyTemplate()); + + ToscaServiceTemplate tttClone0 = new ToscaServiceTemplate(tst); + assertEquals(tst, tttClone0); + assertEquals(0, tst.compareTo(tttClone0)); + + ToscaServiceTemplate tttClone1 = new ToscaServiceTemplate(); + tst.copyTo(tttClone1); + assertEquals(tst, tttClone1); + assertEquals(0, tst.compareTo(tttClone1)); + + assertEquals(-1, tst.compareTo(null)); + assertEquals(0, tst.compareTo(tst)); + assertFalse(tst.compareTo(tst.getKey()) == 0); + + PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1"); + ToscaServiceTemplate otherDt = new ToscaServiceTemplate(otherDtKey); + + assertFalse(tst.compareTo(otherDt) == 0); + otherDt.setKey(tstKey); + assertFalse(tst.compareTo(otherDt) == 0); + otherDt.setToscaDefinitionsVersion("Tosca Version"); + assertFalse(tst.compareTo(otherDt) == 0); + otherDt.setDataTypes(dataTypes); + assertFalse(tst.compareTo(otherDt) == 0); + otherDt.setPolicyTypes(policyTypes); + assertFalse(tst.compareTo(otherDt) == 0); + otherDt.setTopologyTemplate(ttt); + assertEquals(0, tst.compareTo(otherDt)); + + try { + tst.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(6, tst.getKeys().size()); + assertEquals(1, new ToscaServiceTemplate().getKeys().size()); + + new ToscaServiceTemplate().clean(); + tst.clean(); + assertEquals(tttClone0, tst); + + assertFalse(new ToscaServiceTemplate().validate(new PfValidationResult()).isValid()); + assertTrue(tst.validate(new PfValidationResult()).isValid()); + + tst.setDescription(null); + assertTrue(tst.validate(new PfValidationResult()).isValid()); + tst.setDescription(""); + assertFalse(tst.validate(new PfValidationResult()).isValid()); + tst.setDescription("A Description"); + assertTrue(tst.validate(new PfValidationResult()).isValid()); + + try { + tst.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplatesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplatesTest.java new file mode 100644 index 000000000..c866f11c7 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplatesTest.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; + +public class ToscaServiceTemplatesTest { + + @Test + public void testServiceTemplates() { + assertNotNull(new ToscaServiceTemplates()); + assertNotNull(new ToscaServiceTemplates(new PfConceptKey())); + assertNotNull(new ToscaServiceTemplates(new PfConceptKey(), new TreeMap<PfConceptKey, ToscaServiceTemplate>())); + assertNotNull(new ToscaServiceTemplates(new ToscaServiceTemplates())); + + try { + new ToscaServiceTemplates((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaServiceTemplates((ToscaServiceTemplates) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaServiceTemplates(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaServiceTemplates(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaServiceTemplates(null, new TreeMap<PfConceptKey, ToscaServiceTemplate>()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTimeIntervalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTimeIntervalTest.java new file mode 100644 index 000000000..33af68343 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTimeIntervalTest.java @@ -0,0 +1,181 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Date; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaTimeInterval. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaTimeIntervalTest { + + @Test + public void testTimeIntervalPojo() { + assertNotNull(new ToscaTimeInterval()); + assertNotNull(new ToscaTimeInterval(new PfReferenceKey())); + assertNotNull(new ToscaTimeInterval(new PfReferenceKey(), new Date(), new Date())); + assertNotNull(new ToscaTimeInterval(new ToscaTimeInterval())); + + try { + new ToscaTimeInterval((PfReferenceKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTimeInterval(null, null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTimeInterval(null, null, new Date()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTimeInterval(null, new Date(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTimeInterval(null, new Date(), new Date()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTimeInterval(new PfReferenceKey(), null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("startTime is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTimeInterval(new PfReferenceKey(), null, new Date()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("startTime is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTimeInterval(new PfReferenceKey(), new Date(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("endTime is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTimeInterval((ToscaTimeInterval) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1"); + PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0"); + Date startTime = new Date(1000); + Date endTime = new Date(2000); + ToscaTimeInterval tti = new ToscaTimeInterval(ttiKey, startTime, endTime); + + ToscaTimeInterval tdtClone0 = new ToscaTimeInterval(tti); + assertEquals(tti, tdtClone0); + assertEquals(0, tti.compareTo(tdtClone0)); + + ToscaTimeInterval tdtClone1 = new ToscaTimeInterval(); + tti.copyTo(tdtClone1); + assertEquals(tti, tdtClone1); + assertEquals(0, tti.compareTo(tdtClone1)); + + assertEquals(-1, tti.compareTo(null)); + assertEquals(0, tti.compareTo(tti)); + assertFalse(tti.compareTo(tti.getKey()) == 0); + + PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTimeInterval"); + ToscaTimeInterval otherDt = new ToscaTimeInterval(otherDtKey); + + assertFalse(tti.compareTo(otherDt) == 0); + otherDt.setKey(ttiKey); + assertFalse(tti.compareTo(otherDt) == 0); + otherDt.setStartTime(startTime); + assertFalse(tti.compareTo(otherDt) == 0); + otherDt.setEndTime(endTime); + assertEquals(0, tti.compareTo(otherDt)); + + try { + tti.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(1, tti.getKeys().size()); + assertEquals(1, new ToscaTimeInterval().getKeys().size()); + + new ToscaTimeInterval().clean(); + tti.clean(); + assertEquals(tdtClone0, tti); + + assertFalse(new ToscaTimeInterval().validate(new PfValidationResult()).isValid()); + assertTrue(tti.validate(new PfValidationResult()).isValid()); + + tti.setStartTime(null); + assertFalse(tti.validate(new PfValidationResult()).isValid()); + tti.setStartTime(new Date(endTime.getTime() + 1)); + assertFalse(tti.validate(new PfValidationResult()).isValid()); + tti.setStartTime(startTime); + assertTrue(tti.validate(new PfValidationResult()).isValid()); + + tti.setEndTime(null); + assertFalse(tti.validate(new PfValidationResult()).isValid()); + tti.setEndTime(new Date(startTime.getTime() - 1)); + assertFalse(tti.validate(new PfValidationResult()).isValid()); + tti.setEndTime(endTime); + assertTrue(tti.validate(new PfValidationResult()).isValid()); + + try { + tti.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplateTest.java new file mode 100644 index 000000000..4a288f183 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplateTest.java @@ -0,0 +1,134 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Map; +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; + +/** + * DAO test for ToscaDatatype. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaTopologyTemplateTest { + + @Test + public void testTopologyTemplatePojo() { + assertNotNull(new ToscaTopologyTemplate()); + assertNotNull(new ToscaTopologyTemplate(new PfConceptKey())); + assertNotNull(new ToscaTopologyTemplate(new ToscaTopologyTemplate())); + + try { + new ToscaTopologyTemplate((PfConceptKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTopologyTemplate((ToscaTopologyTemplate) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey tttKey = new PfConceptKey("ttt", "0.0.1"); + ToscaTopologyTemplate ttt = new ToscaTopologyTemplate(tttKey); + + ttt.setDescription("A Description"); + assertEquals("A Description", ttt.getDescription()); + + PfConceptKey policy0TypeKey = new PfConceptKey("Policy0Type", "0.0.1"); + PfConceptKey policy0Key = new PfConceptKey("Policy0", "0.0.1"); + + ToscaPolicy policy0 = new ToscaPolicy(policy0Key, policy0TypeKey); + PfConceptKey polsKey = new PfConceptKey("pols", "0.0.1"); + Map<PfConceptKey, ToscaPolicy> policyMap = new TreeMap<>(); + policyMap.put(policy0Key, policy0); + ToscaPolicies policies = new ToscaPolicies(polsKey, policyMap); + ttt.setPolicies(policies); + + ToscaTopologyTemplate tttClone0 = new ToscaTopologyTemplate(ttt); + assertEquals(ttt, tttClone0); + assertEquals(0, ttt.compareTo(tttClone0)); + + ToscaTopologyTemplate tttClone1 = new ToscaTopologyTemplate(); + ttt.copyTo(tttClone1); + assertEquals(ttt, tttClone1); + assertEquals(0, ttt.compareTo(tttClone1)); + + assertEquals(-1, ttt.compareTo(null)); + assertEquals(0, ttt.compareTo(ttt)); + assertFalse(ttt.compareTo(ttt.getKey()) == 0); + + PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1"); + ToscaTopologyTemplate otherDt = new ToscaTopologyTemplate(otherDtKey); + + assertFalse(ttt.compareTo(otherDt) == 0); + otherDt.setKey(tttKey); + assertFalse(ttt.compareTo(otherDt) == 0); + otherDt.setDescription("A Description"); + assertFalse(ttt.compareTo(otherDt) == 0); + otherDt.setPolicies(policies); + assertEquals(0, ttt.compareTo(otherDt)); + + try { + ttt.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(4, ttt.getKeys().size()); + assertEquals(1, new ToscaTopologyTemplate().getKeys().size()); + + new ToscaTopologyTemplate().clean(); + ttt.clean(); + assertEquals(tttClone0, ttt); + + assertFalse(new ToscaTopologyTemplate().validate(new PfValidationResult()).isValid()); + assertTrue(ttt.validate(new PfValidationResult()).isValid()); + + ttt.setDescription(null); + assertTrue(ttt.validate(new PfValidationResult()).isValid()); + ttt.setDescription(""); + assertFalse(ttt.validate(new PfValidationResult()).isValid()); + ttt.setDescription("A Description"); + assertTrue(ttt.validate(new PfValidationResult()).isValid()); + + try { + ttt.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTriggerTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTriggerTest.java new file mode 100644 index 000000000..d1423a6a4 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/ToscaTriggerTest.java @@ -0,0 +1,228 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.time.Duration; +import java.util.Date; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.concepts.ToscaConstraintLogical.Operation; + +/** + * DAO test for ToscaTrigger. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaTriggerTest { + + @Test + public void testTriggerPojo() { + assertNotNull(new ToscaTrigger()); + assertNotNull(new ToscaTrigger(new PfReferenceKey())); + assertNotNull(new ToscaTrigger(new PfReferenceKey(), "EventType", "Action")); + assertNotNull(new ToscaTrigger(new ToscaTrigger())); + + try { + new ToscaTrigger((PfReferenceKey) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTrigger(null, null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTrigger(null, "EventType", null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTrigger(null, "EventType", "Action"); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTrigger(null, null, "Action"); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTrigger(new PfReferenceKey(), null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("eventType is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTrigger(new PfReferenceKey(), "EventType", null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("action is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTrigger(new PfReferenceKey(), null, "Action"); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("eventType is marked @NonNull but is null", exc.getMessage()); + } + + try { + new ToscaTrigger((ToscaTrigger) null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + PfConceptKey tparentKey = new PfConceptKey("tParentKey", "0.0.1"); + PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0"); + ToscaTrigger tdt = new ToscaTrigger(tkey, "EventType", "Action"); + + ToscaTimeInterval schedule = new ToscaTimeInterval(new PfReferenceKey(tkey, "sched"), new Date(), new Date()); + tdt.setSchedule(schedule); + + ToscaEventFilter targetFilter = + new ToscaEventFilter(new PfReferenceKey(tkey, "filter"), new PfConceptKey("NodeName", "0.0.1")); + tdt.setTargetFilter(targetFilter); + + ToscaConstraintLogicalString lsc = + new ToscaConstraintLogicalString(new PfReferenceKey(tkey, "sc"), Operation.EQ, "hello"); + tdt.setCondition(lsc); + assertEquals(lsc, tdt.getCondition()); + tdt.setConstraint(lsc); + assertEquals(lsc, tdt.getConstraint()); + + tdt.setPeriod(Duration.ZERO); + assertEquals(Duration.ZERO, tdt.getPeriod()); + + tdt.setDescription("A Description"); + assertEquals("A Description", tdt.getDescription()); + + tdt.setMethod("A Method"); + assertEquals("A Method", tdt.getMethod()); + + ToscaTrigger tdtClone0 = new ToscaTrigger(tdt); + assertEquals(tdt, tdtClone0); + assertEquals(0, tdt.compareTo(tdtClone0)); + + ToscaTrigger tdtClone1 = new ToscaTrigger(); + tdt.copyTo(tdtClone1); + assertEquals(tdt, tdtClone1); + assertEquals(0, tdt.compareTo(tdtClone1)); + + assertEquals(-1, tdt.compareTo(null)); + assertEquals(0, tdt.compareTo(tdt)); + assertFalse(tdt.compareTo(tdt.getKey()) == 0); + + PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTrigger"); + ToscaTrigger otherDt = new ToscaTrigger(otherDtKey); + + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setKey(tkey); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setDescription("A Description"); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setEventType("EventType"); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setSchedule(schedule); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setTargetFilter(targetFilter); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setCondition(lsc); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setConstraint(lsc); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setPeriod(Duration.ZERO); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setMethod("A Method"); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setAction("Action"); + assertEquals(0, tdt.compareTo(otherDt)); + + otherDt.setEvaluations(100); + assertFalse(tdt.compareTo(otherDt) == 0); + otherDt.setEvaluations(0); + assertEquals(0, tdt.compareTo(otherDt)); + + try { + tdt.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(6, tdt.getKeys().size()); + assertEquals(1, new ToscaTrigger().getKeys().size()); + + new ToscaTrigger().clean(); + tdt.clean(); + assertEquals(tdtClone0, tdt); + + assertFalse(new ToscaTrigger().validate(new PfValidationResult()).isValid()); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + + tdt.setDescription(null); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + tdt.setDescription(""); + assertFalse(tdt.validate(new PfValidationResult()).isValid()); + tdt.setDescription("A Description"); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + + tdt.setEvaluations(-1); + assertFalse(tdt.validate(new PfValidationResult()).isValid()); + tdt.setEvaluations(100); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + + tdt.setMethod(null); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + tdt.setMethod(""); + assertFalse(tdt.validate(new PfValidationResult()).isValid()); + tdt.setMethod("A Method"); + assertTrue(tdt.validate(new PfValidationResult()).isValid()); + + try { + tdt.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/testconcepts/DummyToscaConstraint.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/testconcepts/DummyToscaConstraint.java new file mode 100644 index 000000000..3b27e1565 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/concepts/testconcepts/DummyToscaConstraint.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts.testconcepts; + +import javax.ws.rs.core.Response; + +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.tosca.concepts.ToscaConstraint; + +/** + * Dummy constraint to test abstract ToscaConstraint class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyToscaConstraint extends ToscaConstraint { + private static final long serialVersionUID = 1L; + + /** + * The Default Constructor creates a {@link DummyToscaConstraint} object with a null key. + */ + public DummyToscaConstraint() { + super(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link DummyToscaConstraint} object with the given concept key. + * + * @param key the key of the constraint + */ + public DummyToscaConstraint(final PfReferenceKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public DummyToscaConstraint(final DummyToscaConstraint copyConcept) { + super(copyConcept); + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } +} |