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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
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;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
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;
42 * DAO test for ToscaPolicy.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class JpaToscaPolicyTest {
48 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
49 private static final String VERSION_001 = "0.0.1";
52 public void testPolicyPojo() {
53 assertNotNull(new JpaToscaPolicy());
54 assertNotNull(new JpaToscaPolicy(new PfConceptKey()));
55 assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey()));
56 assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy()));
58 ToscaPolicy pol = new ToscaPolicy();
60 assertNotNull(new JpaToscaPolicy(pol));
62 assertThatThrownBy(() -> {
63 new JpaToscaPolicy((PfConceptKey) null);
64 }).hasMessageMatching(KEY_IS_NULL);
66 assertThatThrownBy(() -> {
67 new JpaToscaPolicy(null, null);
68 }).hasMessageMatching(KEY_IS_NULL);
70 assertThatThrownBy(() -> {
71 new JpaToscaPolicy(new PfConceptKey(), null);
72 }).hasMessageMatching("type is marked .*on.*ull but is null");
74 assertThatThrownBy(() -> {
75 new JpaToscaPolicy(null, new PfConceptKey());
76 }).hasMessageMatching(KEY_IS_NULL);
78 assertThatThrownBy(() -> new JpaToscaPolicy((JpaToscaPolicy) null)).isInstanceOf(NullPointerException.class);
80 PfConceptKey tpKey = new PfConceptKey("tdt", VERSION_001);
81 PfConceptKey ptKey = new PfConceptKey("policyType", VERSION_001);
82 JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
84 Map<String, String> propertyMap = new HashMap<>();
85 propertyMap.put("Property", "\"Property Value\"");
86 tp.setProperties(propertyMap);
87 assertEquals(propertyMap, tp.getProperties());
89 List<PfConceptKey> targets = new ArrayList<>();
90 PfConceptKey target = new PfConceptKey("target", VERSION_001);
92 tp.setTargets(targets);
93 assertEquals(targets, tp.getTargets());
95 JpaToscaPolicy tdtClone0 = new JpaToscaPolicy(tp);
96 assertEquals(tp, tdtClone0);
97 assertEquals(0, tp.compareTo(tdtClone0));
99 JpaToscaPolicy tdtClone1 = new JpaToscaPolicy(tp);
100 assertEquals(tp, tdtClone1);
101 assertEquals(0, tp.compareTo(tdtClone1));
103 assertEquals(-1, tp.compareTo(null));
104 assertEquals(0, tp.compareTo(tp));
105 assertFalse(tp.compareTo(tp.getKey()) == 0);
107 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
108 JpaToscaPolicy otherDt = new JpaToscaPolicy(otherDtKey);
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));
120 assertEquals(3, tp.getKeys().size());
121 assertEquals(2, new JpaToscaPolicy().getKeys().size());
123 new JpaToscaPolicy().clean();
125 assertEquals(tdtClone0, tp);
127 assertFalse(new JpaToscaPolicy().validate(new PfValidationResult()).isValid());
128 assertTrue(tp.validate(new PfValidationResult()).isValid());
130 tp.getProperties().put(null, null);
131 assertFalse(tp.validate(new PfValidationResult()).isValid());
132 tp.getProperties().remove(null);
133 assertTrue(tp.validate(new PfValidationResult()).isValid());
135 tp.getProperties().put("Key", null);
136 assertFalse(tp.validate(new PfValidationResult()).isValid());
137 tp.getProperties().remove("Key");
138 assertTrue(tp.validate(new PfValidationResult()).isValid());
140 tp.getProperties().put(null, "Value");
141 assertFalse(tp.validate(new PfValidationResult()).isValid());
142 tp.getProperties().remove(null);
143 assertTrue(tp.validate(new PfValidationResult()).isValid());
145 tp.getTargets().add(null);
146 assertFalse(tp.validate(new PfValidationResult()).isValid());
147 tp.getTargets().remove(null);
148 assertTrue(tp.validate(new PfValidationResult()).isValid());
150 PfConceptKey tpTypeKey = tp.getKey();
151 assertNotNull(tpTypeKey);
153 assertFalse(tp.validate(new PfValidationResult()).isValid());
154 tp.setType(PfConceptKey.getNullKey());
155 assertFalse(tp.validate(new PfValidationResult()).isValid());
156 tp.setType(tpTypeKey);
157 assertTrue(tp.validate(new PfValidationResult()).isValid());
159 assertThatThrownBy(() -> {
161 }).hasMessageMatching("resultIn is marked .*on.*ull but is null");
163 assertNotNull(tp.toAuthorative());
164 tp.getType().setVersion(PfKey.NULL_KEY_VERSION);
165 assertNotNull(tp.toAuthorative());
166 tp.setProperties(null);
167 assertNotNull(tp.toAuthorative());
169 assertThatThrownBy(() -> {
170 tp.fromAuthorative(null);
171 }).hasMessageMatching("toscaPolicy is marked .*on.*ull but is null");
173 pol = new ToscaPolicy();
174 pol.setName("policy");
175 pol.setVersion("1.2.3");
176 pol.setType("poltype");
177 pol.setTypeVersion("2.2.3");
178 tp.fromAuthorative(pol);
179 assertEquals("2.2.3", tp.getType().getVersion());