2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 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 org.junit.Test;
33 import org.onap.policy.common.parameters.ValidationResult;
34 import org.onap.policy.common.utils.coder.Coder;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 * Test methods not tested by {@link ModelsTest}.
43 public class DeploymentSubGroupTest {
44 private static final String VERSION_300 = "3.0.0";
45 private static final Coder coder = new StandardCoder();
48 public void testCopyConstructor() {
49 assertThatThrownBy(() -> new DeploymentSubGroup(null)).isInstanceOf(NullPointerException.class);
51 final DeploymentSubGroup orig = new DeploymentSubGroup();
53 // verify with null values
54 assertEquals("DeploymentSubGroup(pdpType=null, action=null, policies=[])",
55 new DeploymentSubGroup(orig).toString());
57 orig.setPdpType("my-type");
58 orig.setAction(Action.POST);
60 final ToscaConceptIdentifier pol1 = new ToscaConceptIdentifier();
61 pol1.setName("policy-A");
62 pol1.setVersion("1.0.0");
63 final ToscaConceptIdentifier pol2 = new ToscaConceptIdentifier();
64 pol2.setName("policy-B");
65 pol1.setVersion("2.0.0");
66 orig.setPolicies(Arrays.asList(pol1, pol2));
68 assertEquals(orig.toString(), new DeploymentSubGroup(orig).toString());
72 public void testValidatePapRest() throws Exception {
73 DeploymentSubGroup subgrp = new DeploymentSubGroup();
75 subgrp.setPdpType("pdp-type");
76 subgrp.setAction(Action.PATCH);
77 subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaConceptIdentifier.class)));
80 ValidationResult result = subgrp.validatePapRest();
81 assertNotNull(result);
82 assertTrue(result.isValid());
83 assertNull(result.getResult());
86 DeploymentSubGroup sub2 = new DeploymentSubGroup(subgrp);
87 sub2.setPdpType(null);
91 sub2 = new DeploymentSubGroup(subgrp);
96 sub2 = new DeploymentSubGroup(subgrp);
97 sub2.setPolicies(null);
101 sub2 = new DeploymentSubGroup(subgrp);
102 sub2.getPolicies().set(0, null);
105 // invalid policy item
106 sub2 = new DeploymentSubGroup(subgrp);
107 sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaConceptIdentifier.class));
111 private void assertInvalid(DeploymentSubGroup sub2) {
112 ValidationResult result = sub2.validatePapRest();
113 assertNotNull(result);
114 assertFalse(result.isValid());
115 assertNotNull(result.getResult());
119 * Makes an identifier. Uses JSON which does no error checking.
121 * @param name name to put into the identifier
122 * @param version version to put into the identifier
123 * @param clazz type of identifier to create
124 * @return a new identifier
125 * @throws CoderException if the JSON cannot be decoded
127 private <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
128 StringBuilder bldr = new StringBuilder();
132 bldr.append("'name':'");
137 if (version != null) {
142 bldr.append("'version':'");
143 bldr.append(version);
149 String json = bldr.toString().replace('\'', '"');
151 return coder.decode(json, clazz);