aad410e89e9e4247bf39907305567cc0635c6465
[policy/models.git] /
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.ToscaDataType;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaDataTypes;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType;
39 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes;
40 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
41 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
42
43 /**
44  * DAO test for ToscaDatatype.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class ToscaServiceTemplateTest {
49
50     @Test
51     public void testServiceTemplatePojo() {
52         assertNotNull(new ToscaServiceTemplate());
53         assertNotNull(new ToscaServiceTemplate(new PfConceptKey()));
54         assertNotNull(new ToscaServiceTemplate(new PfConceptKey(), ""));
55         assertNotNull(new ToscaServiceTemplate(new ToscaServiceTemplate()));
56
57         try {
58             new ToscaServiceTemplate((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 ToscaServiceTemplate(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 ToscaServiceTemplate(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 ToscaServiceTemplate(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 ToscaServiceTemplate((ToscaServiceTemplate) 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         ToscaServiceTemplate tst = new ToscaServiceTemplate(tstKey, "Tosca Version");
94
95         PfConceptKey dataTypeKey = new PfConceptKey("DataType", "0.0.1");
96         ToscaDataType dataType0 = new ToscaDataType(dataTypeKey);
97         PfConceptKey dtsKey = new PfConceptKey("dts", "0.0.1");
98         Map<PfConceptKey, ToscaDataType> dataTypeMap = new TreeMap<>();
99         dataTypeMap.put(dataTypeKey, dataType0);
100         ToscaDataTypes dataTypes = new ToscaDataTypes(dtsKey, dataTypeMap);
101         tst.setDataTypes(dataTypes);
102         assertEquals(dataTypes, tst.getDataTypes());
103
104         PfConceptKey policyTypeKey = new PfConceptKey("DataType", "0.0.1");
105         ToscaPolicyType policyType0 = new ToscaPolicyType(policyTypeKey);
106         PfConceptKey ptsKey = new PfConceptKey("dts", "0.0.1");
107         Map<PfConceptKey, ToscaPolicyType> policyTypeMap = new TreeMap<>();
108         policyTypeMap.put(policyTypeKey, policyType0);
109         ToscaPolicyTypes policyTypes = new ToscaPolicyTypes(ptsKey, policyTypeMap);
110         tst.setPolicyTypes(policyTypes);
111         assertEquals(policyTypes, tst.getPolicyTypes());
112
113         PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
114         ToscaTopologyTemplate ttt = new ToscaTopologyTemplate(tttKey);
115         tst.setTopologyTemplate(ttt);
116         assertEquals(ttt, tst.getTopologyTemplate());
117
118         ToscaServiceTemplate tttClone0 = new ToscaServiceTemplate(tst);
119         assertEquals(tst, tttClone0);
120         assertEquals(0, tst.compareTo(tttClone0));
121
122         ToscaServiceTemplate tttClone1 = new ToscaServiceTemplate();
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         ToscaServiceTemplate otherDt = new ToscaServiceTemplate(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 ToscaServiceTemplate().getKeys().size());
155
156         new ToscaServiceTemplate().clean();
157         tst.clean();
158         assertEquals(tttClone0, tst);
159
160         assertFalse(new ToscaServiceTemplate().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 }