95c51e9125664e7703cc6524d6bb2ba975010115
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaServiceTemplateTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
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;
29
30 import java.util.Map;
31 import java.util.TreeMap;
32
33 import org.junit.Test;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfReferenceKey;
36 import org.onap.policy.models.base.PfValidationResult;
37
38 /**
39  * DAO test for ToscaDatatype.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 public class JpaToscaServiceTemplateTest {
44
45     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
46     private static final String VERSION_001 = "0.0.1";
47
48     @Test
49     public void testServiceTemplatePojo() {
50         assertNotNull(new JpaToscaServiceTemplate());
51         assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey()));
52         assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey(), ""));
53         assertNotNull(new JpaToscaServiceTemplate(new JpaToscaServiceTemplate()));
54
55         assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
56
57         assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
58
59         assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
60
61         assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
62                 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
63
64         assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
65                 .isInstanceOf(NullPointerException.class);
66
67         PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
68         JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
69
70         PfConceptKey dataTypeKey = new PfConceptKey("DataType", VERSION_001);
71         JpaToscaDataType dataType0 = new JpaToscaDataType(dataTypeKey);
72         PfConceptKey dtsKey = new PfConceptKey("dts", VERSION_001);
73         Map<PfConceptKey, JpaToscaDataType> dataTypeMap = new TreeMap<>();
74         dataTypeMap.put(dataTypeKey, dataType0);
75         JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dtsKey, dataTypeMap);
76         tst.setDataTypes(dataTypes);
77         assertEquals(dataTypes, tst.getDataTypes());
78
79         PfConceptKey policyTypeKey = new PfConceptKey("DataType", VERSION_001);
80         JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(policyTypeKey);
81         PfConceptKey ptsKey = new PfConceptKey("dts", VERSION_001);
82         Map<PfConceptKey, JpaToscaPolicyType> policyTypeMap = new TreeMap<>();
83         policyTypeMap.put(policyTypeKey, policyType0);
84         JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(ptsKey, policyTypeMap);
85         tst.setPolicyTypes(policyTypes);
86         assertEquals(policyTypes, tst.getPolicyTypes());
87
88         PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
89         JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
90         tst.setTopologyTemplate(ttt);
91         assertEquals(ttt, tst.getTopologyTemplate());
92
93         JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
94         assertEquals(tst, tttClone0);
95         assertEquals(0, tst.compareTo(tttClone0));
96
97         JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
98         assertEquals(tst, tttClone1);
99         assertEquals(0, tst.compareTo(tttClone1));
100
101         assertEquals(-1, tst.compareTo(null));
102         assertEquals(0, tst.compareTo(tst));
103         assertFalse(tst.compareTo(tst.getKey()) == 0);
104
105         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
106         JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
107
108         assertFalse(tst.compareTo(otherDt) == 0);
109         otherDt.setKey(tstKey);
110         assertFalse(tst.compareTo(otherDt) == 0);
111         otherDt.setToscaDefinitionsVersion("Tosca Version");
112         assertFalse(tst.compareTo(otherDt) == 0);
113         otherDt.setDataTypes(dataTypes);
114         assertFalse(tst.compareTo(otherDt) == 0);
115         otherDt.setPolicyTypes(policyTypes);
116         assertFalse(tst.compareTo(otherDt) == 0);
117         otherDt.setTopologyTemplate(ttt);
118         assertEquals(0, tst.compareTo(otherDt));
119
120         assertEquals(6, tst.getKeys().size());
121         assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
122
123         new JpaToscaServiceTemplate().clean();
124         tst.clean();
125         assertEquals(tttClone0, tst);
126
127         assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
128         assertTrue(tst.validate(new PfValidationResult()).isValid());
129
130         tst.setDescription(null);
131         assertTrue(tst.validate(new PfValidationResult()).isValid());
132         tst.setDescription("");
133         assertFalse(tst.validate(new PfValidationResult()).isValid());
134         tst.setDescription("A Description");
135         assertTrue(tst.validate(new PfValidationResult()).isValid());
136
137         assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
138     }
139 }