ba55426c71ada882e2c59337acb59c9504c5ddb9
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / PdpSubGroupTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020 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.models.pdp.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Map;
34 import java.util.TreeMap;
35 import org.junit.Test;
36 import org.onap.policy.common.parameters.ValidationResult;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
42
43 /**
44  * Test methods not tested by {@link ModelsTest}.
45  */
46 public class PdpSubGroupTest {
47     private static final String VERSION_300 = "3.0.0";
48     private static final Coder coder = new StandardCoder();
49
50     @Test
51     public void testCopyConstructor() {
52         assertThatThrownBy(() -> new PdpSubGroup(null)).isInstanceOf(NullPointerException.class);
53
54         final PdpSubGroup orig = new PdpSubGroup();
55
56         // verify with null values
57         assertEquals("PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], "
58                         + "currentInstanceCount=0, desiredInstanceCount=0, properties=null, pdpInstances=[])",
59                         new PdpSubGroup(orig).toString());
60
61         // verify with all values
62         orig.setCurrentInstanceCount(10);
63         orig.setDesiredInstanceCount(11);
64
65         final Pdp inst1 = new Pdp();
66         inst1.setInstanceId("my-id-A");
67         final Pdp inst2 = new Pdp();
68         inst2.setInstanceId("my-id-B");
69         orig.setPdpInstances(Arrays.asList(inst1, inst2));
70
71         orig.setPdpType("my-type");
72
73         final ToscaPolicyIdentifier pol1 = new ToscaPolicyIdentifier();
74         pol1.setName("policy-A");
75         pol1.setVersion("1.0.0");
76         final ToscaPolicyIdentifier pol2 = new ToscaPolicyIdentifier();
77         pol2.setName("policy-B");
78         pol1.setVersion("2.0.0");
79         orig.setPolicies(Arrays.asList(pol1, pol2));
80
81         final Map<String, String> props = new TreeMap<>();
82         props.put("key-A", "value-A");
83         props.put("key-B", "value-B");
84         orig.setProperties(props);
85
86         final ToscaPolicyTypeIdentifier supp1 = new ToscaPolicyTypeIdentifier("supp-A", "1.2");
87         final ToscaPolicyTypeIdentifier supp2 = new ToscaPolicyTypeIdentifier("supp-B", "3.4");
88         orig.setSupportedPolicyTypes(Arrays.asList(supp1, supp2));
89
90         assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
91     }
92
93     @Test
94     public void testValidatePapRest_GroupUpdateFlow() throws Exception {
95         PdpSubGroup subgrp = new PdpSubGroup();
96         // with supported policy type and policies
97         subgrp.setDesiredInstanceCount(1);
98         subgrp.setPdpType("pdp-type");
99         subgrp.setSupportedPolicyTypes(
100                         Arrays.asList(makeIdent("type-X", VERSION_300, ToscaPolicyTypeIdentifier.class)));
101         subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class)));
102
103         ValidationResult result = subgrp.validatePapRest(false);
104         assertNotNull(result);
105         assertTrue(result.isValid());
106         assertNull(result.getResult());
107
108         // without supported policy type and policies
109         PdpSubGroup subgrp2 = new PdpSubGroup();
110         subgrp2.setDesiredInstanceCount(1);
111         subgrp2.setPdpType("pdp-type");
112
113         // valid
114         result = subgrp2.validatePapRest(true);
115         assertNotNull(result);
116         assertTrue(result.isValid());
117         assertNull(result.getResult());
118
119         // invalid
120         result = subgrp2.validatePapRest(false);
121         assertNotNull(result);
122         assertFalse(result.isValid());
123         assertNotNull(result.getResult());
124     }
125
126     @Test
127     public void testValidatePapRest() throws Exception {
128         PdpSubGroup subgrp = new PdpSubGroup();
129
130         subgrp.setDesiredInstanceCount(1);
131         subgrp.setPdpType("pdp-type");
132         subgrp.setSupportedPolicyTypes(
133                         Arrays.asList(makeIdent("type-X", VERSION_300, ToscaPolicyTypeIdentifier.class)));
134         subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class)));
135
136         // valid
137         ValidationResult result = subgrp.validatePapRest(false);
138         assertNotNull(result);
139         assertTrue(result.isValid());
140         assertNull(result.getResult());
141
142         // zero count
143         PdpSubGroup sub2 = new PdpSubGroup(subgrp);
144         sub2.setDesiredInstanceCount(0);
145         assertInvalid(sub2);
146
147         // negative count
148         sub2 = new PdpSubGroup(subgrp);
149         sub2.setDesiredInstanceCount(-1);
150         assertInvalid(sub2);
151
152         // null pdp type
153         sub2 = new PdpSubGroup(subgrp);
154         sub2.setPdpType(null);
155         assertInvalid(sub2);
156
157         // null policy types
158         sub2 = new PdpSubGroup(subgrp);
159         sub2.setSupportedPolicyTypes(null);
160         assertInvalid(sub2);
161
162         // empty policy types
163         sub2 = new PdpSubGroup(subgrp);
164         sub2.setSupportedPolicyTypes(Collections.emptyList());
165         assertInvalid(sub2);
166
167         // null policy type item
168         sub2 = new PdpSubGroup(subgrp);
169         sub2.getSupportedPolicyTypes().set(0, null);
170         assertInvalid(sub2);
171
172         // invalid policy type item
173         sub2 = new PdpSubGroup(subgrp);
174         sub2.getSupportedPolicyTypes().set(0, makeIdent(null, VERSION_300, ToscaPolicyTypeIdentifier.class));
175         assertInvalid(sub2);
176
177         // null policies
178         sub2 = new PdpSubGroup(subgrp);
179         sub2.setPolicies(null);
180         assertInvalid(sub2);
181
182         // null policy item
183         sub2 = new PdpSubGroup(subgrp);
184         sub2.getPolicies().set(0, null);
185         assertInvalid(sub2);
186
187         // invalid policy item
188         sub2 = new PdpSubGroup(subgrp);
189         sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaPolicyIdentifier.class));
190         assertInvalid(sub2);
191     }
192
193     private void assertInvalid(PdpSubGroup sub2) {
194         ValidationResult result = sub2.validatePapRest(false);
195         assertNotNull(result);
196         assertFalse(result.isValid());
197         assertNotNull(result.getResult());
198     }
199
200     /**
201      * Makes an identifier. Uses JSON which does no error checking.
202      *
203      * @param name name to put into the identifier
204      * @param version version to put into the identifier
205      * @param clazz type of identifier to create
206      * @return a new identifier
207      * @throws CoderException if the JSON cannot be decoded
208      */
209     public <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
210         StringBuilder bldr = new StringBuilder();
211         bldr.append("{");
212
213         if (name != null) {
214             bldr.append("'name':'");
215             bldr.append(name);
216             bldr.append("'");
217         }
218
219         if (version != null) {
220             if (name != null) {
221                 bldr.append(',');
222             }
223
224             bldr.append("'version':'");
225             bldr.append(version);
226             bldr.append("'");
227         }
228
229         bldr.append("}");
230
231         String json = bldr.toString().replace('\'', '"');
232
233         return coder.decode(json, clazz);
234     }
235 }