Refactor to authorative TOSCA serializtion
[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.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33
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;
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         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
74         constraints.add(lsc);
75         tdt.setConstraints(constraints);
76         assertEquals(constraints, tdt.getConstraints());
77
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());
83
84         JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
85         assertEquals(tdt, tdtClone0);
86         assertEquals(0, tdt.compareTo(tdtClone0));
87
88         JpaToscaDataType tdtClone1 = new JpaToscaDataType();
89         tdt.copyTo(tdtClone1);
90         assertEquals(tdt, tdtClone1);
91         assertEquals(0, tdt.compareTo(tdtClone1));
92
93         assertEquals(-1, tdt.compareTo(null));
94         assertEquals(0, tdt.compareTo(tdt));
95         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
96
97         PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
98         JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
99
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));
107
108         try {
109             tdt.copyTo(null);
110             fail("test should throw an exception");
111         } catch (Exception exc) {
112             assertEquals("target is marked @NonNull but is null", exc.getMessage());
113         }
114
115         assertEquals(3, tdt.getKeys().size());
116         assertEquals(1, new JpaToscaDataType().getKeys().size());
117
118         new JpaToscaDataType().clean();
119         tdt.clean();
120         assertEquals(tdtClone0, tdt);
121
122         assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
123         assertTrue(tdt.validate(new PfValidationResult()).isValid());
124
125         tdt.getConstraints().add(null);
126         assertFalse(tdt.validate(new PfValidationResult()).isValid());
127         tdt.getConstraints().remove(null);
128         assertTrue(tdt.validate(new PfValidationResult()).isValid());
129
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());
134
135         try {
136             tdt.validate(null);
137             fail("test should throw an exception");
138         } catch (Exception exc) {
139             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
140         }
141
142     }
143 }