Merge "Restructure for authorative models"
[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 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.Map;
30 import java.util.TreeMap;
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.JpaToscaDataType;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
42
43 /**
44  * DAO test for ToscaDatatype.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class JpaToscaServiceTemplateTest {
49
50     @Test
51     public void testServiceTemplatePojo() {
52         assertNotNull(new JpaToscaServiceTemplate());
53         assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey()));
54         assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey(), ""));
55         assertNotNull(new JpaToscaServiceTemplate(new JpaToscaServiceTemplate()));
56
57         try {
58             new JpaToscaServiceTemplate((PfConceptKey) null);
59             fail("test should throw an exception");
60         } catch (Exception exc) {
61             assertEquals("key is marked @NonNull but is null", exc.getMessage());
62         }
63
64         try {
65             new JpaToscaServiceTemplate(null, null);
66             fail("test should throw an exception");
67         } catch (Exception exc) {
68             assertEquals("key is marked @NonNull but is null", exc.getMessage());
69         }
70
71         try {
72             new JpaToscaServiceTemplate(null, "");
73             fail("test should throw an exception");
74         } catch (Exception exc) {
75             assertEquals("key is marked @NonNull but is null", exc.getMessage());
76         }
77
78         try {
79             new JpaToscaServiceTemplate(new PfConceptKey(), null);
80             fail("test should throw an exception");
81         } catch (Exception exc) {
82             assertEquals("toscaDefinitionsVersion is marked @NonNull but is null", exc.getMessage());
83         }
84
85         try {
86             new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null);
87             fail("test should throw an exception");
88         } catch (Exception exc) {
89             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
90         }
91
92         PfConceptKey tstKey = new PfConceptKey("tst", "0.0.1");
93         JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
94
95         PfConceptKey dataTypeKey = new PfConceptKey("DataType", "0.0.1");
96         JpaToscaDataType dataType0 = new JpaToscaDataType(dataTypeKey);
97         PfConceptKey dtsKey = new PfConceptKey("dts", "0.0.1");
98         Map<PfConceptKey, JpaToscaDataType> dataTypeMap = new TreeMap<>();
99         dataTypeMap.put(dataTypeKey, dataType0);
100         JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dtsKey, dataTypeMap);
101         tst.setDataTypes(dataTypes);
102         assertEquals(dataTypes, tst.getDataTypes());
103
104         PfConceptKey policyTypeKey = new PfConceptKey("DataType", "0.0.1");
105         JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(policyTypeKey);
106         PfConceptKey ptsKey = new PfConceptKey("dts", "0.0.1");
107         Map<PfConceptKey, JpaToscaPolicyType> policyTypeMap = new TreeMap<>();
108         policyTypeMap.put(policyTypeKey, policyType0);
109         JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(ptsKey, policyTypeMap);
110         tst.setPolicyTypes(policyTypes);
111         assertEquals(policyTypes, tst.getPolicyTypes());
112
113         PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
114         JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
115         tst.setTopologyTemplate(ttt);
116         assertEquals(ttt, tst.getTopologyTemplate());
117
118         JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
119         assertEquals(tst, tttClone0);
120         assertEquals(0, tst.compareTo(tttClone0));
121
122         JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate();
123         tst.copyTo(tttClone1);
124         assertEquals(tst, tttClone1);
125         assertEquals(0, tst.compareTo(tttClone1));
126
127         assertEquals(-1, tst.compareTo(null));
128         assertEquals(0, tst.compareTo(tst));
129         assertFalse(tst.compareTo(tst.getKey()) == 0);
130
131         PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
132         JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
133
134         assertFalse(tst.compareTo(otherDt) == 0);
135         otherDt.setKey(tstKey);
136         assertFalse(tst.compareTo(otherDt) == 0);
137         otherDt.setToscaDefinitionsVersion("Tosca Version");
138         assertFalse(tst.compareTo(otherDt) == 0);
139         otherDt.setDataTypes(dataTypes);
140         assertFalse(tst.compareTo(otherDt) == 0);
141         otherDt.setPolicyTypes(policyTypes);
142         assertFalse(tst.compareTo(otherDt) == 0);
143         otherDt.setTopologyTemplate(ttt);
144         assertEquals(0, tst.compareTo(otherDt));
145
146         try {
147             tst.copyTo(null);
148             fail("test should throw an exception");
149         } catch (Exception exc) {
150             assertEquals("target is marked @NonNull but is null", exc.getMessage());
151         }
152
153         assertEquals(6, tst.getKeys().size());
154         assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
155
156         new JpaToscaServiceTemplate().clean();
157         tst.clean();
158         assertEquals(tttClone0, tst);
159
160         assertFalse(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
161         assertTrue(tst.validate(new PfValidationResult()).isValid());
162
163         tst.setDescription(null);
164         assertTrue(tst.validate(new PfValidationResult()).isValid());
165         tst.setDescription("");
166         assertFalse(tst.validate(new PfValidationResult()).isValid());
167         tst.setDescription("A Description");
168         assertTrue(tst.validate(new PfValidationResult()).isValid());
169
170         try {
171             tst.validate(null);
172             fail("test should throw an exception");
173         } catch (Exception exc) {
174             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
175         }
176     }
177 }