Finish unit test on policy-models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTypeTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
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;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34
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;
45
46 /**
47  * DAO test for ToscaPolicyType.
48  *
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 public class JpaToscaPolicyTypeTest {
52     @Test
53     public void testPolicyTypePojo() {
54         assertNotNull(new JpaToscaPolicyType());
55         assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
56         assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
57
58         try {
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());
63         }
64
65         try {
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());
70         }
71
72         PfConceptKey ptKey = new PfConceptKey("tdt", "0.0.1");
73         JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
74
75         PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", "0.0.1");
76         tpt.setDerivedFrom(derivedFromKey);
77
78         Map<String, String> metadata = new HashMap<>();
79         metadata.put("key", "value");
80         tpt.setMetadata(metadata);
81         assertEquals(metadata, tpt.getMetadata());
82
83         tpt.setDescription("A Description");
84
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());
91
92         List<PfConceptKey> targets = new ArrayList<>();
93         PfConceptKey target = new PfConceptKey("target", "0.0.1");
94         targets.add(target);
95         tpt.setTargets(targets);
96         assertEquals(targets, tpt.getTargets());
97
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());
103
104         JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
105         assertEquals(tpt, tdtClone0);
106         assertEquals(0, tpt.compareTo(tdtClone0));
107
108         JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType();
109         tpt.copyTo(tdtClone1);
110         assertEquals(tpt, tdtClone1);
111         assertEquals(0, tpt.compareTo(tdtClone1));
112
113         assertEquals(-1, tpt.compareTo(null));
114         assertEquals(0, tpt.compareTo(tpt));
115         assertFalse(tpt.compareTo(tpt.getKey()) == 0);
116
117         PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
118         JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
119
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));
135
136         try {
137             tpt.copyTo(null);
138             fail("test should throw an exception");
139         } catch (Exception exc) {
140             assertEquals("target is marked @NonNull but is null", exc.getMessage());
141         }
142
143         assertEquals(6, tpt.getKeys().size());
144         assertEquals(1, new JpaToscaPolicyType().getKeys().size());
145
146         new JpaToscaPolicyType().clean();
147         tpt.clean();
148         assertEquals(tdtClone0, tpt);
149
150         assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
151         assertTrue(tpt.validate(new PfValidationResult()).isValid());
152
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());
157
158         tpt.getTargets().add(null);
159         assertFalse(tpt.validate(new PfValidationResult()).isValid());
160         tpt.getTargets().remove(null);
161         assertTrue(tpt.validate(new PfValidationResult()).isValid());
162
163         tpt.getTriggers().add(null);
164         assertFalse(tpt.validate(new PfValidationResult()).isValid());
165         tpt.getTriggers().remove(null);
166         assertTrue(tpt.validate(new PfValidationResult()).isValid());
167
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());
172
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());
177
178         tpt.setDescription("");;
179         assertFalse(tpt.validate(new PfValidationResult()).isValid());
180         tpt.setDescription("A Description");
181         assertTrue(tpt.validate(new PfValidationResult()).isValid());
182
183         tpt.setDerivedFrom(PfConceptKey.getNullKey());
184         assertFalse(tpt.validate(new PfValidationResult()).isValid());
185         tpt.setDerivedFrom(derivedFromKey);
186         assertTrue(tpt.validate(new PfValidationResult()).isValid());
187
188         try {
189             tpt.validate(null);
190             fail("test should throw an exception");
191         } catch (Exception exc) {
192             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
193         }
194
195         try {
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());
200         }
201
202         try {
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());
207         }
208
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);
213
214         assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
215
216         assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));
217     }
218 }