Merge "Added VFModule count"
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPropertyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfReferenceKey;
35 import org.onap.policy.models.base.PfValidationResult;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
37
38 /**
39  * DAO test for ToscaProperty.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 public class JpaToscaPropertyTest {
44
45     private static final String KEY_IS_NULL = "key is marked @NonNull but is null";
46     private static final String DEFAULT_KEY = "defaultKey";
47     private static final String A_DESCRIPTION = "A Description";
48     private static final String VERSION_001 = "0.0.1";
49
50     @Test
51     public void testPropertyPojo() {
52         assertNotNull(new JpaToscaProperty());
53         assertNotNull(new JpaToscaProperty(new PfReferenceKey()));
54         assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey()));
55         assertNotNull(new JpaToscaProperty(new JpaToscaProperty()));
56
57         assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessage(KEY_IS_NULL);
58
59         assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessage(KEY_IS_NULL);
60
61         assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessage(KEY_IS_NULL);
62
63         assertThatThrownBy(() -> new JpaToscaProperty(new PfReferenceKey(), null))
64                         .hasMessage("type is marked @NonNull but is null");
65
66         PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
67         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
68         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
69         JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
70
71         tp.setDescription(A_DESCRIPTION);
72         assertEquals(A_DESCRIPTION, tp.getDescription());
73
74         tp.setRequired(false);
75         assertFalse(tp.isRequired());
76
77         tp.setDefaultValue(DEFAULT_KEY);
78
79         tp.setStatus(ToscaProperty.Status.SUPPORTED);
80
81         List<JpaToscaConstraint> constraints = new ArrayList<>();
82         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
83         constraints.add(lsc);
84         tp.setConstraints(constraints);
85         assertEquals(constraints, tp.getConstraints());
86
87         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
88         JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
89         tp.setEntrySchema(tes);
90
91         JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
92         assertEquals(tp, tdtClone0);
93         assertEquals(0, tp.compareTo(tdtClone0));
94
95         JpaToscaProperty tdtClone1 = new JpaToscaProperty(tp);
96         assertEquals(tp, tdtClone1);
97         assertEquals(0, tp.compareTo(tdtClone1));
98
99         assertEquals(-1, tp.compareTo(null));
100         assertEquals(0, tp.compareTo(tp));
101         assertFalse(tp.compareTo(tp.getKey()) == 0);
102
103         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
104         JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
105
106         assertFalse(tp.compareTo(otherDt) == 0);
107         otherDt.setKey(pkey);
108         assertFalse(tp.compareTo(otherDt) == 0);
109         otherDt.setType(ptypeKey);
110         assertFalse(tp.compareTo(otherDt) == 0);
111         otherDt.setDescription(A_DESCRIPTION);
112         assertFalse(tp.compareTo(otherDt) == 0);
113         otherDt.setRequired(false);
114         assertFalse(tp.compareTo(otherDt) == 0);
115         otherDt.setDefaultValue(DEFAULT_KEY);
116         assertFalse(tp.compareTo(otherDt) == 0);
117         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
118         assertFalse(tp.compareTo(otherDt) == 0);
119         assertFalse(tp.compareTo(otherDt) == 0);
120         otherDt.setConstraints(constraints);
121         assertFalse(tp.compareTo(otherDt) == 0);
122         otherDt.setEntrySchema(tes);
123         assertEquals(0, tp.compareTo(otherDt));
124
125         otherDt.setRequired(true);
126         assertFalse(tp.compareTo(otherDt) == 0);
127         otherDt.setRequired(false);
128         assertEquals(0, tp.compareTo(otherDt));
129
130         otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
131         assertFalse(tp.compareTo(otherDt) == 0);
132         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
133         assertEquals(0, tp.compareTo(otherDt));
134
135         assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
136                         .isInstanceOf(NullPointerException.class);
137
138         assertEquals(3, tp.getKeys().size());
139         assertEquals(2, new JpaToscaProperty().getKeys().size());
140
141         new JpaToscaProperty().clean();
142         tp.clean();
143         assertEquals(tdtClone0, tp);
144
145         assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
146         assertTrue(tp.validate(new PfValidationResult()).isValid());
147
148         tp.setDescription(null);
149         assertTrue(tp.validate(new PfValidationResult()).isValid());
150         tp.setDescription("");
151         assertFalse(tp.validate(new PfValidationResult()).isValid());
152         tp.setDescription(A_DESCRIPTION);
153         assertTrue(tp.validate(new PfValidationResult()).isValid());
154
155         tp.setType(null);
156         assertFalse(tp.validate(new PfValidationResult()).isValid());
157         tp.setType(typeKey);
158         assertTrue(tp.validate(new PfValidationResult()).isValid());
159
160         tp.setType(PfConceptKey.getNullKey());
161         assertFalse(tp.validate(new PfValidationResult()).isValid());
162         tp.setType(typeKey);
163         assertTrue(tp.validate(new PfValidationResult()).isValid());
164
165         tp.setDefaultValue(null);
166         assertTrue(tp.validate(new PfValidationResult()).isValid());
167         tp.setDefaultValue("");
168         assertFalse(tp.validate(new PfValidationResult()).isValid());
169         tp.setDefaultValue(DEFAULT_KEY);
170         assertTrue(tp.validate(new PfValidationResult()).isValid());
171
172         tp.getConstraints().add(null);
173         assertFalse(tp.validate(new PfValidationResult()).isValid());
174         tp.getConstraints().remove(null);
175         assertTrue(tp.validate(new PfValidationResult()).isValid());
176
177         assertThatThrownBy(() -> tp.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
178     }
179 }