2 * ============LICENSE_START=======================================================
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
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.ToscaConceptIdentifier;
43 * Test methods not tested by {@link ModelsTest}.
45 public class PdpSubGroupTest {
46 private static final String VERSION_300 = "3.0.0";
47 private static final Coder coder = new StandardCoder();
50 public void testCopyConstructor() {
51 assertThatThrownBy(() -> new PdpSubGroup(null)).isInstanceOf(NullPointerException.class);
53 final PdpSubGroup orig = new PdpSubGroup();
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());
60 // verify with all values
61 orig.setCurrentInstanceCount(10);
62 orig.setDesiredInstanceCount(11);
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));
70 orig.setPdpType("my-type");
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));
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);
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));
89 assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
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)));
102 ValidationResult result = subgrp.validatePapRest(false);
103 assertNotNull(result);
104 assertTrue(result.isValid());
105 assertNull(result.getResult());
107 // without supported policy type and policies
108 PdpSubGroup subgrp2 = new PdpSubGroup();
109 subgrp2.setDesiredInstanceCount(1);
110 subgrp2.setPdpType("pdp-type");
113 result = subgrp2.validatePapRest(true);
114 assertNotNull(result);
115 assertTrue(result.isValid());
116 assertNull(result.getResult());
119 result = subgrp2.validatePapRest(false);
120 assertNotNull(result);
121 assertFalse(result.isValid());
122 assertNotNull(result.getResult());
126 public void testValidatePapRest() throws Exception {
127 PdpSubGroup subgrp = new PdpSubGroup();
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)));
136 ValidationResult result = subgrp.validatePapRest(false);
137 assertNotNull(result);
138 assertTrue(result.isValid());
139 assertNull(result.getResult());
142 PdpSubGroup sub2 = new PdpSubGroup(subgrp);
143 sub2.setDesiredInstanceCount(0);
147 sub2 = new PdpSubGroup(subgrp);
148 sub2.setDesiredInstanceCount(-1);
152 sub2 = new PdpSubGroup(subgrp);
153 sub2.setPdpType(null);
157 sub2 = new PdpSubGroup(subgrp);
158 sub2.setSupportedPolicyTypes(null);
161 // empty policy types
162 sub2 = new PdpSubGroup(subgrp);
163 sub2.setSupportedPolicyTypes(Collections.emptyList());
166 // null policy type item
167 sub2 = new PdpSubGroup(subgrp);
168 sub2.getSupportedPolicyTypes().set(0, null);
171 // invalid policy type item
172 sub2 = new PdpSubGroup(subgrp);
173 sub2.getSupportedPolicyTypes().set(0, makeIdent(null, VERSION_300, ToscaConceptIdentifier.class));
177 sub2 = new PdpSubGroup(subgrp);
178 sub2.setPolicies(null);
182 sub2 = new PdpSubGroup(subgrp);
183 sub2.getPolicies().set(0, null);
186 // invalid policy item
187 sub2 = new PdpSubGroup(subgrp);
188 sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaConceptIdentifier.class));
192 private void assertInvalid(PdpSubGroup sub2) {
193 ValidationResult result = sub2.validatePapRest(false);
194 assertNotNull(result);
195 assertFalse(result.isValid());
196 assertNotNull(result.getResult());
200 * Makes an identifier. Uses JSON which does no error checking.
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
208 public <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
209 StringBuilder bldr = new StringBuilder();
213 bldr.append("'name':'");
218 if (version != null) {
223 bldr.append("'version':'");
224 bldr.append(version);
230 String json = bldr.toString().replace('\'', '"');
232 return coder.decode(json, clazz);