diff options
author | liamfallon <liam.fallon@est.tech> | 2019-02-26 13:57:39 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-02-28 10:32:01 +0000 |
commit | f240fda5e8f7e940d6033b204c2dac48a9dc7c4e (patch) | |
tree | 98f16f96a2ef8ced0895ae1aaa09c021a96f15f0 /models-base/src/test/java/org/onap | |
parent | 9ddbc872354eb5399bab9d9c23188b4be98d7dd7 (diff) |
Add basic model object concepts
This review introduces the basic concepts that all model objects
inherit from. Using this approach, all concepts that inherit from
these types can use standardized DAO handling and marrshal/unmarshal
handling
This approach is a more generic version of the approach used in the
APEX PDP for model handling. The APEX model handling will inherit this
module.
Issue-ID: POLICY-1264
Change-Id: I35b76659ab66cf3f33bee59a5528e49c7edf7796
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-base/src/test/java/org/onap')
5 files changed, 485 insertions, 0 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 new file mode 100644 index 000000000..92e928e2e --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java @@ -0,0 +1,56 @@ +/* + * ============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.assertNotNull; + +import java.io.IOException; + +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")); + + String key = "A String"; + PfModelException ae = new PfModelException("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")); + + String rkey = "A String"; + PfModelRuntimeException re = new PfModelRuntimeException("Runtime Message", + new IOException("IO runtime exception message"), rkey); + assertEquals("Runtime Message\ncaused by: Runtime Message\ncaused by: IO runtime exception message", + re.getCascadedMessage()); + assertEquals(key, re.getObject()); + } +} 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 new file mode 100644 index 000000000..dd28f33ba --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java @@ -0,0 +1,172 @@ +/* + * ============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.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; + +public class PfKeyTest { + + @Test + public void testArtifactKey() { + try { + new PfConceptKey("some bad key id"); + fail("This test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("parameter \"id\": value \"some bad key id\", " + + "does not match regular expression \"[A-Za-z0-9\\-_\\.]+:[0-9].[0-9].[0-9]\"", + e.getMessage()); + } + + PfConceptKey someKey0 = new PfConceptKey(); + assertEquals(PfConceptKey.getNullKey(), someKey0); + + PfConceptKey someKey1 = new PfConceptKey("name", "0.0.1"); + PfConceptKey someKey2 = new PfConceptKey(someKey1); + PfConceptKey someKey3 = new PfConceptKey(someKey1.getId()); + assertEquals(someKey1, someKey2); + assertEquals(someKey1, someKey3); + + assertEquals(someKey2, someKey1.getKey()); + assertEquals(1, someKey1.getKeys().size()); + + someKey0.setName("zero"); + someKey0.setVersion("0.0.2"); + + someKey3.setVersion("0.0.2"); + + PfConceptKey someKey4 = new PfConceptKey(someKey1); + someKey4.setVersion("0.1.2"); + + PfConceptKey someKey5 = new PfConceptKey(someKey1); + someKey5.setVersion("1.2.2"); + + PfConceptKey someKey6 = new PfConceptKey(someKey1); + someKey6.setVersion("3"); + + assertEquals("name:0.1.2", someKey4.getId()); + + PfConcept pfc = new DummyPfConcept(); + assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId()); + assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId())); + + 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.MAJOR, someKey5.getCompatibility(someKey1)); + assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1)); + + assertTrue(someKey1.isCompatible(someKey2)); + assertTrue(someKey1.isCompatible(someKey3)); + assertTrue(someKey1.isCompatible(someKey4)); + assertFalse(someKey1.isCompatible(someKey0)); + assertFalse(someKey1.isCompatible(someKey5)); + assertFalse(someKey1.isCompatible(new DummyPfKey())); + + assertEquals(PfValidationResult.ValidationResult.VALID, + someKey0.validate(new PfValidationResult()).getValidationResult()); + assertEquals(PfValidationResult.ValidationResult.VALID, + someKey1.validate(new PfValidationResult()).getValidationResult()); + assertEquals(PfValidationResult.ValidationResult.VALID, + someKey2.validate(new PfValidationResult()).getValidationResult()); + assertEquals(PfValidationResult.ValidationResult.VALID, + someKey3.validate(new PfValidationResult()).getValidationResult()); + assertEquals(PfValidationResult.ValidationResult.VALID, + someKey4.validate(new PfValidationResult()).getValidationResult()); + assertEquals(PfValidationResult.ValidationResult.VALID, + someKey5.validate(new PfValidationResult()).getValidationResult()); + assertEquals(PfValidationResult.ValidationResult.VALID, + someKey6.validate(new PfValidationResult()).getValidationResult()); + + someKey0.clean(); + assertNotNull(someKey0.toString()); + + PfConceptKey someKey7 = new PfConceptKey(someKey1); + assertEquals(150332875, someKey7.hashCode()); + assertEquals(0, someKey7.compareTo(someKey1)); + assertEquals(-12, someKey7.compareTo(someKey0)); + + try { + someKey0.compareTo(null); + } catch (IllegalArgumentException e) { + assertEquals("comparison object may not be null", e.getMessage()); + } + + assertEquals(0, someKey0.compareTo(someKey0)); + assertEquals(161539407, someKey0.compareTo(new DummyPfKey())); + + assertFalse(someKey0.equals(null)); + assertTrue(someKey0.equals(someKey0)); + assertFalse(((PfKey) someKey0).equals(new DummyPfKey())); + } + + + @Test + public void testValidation() { + PfConceptKey testKey = new PfConceptKey("TheKey", "0.0.1"); + assertEquals("TheKey:0.0.1", testKey.getId()); + + try { + Field nameField = testKey.getClass().getDeclaredField("name"); + nameField.setAccessible(true); + nameField.set(testKey, "Key Name"); + PfValidationResult validationResult = new PfValidationResult(); + testKey.validate(validationResult); + nameField.set(testKey, "TheKey"); + nameField.setAccessible(false); + assertEquals( + "name invalid-parameter name with value Key Name " + + "does not match regular expression [A-Za-z0-9\\-_\\.]+", + validationResult.getMessageList().get(0).getMessage()); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + + try { + Field versionField = testKey.getClass().getDeclaredField("version"); + versionField.setAccessible(true); + versionField.set(testKey, "Key Version"); + PfValidationResult validationResult = new PfValidationResult(); + testKey.validate(validationResult); + versionField.set(testKey, "0.0.1"); + versionField.setAccessible(false); + assertEquals( + "version invalid-parameter version with value Key Version " + + "does not match regular expression [A-Za-z0-9.]+", + validationResult.getMessageList().get(0).getMessage()); + } catch (Exception validationException) { + fail("test should not throw an exception"); + } + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/ValidationTest.java b/models-base/src/test/java/org/onap/policy/models/base/ValidationTest.java new file mode 100644 index 000000000..385039c5b --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/ValidationTest.java @@ -0,0 +1,90 @@ +/* + * ============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 org.junit.Test; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +public class ValidationTest { + + @Test + public void test() { + PfValidationResult result = new PfValidationResult(); + PfConceptKey pfKeyey = new PfConceptKey("PK", "0.0.1"); + result = pfKeyey.validate(result); + + assertNotNull(result); + assertTrue(result.isOk()); + assertTrue(result.isValid()); + assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult()); + assertNotNull(result.getMessageList()); + + PfValidationMessage vmess0 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, + ValidationResult.VALID, "Some message"); + result.addValidationMessage(vmess0); + + assertTrue(result.isOk()); + assertTrue(result.isValid()); + assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult()); + assertNotNull(result.getMessageList()); + assertNotNull("hello", result.toString()); + + PfValidationMessage vmess1 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, + ValidationResult.OBSERVATION, "Some message"); + result.addValidationMessage(vmess1); + + assertTrue(result.isOk()); + assertTrue(result.isValid()); + assertEquals(PfValidationResult.ValidationResult.OBSERVATION, result.getValidationResult()); + assertNotNull(result.getMessageList()); + assertNotNull("hello", result.toString()); + + PfValidationMessage vmess2 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, + ValidationResult.WARNING, "Some message"); + result.addValidationMessage(vmess2); + + assertFalse(result.isOk()); + assertTrue(result.isValid()); + assertEquals(PfValidationResult.ValidationResult.WARNING, result.getValidationResult()); + assertNotNull(result.getMessageList()); + assertNotNull("hello", result.toString()); + + PfValidationMessage vmess3 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, + ValidationResult.INVALID, "Some message"); + result.addValidationMessage(vmess3); + + assertFalse(result.isOk()); + assertFalse(result.isValid()); + assertEquals(PfValidationResult.ValidationResult.INVALID, result.getValidationResult()); + assertNotNull(result.getMessageList()); + assertNotNull("hello", result.toString()); + + assertEquals(PfValidationResult.ValidationResult.INVALID, result.getMessageList().get(3).getValidationResult()); + assertEquals("Some message", result.getMessageList().get(3).getMessage()); + assertEquals(PfConceptKey.class.getCanonicalName(), result.getMessageList().get(3).getObservedClass()); + assertEquals(PfConceptKey.getNullKey(), result.getMessageList().get(3).getObservedKey()); + } +} 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/testpojos/DummyPfConcept.java new file mode 100644 index 000000000..85727f45e --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfConcept.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.base.testpojos; + +import java.util.List; + +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; + +public class DummyPfConcept extends PfConcept { + 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 null; + } + + @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; + } + + @Override + public PfConcept copyTo(PfConcept target) { + return null; + } +} 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/testpojos/DummyPfKey.java new file mode 100644 index 000000000..19358a1de --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfKey.java @@ -0,0 +1,91 @@ +/*- + * ============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.testpojos; + +import java.util.List; + +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfValidationResult; + +public class DummyPfKey extends PfKey { + private static final long serialVersionUID = 1L; + + @Override + public int compareTo(PfConcept arg0) { + return 0; + } + + @Override + public String getId() { + return null; + } + + @Override + public Compatibility getCompatibility(PfKey otherKey) { + return null; + } + + @Override + public boolean isCompatible(PfKey otherKey) { + return false; + } + + @Override + public PfKey getKey() { + return null; + } + + @Override + public List<PfKey> getKeys() { + return null; + } + + @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; + } + + @Override + public PfConcept copyTo(PfConcept target) { + return null; + } +} |