Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.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.assertTrue;
29
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import org.junit.Test;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfKey;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
39
40 /**
41  * DAO test for ToscaPolicy.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class JpaToscaPolicyTest {
46
47     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
48     private static final String VERSION_001 = "0.0.1";
49
50     @Test
51     public void testPolicyPojo() {
52         assertNotNull(new JpaToscaPolicy());
53         assertNotNull(new JpaToscaPolicy(new PfConceptKey()));
54         assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey()));
55         assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy()));
56
57         final ToscaPolicy pol = new ToscaPolicy();
58         pol.setType("type");
59         assertThatThrownBy(() -> {
60             new JpaToscaPolicy(pol);
61         }).hasMessage(
62             "PolicyType version not specified, the version of the PolicyType for this policy must be specified in the "
63                 + "type_version field");
64
65         assertThatThrownBy(() -> {
66             new JpaToscaPolicy((PfConceptKey) null);
67         }).hasMessageMatching(KEY_IS_NULL);
68
69         assertThatThrownBy(() -> {
70             new JpaToscaPolicy(null, null);
71         }).hasMessageMatching(KEY_IS_NULL);
72
73         assertThatThrownBy(() -> {
74             new JpaToscaPolicy(new PfConceptKey(), null);
75         }).hasMessageMatching("type is marked .*on.*ull but is null");
76
77         assertThatThrownBy(() -> {
78             new JpaToscaPolicy(null, new PfConceptKey());
79         }).hasMessageMatching(KEY_IS_NULL);
80
81         assertThatThrownBy(() -> new JpaToscaPolicy((JpaToscaPolicy) null)).isInstanceOf(NullPointerException.class);
82
83         PfConceptKey tpKey = new PfConceptKey("tdt", VERSION_001);
84         PfConceptKey ptKey = new PfConceptKey("policyType", VERSION_001);
85         JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
86
87         Map<String, String> propertyMap = new HashMap<>();
88         propertyMap.put("Property", "\"Property Value\"");
89         tp.setProperties(propertyMap);
90         assertEquals(propertyMap, tp.getProperties());
91
92         List<PfConceptKey> targets = new ArrayList<>();
93         PfConceptKey target = new PfConceptKey("target", VERSION_001);
94         targets.add(target);
95         tp.setTargets(targets);
96         assertEquals(targets, tp.getTargets());
97
98         JpaToscaPolicy tdtClone0 = new JpaToscaPolicy(tp);
99         assertEquals(tp, tdtClone0);
100         assertEquals(0, tp.compareTo(tdtClone0));
101
102         JpaToscaPolicy tdtClone1 = new JpaToscaPolicy(tp);
103         assertEquals(tp, tdtClone1);
104         assertEquals(0, tp.compareTo(tdtClone1));
105
106         assertEquals(-1, tp.compareTo(null));
107         assertEquals(0, tp.compareTo(tp));
108         assertFalse(tp.compareTo(tp.getKey()) == 0);
109
110         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
111         JpaToscaPolicy otherDt = new JpaToscaPolicy(otherDtKey);
112
113         assertFalse(tp.compareTo(otherDt) == 0);
114         otherDt.setKey(tpKey);
115         assertFalse(tp.compareTo(otherDt) == 0);
116         otherDt.setType(ptKey);
117         assertFalse(tp.compareTo(otherDt) == 0);
118         otherDt.setProperties(propertyMap);
119         assertFalse(tp.compareTo(otherDt) == 0);
120         otherDt.setTargets(targets);
121         assertEquals(0, tp.compareTo(otherDt));
122
123         assertEquals(3, tp.getKeys().size());
124         assertEquals(2, new JpaToscaPolicy().getKeys().size());
125
126         new JpaToscaPolicy().clean();
127         tp.clean();
128         assertEquals(tdtClone0, tp);
129
130         assertFalse(new JpaToscaPolicy().validate(new PfValidationResult()).isValid());
131         assertTrue(tp.validate(new PfValidationResult()).isValid());
132
133         tp.getProperties().put(null, null);
134         assertFalse(tp.validate(new PfValidationResult()).isValid());
135         tp.getProperties().remove(null);
136         assertTrue(tp.validate(new PfValidationResult()).isValid());
137
138         tp.getProperties().put("Key", null);
139         assertFalse(tp.validate(new PfValidationResult()).isValid());
140         tp.getProperties().remove("Key");
141         assertTrue(tp.validate(new PfValidationResult()).isValid());
142
143         tp.getProperties().put(null, "Value");
144         assertFalse(tp.validate(new PfValidationResult()).isValid());
145         tp.getProperties().remove(null);
146         assertTrue(tp.validate(new PfValidationResult()).isValid());
147
148         tp.getTargets().add(null);
149         assertFalse(tp.validate(new PfValidationResult()).isValid());
150         tp.getTargets().remove(null);
151         assertTrue(tp.validate(new PfValidationResult()).isValid());
152
153         PfConceptKey tpTypeKey = tp.getKey();
154         assertNotNull(tpTypeKey);
155         tp.setType(null);
156         assertFalse(tp.validate(new PfValidationResult()).isValid());
157         tp.setType(PfConceptKey.getNullKey());
158         assertFalse(tp.validate(new PfValidationResult()).isValid());
159         tp.setType(tpTypeKey);
160         assertTrue(tp.validate(new PfValidationResult()).isValid());
161
162         assertThatThrownBy(() -> {
163             tp.validate(null);
164         }).hasMessageMatching("resultIn is marked .*on.*ull but is null");
165
166         assertNotNull(tp.toAuthorative());
167         tp.getType().setVersion(PfKey.NULL_KEY_VERSION);
168         assertNotNull(tp.toAuthorative());
169         tp.setProperties(null);
170         assertNotNull(tp.toAuthorative());
171
172         assertThatThrownBy(() -> {
173             tp.fromAuthorative(null);
174         }).hasMessageMatching("toscaPolicy is marked .*on.*ull but is null");
175
176         ToscaPolicy pol1 = new ToscaPolicy();
177         pol1.setName("policy");
178         pol1.setVersion("1.2.3");
179         pol1.setType("poltype");
180         pol1.setTypeVersion("2.2.3");
181         tp.fromAuthorative(pol1);
182         assertEquals("2.2.3", tp.getType().getVersion());
183     }
184 }