2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.concepts;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
30 import java.util.Arrays;
31 import org.junit.Test;
32 import org.onap.policy.common.parameters.ValidationResult;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
40 * Test methods not tested by {@link ModelsTest}.
42 public class DeploymentSubGroupTest {
43 private static final String VERSION_300 = "3.0.0";
44 private static final Coder coder = new StandardCoder();
47 public void testCopyConstructor() {
48 assertThatThrownBy(() -> new DeploymentSubGroup(null)).isInstanceOf(NullPointerException.class);
50 final DeploymentSubGroup orig = new DeploymentSubGroup();
52 // verify with null values
53 assertEquals("DeploymentSubGroup(pdpType=null, action=null, policies=[])",
54 new DeploymentSubGroup(orig).toString());
56 orig.setPdpType("my-type");
57 orig.setAction(Action.POST);
59 final ToscaPolicyIdentifier pol1 = new ToscaPolicyIdentifier();
60 pol1.setName("policy-A");
61 pol1.setVersion("1.0.0");
62 final ToscaPolicyIdentifier pol2 = new ToscaPolicyIdentifier();
63 pol2.setName("policy-B");
64 pol1.setVersion("2.0.0");
65 orig.setPolicies(Arrays.asList(pol1, pol2));
67 assertEquals(orig.toString(), new DeploymentSubGroup(orig).toString());
71 public void testValidatePapRest() throws Exception {
72 DeploymentSubGroup subgrp = new DeploymentSubGroup();
74 subgrp.setPdpType("pdp-type");
75 subgrp.setAction(Action.PATCH);
76 subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class)));
79 ValidationResult result = subgrp.validatePapRest();
80 assertNotNull(result);
81 assertTrue(result.isValid());
82 assertNull(result.getResult());
85 DeploymentSubGroup sub2 = new DeploymentSubGroup(subgrp);
86 sub2.setPdpType(null);
90 sub2 = new DeploymentSubGroup(subgrp);
95 sub2 = new DeploymentSubGroup(subgrp);
96 sub2.setPolicies(null);
100 sub2 = new DeploymentSubGroup(subgrp);
101 sub2.getPolicies().set(0, null);
104 // invalid policy item
105 sub2 = new DeploymentSubGroup(subgrp);
106 sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaPolicyIdentifier.class));
110 private void assertInvalid(DeploymentSubGroup sub2) {
111 ValidationResult result = sub2.validatePapRest();
112 assertNotNull(result);
113 assertFalse(result.isValid());
114 assertNotNull(result.getResult());
118 * Makes an identifier. Uses JSON which does no error checking.
120 * @param name name to put into the identifier
121 * @param version version to put into the identifier
122 * @param clazz type of identifier to create
123 * @return a new identifier
124 * @throws CoderException if the JSON cannot be decoded
126 private <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
127 StringBuilder bldr = new StringBuilder();
131 bldr.append("'name':'");
136 if (version != null) {
141 bldr.append("'version':'");
142 bldr.append(version);
148 String json = bldr.toString().replace('\'', '"');
150 return coder.decode(json, clazz);