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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
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;
29 import java.util.ArrayList;
30 import java.util.LinkedHashMap;
31 import java.util.List;
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;
45 * DAO test for ToscaDatatype.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class JpaToscaDataTypeTest {
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()));
58 assertThatThrownBy(() -> {
59 new JpaToscaDataType((PfConceptKey) null);
60 }).hasMessage("key is marked @NonNull but is null");
62 assertThatThrownBy(() -> {
63 new JpaToscaDataType((JpaToscaDataType) null);
64 }).hasMessage("copyConcept is marked @NonNull but is null");
66 PfConceptKey dtKey = new PfConceptKey("tdt", "0.0.1");
67 JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
69 List<JpaToscaConstraint> constraints = new ArrayList<>();
70 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
72 tdt.setConstraints(constraints);
73 assertEquals(constraints, tdt.getConstraints());
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());
81 JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
82 assertEquals(tdt, tdtClone0);
83 assertEquals(0, tdt.compareTo(tdtClone0));
85 JpaToscaDataType tdtClone1 = new JpaToscaDataType();
86 tdt.copyTo(tdtClone1);
87 assertEquals(tdt, tdtClone1);
88 assertEquals(0, tdt.compareTo(tdtClone1));
90 assertEquals(-1, tdt.compareTo(null));
91 assertEquals(0, tdt.compareTo(tdt));
92 assertFalse(tdt.compareTo(tdt.getKey()) == 0);
94 PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
95 JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
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));
105 assertThatThrownBy(() -> {
107 }).hasMessage("target is marked @NonNull but is null");
109 assertEquals(3, tdt.getKeys().size());
110 assertEquals(1, new JpaToscaDataType().getKeys().size());
112 new JpaToscaDataType().clean();
114 assertEquals(tdtClone0, tdt);
116 assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
117 assertTrue(tdt.validate(new PfValidationResult()).isValid());
119 tdt.getConstraints().add(null);
120 assertFalse(tdt.validate(new PfValidationResult()).isValid());
121 tdt.getConstraints().remove(null);
122 assertTrue(tdt.validate(new PfValidationResult()).isValid());
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());
129 assertThatThrownBy(() -> {
131 }).hasMessage("resultIn is marked @NonNull but is null");
133 ToscaDataType dat = new ToscaDataType();
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);
141 JpaToscaDataType tdta = new JpaToscaDataType();
142 tdta.fromAuthorative(dat);
143 assertEquals("name", tdta.getKey().getName());
145 ToscaDataType datOut = tdta.toAuthorative();
146 assertNotNull(datOut);