aboutsummaryrefslogtreecommitdiffstats
path: root/models-base
diff options
context:
space:
mode:
Diffstat (limited to 'models-base')
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfUtils.java23
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/keys/PolicyTypeIdent.java42
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java20
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/keys/TestModels.java46
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/keys/TestPolicyTypeIdent.java60
5 files changed, 188 insertions, 3 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
index a18315ceb..8e77d3fcf 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
@@ -1,6 +1,7 @@
-/*-
+/*
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications 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.
@@ -20,6 +21,10 @@
package org.onap.policy.models.base;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
/**
* Utility class for Policy Framework concept utilities.
*
@@ -56,4 +61,20 @@ public final class PfUtils {
return 0;
}
+
+ /**
+ * Convenience method to apply a mapping function to all of the elements of a list,
+ * generating a new list.
+ *
+ * @param source list whose elements are to be mapped, or {@code null}
+ * @param mapFunc mapping function
+ * @return a new list, containing mappings of all of the items in the original list
+ */
+ public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
+ if (source == null) {
+ return null;
+ }
+
+ return source.stream().map(mapFunc).collect(Collectors.toList());
+ }
}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/keys/PolicyTypeIdent.java b/models-base/src/main/java/org/onap/policy/models/base/keys/PolicyTypeIdent.java
new file mode 100644
index 000000000..09e03816e
--- /dev/null
+++ b/models-base/src/main/java/org/onap/policy/models/base/keys/PolicyTypeIdent.java
@@ -0,0 +1,42 @@
+/*
+ * ============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.base.keys;
+
+import lombok.NoArgsConstructor;
+import lombok.NonNull;
+import org.onap.policy.models.base.PfConceptKey;
+
+/**
+ * Identifies a policy type. Both the name and version must be non-null.
+ */
+@NonNull
+@NoArgsConstructor
+public class PolicyTypeIdent extends PfConceptKey {
+ private static final long serialVersionUID = 1L;
+
+ public PolicyTypeIdent(String name, String version) {
+ super(name, version);
+ }
+
+ public PolicyTypeIdent(PolicyTypeIdent source) {
+ super(source);
+ }
+}
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
index 2b495a1e7..bdbab5c36 100644
--- 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
@@ -1,6 +1,7 @@
-/*-
+/*
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications 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.
@@ -22,7 +23,10 @@ package org.onap.policy.models.base;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import java.util.Arrays;
+import java.util.List;
import org.junit.Test;
/**
@@ -33,11 +37,23 @@ import org.junit.Test;
public class PfUtilsTest {
@Test
- public void testPfUtils() {
+ public void testCompareObjects() {
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"));
}
+
+ @Test
+ public void testMapList() {
+ assertNull(PfUtils.mapList(null, item -> {
+ throw new RuntimeException("should not be invoked");
+ }));
+
+ List<String> origList = Arrays.asList("abc", "def");
+ List<String> newList = PfUtils.mapList(origList, text -> text + "X");
+
+ assertEquals(Arrays.asList("abcX", "defX"), newList);
+ }
}
diff --git a/models-base/src/test/java/org/onap/policy/models/base/keys/TestModels.java b/models-base/src/test/java/org/onap/policy/models/base/keys/TestModels.java
new file mode 100644
index 000000000..a1bf3be68
--- /dev/null
+++ b/models-base/src/test/java/org/onap/policy/models/base/keys/TestModels.java
@@ -0,0 +1,46 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Nordix Foundation.
+ * Modifications 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.base.keys;
+
+import com.openpojo.reflection.filters.FilterPackageInfo;
+import com.openpojo.validation.Validator;
+import com.openpojo.validation.ValidatorBuilder;
+import com.openpojo.validation.test.impl.GetterTester;
+import com.openpojo.validation.test.impl.SetterTester;
+
+import org.junit.Test;
+import org.onap.policy.common.utils.validation.ToStringTester;
+
+/**
+ * Class to perform unit testing of models.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
+ */
+public class TestModels {
+
+ @Test
+ public void testModels() {
+ final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterTester())
+ .with(new GetterTester()).build();
+ validator.validate(TestModels.class.getPackage().getName(), new FilterPackageInfo());
+ }
+}
diff --git a/models-base/src/test/java/org/onap/policy/models/base/keys/TestPolicyTypeIdent.java b/models-base/src/test/java/org/onap/policy/models/base/keys/TestPolicyTypeIdent.java
new file mode 100644
index 000000000..1638a87da
--- /dev/null
+++ b/models-base/src/test/java/org/onap/policy/models/base/keys/TestPolicyTypeIdent.java
@@ -0,0 +1,60 @@
+/*
+ * ============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.base.keys;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.onap.policy.models.base.keys.PolicyTypeIdent;
+import org.onap.policy.models.base.keys.TestModels;
+
+/**
+ * Test the other constructors, as {@link TestModels} tests the other methods.
+ */
+public class TestPolicyTypeIdent {
+ private static final String NAME = "my-name";
+ private static final String VERSION = "1.2.3";
+
+ @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());
+ }
+}