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