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