2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.models.pdp.concepts;
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;
31 import java.util.Arrays;
32 import java.util.Collections;
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;
44 * Test methods not tested by {@link ModelsTest}.
46 public class PdpSubGroupTest {
47 private static final String VERSION_300 = "3.0.0";
48 private static final Coder coder = new StandardCoder();
51 public void testCopyConstructor() {
52 assertThatThrownBy(() -> new PdpSubGroup(null)).isInstanceOf(NullPointerException.class);
54 final PdpSubGroup orig = new PdpSubGroup();
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());
61 // verify with all values
62 orig.setCurrentInstanceCount(10);
63 orig.setDesiredInstanceCount(11);
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));
71 orig.setPdpType("my-type");
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));
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);
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));
90 assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
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)));
103 ValidationResult result = subgrp.validatePapRest(false);
104 assertNotNull(result);
105 assertTrue(result.isValid());
106 assertNull(result.getResult());
108 // without supported policy type and policies
109 PdpSubGroup subgrp2 = new PdpSubGroup();
110 subgrp2.setDesiredInstanceCount(1);
111 subgrp2.setPdpType("pdp-type");
114 result = subgrp2.validatePapRest(true);
115 assertNotNull(result);
116 assertTrue(result.isValid());
117 assertNull(result.getResult());
120 result = subgrp2.validatePapRest(false);
121 assertNotNull(result);
122 assertFalse(result.isValid());
123 assertNotNull(result.getResult());
127 public void testValidatePapRest() throws Exception {
128 PdpSubGroup subgrp = new PdpSubGroup();
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)));
137 ValidationResult result = subgrp.validatePapRest(false);
138 assertNotNull(result);
139 assertTrue(result.isValid());
140 assertNull(result.getResult());
143 PdpSubGroup sub2 = new PdpSubGroup(subgrp);
144 sub2.setDesiredInstanceCount(0);
148 sub2 = new PdpSubGroup(subgrp);
149 sub2.setDesiredInstanceCount(-1);
153 sub2 = new PdpSubGroup(subgrp);
154 sub2.setPdpType(null);
158 sub2 = new PdpSubGroup(subgrp);
159 sub2.setSupportedPolicyTypes(null);
162 // empty policy types
163 sub2 = new PdpSubGroup(subgrp);
164 sub2.setSupportedPolicyTypes(Collections.emptyList());
167 // null policy type item
168 sub2 = new PdpSubGroup(subgrp);
169 sub2.getSupportedPolicyTypes().set(0, null);
172 // invalid policy type item
173 sub2 = new PdpSubGroup(subgrp);
174 sub2.getSupportedPolicyTypes().set(0, makeIdent(null, VERSION_300, ToscaPolicyTypeIdentifier.class));
178 sub2 = new PdpSubGroup(subgrp);
179 sub2.setPolicies(null);
183 sub2 = new PdpSubGroup(subgrp);
184 sub2.getPolicies().set(0, null);
187 // invalid policy item
188 sub2 = new PdpSubGroup(subgrp);
189 sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaPolicyIdentifier.class));
193 private void assertInvalid(PdpSubGroup sub2) {
194 ValidationResult result = sub2.validatePapRest(false);
195 assertNotNull(result);
196 assertFalse(result.isValid());
197 assertNotNull(result.getResult());
201 * Makes an identifier. Uses JSON which does no error checking.
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
209 public <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
210 StringBuilder bldr = new StringBuilder();
214 bldr.append("'name':'");
219 if (version != null) {
224 bldr.append("'version':'");
225 bldr.append(version);
231 String json = bldr.toString().replace('\'', '"');
233 return coder.decode(json, clazz);