From 97956f188f4a8d92d734bf491d5e15a78a03459f Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 12 Sep 2018 18:01:03 -0400 Subject: implement Serializable in additional classes Change-Id: I5d5acb9d71dc49eaa9fb397da5988ba3d8bd5f1d Issue-ID: POLICY-1106 Signed-off-by: Jim Hahn --- .../controlloop/policy/ControlLoopPolicyTest.java | 7 + .../policy/controlloop/policy/ControlLoopTest.java | 8 +- .../onap/policy/controlloop/policy/PolicyTest.java | 210 +++++++++++++++++++++ 3 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java (limited to 'controlloop/common/policy-yaml/src/test/java') diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyTest.java index fcfe1dcfe..a4d1baa75 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopPolicyTest.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.InputStream; import org.junit.Test; +import org.onap.policy.common.utils.io.Serializer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.DumperOptions; @@ -114,6 +115,12 @@ public class ControlLoopPolicyTest { // Seems we cannot use assertEquals here. Need advice. // //assertEquals(newObject, obj); + + // test serialization + ControlLoopPolicy policy = (ControlLoopPolicy) obj; + ControlLoopPolicy policy2 = Serializer.roundTrip(policy); + assertTrue(policy.equals(policy2)); + } catch (FileNotFoundException e) { fail(e.getLocalizedMessage()); } catch (IOException e) { diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java index daab1a26b..0349552af 100644 --- a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/ControlLoopTest.java @@ -23,12 +23,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.junit.Ignore; import org.junit.Test; import org.onap.policy.aai.Pnf; +import org.onap.policy.common.utils.io.Serializer; import org.onap.policy.sdc.Resource; import org.onap.policy.sdc.ResourceType; import org.onap.policy.sdc.Service; @@ -82,7 +84,7 @@ public class ControlLoopTest { } @Test - public void testEquals() { + public void testEquals() throws IOException { final Pnf pnf = new Pnf(); pnf.setPnfName("pnf 1"); @@ -128,6 +130,10 @@ public class ControlLoopTest { assertTrue(controlLoop1.equals(controlLoop2)); assertEquals(controlLoop1.hashCode(), controlLoop2.hashCode()); + + controlLoop2 = Serializer.roundTrip(controlLoop1); + assertTrue(controlLoop1.equals(controlLoop2)); + assertEquals(controlLoop1.hashCode(), controlLoop2.hashCode()); } @Test diff --git a/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java new file mode 100644 index 000000000..6e13b326b --- /dev/null +++ b/controlloop/common/policy-yaml/src/test/java/org/onap/policy/controlloop/policy/PolicyTest.java @@ -0,0 +1,210 @@ +/* + * ============LICENSE_START======================================================= + * policy-yaml unit test + * ================================================================================ + * Copyright (C) 2018 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.controlloop.policy; + +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 java.io.IOException; +import java.util.Map; +import java.util.TreeMap; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.io.Serializer; + +public class PolicyTest { + private Policy policy; + + @Before + public void setUp() { + policy = new Policy(); + } + + @Test + public void testHashCode() { + assertTrue(policy.hashCode() != 0); + + policy.setActor("a"); + int hc1 = policy.hashCode(); + + policy.setActor("b"); + assertTrue(hc1 != policy.hashCode()); + } + + @Test + public void test() throws IOException { + OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); + operationsAccumulateParams.setLimit(10); + + Map payload = new TreeMap<>(); + payload.put("mykey", "myvalue"); + + Target target = new Target(); + target.setResourceID("myresource"); + + policy.setActor("act"); + policy.setDescription("desc"); + policy.setFailure("fail"); + policy.setFailure_exception("failex"); + policy.setFailure_guard("failguard"); + policy.setFailure_retries("failretry"); + policy.setFailure_timeout("failtimeout"); + policy.setId("myid"); + policy.setName("myname"); + policy.setOperationsAccumulateParams(operationsAccumulateParams); + policy.setPayload(payload); + policy.setRecipe("myrecipe"); + policy.setRetry(20); + policy.setSuccess("succ"); + policy.setTarget(target); + policy.setTimeout(30); + + assertEquals("act", policy.getActor()); + assertEquals("desc", policy.getDescription()); + assertEquals("fail", policy.getFailure()); + assertEquals("failex", policy.getFailure_exception()); + assertEquals("failguard", policy.getFailure_guard()); + assertEquals("failretry", policy.getFailure_retries()); + assertEquals("failtimeout", policy.getFailure_timeout()); + assertEquals("myid", policy.getId()); + assertEquals("myname", policy.getName()); + assertEquals(operationsAccumulateParams, policy.getOperationsAccumulateParams()); + assertEquals(payload, policy.getPayload()); + assertEquals("myrecipe", policy.getRecipe()); + assertEquals(20, policy.getRetry().intValue()); + assertEquals("succ", policy.getSuccess()); + assertEquals(target, policy.getTarget()); + assertEquals(30, policy.getTimeout().intValue()); + + assertTrue(policy.equals(policy)); + assertTrue(policy.hashCode() != new Policy().hashCode()); + assertFalse(policy.equals(new Policy())); + + Policy policy2 = Serializer.roundTrip(policy); + assertTrue(policy.equals(policy2)); + assertEquals(policy.hashCode(), policy2.hashCode()); + + policy2 = new Policy(policy); + assertTrue(policy.equals(policy2)); + assertEquals(policy.hashCode(), policy2.hashCode()); + } + + @Test + public void testPolicyString() { + policy = new Policy("justId"); + assertEquals("justId", policy.getId()); + } + + @Test + public void testPolicyStringStringStringMapOfStringStringTarget() { + Map payload = new TreeMap<>(); + payload.put("mykeyB", "myvalueB"); + + Target target = new Target(); + target.setResourceID("myresourceB"); + + policy = new Policy("nameB", "actorB", "recipeB", payload, target); + assertEquals("nameB", policy.getName()); + assertEquals("actorB", policy.getActor()); + assertEquals("recipeB", policy.getRecipe()); + assertEquals(payload, policy.getPayload()); + assertEquals(target, policy.getTarget()); + + assertTrue(policy.hashCode() != new Policy().hashCode()); + } + + @Test + public void testPolicyStringStringStringMapOfStringStringTargetIntegerInteger() { + Map payload = new TreeMap<>(); + payload.put("mykeyC", "myvalueC"); + + Target target = new Target(); + target.setResourceID("myresourceC"); + + policy = new Policy("nameC", "actorC", "recipeC", payload, target, 201, 202); + assertEquals("nameC", policy.getName()); + assertEquals("actorC", policy.getActor()); + assertEquals("recipeC", policy.getRecipe()); + assertEquals(payload, policy.getPayload()); + assertEquals(target, policy.getTarget()); + assertEquals(201, policy.getRetry().intValue()); + assertEquals(202, policy.getTimeout().intValue()); + + assertTrue(policy.hashCode() != new Policy().hashCode()); + } + + @Test + public void testPolicyStringStringStringStringMapOfStringStringTargetStringIntegerInteger() { + Map payload = new TreeMap<>(); + payload.put("mykeyD", "myvalueD"); + + Target target = new Target(); + target.setResourceID("myresourceD"); + + policy = new Policy("idD", "nameD", "descD", "actorD", payload, target, "recipeD", 301, 302); + assertEquals("idD", policy.getId()); + assertEquals("nameD", policy.getName()); + assertEquals("descD", policy.getDescription()); + assertEquals("actorD", policy.getActor()); + assertEquals(payload, policy.getPayload()); + assertEquals(target, policy.getTarget()); + assertEquals("recipeD", policy.getRecipe()); + assertEquals(301, policy.getRetry().intValue()); + assertEquals(302, policy.getTimeout().intValue()); + + assertTrue(policy.hashCode() != new Policy().hashCode()); + } + + @Test + public void testIsValid() { + assertFalse(policy.isValid()); + + Target target = new Target(); + target.setResourceID("myresourceV"); + + policy = new Policy("nameV", "actorV", "recipeV", null, target); + assertEquals(null, policy.getPayload()); + assertTrue(policy.isValid()); + } + + @Test + public void testToString() { + assertNotNull(policy.toString()); + } + + @Test + public void testEqualsObject() { + assertTrue(policy.equals(policy)); + + policy.setId("idE"); + assertFalse(policy.equals(new Policy())); + + Policy policy2 = new Policy(); + policy2.setId(policy.getId()); + assertTrue(policy.equals(policy2)); + + policy2.setId("idX"); + assertFalse(policy.equals(policy2)); + } + +} -- cgit 1.2.3-korg