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