Merge "Add copy constructors to more models-pdp classes"
[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.List;
32 import java.util.Map;
33
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;
42
43 /**
44  * DAO test for ToscaPolicyType.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class JpaToscaPolicyTypeTest {
49
50     @Test
51     public void testPolicyTypePojo() {
52         assertNotNull(new JpaToscaPolicyType());
53         assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
54         assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
55
56         try {
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());
61         }
62
63         try {
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());
68         }
69
70         PfConceptKey ptKey = new PfConceptKey("tdt", "0.0.1");
71         JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
72
73         PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", "0.0.1");
74         tpt.setDerivedFrom(derivedFromKey);
75
76         Map<String, String> metadata = new HashMap<>();
77         metadata.put("key", "value");
78         tpt.setMetadata(metadata);
79         assertEquals(metadata, tpt.getMetadata());
80
81         tpt.setDescription("A Description");
82
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);
86         properties.add(tp);
87         tpt.setProperties(properties);
88         assertEquals(properties, tpt.getProperties());
89
90         List<PfConceptKey> targets = new ArrayList<>();
91         PfConceptKey target = new PfConceptKey("target", "0.0.1");
92         targets.add(target);
93         tpt.setTargets(targets);
94         assertEquals(targets, tpt.getTargets());
95
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());
101
102         JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
103         assertEquals(tpt, tdtClone0);
104         assertEquals(0, tpt.compareTo(tdtClone0));
105
106         JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType();
107         tpt.copyTo(tdtClone1);
108         assertEquals(tpt, tdtClone1);
109         assertEquals(0, tpt.compareTo(tdtClone1));
110
111         assertEquals(-1, tpt.compareTo(null));
112         assertEquals(0, tpt.compareTo(tpt));
113         assertFalse(tpt.compareTo(tpt.getKey()) == 0);
114
115         PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
116         JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
117
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));
133
134         try {
135             tpt.copyTo(null);
136             fail("test should throw an exception");
137         } catch (Exception exc) {
138             assertEquals("target is marked @NonNull but is null", exc.getMessage());
139         }
140
141         assertEquals(6, tpt.getKeys().size());
142         assertEquals(1, new JpaToscaPolicyType().getKeys().size());
143
144         new JpaToscaPolicyType().clean();
145         tpt.clean();
146         assertEquals(tdtClone0, tpt);
147
148         assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
149         assertTrue(tpt.validate(new PfValidationResult()).isValid());
150
151         tpt.getProperties().add(null);
152         assertFalse(tpt.validate(new PfValidationResult()).isValid());
153         tpt.getProperties().remove(null);
154         assertTrue(tpt.validate(new PfValidationResult()).isValid());
155
156         tpt.getTargets().add(null);
157         assertFalse(tpt.validate(new PfValidationResult()).isValid());
158         tpt.getTargets().remove(null);
159         assertTrue(tpt.validate(new PfValidationResult()).isValid());
160
161         tpt.getTriggers().add(null);
162         assertFalse(tpt.validate(new PfValidationResult()).isValid());
163         tpt.getTriggers().remove(null);
164         assertTrue(tpt.validate(new PfValidationResult()).isValid());
165
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());
170
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());
175
176         tpt.setDescription("");;
177         assertFalse(tpt.validate(new PfValidationResult()).isValid());
178         tpt.setDescription("A Description");
179         assertTrue(tpt.validate(new PfValidationResult()).isValid());
180
181         tpt.setDerivedFrom(PfConceptKey.getNullKey());
182         assertFalse(tpt.validate(new PfValidationResult()).isValid());
183         tpt.setDerivedFrom(derivedFromKey);
184         assertTrue(tpt.validate(new PfValidationResult()).isValid());
185
186         try {
187             tpt.validate(null);
188             fail("test should throw an exception");
189         } catch (Exception exc) {
190             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
191         }
192
193         try {
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());
198         }
199
200         try {
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());
205         }
206
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);
211     }
212 }