Finish unit test on policy-models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataTypeTest.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.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.ArrayList;
30 import java.util.LinkedHashMap;
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.authorative.concepts.ToscaConstraint;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
43
44 /**
45  * DAO test for ToscaDatatype.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class JpaToscaDataTypeTest {
50
51     @Test
52     public void testDataTypePojo() {
53         assertNotNull(new JpaToscaDataType());
54         assertNotNull(new JpaToscaDataType(new PfConceptKey()));
55         assertNotNull(new JpaToscaDataType(new JpaToscaDataType()));
56         assertNotNull(new JpaToscaDataType(new ToscaDataType()));
57
58         assertThatThrownBy(() -> {
59             new JpaToscaDataType((PfConceptKey) null);
60         }).hasMessage("key is marked @NonNull but is null");
61
62         assertThatThrownBy(() -> {
63             new JpaToscaDataType((JpaToscaDataType) null);
64         }).hasMessage("copyConcept is marked @NonNull but is null");
65
66         PfConceptKey dtKey = new PfConceptKey("tdt", "0.0.1");
67         JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
68
69         List<JpaToscaConstraint> constraints = new ArrayList<>();
70         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
71         constraints.add(lsc);
72         tdt.setConstraints(constraints);
73         assertEquals(constraints, tdt.getConstraints());
74
75         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
76         JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", "0.0.1"));
77         properties.put(tp.getKey().getLocalName(), tp);
78         tdt.setProperties(properties);
79         assertEquals(properties, tdt.getProperties());
80
81         JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
82         assertEquals(tdt, tdtClone0);
83         assertEquals(0, tdt.compareTo(tdtClone0));
84
85         JpaToscaDataType tdtClone1 = new JpaToscaDataType();
86         tdt.copyTo(tdtClone1);
87         assertEquals(tdt, tdtClone1);
88         assertEquals(0, tdt.compareTo(tdtClone1));
89
90         assertEquals(-1, tdt.compareTo(null));
91         assertEquals(0, tdt.compareTo(tdt));
92         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
93
94         PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
95         JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
96
97         assertFalse(tdt.compareTo(otherDt) == 0);
98         otherDt.setKey(dtKey);
99         assertFalse(tdt.compareTo(otherDt) == 0);
100         otherDt.setConstraints(constraints);
101         assertFalse(tdt.compareTo(otherDt) == 0);
102         otherDt.setProperties(properties);
103         assertEquals(0, tdt.compareTo(otherDt));
104
105         assertThatThrownBy(() -> {
106             tdt.copyTo(null);
107         }).hasMessage("target is marked @NonNull but is null");
108
109         assertEquals(3, tdt.getKeys().size());
110         assertEquals(1, new JpaToscaDataType().getKeys().size());
111
112         new JpaToscaDataType().clean();
113         tdt.clean();
114         assertEquals(tdtClone0, tdt);
115
116         assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
117         assertTrue(tdt.validate(new PfValidationResult()).isValid());
118
119         tdt.getConstraints().add(null);
120         assertFalse(tdt.validate(new PfValidationResult()).isValid());
121         tdt.getConstraints().remove(null);
122         assertTrue(tdt.validate(new PfValidationResult()).isValid());
123
124         tdt.getProperties().put(null, null);
125         assertFalse(tdt.validate(new PfValidationResult()).isValid());
126         tdt.getProperties().remove(null);
127         assertTrue(tdt.validate(new PfValidationResult()).isValid());
128
129         assertThatThrownBy(() -> {
130             tdt.validate(null);
131         }).hasMessage("resultIn is marked @NonNull but is null");
132
133         ToscaDataType dat = new ToscaDataType();
134         dat.setName("name");
135         dat.setVersion("1.2.3");
136         dat.setConstraints(new ArrayList<>());
137         ToscaConstraint constraint = new ToscaConstraint();
138         constraint.setEqual("EqualTo");
139         dat.getConstraints().add(constraint);
140
141         JpaToscaDataType tdta = new JpaToscaDataType();
142         tdta.fromAuthorative(dat);
143         assertEquals("name", tdta.getKey().getName());
144
145         ToscaDataType datOut = tdta.toAuthorative();
146         assertNotNull(datOut);
147     }
148 }