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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.LinkedHashMap;
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.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;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
44 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger;
47 * DAO test for ToscaPolicyType.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class JpaToscaPolicyTypeTest {
53 public void testPolicyTypePojo() {
54 assertNotNull(new JpaToscaPolicyType());
55 assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
56 assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
59 new JpaToscaPolicyType((PfConceptKey) null);
60 fail("test should throw an exception");
61 } catch (Exception exc) {
62 assertEquals("key is marked @NonNull but is null", exc.getMessage());
66 new JpaToscaPolicyType((JpaToscaPolicyType) null);
67 fail("test should throw an exception");
68 } catch (Exception exc) {
69 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
72 PfConceptKey ptKey = new PfConceptKey("tdt", "0.0.1");
73 JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
75 PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", "0.0.1");
76 tpt.setDerivedFrom(derivedFromKey);
78 Map<String, String> metadata = new HashMap<>();
79 metadata.put("key", "value");
80 tpt.setMetadata(metadata);
81 assertEquals(metadata, tpt.getMetadata());
83 tpt.setDescription("A Description");
85 PfConceptKey propTypeKey = new PfConceptKey("propType", "0.0.1");
86 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
87 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
88 properties.put(tp.getKey().getLocalName(), tp);
89 tpt.setProperties(properties);
90 assertEquals(properties, tpt.getProperties());
92 List<PfConceptKey> targets = new ArrayList<>();
93 PfConceptKey target = new PfConceptKey("target", "0.0.1");
95 tpt.setTargets(targets);
96 assertEquals(targets, tpt.getTargets());
98 List<JpaToscaTrigger> triggers = new ArrayList<>();
99 JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
100 triggers.add(trigger);
101 tpt.setTriggers(triggers);
102 assertEquals(triggers, tpt.getTriggers());
104 JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
105 assertEquals(tpt, tdtClone0);
106 assertEquals(0, tpt.compareTo(tdtClone0));
108 JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType();
109 tpt.copyTo(tdtClone1);
110 assertEquals(tpt, tdtClone1);
111 assertEquals(0, tpt.compareTo(tdtClone1));
113 assertEquals(-1, tpt.compareTo(null));
114 assertEquals(0, tpt.compareTo(tpt));
115 assertFalse(tpt.compareTo(tpt.getKey()) == 0);
117 PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
118 JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
120 assertFalse(tpt.compareTo(otherDt) == 0);
121 otherDt.setKey(ptKey);
122 assertFalse(tpt.compareTo(otherDt) == 0);
123 otherDt.setDerivedFrom(derivedFromKey);
124 assertFalse(tpt.compareTo(otherDt) == 0);
125 otherDt.setMetadata(metadata);
126 assertFalse(tpt.compareTo(otherDt) == 0);
127 otherDt.setDescription("A Description");
128 assertFalse(tpt.compareTo(otherDt) == 0);
129 otherDt.setProperties(properties);
130 assertFalse(tpt.compareTo(otherDt) == 0);
131 otherDt.setTargets(targets);
132 assertFalse(tpt.compareTo(otherDt) == 0);
133 otherDt.setTriggers(triggers);
134 assertEquals(0, tpt.compareTo(otherDt));
138 fail("test should throw an exception");
139 } catch (Exception exc) {
140 assertEquals("target is marked @NonNull but is null", exc.getMessage());
143 assertEquals(6, tpt.getKeys().size());
144 assertEquals(1, new JpaToscaPolicyType().getKeys().size());
146 new JpaToscaPolicyType().clean();
148 assertEquals(tdtClone0, tpt);
150 assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
151 assertTrue(tpt.validate(new PfValidationResult()).isValid());
153 tpt.getProperties().put(null, null);
154 assertFalse(tpt.validate(new PfValidationResult()).isValid());
155 tpt.getProperties().remove(null);
156 assertTrue(tpt.validate(new PfValidationResult()).isValid());
158 tpt.getTargets().add(null);
159 assertFalse(tpt.validate(new PfValidationResult()).isValid());
160 tpt.getTargets().remove(null);
161 assertTrue(tpt.validate(new PfValidationResult()).isValid());
163 tpt.getTriggers().add(null);
164 assertFalse(tpt.validate(new PfValidationResult()).isValid());
165 tpt.getTriggers().remove(null);
166 assertTrue(tpt.validate(new PfValidationResult()).isValid());
168 tpt.getMetadata().put(null, null);
169 assertFalse(tpt.validate(new PfValidationResult()).isValid());
170 tpt.getMetadata().remove(null);
171 assertTrue(tpt.validate(new PfValidationResult()).isValid());
173 tpt.getMetadata().put("nullKey", null);
174 assertFalse(tpt.validate(new PfValidationResult()).isValid());
175 tpt.getMetadata().remove("nullKey");
176 assertTrue(tpt.validate(new PfValidationResult()).isValid());
178 tpt.setDescription("");;
179 assertFalse(tpt.validate(new PfValidationResult()).isValid());
180 tpt.setDescription("A Description");
181 assertTrue(tpt.validate(new PfValidationResult()).isValid());
183 tpt.setDerivedFrom(PfConceptKey.getNullKey());
184 assertFalse(tpt.validate(new PfValidationResult()).isValid());
185 tpt.setDerivedFrom(derivedFromKey);
186 assertTrue(tpt.validate(new PfValidationResult()).isValid());
190 fail("test should throw an exception");
191 } catch (Exception exc) {
192 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
196 new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null);
197 fail("test should throw an exception");
198 } catch (Exception exc) {
199 assertEquals("key is marked @NonNull but is null", exc.getMessage());
203 new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null);
204 fail("test should throw an exception");
205 } catch (Exception exc) {
206 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
209 JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<ToscaPolicy>(tpt.getKey());
210 assertEquals(-1, tet.compareTo(null));
211 assertEquals(0, tet.compareTo(tet));
212 assertFalse(tet.compareTo(tet.getKey()) == 0);
214 assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
216 assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));