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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
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;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
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;
46 * DAO test for ToscaDatatype.
48 * @author Liam Fallon (liam.fallon@est.tech)
50 public class JpaToscaDataTypeTest {
52 private static final String VERSION_001 = "0.0.1";
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()));
61 assertThatThrownBy(() -> {
62 new JpaToscaDataType((PfConceptKey) null);
63 }).hasMessage("key is marked @NonNull but is null");
65 assertThatThrownBy(() -> {
66 new JpaToscaDataType((JpaToscaDataType) null);
67 }).hasMessage("copyConcept is marked @NonNull but is null");
69 PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001);
70 JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
72 List<JpaToscaConstraint> constraints = new ArrayList<>();
73 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
75 tdt.setConstraints(constraints);
76 assertEquals(constraints, tdt.getConstraints());
78 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
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());
85 JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
86 assertEquals(tdt, tdtClone0);
87 assertEquals(0, tdt.compareTo(tdtClone0));
89 JpaToscaDataType tdtClone1 = new JpaToscaDataType();
90 tdt.copyTo(tdtClone1);
91 assertEquals(tdt, tdtClone1);
92 assertEquals(0, tdt.compareTo(tdtClone1));
94 assertEquals(-1, tdt.compareTo(null));
95 assertEquals(0, tdt.compareTo(tdt));
96 assertFalse(tdt.compareTo(tdt.getKey()) == 0);
98 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
99 JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
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));
109 assertThatThrownBy(() -> {
111 }).hasMessage("target is marked @NonNull but is null");
113 assertEquals(3, tdt.getKeys().size());
114 assertEquals(1, new JpaToscaDataType().getKeys().size());
116 new JpaToscaDataType().clean();
118 assertEquals(tdtClone0, tdt);
120 assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
121 assertTrue(tdt.validate(new PfValidationResult()).isValid());
123 tdt.getConstraints().add(null);
124 assertFalse(tdt.validate(new PfValidationResult()).isValid());
125 tdt.getConstraints().remove(null);
126 assertTrue(tdt.validate(new PfValidationResult()).isValid());
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());
133 assertThatThrownBy(() -> {
135 }).hasMessage("resultIn is marked @NonNull but is null");
137 ToscaDataType dat = new ToscaDataType();
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);
145 JpaToscaDataType tdta = new JpaToscaDataType();
146 tdta.fromAuthorative(dat);
147 assertEquals("name", tdta.getKey().getName());
149 ToscaDataType datOut = tdta.toAuthorative();
150 assertNotNull(datOut);