2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.base.PfValidationResult;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
40 * DAO test for ToscaDatatype.
42 * @author Liam Fallon (liam.fallon@est.tech)
44 public class JpaToscaTopologyTemplateTest {
46 private static final String A_DESCRIPTION = "A Description";
47 private static final String VERSION_001 = "0.0.1";
50 public void testTopologyTemplatePojo() {
51 assertNotNull(new JpaToscaTopologyTemplate());
52 assertNotNull(new JpaToscaTopologyTemplate(new PfReferenceKey()));
53 assertNotNull(new JpaToscaTopologyTemplate(new JpaToscaTopologyTemplate()));
54 assertNotNull(new JpaToscaTopologyTemplate(new ToscaTopologyTemplate()));
56 assertThatThrownBy(() -> new JpaToscaTopologyTemplate((PfReferenceKey) null))
57 .hasMessageMatching("key is marked .*on.*ull but is null");
59 assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
60 .isInstanceOf(NullPointerException.class);
62 PfReferenceKey tttKey = new PfReferenceKey("tst", VERSION_001, "ttt");
63 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
65 ttt.setDescription(A_DESCRIPTION);
66 assertEquals(A_DESCRIPTION, ttt.getDescription());
68 PfConceptKey policy0TypeKey = new PfConceptKey("Policy0Type", VERSION_001);
69 PfConceptKey policy0Key = new PfConceptKey("Policy0", VERSION_001);
71 JpaToscaPolicy policy0 = new JpaToscaPolicy(policy0Key, policy0TypeKey);
72 PfConceptKey polsKey = new PfConceptKey("pols", VERSION_001);
73 Map<PfConceptKey, JpaToscaPolicy> policyMap = new TreeMap<>();
74 policyMap.put(policy0Key, policy0);
75 JpaToscaPolicies policies = new JpaToscaPolicies(polsKey, policyMap);
76 ttt.setPolicies(policies);
78 JpaToscaTopologyTemplate tttClone0 = new JpaToscaTopologyTemplate(ttt);
79 assertEquals(ttt, tttClone0);
80 assertEquals(0, ttt.compareTo(tttClone0));
82 JpaToscaTopologyTemplate tttClone1 = new JpaToscaTopologyTemplate(ttt);
83 assertEquals(ttt, tttClone1);
84 assertEquals(0, ttt.compareTo(tttClone1));
86 assertEquals(-1, ttt.compareTo(null));
87 assertEquals(0, ttt.compareTo(ttt));
88 assertNotEquals(0, ttt.compareTo(ttt.getKey()));
90 PfReferenceKey otherDtKey = new PfReferenceKey("otherSt", VERSION_001, "otherDt");
91 JpaToscaTopologyTemplate otherDt = new JpaToscaTopologyTemplate(otherDtKey);
93 assertNotEquals(0, ttt.compareTo(otherDt));
94 otherDt.setKey(tttKey);
95 assertNotEquals(0, ttt.compareTo(otherDt));
96 otherDt.setDescription(A_DESCRIPTION);
97 assertNotEquals(0, ttt.compareTo(otherDt));
98 otherDt.setPolicies(policies);
99 assertEquals(0, ttt.compareTo(otherDt));
101 assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
102 .isInstanceOf(NullPointerException.class);
104 assertEquals(4, ttt.getKeys().size());
105 assertEquals(1, new JpaToscaTopologyTemplate().getKeys().size());
107 new JpaToscaTopologyTemplate().clean();
109 assertEquals(tttClone0, ttt);
111 assertTrue(new JpaToscaTopologyTemplate().validate(new PfValidationResult()).isValid());
112 assertTrue(ttt.validate(new PfValidationResult()).isValid());
114 ttt.setKey(PfReferenceKey.getNullKey());
115 assertFalse(ttt.validate(new PfValidationResult()).isValid());
117 assertTrue(ttt.validate(new PfValidationResult()).isValid());
119 ttt.setDescription(null);
120 assertTrue(ttt.validate(new PfValidationResult()).isValid());
121 ttt.setDescription("");
122 assertFalse(ttt.validate(new PfValidationResult()).isValid());
123 ttt.setDescription(A_DESCRIPTION);
124 assertTrue(ttt.validate(new PfValidationResult()).isValid());
126 assertThatThrownBy(() -> ttt.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");