Merge "Fix more sonar issues in models: yaml to dao"
[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  *  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
35 import org.junit.Test;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfValidationResult;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
41
42 /**
43  * DAO test for ToscaPolicy.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaToscaPolicyTest {
48
49     private static final String KEY_IS_NULL = "key is marked @NonNull but is null";
50     private static final String VERSION_001 = "0.0.1";
51
52     @Test
53     public void testPolicyPojo() {
54         assertNotNull(new JpaToscaPolicy());
55         assertNotNull(new JpaToscaPolicy(new PfConceptKey()));
56         assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey()));
57         assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy()));
58
59         ToscaPolicy pol = new ToscaPolicy();
60         pol.setType("type");
61         assertNotNull(new JpaToscaPolicy(pol));
62
63         assertThatThrownBy(() -> {
64             new JpaToscaPolicy((PfConceptKey) null);
65         }).hasMessage(KEY_IS_NULL);
66
67         assertThatThrownBy(() -> {
68             new JpaToscaPolicy(null, null);
69         }).hasMessage(KEY_IS_NULL);
70
71         assertThatThrownBy(() -> {
72             new JpaToscaPolicy(new PfConceptKey(), null);
73         }).hasMessage("type is marked @NonNull but is null");
74
75         assertThatThrownBy(() -> {
76             new JpaToscaPolicy(null, new PfConceptKey());
77         }).hasMessage(KEY_IS_NULL);
78
79         assertThatThrownBy(() -> {
80             new JpaToscaPolicy((JpaToscaPolicy) null);
81         }).hasMessage("copyConcept is marked @NonNull but is null");
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();
103         tp.copyTo(tdtClone1);
104         assertEquals(tp, tdtClone1);
105         assertEquals(0, tp.compareTo(tdtClone1));
106
107         assertEquals(-1, tp.compareTo(null));
108         assertEquals(0, tp.compareTo(tp));
109         assertFalse(tp.compareTo(tp.getKey()) == 0);
110
111         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
112         JpaToscaPolicy otherDt = new JpaToscaPolicy(otherDtKey);
113
114         assertFalse(tp.compareTo(otherDt) == 0);
115         otherDt.setKey(tpKey);
116         assertFalse(tp.compareTo(otherDt) == 0);
117         otherDt.setType(ptKey);
118         assertFalse(tp.compareTo(otherDt) == 0);
119         otherDt.setProperties(propertyMap);
120         assertFalse(tp.compareTo(otherDt) == 0);
121         otherDt.setTargets(targets);
122         assertEquals(0, tp.compareTo(otherDt));
123
124         assertThatThrownBy(() -> {
125             tp.copyTo(null);
126         }).hasMessage("target is marked @NonNull but is null");
127
128         assertEquals(3, tp.getKeys().size());
129         assertEquals(2, new JpaToscaPolicy().getKeys().size());
130
131         new JpaToscaPolicy().clean();
132         tp.clean();
133         assertEquals(tdtClone0, tp);
134
135         assertFalse(new JpaToscaPolicy().validate(new PfValidationResult()).isValid());
136         assertTrue(tp.validate(new PfValidationResult()).isValid());
137
138         tp.getProperties().put(null, null);
139         assertFalse(tp.validate(new PfValidationResult()).isValid());
140         tp.getProperties().remove(null);
141         assertTrue(tp.validate(new PfValidationResult()).isValid());
142
143         tp.getProperties().put("Key", null);
144         assertFalse(tp.validate(new PfValidationResult()).isValid());
145         tp.getProperties().remove("Key");
146         assertTrue(tp.validate(new PfValidationResult()).isValid());
147
148         tp.getProperties().put(null, "Value");
149         assertFalse(tp.validate(new PfValidationResult()).isValid());
150         tp.getProperties().remove(null);
151         assertTrue(tp.validate(new PfValidationResult()).isValid());
152
153         tp.getTargets().add(null);
154         assertFalse(tp.validate(new PfValidationResult()).isValid());
155         tp.getTargets().remove(null);
156         assertTrue(tp.validate(new PfValidationResult()).isValid());
157
158         PfConceptKey tpTypeKey = tp.getKey();
159         assertNotNull(tpTypeKey);
160         tp.setType(null);
161         assertFalse(tp.validate(new PfValidationResult()).isValid());
162         tp.setType(PfConceptKey.getNullKey());
163         assertFalse(tp.validate(new PfValidationResult()).isValid());
164         tp.setType(tpTypeKey);
165         assertTrue(tp.validate(new PfValidationResult()).isValid());
166
167         assertThatThrownBy(() -> {
168             tp.validate(null);
169         }).hasMessage("resultIn is marked @NonNull but is null");
170
171         assertNotNull(tp.toAuthorative());
172         tp.getType().setVersion(PfKey.NULL_KEY_VERSION);
173         assertNotNull(tp.toAuthorative());
174         tp.setProperties(null);
175         assertNotNull(tp.toAuthorative());
176
177         assertThatThrownBy(() -> {
178             tp.fromAuthorative(null);
179         }).hasMessage("toscaPolicy is marked @NonNull but is null");
180
181         pol = new ToscaPolicy();
182         pol.setName("policy");
183         pol.setVersion("1.2.3");
184         pol.setType("poltype");
185         pol.setTypeVersion("2.2.3");
186         tp.fromAuthorative(pol);
187         assertEquals("2.2.3", tp.getType().getVersion());
188     }
189 }