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