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