2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 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.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
32 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.tosca.authorative.concepts.ToscaTopologyTemplate;
39 * DAO test for ToscaDatatype.
41 * @author Liam Fallon (liam.fallon@est.tech)
43 public class JpaToscaTopologyTemplateTest {
45 private static final String A_DESCRIPTION = "A Description";
46 private static final String VERSION_001 = "0.0.1";
49 public void testTopologyTemplatePojo() {
50 assertNotNull(new JpaToscaTopologyTemplate());
51 assertNotNull(new JpaToscaTopologyTemplate(new PfReferenceKey()));
52 assertNotNull(new JpaToscaTopologyTemplate(new JpaToscaTopologyTemplate()));
53 assertNotNull(new JpaToscaTopologyTemplate(new ToscaTopologyTemplate()));
55 assertThatThrownBy(() -> new JpaToscaTopologyTemplate((PfReferenceKey) null))
56 .hasMessageMatching("key is marked .*on.*ull but is null");
58 assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
59 .isInstanceOf(NullPointerException.class);
61 assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
62 .isInstanceOf(NullPointerException.class);
66 public void testTopologyTemplates() {
67 PfReferenceKey tttKey = new PfReferenceKey("tst", VERSION_001, "ttt");
68 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
70 ttt.setDescription(A_DESCRIPTION);
71 assertEquals(A_DESCRIPTION, ttt.getDescription());
73 PfConceptKey policy0TypeKey = new PfConceptKey("Policy0Type", VERSION_001);
74 PfConceptKey policy0Key = new PfConceptKey("Policy0", VERSION_001);
76 JpaToscaPolicy policy0 = new JpaToscaPolicy(policy0Key, policy0TypeKey);
77 PfConceptKey polsKey = new PfConceptKey("pols", VERSION_001);
78 Map<PfConceptKey, JpaToscaPolicy> policyMap = new TreeMap<>();
79 policyMap.put(policy0Key, policy0);
80 JpaToscaPolicies policies = new JpaToscaPolicies(polsKey, policyMap);
81 ttt.setPolicies(policies);
83 JpaToscaTopologyTemplate tttClone0 = new JpaToscaTopologyTemplate(ttt);
84 checkEqualsTopologyTemplate(ttt, tttClone0);
86 JpaToscaTopologyTemplate tttClone1 = new JpaToscaTopologyTemplate(ttt);
87 checkEqualsTopologyTemplate(ttt, tttClone1);
89 assertEquals(-1, ttt.compareTo(null));
90 assertEquals(0, ttt.compareTo(ttt));
91 assertNotEquals(0, ttt.compareTo(ttt.getKey()));
93 PfReferenceKey otherDtKey = new PfReferenceKey("otherSt", VERSION_001, "otherDt");
94 JpaToscaTopologyTemplate otherDt = new JpaToscaTopologyTemplate(otherDtKey);
96 assertNotEquals(0, ttt.compareTo(otherDt));
97 otherDt.setKey(tttKey);
98 assertNotEquals(0, ttt.compareTo(otherDt));
99 otherDt.setDescription(A_DESCRIPTION);
100 assertNotEquals(0, ttt.compareTo(otherDt));
101 otherDt.setPolicies(policies);
102 assertEquals(0, ttt.compareTo(otherDt));
104 assertEquals(4, ttt.getKeys().size());
105 assertEquals(1, new JpaToscaTopologyTemplate().getKeys().size());
109 public void testTopologyTemplateValidation() {
110 PfReferenceKey tttKey = new PfReferenceKey("tst", VERSION_001, "ttt");
111 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
113 JpaToscaTopologyTemplate tttClone0 = new JpaToscaTopologyTemplate(ttt);
115 new JpaToscaTopologyTemplate().clean();
117 assertEquals(tttClone0, ttt);
119 assertTrue(new JpaToscaTopologyTemplate().validate("").isValid());
120 assertTrue(ttt.validate("").isValid());
122 ttt.setKey(PfReferenceKey.getNullKey());
123 assertFalse(ttt.validate("").isValid());
125 assertTrue(ttt.validate("").isValid());
127 ttt.setDescription(null);
128 assertTrue(ttt.validate("").isValid());
129 ttt.setDescription("");
130 assertFalse(ttt.validate("").isValid());
131 ttt.setDescription(A_DESCRIPTION);
132 assertTrue(ttt.validate("").isValid());
134 assertThatThrownBy(() -> ttt.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
137 private void checkEqualsTopologyTemplate(JpaToscaTopologyTemplate ttt1, JpaToscaTopologyTemplate ttt2) {
138 assertEquals(ttt1, ttt2);
139 assertEquals(0, ttt1.compareTo(ttt2));