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
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.LinkedHashMap;
33 import java.util.List;
35 import org.junit.Test;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfReferenceKey;
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.authorative.concepts.ToscaPolicyType;
43 * DAO test for ToscaPolicyType.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class JpaToscaPolicyTypeTest {
48 private static final String A_DESCRIPTION = "A Description";
49 private static final String VERSION_001 = "0.0.1";
52 public void testPolicyTypePojo() {
53 assertNotNull(new JpaToscaPolicyType());
54 assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
55 assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
57 assertThatThrownBy(() -> new JpaToscaPolicyType((PfConceptKey) null))
58 .hasMessage("key is marked @NonNull but is null");
60 assertThatThrownBy(() -> new JpaToscaPolicyType((JpaToscaPolicyType) null))
61 .hasMessage("copyConcept is marked @NonNull but is null");
63 PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
64 JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
66 PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
67 tpt.setDerivedFrom(derivedFromKey);
69 Map<String, String> metadata = new HashMap<>();
70 metadata.put("key", "value");
71 tpt.setMetadata(metadata);
72 assertEquals(metadata, tpt.getMetadata());
74 tpt.setDescription(A_DESCRIPTION);
76 PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
77 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
78 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
79 properties.put(tp.getKey().getLocalName(), tp);
80 tpt.setProperties(properties);
81 assertEquals(properties, tpt.getProperties());
83 List<PfConceptKey> targets = new ArrayList<>();
84 PfConceptKey target = new PfConceptKey("target", VERSION_001);
86 tpt.setTargets(targets);
87 assertEquals(targets, tpt.getTargets());
89 List<JpaToscaTrigger> triggers = new ArrayList<>();
90 JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
91 triggers.add(trigger);
92 tpt.setTriggers(triggers);
93 assertEquals(triggers, tpt.getTriggers());
95 JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
96 assertEquals(tpt, tdtClone0);
97 assertEquals(0, tpt.compareTo(tdtClone0));
99 JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType();
100 tpt.copyTo(tdtClone1);
101 assertEquals(tpt, tdtClone1);
102 assertEquals(0, tpt.compareTo(tdtClone1));
104 assertEquals(-1, tpt.compareTo(null));
105 assertEquals(0, tpt.compareTo(tpt));
106 assertFalse(tpt.compareTo(tpt.getKey()) == 0);
108 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
109 JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
111 assertFalse(tpt.compareTo(otherDt) == 0);
112 otherDt.setKey(ptKey);
113 assertFalse(tpt.compareTo(otherDt) == 0);
114 otherDt.setDerivedFrom(derivedFromKey);
115 assertFalse(tpt.compareTo(otherDt) == 0);
116 otherDt.setMetadata(metadata);
117 assertFalse(tpt.compareTo(otherDt) == 0);
118 otherDt.setDescription(A_DESCRIPTION);
119 assertFalse(tpt.compareTo(otherDt) == 0);
120 otherDt.setProperties(properties);
121 assertFalse(tpt.compareTo(otherDt) == 0);
122 otherDt.setTargets(targets);
123 assertFalse(tpt.compareTo(otherDt) == 0);
124 otherDt.setTriggers(triggers);
125 assertEquals(0, tpt.compareTo(otherDt));
127 assertThatThrownBy(() -> tpt.copyTo(null)).hasMessage("target is marked @NonNull but is null");
129 assertEquals(6, tpt.getKeys().size());
130 assertEquals(1, new JpaToscaPolicyType().getKeys().size());
132 new JpaToscaPolicyType().clean();
134 assertEquals(tdtClone0, tpt);
136 assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
137 assertTrue(tpt.validate(new PfValidationResult()).isValid());
139 tpt.getProperties().put(null, null);
140 assertFalse(tpt.validate(new PfValidationResult()).isValid());
141 tpt.getProperties().remove(null);
142 assertTrue(tpt.validate(new PfValidationResult()).isValid());
144 tpt.getTargets().add(null);
145 assertFalse(tpt.validate(new PfValidationResult()).isValid());
146 tpt.getTargets().remove(null);
147 assertTrue(tpt.validate(new PfValidationResult()).isValid());
149 tpt.getTriggers().add(null);
150 assertFalse(tpt.validate(new PfValidationResult()).isValid());
151 tpt.getTriggers().remove(null);
152 assertTrue(tpt.validate(new PfValidationResult()).isValid());
154 tpt.getMetadata().put(null, null);
155 assertFalse(tpt.validate(new PfValidationResult()).isValid());
156 tpt.getMetadata().remove(null);
157 assertTrue(tpt.validate(new PfValidationResult()).isValid());
159 tpt.getMetadata().put("nullKey", null);
160 assertFalse(tpt.validate(new PfValidationResult()).isValid());
161 tpt.getMetadata().remove("nullKey");
162 assertTrue(tpt.validate(new PfValidationResult()).isValid());
164 tpt.setDescription("");;
165 assertFalse(tpt.validate(new PfValidationResult()).isValid());
166 tpt.setDescription(A_DESCRIPTION);
167 assertTrue(tpt.validate(new PfValidationResult()).isValid());
169 tpt.setDerivedFrom(PfConceptKey.getNullKey());
170 assertFalse(tpt.validate(new PfValidationResult()).isValid());
171 tpt.setDerivedFrom(derivedFromKey);
172 assertTrue(tpt.validate(new PfValidationResult()).isValid());
174 assertThatThrownBy(() -> tpt.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
176 assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null))
177 .hasMessage("key is marked @NonNull but is null");
179 assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null))
180 .hasMessage("copyConcept is marked @NonNull but is null");
182 JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey());
183 assertEquals(-1, tet.compareTo(null));
184 assertEquals(0, tet.compareTo(tet));
185 assertFalse(tet.compareTo(tet.getKey()) == 0);
187 assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
189 assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));