diff options
Diffstat (limited to 'models-pdp/src/test/java')
4 files changed, 349 insertions, 0 deletions
diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/IdentTestBase.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/IdentTestBase.java new file mode 100644 index 000000000..10bc9a997 --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/IdentTestBase.java @@ -0,0 +1,81 @@ +/* + * ============LICENSE_START======================================================= + * ONAP Policy Models + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.pdp.concepts; + +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; + +/** + * Super class to test identity keys. + * + * @param <T> type of key being tested + */ +public class IdentTestBase<T> { + + private static final Coder coder = new StandardCoder(); + + private final Class<T> clazz; + + + /** + * Constructs the object. + * @param clazz the type of class being tested + */ + public IdentTestBase(Class<T> clazz) { + this.clazz = clazz; + } + + /** + * Makes an identifier. Uses JSON which does no error checking. + * + * @param name name to put into the identifier + * @param version version to put into the identifier + * @return a new identifier + * @throws CoderException if the JSON cannot be decoded + */ + public T makeIdent(String name, String version) throws CoderException { + StringBuilder bldr = new StringBuilder(); + bldr.append("{"); + + if (name != null) { + bldr.append("'name':'"); + bldr.append(name); + bldr.append("'"); + } + + if (version != null) { + if (name != null) { + bldr.append(','); + } + + bldr.append("'version':'"); + bldr.append(version); + bldr.append("'"); + } + + bldr.append("}"); + + String json = bldr.toString().replace('\'', '"'); + + return coder.decode(json, clazz); + } +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyIdent.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyIdent.java new file mode 100644 index 000000000..4cd5570e2 --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyIdent.java @@ -0,0 +1,90 @@ +/* + * ============LICENSE_START======================================================= + * ONAP Policy Models + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.pdp.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.models.base.PfValidationResult; + +/** + * Test the other constructors, as {@link TestModels} tests the other methods. + */ +public class TestPolicyIdent extends IdentTestBase<PolicyIdent> { + private static final String NAME = "my-name"; + private static final String VERSION = "1.2.3"; + + public TestPolicyIdent() { + super(PolicyIdent.class); + } + + @Test + public void testAllArgsConstructor() { + assertThatThrownBy(() -> new PolicyIdent(null, VERSION)).isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new PolicyIdent(NAME, null)).isInstanceOf(NullPointerException.class); + + PolicyIdent orig = new PolicyIdent(NAME, VERSION); + assertEquals(NAME, orig.getName()); + assertEquals(VERSION, orig.getVersion()); + } + + @Test + public void testCopyConstructor() { + assertThatThrownBy(() -> new PolicyIdent(null)).isInstanceOf(NullPointerException.class); + + PolicyIdent orig = new PolicyIdent(); + + // verify with null values + assertEquals(orig.toString(), new PolicyIdent(orig).toString()); + + // verify with all values + orig = new PolicyIdent(NAME, VERSION); + assertEquals(orig.toString(), new PolicyIdent(orig).toString()); + } + + @Test + public void testValidate() throws Exception { + assertTrue(makeIdent(NAME, VERSION).validate(new PfValidationResult()).isValid()); + + // everything is null + PfValidationResult result = makeIdent(null, null).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(2, result.getMessageList().size()); + + // name is null + result = makeIdent(null, VERSION).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + + // version is null + result = makeIdent(NAME, null).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + + // version is invalid + result = makeIdent(NAME, "!!!" + VERSION).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + } +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyIdentOptVersion.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyIdentOptVersion.java new file mode 100644 index 000000000..3428ac1be --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyIdentOptVersion.java @@ -0,0 +1,87 @@ +/* + * ============LICENSE_START======================================================= + * ONAP Policy Models + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.pdp.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.models.base.PfValidationResult; + +/** + * Test the other constructors, as {@link TestModels} tests the other methods. + */ +public class TestPolicyIdentOptVersion extends IdentTestBase<PolicyIdentOptVersion> { + private static final String NAME = "my-name"; + private static final String VERSION = "1.2.3"; + + public TestPolicyIdentOptVersion() { + super(PolicyIdentOptVersion.class); + } + + @Test + public void testCopyConstructor() throws Exception { + assertThatThrownBy(() -> new PolicyIdentOptVersion(null)).isInstanceOf(NullPointerException.class); + + PolicyIdentOptVersion orig = new PolicyIdentOptVersion(); + + // verify with null values + assertEquals(orig.toString(), new PolicyIdentOptVersion(orig).toString()); + + // verify with all values + orig = makeIdent(NAME, VERSION); + assertEquals(orig.toString(), new PolicyIdentOptVersion(orig).toString()); + } + + @Test + public void testValidate() throws Exception { + assertThatThrownBy(() -> makeIdent(NAME, VERSION).validate(null)).isInstanceOf(NullPointerException.class); + assertTrue(makeIdent(NAME, VERSION).validate(new PfValidationResult()).isValid()); + assertTrue(makeIdent(NAME, null).validate(new PfValidationResult()).isValid()); + + // everything is null + PfValidationResult result = makeIdent(null, null).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + + // name is null + result = makeIdent(null, VERSION).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + + // name is null, version is invalid + result = makeIdent(null, "$$$" + VERSION).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(2, result.getMessageList().size()); + + // name is invalid + result = makeIdent("!!!invalid name$$$", VERSION).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + + // version is invalid + result = makeIdent(NAME, "!!!" + VERSION).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + } +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyTypeIdent.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyTypeIdent.java new file mode 100644 index 000000000..5b7494ebf --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPolicyTypeIdent.java @@ -0,0 +1,91 @@ +/* + * ============LICENSE_START======================================================= + * ONAP Policy Models + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.pdp.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.models.base.PfValidationResult; + +/** + * Test the other constructors, as {@link TestModels} tests the other methods. + */ +public class TestPolicyTypeIdent extends IdentTestBase<PolicyTypeIdent> { + private static final String NAME = "my-name"; + private static final String VERSION = "1.2.3"; + + public TestPolicyTypeIdent() { + super(PolicyTypeIdent.class); + } + + @Test + public void testAllArgsConstructor() { + assertThatThrownBy(() -> new PolicyTypeIdent(null, VERSION)).isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new PolicyTypeIdent(NAME, null)).isInstanceOf(NullPointerException.class); + + PolicyTypeIdent orig = new PolicyTypeIdent(NAME, VERSION); + assertEquals(NAME, orig.getName()); + assertEquals(VERSION, orig.getVersion()); + } + + @Test + public void testCopyConstructor() { + assertThatThrownBy(() -> new PolicyTypeIdent(null)).isInstanceOf(NullPointerException.class); + + PolicyTypeIdent orig = new PolicyTypeIdent(); + + // verify with null values + assertEquals(orig.toString(), new PolicyTypeIdent(orig).toString()); + + // verify with all values + orig = new PolicyTypeIdent(NAME, VERSION); + assertEquals(orig.toString(), new PolicyTypeIdent(orig).toString()); + } + + @Test + public void testValidate() throws Exception { + assertTrue(makeIdent(NAME, VERSION).validate(new PfValidationResult()).isValid()); + + // everything is null + PfValidationResult result = makeIdent(null, null).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(2, result.getMessageList().size()); + + // name is null + result = makeIdent(null, VERSION).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + + // version is null + result = makeIdent(NAME, null).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + + // version is invalid + result = makeIdent(NAME, "!!!" + VERSION).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertEquals(1, result.getMessageList().size()); + } + +} |