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.simple.concepts.JpaToscaEntityType;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger;
46 * DAO test for ToscaPolicyType.
48 * @author Liam Fallon (liam.fallon@est.tech)
50 public class JpaToscaPolicyTypeTest {
52 public void testPolicyTypePojo() {
53 assertNotNull(new JpaToscaPolicyType());
54 assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
55 assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
58 new JpaToscaPolicyType((PfConceptKey) null);
59 fail("test should throw an exception");
60 } catch (Exception exc) {
61 assertEquals("key is marked @NonNull but is null", exc.getMessage());
65 new JpaToscaPolicyType((JpaToscaPolicyType) null);
66 fail("test should throw an exception");
67 } catch (Exception exc) {
68 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
71 PfConceptKey ptKey = new PfConceptKey("tdt", "0.0.1");
72 JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
74 PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", "0.0.1");
75 tpt.setDerivedFrom(derivedFromKey);
77 Map<String, String> metadata = new HashMap<>();
78 metadata.put("key", "value");
79 tpt.setMetadata(metadata);
80 assertEquals(metadata, tpt.getMetadata());
82 tpt.setDescription("A Description");
84 PfConceptKey propTypeKey = new PfConceptKey("propType", "0.0.1");
85 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
86 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
87 properties.put(tp.getKey().getLocalName(), tp);
88 tpt.setProperties(properties);
89 assertEquals(properties, tpt.getProperties());
91 List<PfConceptKey> targets = new ArrayList<>();
92 PfConceptKey target = new PfConceptKey("target", "0.0.1");
94 tpt.setTargets(targets);
95 assertEquals(targets, tpt.getTargets());
97 List<JpaToscaTrigger> triggers = new ArrayList<>();
98 JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
99 triggers.add(trigger);
100 tpt.setTriggers(triggers);
101 assertEquals(triggers, tpt.getTriggers());
103 JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
104 assertEquals(tpt, tdtClone0);
105 assertEquals(0, tpt.compareTo(tdtClone0));
107 JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType();
108 tpt.copyTo(tdtClone1);
109 assertEquals(tpt, tdtClone1);
110 assertEquals(0, tpt.compareTo(tdtClone1));
112 assertEquals(-1, tpt.compareTo(null));
113 assertEquals(0, tpt.compareTo(tpt));
114 assertFalse(tpt.compareTo(tpt.getKey()) == 0);
116 PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
117 JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
119 assertFalse(tpt.compareTo(otherDt) == 0);
120 otherDt.setKey(ptKey);
121 assertFalse(tpt.compareTo(otherDt) == 0);
122 otherDt.setDerivedFrom(derivedFromKey);
123 assertFalse(tpt.compareTo(otherDt) == 0);
124 otherDt.setMetadata(metadata);
125 assertFalse(tpt.compareTo(otherDt) == 0);
126 otherDt.setDescription("A Description");
127 assertFalse(tpt.compareTo(otherDt) == 0);
128 otherDt.setProperties(properties);
129 assertFalse(tpt.compareTo(otherDt) == 0);
130 otherDt.setTargets(targets);
131 assertFalse(tpt.compareTo(otherDt) == 0);
132 otherDt.setTriggers(triggers);
133 assertEquals(0, tpt.compareTo(otherDt));
137 fail("test should throw an exception");
138 } catch (Exception exc) {
139 assertEquals("target is marked @NonNull but is null", exc.getMessage());
142 assertEquals(6, tpt.getKeys().size());
143 assertEquals(1, new JpaToscaPolicyType().getKeys().size());
145 new JpaToscaPolicyType().clean();
147 assertEquals(tdtClone0, tpt);
149 assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
150 assertTrue(tpt.validate(new PfValidationResult()).isValid());
152 tpt.getProperties().put(null, null);
153 assertFalse(tpt.validate(new PfValidationResult()).isValid());
154 tpt.getProperties().remove(null);
155 assertTrue(tpt.validate(new PfValidationResult()).isValid());
157 tpt.getTargets().add(null);
158 assertFalse(tpt.validate(new PfValidationResult()).isValid());
159 tpt.getTargets().remove(null);
160 assertTrue(tpt.validate(new PfValidationResult()).isValid());
162 tpt.getTriggers().add(null);
163 assertFalse(tpt.validate(new PfValidationResult()).isValid());
164 tpt.getTriggers().remove(null);
165 assertTrue(tpt.validate(new PfValidationResult()).isValid());
167 tpt.getMetadata().put(null, null);
168 assertFalse(tpt.validate(new PfValidationResult()).isValid());
169 tpt.getMetadata().remove(null);
170 assertTrue(tpt.validate(new PfValidationResult()).isValid());
172 tpt.getMetadata().put("nullKey", null);
173 assertFalse(tpt.validate(new PfValidationResult()).isValid());
174 tpt.getMetadata().remove("nullKey");
175 assertTrue(tpt.validate(new PfValidationResult()).isValid());
177 tpt.setDescription("");;
178 assertFalse(tpt.validate(new PfValidationResult()).isValid());
179 tpt.setDescription("A Description");
180 assertTrue(tpt.validate(new PfValidationResult()).isValid());
182 tpt.setDerivedFrom(PfConceptKey.getNullKey());
183 assertFalse(tpt.validate(new PfValidationResult()).isValid());
184 tpt.setDerivedFrom(derivedFromKey);
185 assertTrue(tpt.validate(new PfValidationResult()).isValid());
189 fail("test should throw an exception");
190 } catch (Exception exc) {
191 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
195 new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null);
196 fail("test should throw an exception");
197 } catch (Exception exc) {
198 assertEquals("key is marked @NonNull but is null", exc.getMessage());
202 new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null);
203 fail("test should throw an exception");
204 } catch (Exception exc) {
205 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
208 JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<ToscaPolicy>(tpt.getKey());
209 assertEquals(-1, tet.compareTo(null));
210 assertEquals(0, tet.compareTo(tet));
211 assertFalse(tet.compareTo(tet.getKey()) == 0);