2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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 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 ToscaPolicyIdentifier pol1 = new ToscaPolicyIdentifier();
73 pol1.setName("policy-A");
74 pol1.setVersion("1.0.0");
75 final ToscaPolicyIdentifier pol2 = new ToscaPolicyIdentifier();
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 ToscaPolicyTypeIdentifier supp1 = new ToscaPolicyTypeIdentifier("supp-A", "1.2");
86 final ToscaPolicyTypeIdentifier supp2 = new ToscaPolicyTypeIdentifier("supp-B", "3.4");
87 orig.setSupportedPolicyTypes(Arrays.asList(supp1, supp2));
89 assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
93 public void testValidatePapRest() throws Exception {
94 PdpSubGroup subgrp = new PdpSubGroup();
96 subgrp.setDesiredInstanceCount(1);
97 subgrp.setPdpType("pdp-type");
98 subgrp.setSupportedPolicyTypes(Arrays.asList(makeIdent("type-X", "3.0.0", ToscaPolicyTypeIdentifier.class)));
99 subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class)));
102 ValidationResult result = subgrp.validatePapRest();
103 assertNotNull(result);
104 assertTrue(result.isValid());
105 assertNull(result.getResult());
108 PdpSubGroup sub2 = new PdpSubGroup(subgrp);
109 sub2.setDesiredInstanceCount(0);
113 sub2 = new PdpSubGroup(subgrp);
114 sub2.setDesiredInstanceCount(-1);
118 sub2 = new PdpSubGroup(subgrp);
119 sub2.setPdpType(null);
123 sub2 = new PdpSubGroup(subgrp);
124 sub2.setSupportedPolicyTypes(null);
127 // empty policy types
128 sub2 = new PdpSubGroup(subgrp);
129 sub2.setSupportedPolicyTypes(Collections.emptyList());
132 // null policy type item
133 sub2 = new PdpSubGroup(subgrp);
134 sub2.getSupportedPolicyTypes().set(0, null);
137 // invalid policy type item
138 sub2 = new PdpSubGroup(subgrp);
139 sub2.getSupportedPolicyTypes().set(0, makeIdent(null, "3.0.0", ToscaPolicyTypeIdentifier.class));
143 sub2 = new PdpSubGroup(subgrp);
144 sub2.setPolicies(null);
148 sub2 = new PdpSubGroup(subgrp);
149 sub2.getPolicies().set(0, null);
152 // invalid policy item
153 sub2 = new PdpSubGroup(subgrp);
154 sub2.getPolicies().set(0, makeIdent(null, "3.0.0", ToscaPolicyIdentifier.class));
158 private void assertInvalid(PdpSubGroup sub2) {
159 ValidationResult result = sub2.validatePapRest();
160 assertNotNull(result);
161 assertFalse(result.isValid());
162 assertNotNull(result.getResult());
166 * Makes an identifier. Uses JSON which does no error checking.
168 * @param name name to put into the identifier
169 * @param version version to put into the identifier
170 * @param clazz type of identifier to create
171 * @return a new identifier
172 * @throws CoderException if the JSON cannot be decoded
174 public <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
175 StringBuilder bldr = new StringBuilder();
179 bldr.append("'name':'");
184 if (version != null) {
189 bldr.append("'version':'");
190 bldr.append(version);
196 String json = bldr.toString().replace('\'', '"');
198 return coder.decode(json, clazz);