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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
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;
31 import java.util.TreeMap;
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;
39 * DAO test for ToscaDatatype.
41 * @author Liam Fallon (liam.fallon@est.tech)
43 public class JpaToscaServiceTemplateTest {
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";
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()));
55 assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
57 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
59 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
61 assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
62 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
64 assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
65 .isInstanceOf(NullPointerException.class);
67 PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
68 JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
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());
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());
88 PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
89 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
90 tst.setTopologyTemplate(ttt);
91 assertEquals(ttt, tst.getTopologyTemplate());
93 JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
94 assertEquals(tst, tttClone0);
95 assertEquals(0, tst.compareTo(tttClone0));
97 JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
98 assertEquals(tst, tttClone1);
99 assertEquals(0, tst.compareTo(tttClone1));
101 assertEquals(-1, tst.compareTo(null));
102 assertEquals(0, tst.compareTo(tst));
103 assertFalse(tst.compareTo(tst.getKey()) == 0);
105 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
106 JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
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));
120 assertEquals(6, tst.getKeys().size());
121 assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
123 new JpaToscaServiceTemplate().clean();
125 assertEquals(tttClone0, tst);
127 assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
128 assertTrue(tst.validate(new PfValidationResult()).isValid());
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());
137 assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");