remove dependency of drool-application/yaml in actors
[policy/models.git] / models-interactions / model-yaml / src / test / java / org / onap / policy / controlloop / policy / PolicyTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-yaml unit test
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.policy;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.io.IOException;
30 import java.util.Map;
31 import java.util.TreeMap;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.io.Serializer;
35
36 public class PolicyTest {
37     private Policy policy;
38
39     @Before
40     public void setUp() {
41         policy = new Policy();
42     }
43
44     @Test
45     public void testHashCode() {
46         assertTrue(policy.hashCode() != 0);
47
48         policy.setActor("a");
49         int hc1 = policy.hashCode();
50
51         policy.setActor("b");
52         assertTrue(hc1 != policy.hashCode());
53     }
54
55     @Test
56     public void test() throws IOException {
57         OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams();
58         operationsAccumulateParams.setLimit(10);
59
60         Map<String, String> payload = new TreeMap<>();
61         payload.put("mykey", "myvalue");
62
63         Target target = new Target();
64         target.setResourceID("myresource");
65
66         policy.setActor("act");
67         policy.setDescription("desc");
68         policy.setFailure("fail");
69         policy.setFailure_exception("failex");
70         policy.setFailure_guard("failguard");
71         policy.setFailure_retries("failretry");
72         policy.setFailure_timeout("failtimeout");
73         policy.setId("myid");
74         policy.setName("myname");
75         policy.setOperationsAccumulateParams(operationsAccumulateParams);
76         policy.setPayload(payload);
77         policy.setRecipe("myrecipe");
78         policy.setRetry(20);
79         policy.setSuccess("succ");
80         policy.setTarget(target);
81         policy.setTimeout(30);
82
83         assertEquals("act", policy.getActor());
84         assertEquals("desc", policy.getDescription());
85         assertEquals("fail", policy.getFailure());
86         assertEquals("failex", policy.getFailure_exception());
87         assertEquals("failguard", policy.getFailure_guard());
88         assertEquals("failretry", policy.getFailure_retries());
89         assertEquals("failtimeout", policy.getFailure_timeout());
90         assertEquals("myid", policy.getId());
91         assertEquals("myname", policy.getName());
92         assertEquals(operationsAccumulateParams, policy.getOperationsAccumulateParams());
93         assertEquals(payload, policy.getPayload());
94         assertEquals("myrecipe", policy.getRecipe());
95         assertEquals(20, policy.getRetry().intValue());
96         assertEquals("succ", policy.getSuccess());
97         assertEquals(target, policy.getTarget());
98         assertEquals(30, policy.getTimeout().intValue());
99
100         assertTrue(policy.equals(policy));
101         assertTrue(policy.hashCode() != new Policy().hashCode());
102         assertFalse(policy.equals(new Policy()));
103
104         Policy policy2 = Serializer.roundTrip(policy);
105         assertTrue(policy.equals(policy2));
106         assertEquals(policy.hashCode(), policy2.hashCode());
107
108         policy2 = new Policy(policy);
109         assertTrue(policy.equals(policy2));
110         assertEquals(policy.hashCode(), policy2.hashCode());
111     }
112
113     @Test
114     public void testPolicyString() {
115         policy = new Policy("justId");
116         assertEquals("justId", policy.getId());
117     }
118
119     @Test
120     public void testPolicyStringStringStringMapOfStringStringTarget() {
121         Map<String, String> payload = new TreeMap<>();
122         payload.put("mykeyB", "myvalueB");
123
124         Target target = new Target();
125         target.setResourceID("myresourceB");
126
127         policy = new Policy("nameB", "actorB", "recipeB", payload, target);
128         assertEquals("nameB", policy.getName());
129         assertEquals("actorB", policy.getActor());
130         assertEquals("recipeB", policy.getRecipe());
131         assertEquals(payload, policy.getPayload());
132         assertEquals(target, policy.getTarget());
133
134         assertTrue(policy.hashCode() != new Policy().hashCode());
135     }
136
137     @Test
138     public void testPolicyStringStringStringMapOfStringStringTargetIntegerInteger() {
139         Map<String, String> payload = new TreeMap<>();
140         payload.put("mykeyC", "myvalueC");
141
142         Target target = new Target();
143         target.setResourceID("myresourceC");
144
145         policy = new Policy("nameC", "actorC", "recipeC", payload, target, 201, 202);
146         assertEquals("nameC", policy.getName());
147         assertEquals("actorC", policy.getActor());
148         assertEquals("recipeC", policy.getRecipe());
149         assertEquals(payload, policy.getPayload());
150         assertEquals(target, policy.getTarget());
151         assertEquals(201, policy.getRetry().intValue());
152         assertEquals(202, policy.getTimeout().intValue());
153
154         assertTrue(policy.hashCode() != new Policy().hashCode());
155     }
156
157     @Test
158     public void testPolicyStringStringStringStringMapOfStringStringTargetStringIntegerInteger() {
159         Map<String, String> payload = new TreeMap<>();
160         payload.put("mykeyD", "myvalueD");
161
162         Target target = new Target();
163         target.setResourceID("myresourceD");
164
165         policy = new Policy(
166                 PolicyParam.builder().id("idD")
167                 .name("nameD")
168                 .description("descD")
169                 .actor("actorD")
170                 .payload(payload)
171                 .target(target)
172                 .recipe("recipeD")
173                 .retries(301)
174                 .timeout(302)
175                 .build());
176         assertEquals("idD", policy.getId());
177         assertEquals("nameD", policy.getName());
178         assertEquals("descD", policy.getDescription());
179         assertEquals("actorD", policy.getActor());
180         assertEquals(payload, policy.getPayload());
181         assertEquals(target, policy.getTarget());
182         assertEquals("recipeD", policy.getRecipe());
183         assertEquals(301, policy.getRetry().intValue());
184         assertEquals(302, policy.getTimeout().intValue());
185
186         assertTrue(policy.hashCode() != new Policy().hashCode());
187     }
188
189     @Test
190     public void testIsValid() {
191         assertFalse(policy.isValid());
192
193         Target target = new Target();
194         target.setResourceID("myresourceV");
195
196         policy = new Policy("nameV", "actorV", "recipeV", null, target);
197         assertEquals(null, policy.getPayload());
198         assertTrue(policy.isValid());
199     }
200
201     @Test
202     public void testToString() {
203         assertNotNull(policy.toString());
204     }
205
206     @Test
207     public void testEqualsObject() {
208         assertTrue(policy.equals(policy));
209
210         policy.setId("idE");
211         assertFalse(policy.equals(new Policy()));
212
213         Policy policy2 = new Policy();
214         policy2.setId(policy.getId());
215         assertTrue(policy.equals(policy2));
216
217         policy2.setId("idX");
218         assertFalse(policy.equals(policy2));
219     }
220
221 }