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