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(() -> new JpaToscaDataType((JpaToscaDataType) null))
66 .isInstanceOf(NullPointerException.class);
68 PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001);
69 JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
71 List<JpaToscaConstraint> constraints = new ArrayList<>();
72 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
74 tdt.setConstraints(constraints);
75 assertEquals(constraints, tdt.getConstraints());
77 Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
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());
84 JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
85 assertEquals(tdt, tdtClone0);
86 assertEquals(0, tdt.compareTo(tdtClone0));
88 JpaToscaDataType tdtClone1 = new JpaToscaDataType(tdt);
89 assertEquals(tdt, tdtClone1);
90 assertEquals(0, tdt.compareTo(tdtClone1));
92 assertEquals(-1, tdt.compareTo(null));
93 assertEquals(0, tdt.compareTo(tdt));
94 assertFalse(tdt.compareTo(tdt.getKey()) == 0);
96 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
97 JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
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));
107 assertEquals(3, tdt.getKeys().size());
108 assertEquals(1, new JpaToscaDataType().getKeys().size());
110 new JpaToscaDataType().clean();
112 assertEquals(tdtClone0, tdt);
114 assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
115 assertTrue(tdt.validate(new PfValidationResult()).isValid());
117 tdt.getConstraints().add(null);
118 assertFalse(tdt.validate(new PfValidationResult()).isValid());
119 tdt.getConstraints().remove(null);
120 assertTrue(tdt.validate(new PfValidationResult()).isValid());
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());
127 assertThatThrownBy(() -> {
129 }).hasMessage("resultIn is marked @NonNull but is null");
131 ToscaDataType dat = new ToscaDataType();
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);
139 JpaToscaDataType tdta = new JpaToscaDataType();
140 tdta.fromAuthorative(dat);
141 assertEquals("name", tdta.getKey().getName());
143 ToscaDataType datOut = tdta.toAuthorative();
144 assertNotNull(datOut);