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.List;
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfReferenceKey;
35 import org.onap.policy.models.base.PfValidationResult;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical.Operation;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogicalString;
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 JpaToscaConstraintLogicalString lsc =
74 new JpaToscaConstraintLogicalString(new PfReferenceKey(dtKey, "sc"), Operation.EQ, "hello");
76 tdt.setConstraints(constraints);
77 assertEquals(constraints, tdt.getConstraints());
79 List<JpaToscaProperty> properties = new ArrayList<>();
80 JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", "0.0.1"));
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", "0.0.1");
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));
111 fail("test should throw an exception");
112 } catch (Exception exc) {
113 assertEquals("target is marked @NonNull but is null", exc.getMessage());
116 assertEquals(4, tdt.getKeys().size());
117 assertEquals(1, new JpaToscaDataType().getKeys().size());
119 new JpaToscaDataType().clean();
121 assertEquals(tdtClone0, tdt);
123 assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
124 assertTrue(tdt.validate(new PfValidationResult()).isValid());
126 tdt.getConstraints().add(null);
127 assertFalse(tdt.validate(new PfValidationResult()).isValid());
128 tdt.getConstraints().remove(null);
129 assertTrue(tdt.validate(new PfValidationResult()).isValid());
131 tdt.getProperties().add(null);
132 assertFalse(tdt.validate(new PfValidationResult()).isValid());
133 tdt.getProperties().remove(null);
134 assertTrue(tdt.validate(new PfValidationResult()).isValid());
138 fail("test should throw an exception");
139 } catch (Exception exc) {
140 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());