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.List;
34 import org.junit.Test;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfReferenceKey;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger;
44 * DAO test for ToscaPolicyType.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class JpaToscaPolicyTypeTest {
51 public void testPolicyTypePojo() {
52 assertNotNull(new JpaToscaPolicyType());
53 assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
54 assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
57 new JpaToscaPolicyType((PfConceptKey) null);
58 fail("test should throw an exception");
59 } catch (Exception exc) {
60 assertEquals("key is marked @NonNull but is null", exc.getMessage());
64 new JpaToscaPolicyType((JpaToscaPolicyType) null);
65 fail("test should throw an exception");
66 } catch (Exception exc) {
67 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
70 PfConceptKey ptKey = new PfConceptKey("tdt", "0.0.1");
71 JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
73 PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", "0.0.1");
74 tpt.setDerivedFrom(derivedFromKey);
76 Map<String, String> metadata = new HashMap<>();
77 metadata.put("key", "value");
78 tpt.setMetadata(metadata);
79 assertEquals(metadata, tpt.getMetadata());
81 tpt.setDescription("A Description");
83 PfConceptKey propTypeKey = new PfConceptKey("propType", "0.0.1");
84 List<JpaToscaProperty> properties = new ArrayList<>();
85 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
87 tpt.setProperties(properties);
88 assertEquals(properties, tpt.getProperties());
90 List<PfConceptKey> targets = new ArrayList<>();
91 PfConceptKey target = new PfConceptKey("target", "0.0.1");
93 tpt.setTargets(targets);
94 assertEquals(targets, tpt.getTargets());
96 List<JpaToscaTrigger> triggers = new ArrayList<>();
97 JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
98 triggers.add(trigger);
99 tpt.setTriggers(triggers);
100 assertEquals(triggers, tpt.getTriggers());
102 JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
103 assertEquals(tpt, tdtClone0);
104 assertEquals(0, tpt.compareTo(tdtClone0));
106 JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType();
107 tpt.copyTo(tdtClone1);
108 assertEquals(tpt, tdtClone1);
109 assertEquals(0, tpt.compareTo(tdtClone1));
111 assertEquals(-1, tpt.compareTo(null));
112 assertEquals(0, tpt.compareTo(tpt));
113 assertFalse(tpt.compareTo(tpt.getKey()) == 0);
115 PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
116 JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
118 assertFalse(tpt.compareTo(otherDt) == 0);
119 otherDt.setKey(ptKey);
120 assertFalse(tpt.compareTo(otherDt) == 0);
121 otherDt.setDerivedFrom(derivedFromKey);
122 assertFalse(tpt.compareTo(otherDt) == 0);
123 otherDt.setMetadata(metadata);
124 assertFalse(tpt.compareTo(otherDt) == 0);
125 otherDt.setDescription("A Description");
126 assertFalse(tpt.compareTo(otherDt) == 0);
127 otherDt.setProperties(properties);
128 assertFalse(tpt.compareTo(otherDt) == 0);
129 otherDt.setTargets(targets);
130 assertFalse(tpt.compareTo(otherDt) == 0);
131 otherDt.setTriggers(triggers);
132 assertEquals(0, tpt.compareTo(otherDt));
136 fail("test should throw an exception");
137 } catch (Exception exc) {
138 assertEquals("target is marked @NonNull but is null", exc.getMessage());
141 assertEquals(6, tpt.getKeys().size());
142 assertEquals(1, new JpaToscaPolicyType().getKeys().size());
144 new JpaToscaPolicyType().clean();
146 assertEquals(tdtClone0, tpt);
148 assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
149 assertTrue(tpt.validate(new PfValidationResult()).isValid());
151 tpt.getProperties().add(null);
152 assertFalse(tpt.validate(new PfValidationResult()).isValid());
153 tpt.getProperties().remove(null);
154 assertTrue(tpt.validate(new PfValidationResult()).isValid());
156 tpt.getTargets().add(null);
157 assertFalse(tpt.validate(new PfValidationResult()).isValid());
158 tpt.getTargets().remove(null);
159 assertTrue(tpt.validate(new PfValidationResult()).isValid());
161 tpt.getTriggers().add(null);
162 assertFalse(tpt.validate(new PfValidationResult()).isValid());
163 tpt.getTriggers().remove(null);
164 assertTrue(tpt.validate(new PfValidationResult()).isValid());
166 tpt.getMetadata().put(null, null);
167 assertFalse(tpt.validate(new PfValidationResult()).isValid());
168 tpt.getMetadata().remove(null);
169 assertTrue(tpt.validate(new PfValidationResult()).isValid());
171 tpt.getMetadata().put("nullKey", null);
172 assertFalse(tpt.validate(new PfValidationResult()).isValid());
173 tpt.getMetadata().remove("nullKey");
174 assertTrue(tpt.validate(new PfValidationResult()).isValid());
176 tpt.setDescription("");;
177 assertFalse(tpt.validate(new PfValidationResult()).isValid());
178 tpt.setDescription("A Description");
179 assertTrue(tpt.validate(new PfValidationResult()).isValid());
181 tpt.setDerivedFrom(PfConceptKey.getNullKey());
182 assertFalse(tpt.validate(new PfValidationResult()).isValid());
183 tpt.setDerivedFrom(derivedFromKey);
184 assertTrue(tpt.validate(new PfValidationResult()).isValid());
188 fail("test should throw an exception");
189 } catch (Exception exc) {
190 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
194 new JpaToscaEntityType((PfConceptKey) null);
195 fail("test should throw an exception");
196 } catch (Exception exc) {
197 assertEquals("key is marked @NonNull but is null", exc.getMessage());
201 new JpaToscaEntityType((JpaToscaEntityType) null);
202 fail("test should throw an exception");
203 } catch (Exception exc) {
204 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
207 JpaToscaEntityType tet = new JpaToscaEntityType(tpt.getKey());
208 assertEquals(-1, tet.compareTo(null));
209 assertEquals(0, tet.compareTo(tet));
210 assertFalse(tet.compareTo(tet.getKey()) == 0);