Merge "Add copy constructors to more models-pdp classes"
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataTypeTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
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;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
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;
41
42 /**
43  * DAO test for ToscaDatatype.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaToscaDataTypeTest {
48
49     @Test
50     public void testDataTypePojo() {
51         assertNotNull(new JpaToscaDataType());
52         assertNotNull(new JpaToscaDataType(new PfConceptKey()));
53         assertNotNull(new JpaToscaDataType(new JpaToscaDataType()));
54
55         try {
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());
60         }
61
62         try {
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());
67         }
68
69         PfConceptKey dtKey = new PfConceptKey("tdt", "0.0.1");
70         JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
71
72         List<JpaToscaConstraint> constraints = new ArrayList<>();
73         JpaToscaConstraintLogicalString lsc =
74                 new JpaToscaConstraintLogicalString(new PfReferenceKey(dtKey, "sc"), Operation.EQ, "hello");
75         constraints.add(lsc);
76         tdt.setConstraints(constraints);
77         assertEquals(constraints, tdt.getConstraints());
78
79         List<JpaToscaProperty> properties = new ArrayList<>();
80         JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", "0.0.1"));
81         properties.add(tp);
82         tdt.setProperties(properties);
83         assertEquals(properties, tdt.getProperties());
84
85         JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
86         assertEquals(tdt, tdtClone0);
87         assertEquals(0, tdt.compareTo(tdtClone0));
88
89         JpaToscaDataType tdtClone1 = new JpaToscaDataType();
90         tdt.copyTo(tdtClone1);
91         assertEquals(tdt, tdtClone1);
92         assertEquals(0, tdt.compareTo(tdtClone1));
93
94         assertEquals(-1, tdt.compareTo(null));
95         assertEquals(0, tdt.compareTo(tdt));
96         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
97
98         PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
99         JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
100
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));
108
109         try {
110             tdt.copyTo(null);
111             fail("test should throw an exception");
112         } catch (Exception exc) {
113             assertEquals("target is marked @NonNull but is null", exc.getMessage());
114         }
115
116         assertEquals(4, tdt.getKeys().size());
117         assertEquals(1, new JpaToscaDataType().getKeys().size());
118
119         new JpaToscaDataType().clean();
120         tdt.clean();
121         assertEquals(tdtClone0, tdt);
122
123         assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
124         assertTrue(tdt.validate(new PfValidationResult()).isValid());
125
126         tdt.getConstraints().add(null);
127         assertFalse(tdt.validate(new PfValidationResult()).isValid());
128         tdt.getConstraints().remove(null);
129         assertTrue(tdt.validate(new PfValidationResult()).isValid());
130
131         tdt.getProperties().add(null);
132         assertFalse(tdt.validate(new PfValidationResult()).isValid());
133         tdt.getProperties().remove(null);
134         assertTrue(tdt.validate(new PfValidationResult()).isValid());
135
136         try {
137             tdt.validate(null);
138             fail("test should throw an exception");
139         } catch (Exception exc) {
140             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
141         }
142
143     }
144 }