Merge "Fix more sonar issues in models: yaml to dao"
[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(() -> {
66             new JpaToscaDataType((JpaToscaDataType) null);
67         }).hasMessage("copyConcept is marked @NonNull but is null");
68
69         PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001);
70         JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
71
72         List<JpaToscaConstraint> constraints = new ArrayList<>();
73         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
74         constraints.add(lsc);
75         tdt.setConstraints(constraints);
76         assertEquals(constraints, tdt.getConstraints());
77
78         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
79         JpaToscaProperty tp =
80                         new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", VERSION_001));
81         properties.put(tp.getKey().getLocalName(), tp);
82         tdt.setProperties(properties);
83         assertEquals(properties, tdt.getProperties());
84
85         JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
86         assertEquals(tdt, tdtClone0);
87         assertEquals(0, tdt.compareTo(tdtClone0));
88
89         JpaToscaDataType tdtClone1 = new JpaToscaDataType();
90         tdt.copyTo(tdtClone1);
91         assertEquals(tdt, tdtClone1);
92         assertEquals(0, tdt.compareTo(tdtClone1));
93
94         assertEquals(-1, tdt.compareTo(null));
95         assertEquals(0, tdt.compareTo(tdt));
96         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
97
98         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
99         JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
100
101         assertFalse(tdt.compareTo(otherDt) == 0);
102         otherDt.setKey(dtKey);
103         assertFalse(tdt.compareTo(otherDt) == 0);
104         otherDt.setConstraints(constraints);
105         assertFalse(tdt.compareTo(otherDt) == 0);
106         otherDt.setProperties(properties);
107         assertEquals(0, tdt.compareTo(otherDt));
108
109         assertThatThrownBy(() -> {
110             tdt.copyTo(null);
111         }).hasMessage("target is marked @NonNull but is null");
112
113         assertEquals(3, tdt.getKeys().size());
114         assertEquals(1, new JpaToscaDataType().getKeys().size());
115
116         new JpaToscaDataType().clean();
117         tdt.clean();
118         assertEquals(tdtClone0, tdt);
119
120         assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
121         assertTrue(tdt.validate(new PfValidationResult()).isValid());
122
123         tdt.getConstraints().add(null);
124         assertFalse(tdt.validate(new PfValidationResult()).isValid());
125         tdt.getConstraints().remove(null);
126         assertTrue(tdt.validate(new PfValidationResult()).isValid());
127
128         tdt.getProperties().put(null, null);
129         assertFalse(tdt.validate(new PfValidationResult()).isValid());
130         tdt.getProperties().remove(null);
131         assertTrue(tdt.validate(new PfValidationResult()).isValid());
132
133         assertThatThrownBy(() -> {
134             tdt.validate(null);
135         }).hasMessage("resultIn is marked @NonNull but is null");
136
137         ToscaDataType dat = new ToscaDataType();
138         dat.setName("name");
139         dat.setVersion("1.2.3");
140         dat.setConstraints(new ArrayList<>());
141         ToscaConstraint constraint = new ToscaConstraint();
142         constraint.setEqual("EqualTo");
143         dat.getConstraints().add(constraint);
144
145         JpaToscaDataType tdta = new JpaToscaDataType();
146         tdta.fromAuthorative(dat);
147         assertEquals("name", tdta.getKey().getName());
148
149         ToscaDataType datOut = tdta.toAuthorative();
150         assertNotNull(datOut);
151     }
152 }