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 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() throws Exception {
95 PdpSubGroup subgrp = new PdpSubGroup();
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)));
104 ValidationResult result = subgrp.validatePapRest();
105 assertNotNull(result);
106 assertTrue(result.isValid());
107 assertNull(result.getResult());
110 PdpSubGroup sub2 = new PdpSubGroup(subgrp);
111 sub2.setDesiredInstanceCount(0);
115 sub2 = new PdpSubGroup(subgrp);
116 sub2.setDesiredInstanceCount(-1);
120 sub2 = new PdpSubGroup(subgrp);
121 sub2.setPdpType(null);
125 sub2 = new PdpSubGroup(subgrp);
126 sub2.setSupportedPolicyTypes(null);
129 // empty policy types
130 sub2 = new PdpSubGroup(subgrp);
131 sub2.setSupportedPolicyTypes(Collections.emptyList());
134 // null policy type item
135 sub2 = new PdpSubGroup(subgrp);
136 sub2.getSupportedPolicyTypes().set(0, null);
139 // invalid policy type item
140 sub2 = new PdpSubGroup(subgrp);
141 sub2.getSupportedPolicyTypes().set(0, makeIdent(null, VERSION_300, ToscaPolicyTypeIdentifier.class));
145 sub2 = new PdpSubGroup(subgrp);
146 sub2.setPolicies(null);
150 sub2 = new PdpSubGroup(subgrp);
151 sub2.getPolicies().set(0, null);
154 // invalid policy item
155 sub2 = new PdpSubGroup(subgrp);
156 sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaPolicyIdentifier.class));
160 private void assertInvalid(PdpSubGroup sub2) {
161 ValidationResult result = sub2.validatePapRest();
162 assertNotNull(result);
163 assertFalse(result.isValid());
164 assertNotNull(result.getResult());
168 * Makes an identifier. Uses JSON which does no error checking.
170 * @param name name to put into the identifier
171 * @param version version to put into the identifier
172 * @param clazz type of identifier to create
173 * @return a new identifier
174 * @throws CoderException if the JSON cannot be decoded
176 public <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
177 StringBuilder bldr = new StringBuilder();
181 bldr.append("'name':'");
186 if (version != null) {
191 bldr.append("'version':'");
192 bldr.append(version);
198 String json = bldr.toString().replace('\'', '"');
200 return coder.decode(json, clazz);