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 .isInstanceOf(NullPointerException.class);
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(tpt);
100 assertEquals(tpt, tdtClone1);
101 assertEquals(0, tpt.compareTo(tdtClone1));
103 assertEquals(-1, tpt.compareTo(null));
104 assertEquals(0, tpt.compareTo(tpt));
105 assertFalse(tpt.compareTo(tpt.getKey()) == 0);
107 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
108 JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
110 assertFalse(tpt.compareTo(otherDt) == 0);
111 otherDt.setKey(ptKey);
112 assertFalse(tpt.compareTo(otherDt) == 0);
113 otherDt.setDerivedFrom(derivedFromKey);
114 assertFalse(tpt.compareTo(otherDt) == 0);
115 otherDt.setMetadata(metadata);
116 assertFalse(tpt.compareTo(otherDt) == 0);
117 otherDt.setDescription(A_DESCRIPTION);
118 assertFalse(tpt.compareTo(otherDt) == 0);
119 otherDt.setProperties(properties);
120 assertFalse(tpt.compareTo(otherDt) == 0);
121 otherDt.setTargets(targets);
122 assertFalse(tpt.compareTo(otherDt) == 0);
123 otherDt.setTriggers(triggers);
124 assertEquals(0, tpt.compareTo(otherDt));
126 assertEquals(6, tpt.getKeys().size());
127 assertEquals(1, new JpaToscaPolicyType().getKeys().size());
129 new JpaToscaPolicyType().clean();
131 assertEquals(tdtClone0, tpt);
133 assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
134 assertTrue(tpt.validate(new PfValidationResult()).isValid());
136 tpt.getProperties().put(null, null);
137 assertFalse(tpt.validate(new PfValidationResult()).isValid());
138 tpt.getProperties().remove(null);
139 assertTrue(tpt.validate(new PfValidationResult()).isValid());
141 tpt.getTargets().add(null);
142 assertFalse(tpt.validate(new PfValidationResult()).isValid());
143 tpt.getTargets().remove(null);
144 assertTrue(tpt.validate(new PfValidationResult()).isValid());
146 tpt.getTriggers().add(null);
147 assertFalse(tpt.validate(new PfValidationResult()).isValid());
148 tpt.getTriggers().remove(null);
149 assertTrue(tpt.validate(new PfValidationResult()).isValid());
151 tpt.getMetadata().put(null, null);
152 assertFalse(tpt.validate(new PfValidationResult()).isValid());
153 tpt.getMetadata().remove(null);
154 assertTrue(tpt.validate(new PfValidationResult()).isValid());
156 tpt.getMetadata().put("nullKey", null);
157 assertFalse(tpt.validate(new PfValidationResult()).isValid());
158 tpt.getMetadata().remove("nullKey");
159 assertTrue(tpt.validate(new PfValidationResult()).isValid());
161 tpt.setDescription("");;
162 assertFalse(tpt.validate(new PfValidationResult()).isValid());
163 tpt.setDescription(A_DESCRIPTION);
164 assertTrue(tpt.validate(new PfValidationResult()).isValid());
166 tpt.setDerivedFrom(PfConceptKey.getNullKey());
167 assertFalse(tpt.validate(new PfValidationResult()).isValid());
168 tpt.setDerivedFrom(derivedFromKey);
169 assertTrue(tpt.validate(new PfValidationResult()).isValid());
171 assertThatThrownBy(() -> tpt.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
173 assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null))
174 .hasMessage("key is marked @NonNull but is null");
176 assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null))
177 .isInstanceOf(NullPointerException.class);
179 JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey());
180 assertEquals(-1, tet.compareTo(null));
181 assertEquals(0, tet.compareTo(tet));
182 assertFalse(tet.compareTo(tet.getKey()) == 0);
184 assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
186 assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));