Fix simple sonar issues in models: errors to sim-pdp
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / PdpSubGroupTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.models.pdp.concepts;
23
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;
30
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Map;
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;
42
43 /**
44  * Test methods not tested by {@link ModelsTest}.
45  */
46 public class PdpSubGroupTest {
47     private static final String VERSION_300 = "3.0.0";
48     private static final Coder coder = new StandardCoder();
49
50     @Test
51     public void testCopyConstructor() {
52         assertThatThrownBy(() -> new PdpSubGroup(null)).isInstanceOf(NullPointerException.class);
53
54         final PdpSubGroup orig = new PdpSubGroup();
55
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());
60
61         // verify with all values
62         orig.setCurrentInstanceCount(10);
63         orig.setDesiredInstanceCount(11);
64
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));
70
71         orig.setPdpType("my-type");
72
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));
80
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);
85
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));
89
90         assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
91     }
92
93     @Test
94     public void testValidatePapRest() throws Exception {
95         PdpSubGroup subgrp = new PdpSubGroup();
96
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)));
102
103         // valid
104         ValidationResult result = subgrp.validatePapRest();
105         assertNotNull(result);
106         assertTrue(result.isValid());
107         assertNull(result.getResult());
108
109         // zero count
110         PdpSubGroup sub2 = new PdpSubGroup(subgrp);
111         sub2.setDesiredInstanceCount(0);
112         assertInvalid(sub2);
113
114         // negative count
115         sub2 = new PdpSubGroup(subgrp);
116         sub2.setDesiredInstanceCount(-1);
117         assertInvalid(sub2);
118
119         // null pdp type
120         sub2 = new PdpSubGroup(subgrp);
121         sub2.setPdpType(null);
122         assertInvalid(sub2);
123
124         // null policy types
125         sub2 = new PdpSubGroup(subgrp);
126         sub2.setSupportedPolicyTypes(null);
127         assertInvalid(sub2);
128
129         // empty policy types
130         sub2 = new PdpSubGroup(subgrp);
131         sub2.setSupportedPolicyTypes(Collections.emptyList());
132         assertInvalid(sub2);
133
134         // null policy type item
135         sub2 = new PdpSubGroup(subgrp);
136         sub2.getSupportedPolicyTypes().set(0, null);
137         assertInvalid(sub2);
138
139         // invalid policy type item
140         sub2 = new PdpSubGroup(subgrp);
141         sub2.getSupportedPolicyTypes().set(0, makeIdent(null, VERSION_300, ToscaPolicyTypeIdentifier.class));
142         assertInvalid(sub2);
143
144         // null policies
145         sub2 = new PdpSubGroup(subgrp);
146         sub2.setPolicies(null);
147         assertInvalid(sub2);
148
149         // null policy item
150         sub2 = new PdpSubGroup(subgrp);
151         sub2.getPolicies().set(0, null);
152         assertInvalid(sub2);
153
154         // invalid policy item
155         sub2 = new PdpSubGroup(subgrp);
156         sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaPolicyIdentifier.class));
157         assertInvalid(sub2);
158     }
159
160     private void assertInvalid(PdpSubGroup sub2) {
161         ValidationResult result = sub2.validatePapRest();
162         assertNotNull(result);
163         assertFalse(result.isValid());
164         assertNotNull(result.getResult());
165     }
166
167     /**
168      * Makes an identifier. Uses JSON which does no error checking.
169      *
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
175      */
176     public <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
177         StringBuilder bldr = new StringBuilder();
178         bldr.append("{");
179
180         if (name != null) {
181             bldr.append("'name':'");
182             bldr.append(name);
183             bldr.append("'");
184         }
185
186         if (version != null) {
187             if (name != null) {
188                 bldr.append(',');
189             }
190
191             bldr.append("'version':'");
192             bldr.append(version);
193             bldr.append("'");
194         }
195
196         bldr.append("}");
197
198         String json = bldr.toString().replace('\'', '"');
199
200         return coder.decode(json, clazz);
201     }
202 }