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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
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.simple.concepts.JpaToscaConstraint;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
43 * DAO test for ToscaDatatype.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class JpaToscaDataTypeTest {
50 public void testDataTypePojo() {
51 assertNotNull(new JpaToscaDataType());
52 assertNotNull(new JpaToscaDataType(new PfConceptKey()));
53 assertNotNull(new JpaToscaDataType(new JpaToscaDataType()));
56 new JpaToscaDataType((PfConceptKey) null);
57 fail("test should throw an exception");
58 } catch (Exception exc) {
59 assertEquals("key is marked @NonNull but is null", exc.getMessage());
63 new JpaToscaDataType((JpaToscaDataType) null);
64 fail("test should throw an exception");
65 } catch (Exception exc) {
66 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
69 PfConceptKey dtKey = new PfConceptKey("tdt", "0.0.1");
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<>();
79 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", "0.0.1"));
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();
89 tdt.copyTo(tdtClone1);
90 assertEquals(tdt, tdtClone1);
91 assertEquals(0, tdt.compareTo(tdtClone1));
93 assertEquals(-1, tdt.compareTo(null));
94 assertEquals(0, tdt.compareTo(tdt));
95 assertFalse(tdt.compareTo(tdt.getKey()) == 0);
97 PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
98 JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
100 assertFalse(tdt.compareTo(otherDt) == 0);
101 otherDt.setKey(dtKey);
102 assertFalse(tdt.compareTo(otherDt) == 0);
103 otherDt.setConstraints(constraints);
104 assertFalse(tdt.compareTo(otherDt) == 0);
105 otherDt.setProperties(properties);
106 assertEquals(0, tdt.compareTo(otherDt));
110 fail("test should throw an exception");
111 } catch (Exception exc) {
112 assertEquals("target is marked @NonNull but is null", exc.getMessage());
115 assertEquals(3, tdt.getKeys().size());
116 assertEquals(1, new JpaToscaDataType().getKeys().size());
118 new JpaToscaDataType().clean();
120 assertEquals(tdtClone0, tdt);
122 assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
123 assertTrue(tdt.validate(new PfValidationResult()).isValid());
125 tdt.getConstraints().add(null);
126 assertFalse(tdt.validate(new PfValidationResult()).isValid());
127 tdt.getConstraints().remove(null);
128 assertTrue(tdt.validate(new PfValidationResult()).isValid());
130 tdt.getProperties().put(null, null);
131 assertFalse(tdt.validate(new PfValidationResult()).isValid());
132 tdt.getProperties().remove(null);
133 assertTrue(tdt.validate(new PfValidationResult()).isValid());
137 fail("test should throw an exception");
138 } catch (Exception exc) {
139 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());