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