511d88f27128aeb6785052653a822c65b0bb6925
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / DeploymentSubGroupTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.pdp.concepts;
22
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;
29
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;
38
39 /**
40  * Test methods not tested by {@link ModelsTest}.
41  */
42 public class DeploymentSubGroupTest {
43     private static final String VERSION_300 = "3.0.0";
44     private static final Coder coder = new StandardCoder();
45
46     @Test
47     public void testCopyConstructor() {
48         assertThatThrownBy(() -> new DeploymentSubGroup(null)).isInstanceOf(NullPointerException.class);
49
50         final DeploymentSubGroup orig = new DeploymentSubGroup();
51
52         // verify with null values
53         assertEquals("DeploymentSubGroup(pdpType=null, action=null, policies=[])",
54                         new DeploymentSubGroup(orig).toString());
55
56         orig.setPdpType("my-type");
57         orig.setAction(Action.POST);
58
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));
66
67         assertEquals(orig.toString(), new DeploymentSubGroup(orig).toString());
68     }
69
70     @Test
71     public void testValidatePapRest() throws Exception {
72         DeploymentSubGroup subgrp = new DeploymentSubGroup();
73
74         subgrp.setPdpType("pdp-type");
75         subgrp.setAction(Action.PATCH);
76         subgrp.setPolicies(Arrays.asList(makeIdent("policy-X", "4.0.0", ToscaPolicyIdentifier.class)));
77
78         // valid
79         ValidationResult result = subgrp.validatePapRest();
80         assertNotNull(result);
81         assertTrue(result.isValid());
82         assertNull(result.getResult());
83
84         // null pdp type
85         DeploymentSubGroup sub2 = new DeploymentSubGroup(subgrp);
86         sub2.setPdpType(null);
87         assertInvalid(sub2);
88
89         // null action
90         sub2 = new DeploymentSubGroup(subgrp);
91         sub2.setAction(null);
92         assertInvalid(sub2);
93
94         // null policies
95         sub2 = new DeploymentSubGroup(subgrp);
96         sub2.setPolicies(null);
97         assertInvalid(sub2);
98
99         // null policy item
100         sub2 = new DeploymentSubGroup(subgrp);
101         sub2.getPolicies().set(0, null);
102         assertInvalid(sub2);
103
104         // invalid policy item
105         sub2 = new DeploymentSubGroup(subgrp);
106         sub2.getPolicies().set(0, makeIdent(null, VERSION_300, ToscaPolicyIdentifier.class));
107         assertInvalid(sub2);
108     }
109
110     private void assertInvalid(DeploymentSubGroup sub2) {
111         ValidationResult result = sub2.validatePapRest();
112         assertNotNull(result);
113         assertFalse(result.isValid());
114         assertNotNull(result.getResult());
115     }
116
117     /**
118      * Makes an identifier. Uses JSON which does no error checking.
119      *
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
125      */
126     private <T> T makeIdent(String name, String version, Class<T> clazz) throws CoderException {
127         StringBuilder bldr = new StringBuilder();
128         bldr.append("{");
129
130         if (name != null) {
131             bldr.append("'name':'");
132             bldr.append(name);
133             bldr.append("'");
134         }
135
136         if (version != null) {
137             if (name != null) {
138                 bldr.append(',');
139             }
140
141             bldr.append("'version':'");
142             bldr.append(version);
143             bldr.append("'");
144         }
145
146         bldr.append("}");
147
148         String json = bldr.toString().replace('\'', '"');
149
150         return coder.decode(json, clazz);
151     }
152 }