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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.concepts;
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;
30 import java.util.TreeMap;
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfValidationResult;
37 * DAO test for ToscaDatatype.
39 * @author Liam Fallon (liam.fallon@est.tech)
41 public class ToscaServiceTemplateTest {
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()));
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());
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());
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());
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());
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());
85 PfConceptKey tstKey = new PfConceptKey("tst", "0.0.1");
86 ToscaServiceTemplate tst = new ToscaServiceTemplate(tstKey, "Tosca Version");
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());
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());
106 PfConceptKey tttKey = new PfConceptKey("TopologyTemplate", "0.0.1");
107 ToscaTopologyTemplate ttt = new ToscaTopologyTemplate(tttKey);
108 tst.setTopologyTemplate(ttt);
109 assertEquals(ttt, tst.getTopologyTemplate());
111 ToscaServiceTemplate tttClone0 = new ToscaServiceTemplate(tst);
112 assertEquals(tst, tttClone0);
113 assertEquals(0, tst.compareTo(tttClone0));
115 ToscaServiceTemplate tttClone1 = new ToscaServiceTemplate();
116 tst.copyTo(tttClone1);
117 assertEquals(tst, tttClone1);
118 assertEquals(0, tst.compareTo(tttClone1));
120 assertEquals(-1, tst.compareTo(null));
121 assertEquals(0, tst.compareTo(tst));
122 assertFalse(tst.compareTo(tst.getKey()) == 0);
124 PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
125 ToscaServiceTemplate otherDt = new ToscaServiceTemplate(otherDtKey);
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));
141 fail("test should throw an exception");
142 } catch (Exception exc) {
143 assertEquals("target is marked @NonNull but is null", exc.getMessage());
146 assertEquals(6, tst.getKeys().size());
147 assertEquals(1, new ToscaServiceTemplate().getKeys().size());
149 new ToscaServiceTemplate().clean();
151 assertEquals(tttClone0, tst);
153 assertFalse(new ToscaServiceTemplate().validate(new PfValidationResult()).isValid());
154 assertTrue(tst.validate(new PfValidationResult()).isValid());
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());
165 fail("test should throw an exception");
166 } catch (Exception exc) {
167 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());