Add validation methods for PAP REST API
[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 Coder coder = new StandardCoder();
48
49     @Test
50     public void testCopyConstructor() {
51         assertThatThrownBy(() -> new PdpSubGroup(null)).isInstanceOf(NullPointerException.class);
52
53         final PdpSubGroup orig = new PdpSubGroup();
54
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());
59
60         // verify with all values
61         orig.setCurrentInstanceCount(10);
62         orig.setDesiredInstanceCount(11);
63
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));
69
70         orig.setPdpType("my-type");
71
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));
79
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);
84
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));
88
89         assertEquals(orig.toString(), new PdpSubGroup(orig).toString());
90     }
91
92     @Test
93     public void testValidatePapRest() throws Exception {
94         PdpSubGroup subgrp = new PdpSubGroup();
95
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)));
100
101         // valid
102         ValidationResult result = subgrp.validatePapRest();
103         assertNotNull(result);
104         assertTrue(result.isValid());
105         assertNull(result.getResult());
106
107         // zero count
108         PdpSubGroup sub2 = new PdpSubGroup(subgrp);
109         sub2.setDesiredInstanceCount(0);
110         assertInvalid(sub2);
111
112         // negative count
113         sub2 = new PdpSubGroup(subgrp);
114         sub2.setDesiredInstanceCount(-1);
115         assertInvalid(sub2);
116
117         // null pdp type
118         sub2 = new PdpSubGroup(subgrp);
119         sub2.setPdpType(null);
120         assertInvalid(sub2);
121
122         // null policy types
123         sub2 = new PdpSubGroup(subgrp);
124         sub2.setSupportedPolicyTypes(null);
125         assertInvalid(sub2);
126
127         // empty policy types
128         sub2 = new PdpSubGroup(subgrp);
129         sub2.setSupportedPolicyTypes(Collections.emptyList());
130         assertInvalid(sub2);
131
132         // null policy type item
133         sub2 = new PdpSubGroup(subgrp);
134         sub2.getSupportedPolicyTypes().set(0, null);
135         assertInvalid(sub2);
136
137         // invalid policy type item
138         sub2 = new PdpSubGroup(subgrp);
139         sub2.getSupportedPolicyTypes().set(0, makeIdent(null, "3.0.0", ToscaPolicyTypeIdentifier.class));
140         assertInvalid(sub2);
141
142         // null policies
143         sub2 = new PdpSubGroup(subgrp);
144         sub2.setPolicies(null);
145         assertInvalid(sub2);
146
147         // null policy item
148         sub2 = new PdpSubGroup(subgrp);
149         sub2.getPolicies().set(0, null);
150         assertInvalid(sub2);
151
152         // invalid policy item
153         sub2 = new PdpSubGroup(subgrp);
154         sub2.getPolicies().set(0, makeIdent(null, "3.0.0", ToscaPolicyIdentifier.class));
155         assertInvalid(sub2);
156     }
157
158     private void assertInvalid(PdpSubGroup sub2) {
159         ValidationResult result = sub2.validatePapRest();
160         assertNotNull(result);
161         assertFalse(result.isValid());
162         assertNotNull(result.getResult());
163     }
164
165     /**
166      * Makes an identifier. Uses JSON which does no error checking.
167      *
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
173      */
174     public <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
175         StringBuilder bldr = new StringBuilder();
176         bldr.append("{");
177
178         if (name != null) {
179             bldr.append("'name':'");
180             bldr.append(name);
181             bldr.append("'");
182         }
183
184         if (version != null) {
185             if (name != null) {
186                 bldr.append(',');
187             }
188
189             bldr.append("'version':'");
190             bldr.append(version);
191             bldr.append("'");
192         }
193
194         bldr.append("}");
195
196         String json = bldr.toString().replace('\'', '"');
197
198         return coder.decode(json, clazz);
199     }
200 }