diff options
Diffstat (limited to 'models-base/src/test/java/org')
15 files changed, 1107 insertions, 67 deletions
diff --git a/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java index c0898c187..0a5b6a0a6 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java @@ -25,32 +25,35 @@ import static org.junit.Assert.assertNotNull; import java.io.IOException; +import javax.ws.rs.core.Response; + import org.junit.Test; public class ExceptionsTest { @Test public void test() { - assertNotNull(new PfModelException("Message")); - assertNotNull(new PfModelException("Message", "String")); - assertNotNull(new PfModelException("Message", new IOException())); - assertNotNull(new PfModelException("Message", new IOException(), "String")); + assertNotNull(new PfModelException(Response.Status.OK, "Message")); + assertNotNull(new PfModelException(Response.Status.OK, "Message", "String")); + assertNotNull(new PfModelException(Response.Status.OK, "Message", new IOException())); + assertNotNull(new PfModelException(Response.Status.OK, "Message", new IOException(), "String")); String key = "A String"; - PfModelException ae = new PfModelException("Message", new IOException("IO exception message"), key); + PfModelException ae = + new PfModelException(Response.Status.OK, "Message", new IOException("IO exception message"), key); assertEquals("Message\ncaused by: Message\ncaused by: IO exception message", ae.getCascadedMessage()); assertEquals(key, ae.getObject()); - assertNotNull(new PfModelRuntimeException("Message")); - assertNotNull(new PfModelRuntimeException("Message", "String")); - assertNotNull(new PfModelRuntimeException("Message", new IOException())); - assertNotNull(new PfModelRuntimeException("Message", new IOException(), "String")); + assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message")); + assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message", "String")); + assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message", new IOException())); + assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message", new IOException(), "String")); String rkey = "A String"; - PfModelRuntimeException re = new PfModelRuntimeException("Runtime Message", - new IOException("IO runtime exception message"), rkey); + PfModelRuntimeException re = new PfModelRuntimeException(Response.Status.OK, "Runtime Message", + new IOException("IO runtime exception message"), rkey); assertEquals("Runtime Message\ncaused by: Runtime Message\ncaused by: IO runtime exception message", - re.getCascadedMessage()); + re.getCascadedMessage()); assertEquals(key, re.getObject()); } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/ModelServiceTest.java b/models-base/src/test/java/org/onap/policy/models/base/ModelServiceTest.java new file mode 100644 index 000000000..0e790d3dc --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/ModelServiceTest.java @@ -0,0 +1,105 @@ +/*- + * ============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.base; + +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.testconcepts.DummyPfModel; + +public class ModelServiceTest { + + @Test + public void testModelService() { + PfModelService.clear(); + + assertFalse(PfModelService.existsModel("NonExistantName")); + try { + PfModelService.getModel("NonExistantName"); + } catch (final Exception e) { + assertEquals("Model for name NonExistantName not found in model service", e.getMessage()); + } + + PfModelService.registerModel("ModelName", new DummyPfModel()); + assertTrue(PfModelService.existsModel("ModelName")); + assertNotNull(PfModelService.getModel("ModelName")); + + PfModelService.deregisterModel("ModelName"); + + assertFalse(PfModelService.existsModel("ModelName")); + try { + PfModelService.getModel("ModelName"); + } catch (final Exception e) { + assertEquals("Model for name ModelName not found in model service", e.getMessage()); + } + + PfModelService.registerModel("ModelName", new DummyPfModel()); + assertTrue(PfModelService.existsModel("ModelName")); + assertNotNull(PfModelService.getModel("ModelName")); + + PfModelService.clear(); + assertFalse(PfModelService.existsModel("ModelName")); + try { + PfModelService.getModel("ModelName"); + } catch (final Exception e) { + assertEquals("Model for name ModelName not found in model service", e.getMessage()); + } + + try { + PfModelService.registerModel(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("modelKey is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfModelService.registerModel("nullModelName", null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("model is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfModelService.registerModel(null, new DummyPfModel()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("modelKey is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfModelService.deregisterModel(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("modelKey is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfModelService.getModel(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("modelKey is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java new file mode 100644 index 000000000..0ed04c4e6 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java @@ -0,0 +1,199 @@ +/*- + * ============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.base; + +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.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.models.base.testconcepts.DummyPfConcept; +import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer; +import org.onap.policy.models.base.testconcepts.DummyPfConceptSub; + +/** + * Test the PfCOnceptCOntainer class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PfConceptContainerTest { + + @Test + public void test() { + DummyPfConceptContainer container = new DummyPfConceptContainer(); + assertNotNull(container); + + container = new DummyPfConceptContainer(); + assertNotNull(container); + + container = new DummyPfConceptContainer(new PfConceptKey()); + assertNotNull(container); + + container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>()); + assertNotNull(container); + + try { + container = new DummyPfConceptContainer((PfConceptKey) null, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + container = new DummyPfConceptContainer(new PfConceptKey(), null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage()); + } + + try { + container = new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + container.getKey().setName("Dummy"); + DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container); + assertNotNull(clonedContainer); + assertEquals("Dummy", clonedContainer.getKey().getName()); + + try { + DummyPfConceptContainer conceptContainter = null; + container = new DummyPfConceptContainer(conceptContainter); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + List<PfKey> keyList = container.getKeys(); + assertEquals(1, keyList.size()); + + PfConceptKey conceptKey = new PfConceptKey("Key", "0.0.1"); + Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>(); + conceptMap.put(conceptKey, new DummyPfConcept(conceptKey)); + + container.setConceptMap(conceptMap); + keyList = container.getKeys(); + assertEquals(2, keyList.size()); + + clonedContainer = new DummyPfConceptContainer(container); + assertNotNull(clonedContainer); + assertEquals("Dummy", clonedContainer.getKey().getName()); + assertEquals(2, clonedContainer.getKeys().size()); + + assertEquals(clonedContainer, container); + container.clean(); + assertEquals(clonedContainer, container); + + PfValidationResult result = new PfValidationResult(); + result = container.validate(result); + assertTrue(result.isOk()); + + assertEquals(0, container.compareTo(clonedContainer)); + + try { + container.copyTo(null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertFalse(container.compareTo(null) == 0); + assertEquals(0, container.compareTo(container)); + assertFalse(container.compareTo(conceptKey) == 0); + + DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container); + testContainer.getKey().setVersion("0.0.2"); + assertFalse(container.compareTo(testContainer) == 0); + testContainer.getKey().setVersion(container.getKey().getVersion()); + assertEquals(0, container.compareTo(testContainer)); + + PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1"); + testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); + assertFalse(container.compareTo(testContainer) == 0); + + try { + container.validate(null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + DummyPfConceptContainer validateContainer = new DummyPfConceptContainer(); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + validateContainer.setKey(new PfConceptKey("VCKey", "0.0.1")); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + + validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); + + validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey())); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + validateContainer.getConceptMap().remove(PfConceptKey.getNullKey()); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); + + validateContainer.getConceptMap().put(testConceptKey, null); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); + + validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey)); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); + + assertEquals(conceptKey, container.get(conceptKey).getKey()); + assertEquals(conceptKey, container.get(conceptKey.getName()).getKey()); + assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey()); + + Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName()); + assertEquals(conceptKey, returnSet.iterator().next().getKey()); + + returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion()); + assertEquals(conceptKey, returnSet.iterator().next().getKey()); + + container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey)); + + DummyPfConceptContainer exceptionOnCopyContainer = new DummyPfConceptContainer(); + try { + container.copyTo(exceptionOnCopyContainer); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals( + "Failed to create a clone of class \"org.onap.policy.models.base.testconcepts.DummyPfConceptSub\"", + exc.getMessage()); + } + } + + @Test(expected = NullPointerException.class) + public void testnullKey() { + PfConceptKey nullKey = null; + new DummyPfConceptContainer(nullKey); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfConceptGetterImplTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfConceptGetterImplTest.java index 0a5ccdc5a..ae5b2ff2b 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfConceptGetterImplTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfConceptGetterImplTest.java @@ -32,12 +32,12 @@ import java.util.TreeSet; import org.junit.Test; /** - * Test the AxConceptGetterImpl class. + * Test the given concept class. */ public class PfConceptGetterImplTest { @Test - public void testAxConceptGetterImpl() { + public void testPfConceptGetterImpl() { NavigableMap<PfConceptKey, PfConceptKey> keyMap = new TreeMap<>(); PfConceptGetterImpl<PfConceptKey> getter = new PfConceptGetterImpl<>(keyMap); diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java index 1ffc5d262..848889cc3 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java @@ -31,8 +31,8 @@ import java.lang.reflect.Field; import org.junit.Test; import org.onap.policy.models.base.PfKey.Compatibility; -import org.onap.policy.models.base.testpojos.DummyPfConcept; -import org.onap.policy.models.base.testpojos.DummyPfKey; +import org.onap.policy.models.base.testconcepts.DummyPfConcept; +import org.onap.policy.models.base.testconcepts.DummyPfKey; public class PfKeyTest { @@ -67,6 +67,9 @@ public class PfKeyTest { PfConceptKey someKey4 = new PfConceptKey(someKey1); someKey4.setVersion("0.1.2"); + PfConceptKey someKey4a = new PfConceptKey(someKey1); + someKey4a.setVersion("0"); + PfConceptKey someKey5 = new PfConceptKey(someKey1); someKey5.setVersion("1.2.2"); @@ -77,13 +80,25 @@ public class PfKeyTest { PfConcept pfc = new DummyPfConcept(); assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId()); + assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId())); + assertTrue(PfConceptKey.getNullKey().isNullKey()); + + try { + PfConceptKey.getNullKey().matchesId(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("id is marked @NonNull but is null", exc.getMessage()); + } + assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey())); assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1)); assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1)); assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1)); assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1)); + assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1)); + assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a)); assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1)); assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1)); @@ -119,8 +134,9 @@ public class PfKeyTest { try { someKey0.compareTo(null); - } catch (IllegalArgumentException e) { - assertEquals("comparison object may not be null", e.getMessage()); + fail("test should throw an exception here"); + } catch (NullPointerException e) { + assertEquals("otherObj is marked @NonNull but is null", e.getMessage()); } assertEquals(0, someKey0.compareTo(someKey0)); @@ -131,6 +147,51 @@ public class PfKeyTest { assertFalse(((PfKey) someKey0).equals(new DummyPfKey())); } + @Test + public void testNullArguments() { + try { + new PfConceptKey((String)null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("id is marked @NonNull but is null", exc.getMessage()); + } + + try { + new PfConceptKey((PfConceptKey)null); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + try { + new PfConceptKey(null, null); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("name is marked @NonNull but is null", exc.getMessage()); + } + + try { + new PfConceptKey("name", null); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("version is marked @NonNull but is null", exc.getMessage()); + } + + try { + new PfConceptKey(null, "0.0.1"); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("name is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfConceptKey key = new PfConceptKey("AKey", "0.0.1"); + key.isCompatible(null); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("otherKey is marked @NonNull but is null", exc.getMessage()); + } + } @Test public void testValidation() { diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java index 7dbde7414..ccdc72dcd 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java @@ -25,24 +25,35 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.models.base.PfKey.Compatibility; +import org.onap.policy.models.base.testconcepts.DummyPfConceptKeySub; public class PfKeyUseTest { + @SuppressWarnings("unlikely-arg-type") @Test - public void test() { + public void testKeyUse() { assertNotNull(new PfKeyUse()); assertNotNull(new PfKeyUse(new PfConceptKey())); assertNotNull(new PfKeyUse(new PfReferenceKey())); + try { + new PfKeyUse((PfKeyUse)null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + PfConceptKey key = new PfConceptKey("Key", "0.0.1"); PfKeyUse keyUse = new PfKeyUse(); keyUse.setKey(key); assertEquals(key, keyUse.getKey()); assertEquals("Key:0.0.1", keyUse.getId()); assertEquals(key, keyUse.getKeys().get(0)); + assertFalse(keyUse.isNullKey()); assertEquals(Compatibility.IDENTICAL, keyUse.getCompatibility(key)); assertTrue(keyUse.isCompatible(key)); @@ -74,5 +85,51 @@ public class PfKeyUseTest { PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey()); PfValidationResult resultNull = new PfValidationResult(); assertEquals(false, keyUseNull.validate(resultNull).isValid()); + + try { + keyUse.setKey(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + keyUse.getCompatibility(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("otherKey is marked @NonNull but is null", exc.getMessage()); + } + + try { + keyUse.isCompatible(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("otherKey is marked @NonNull but is null", exc.getMessage()); + } + + try { + keyUse.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("result is marked @NonNull but is null", exc.getMessage()); + } + + PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey())); + PfKeyUse targetKeyUse = new PfKeyUse(key); + + try { + keyUse.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + try { + testKeyUse.copyTo(targetKeyUse); + keyUse.isCompatible(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("error copying concept key: Some error message", exc.getMessage()); + } } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java new file mode 100644 index 000000000..cf7c41f6b --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java @@ -0,0 +1,145 @@ +package org.onap.policy.models.base; +/*- + * ============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========================================================= + */ + +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.testconcepts.DummyPfModel; + +/** + * Test of the PfModel clas. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PfModelTest { + + @Test + public void testPfModel() { + assertNotNull(new DummyPfModel()); + assertNotNull(new DummyPfModel(new PfConceptKey())); + assertNotNull(new DummyPfModel(new DummyPfModel())); + + try { + new DummyPfModel((PfConceptKey)null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + DummyPfModel nullModel = null; + new DummyPfModel(nullModel); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + DummyPfModel dpm = new DummyPfModel(new PfConceptKey("modelKey", "0.0.1")); + DummyPfModel dpmClone = new DummyPfModel(dpm); + assertEquals(dpm, dpmClone); + + assertEquals(1, dpm.getKeys().size()); + + dpmClone.clean(); + assertEquals(dpm, dpmClone); + + try { + dpm.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(0, dpm.compareTo(dpmClone)); + assertEquals(-1, dpm.compareTo(null)); + assertEquals(0, dpm.compareTo(dpm)); + assertFalse(dpm.compareTo(dpm.getKey()) == 0); + } + + @Test + public void testPfModelValidation() { + PfConceptKey dpmKey = new PfConceptKey("modelKey", "0.0.1"); + DummyPfModel dpm = new DummyPfModel(dpmKey); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + try { + dpm.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + dpm.setKey(PfConceptKey.getNullKey()); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.setKey(dpmKey); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + dpm.getKeyList().add(PfReferenceKey.getNullKey()); + dpm.getKeyList().add(new PfKeyUse(PfReferenceKey.getNullKey())); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().clear(); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfConceptKey goodCKey = new PfConceptKey("goodCKey", "0.0.1"); + PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName"); + + dpm.getKeyList().add(goodCKey); + dpm.getKeyList().add(goodRKey); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfConceptKey goodCKeyDup = new PfConceptKey(goodCKey); + dpm.getKeyList().add(goodCKeyDup); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().remove(goodCKeyDup); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfReferenceKey goodRKeyDup = new PfReferenceKey(goodRKey); + dpm.getKeyList().add(goodRKeyDup); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().remove(goodRKeyDup); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey); + dpm.getKeyList().add(goodCKeyUse); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey); + dpm.getKeyList().add(goodRKeyUse); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfConceptKey badCKey = new PfConceptKey("badCKey", "0.0.1"); + PfKeyUse badCKeyUse = new PfKeyUse(badCKey); + dpm.getKeyList().add(badCKeyUse); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().remove(badCKeyUse); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfKeyUse badRKeyUse = new PfKeyUse(new PfReferenceKey(badCKey, "badLocalName")); + dpm.getKeyList().add(badRKeyUse); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().remove(badRKeyUse); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java index feedc2cc3..64d4bc6b8 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java @@ -47,10 +47,20 @@ public class PfReferenceKeyTest { assertEquals(PfReferenceKey.getNullKey().getKey(), PfReferenceKey.getNullKey()); assertEquals("NULL:0.0.0:NULL:NULL", PfReferenceKey.getNullKey().getId()); + try { + new PfReferenceKey(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("parameter \"localName\" is null", exc.getMessage()); + } + PfReferenceKey testReferenceKey = new PfReferenceKey(); testReferenceKey.setParentConceptKey(new PfConceptKey("PN", "0.0.1")); assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId()); + assertEquals(1, testReferenceKey.getKeys().size()); + assertFalse(testReferenceKey.isNullKey()); + testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", "0.0.1", "LN")); assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId()); diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java new file mode 100644 index 000000000..2b495a1e7 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java @@ -0,0 +1,43 @@ +/*- + * ============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.base; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +/** + * Test the PfUtils class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PfUtilsTest { + + @Test + public void testPfUtils() { + assertEquals(0, PfUtils.compareObjects(null, null)); + assertEquals(-1, PfUtils.compareObjects("hello", null)); + assertEquals(1, PfUtils.compareObjects(null, "hello")); + assertFalse(PfUtils.compareObjects("hello", "goodbye") == 0); + assertEquals(0, PfUtils.compareObjects("hello", "hello")); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java new file mode 100644 index 000000000..9fb6b5793 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java @@ -0,0 +1,137 @@ +/*- + * ============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.base.testconcepts; + +import java.util.List; + +import javax.persistence.EmbeddedId; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +@Data +@EqualsAndHashCode(callSuper = false) +public class DummyPfConcept extends PfConcept { + private static final long serialVersionUID = 1L; + @EmbeddedId + private PfConceptKey key; + + private String description; + + /** + * The Default Constructor creates a {@link DummyPfConcept} object with a null key. + */ + public DummyPfConcept() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link DummyPfConcept} object with the given concept key. + * + * @param key the key + */ + public DummyPfConcept(@NonNull final PfConceptKey key) { + this.key = key; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public DummyPfConcept(final DummyPfConcept copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + return keyList; + } + + @Override + public void clean() { + key.clean(); + + description = (description != null ? description.trim() : null); + } + + @Override + public PfValidationResult validate(PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (description != null && description.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property description may not be blank")); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final DummyPfConcept other = (DummyPfConcept) otherConcept; + if (!key.equals(other.key)) { + return key.compareTo(other.key); + } + + return ObjectUtils.compare(description, other.description); + } + + @Override + public PfConcept copyTo(@NonNull PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, PfConcept.class); + + final DummyPfConcept copy = ((DummyPfConcept) copyObject); + copy.setKey(new PfConceptKey(key)); + copy.setDescription(description); + + return copy; + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptContainer.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptContainer.java new file mode 100644 index 000000000..ac72ef8f6 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptContainer.java @@ -0,0 +1,77 @@ +/*- + * ============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.base.testconcepts; + +import java.util.Map; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; + +/** + * Dummy container for PF concepts. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyPfConceptContainer extends PfConceptContainer<DummyPfConcept> { + private static final long serialVersionUID = -3018432331484294280L; + + + /** + * The Default Constructor creates a {@link DummyPfConceptContainer} object with a null artifact key + * and creates an empty concept map. + */ + public DummyPfConceptContainer() { + super(); + } + + /** + * The Key Constructor creates a {@link DummyPfConceptContainer} object with the given artifact key and + * creates an empty concept map. + * + * @param key the concept key + */ + public DummyPfConceptContainer(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public DummyPfConceptContainer(@NonNull final PfConceptKey key, + @NonNull final Map<PfConceptKey, DummyPfConcept> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public DummyPfConceptContainer(@NonNull final DummyPfConceptContainer copyConcept) { + super(copyConcept); + } + +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfConcept.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java index f28477f70..da18cba20 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfConcept.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java @@ -18,60 +18,36 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.base.testpojos; +package org.onap.policy.models.base.testconcepts; -import java.util.Arrays; -import java.util.List; +import javax.ws.rs.core.Response; + +import lombok.NonNull; -import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfKey; -import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfModelRuntimeException; -public class DummyPfConcept extends PfConcept { +/** + * KeyUse subclass that throws exception on default constructor for testing. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyPfConceptKeySub extends PfConceptKey { private static final long serialVersionUID = 1L; - @Override - public int compareTo(PfConcept concept) { - return 0; - } - - @Override - public PfKey getKey() { - return new PfConceptKey(); - } - - @Override - public List<PfKey> getKeys() { - return Arrays.asList(getKey()); - } - - @Override - public PfValidationResult validate(PfValidationResult result) { - return null; - } - - @Override - public void clean() { - } - - @Override - public boolean equals(Object otherObject) { - return false; - } - - @Override - public String toString() { - return null; - } - - @Override - public int hashCode() { - return 0; + /** + * The Default Constructor creates this concept with a null key. + */ + public DummyPfConceptKeySub() { + throw new PfModelRuntimeException(Response.Status.BAD_GATEWAY, "Some error message"); } - @Override - public PfConcept copyTo(PfConcept target) { - return null; + /** + * This constructor creates an instance of this class, and holds a reference to a used key. + * + * @param usedKey a used key + */ + public DummyPfConceptKeySub(@NonNull final PfConceptKey usedKey) { + super(usedKey); } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptSub.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptSub.java new file mode 100644 index 000000000..12875eaca --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptSub.java @@ -0,0 +1,48 @@ +/*- + * ============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.base.testconcepts; + +import javax.ws.rs.core.Response; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelRuntimeException; + +public class DummyPfConceptSub extends DummyPfConcept { + private static final long serialVersionUID = 1L; + + /** + * The Default Constructor creates a {@link DummyPfConceptSub} object with a null key. + */ + public DummyPfConceptSub() { + throw new PfModelRuntimeException(Response.Status.BAD_GATEWAY, "Some error message"); + } + + /** + * The Key Constructor creates a {@link DummyPfConceptSub} object with the given concept key. + * + * @param key the key + */ + public DummyPfConceptSub(@NonNull final PfConceptKey key) { + super(key); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfKey.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java index 247ea88d1..6cf41e60c 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfKey.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.base.testpojos; +package org.onap.policy.models.base.testconcepts; import java.util.Arrays; import java.util.List; @@ -41,6 +41,11 @@ public class DummyPfKey extends PfKey { } @Override + public boolean isNullKey() { + return false; + } + + @Override public Compatibility getCompatibility(PfKey otherKey) { return null; } diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java new file mode 100644 index 000000000..199a37f59 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java @@ -0,0 +1,174 @@ +/*- + * ============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.base.testconcepts; + +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.core.Response; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModel; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfValidationResult; + +@Data +@EqualsAndHashCode(callSuper = true) +public class DummyPfModel extends PfModel { + private static final long serialVersionUID = 8800599637708309945L; + + private List<PfKey> keyList; + + /** + * The Default Constructor creates a {@link DummyPfModel} object with a null concept key and + * creates an empty TOSCA model. + */ + public DummyPfModel() { + super(); + super.setKey(new PfConceptKey()); + this.keyList = new ArrayList<PfKey>(); + } + + /** + * The Key Constructor creates a {@link DummyPfModel} object with the given concept key and + * creates an empty TOSCA model. + * + * @param key the TOSCA model key + */ + public DummyPfModel(final PfConceptKey key) { + super(key); + this.keyList = new ArrayList<PfKey>(); + } + + /** + * Constructor that initiates a {@link ToscaModel} with all its fields. + * + * @param key the TOSCA model key + * @param keyList the service templates in the event model + */ + public DummyPfModel(final PfConceptKey key, final List<PfKey> keyList) { + super(key); + this.keyList = keyList; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public DummyPfModel(final DummyPfModel copyConcept) { + super(copyConcept); + } + + @Override + public void register() { + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> listOfKeys = super.getKeys(); + + listOfKeys.addAll(keyList); + + return listOfKeys; + } + + @Override + public void clean() { + super.clean(); + for (PfKey pfKey : keyList) { + pfKey.clean(); + } + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + for (PfKey pfKey : keyList) { + result = pfKey.validate(result); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (super.compareTo(otherConcept) != 0) { + return super.compareTo(otherConcept); + } + + if (otherConcept == null) { + return -1; + } + + if (this == otherConcept) { + return 0; + } + + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final DummyPfModel other = (DummyPfModel) otherConcept; + if (!super.equals(other)) { + return super.compareTo(other); + } + + if (!keyList.equals(other.keyList)) { + return (keyList.hashCode() - other.keyList.hashCode()); + } + + return 0; + } + + @Override + public PfConcept copyTo(final PfConcept targetObject) { + super.copyTo(targetObject); + + Assertions.instanceOf(targetObject, DummyPfModel.class); + + final DummyPfModel copy = ((DummyPfModel) targetObject); + + final List<PfKey> newKeyList = new ArrayList<>(); + for (final PfKey pfKey : keyList) { + PfKey newPfKey; + try { + newPfKey = pfKey.getClass().newInstance(); + } catch (final Exception e) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, + "error copying concept key: " + e.getMessage(), e); + } + newPfKey.copyTo(pfKey); + newKeyList.add(newPfKey); + } + copy.setKeyList(newKeyList); + + + return copy; + } +} |