2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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;
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;
38 * DAO test for ToscaDatatype.
40 * @author Liam Fallon (liam.fallon@est.tech)
42 public class JpaToscaServiceTemplateTest {
44 private static final String KEY_IS_NULL = "key is marked @NonNull but is null";
45 private static final String VERSION_001 = "0.0.1";
48 public void testServiceTemplatePojo() {
49 assertNotNull(new JpaToscaServiceTemplate());
50 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey()));
51 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey(), ""));
52 assertNotNull(new JpaToscaServiceTemplate(new JpaToscaServiceTemplate()));
54 assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessage(KEY_IS_NULL);
56 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessage(KEY_IS_NULL);
58 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessage(KEY_IS_NULL);
60 assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
61 .hasMessage("toscaDefinitionsVersion is marked @NonNull but is null");
63 assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
64 .hasMessage("copyConcept is marked @NonNull but is null");
66 PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
67 JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
69 PfConceptKey dataTypeKey = new PfConceptKey("DataType", VERSION_001);
70 JpaToscaDataType dataType0 = new JpaToscaDataType(dataTypeKey);
71 PfConceptKey dtsKey = new PfConceptKey("dts", VERSION_001);
72 Map<PfConceptKey, JpaToscaDataType> dataTypeMap = new TreeMap<>();
73 dataTypeMap.put(dataTypeKey, dataType0);
74 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dtsKey, dataTypeMap);
75 tst.setDataTypes(dataTypes);
76 assertEquals(dataTypes, tst.getDataTypes());
78 PfConceptKey policyTypeKey = new PfConceptKey("DataType", VERSION_001);
79 JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(policyTypeKey);
80 PfConceptKey ptsKey = new PfConceptKey("dts", VERSION_001);
81 Map<PfConceptKey, JpaToscaPolicyType> policyTypeMap = new TreeMap<>();
82 policyTypeMap.put(policyTypeKey, policyType0);
83 JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(ptsKey, policyTypeMap);
84 tst.setPolicyTypes(policyTypes);
85 assertEquals(policyTypes, tst.getPolicyTypes());
87 PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
88 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
89 tst.setTopologyTemplate(ttt);
90 assertEquals(ttt, tst.getTopologyTemplate());
92 JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
93 assertEquals(tst, tttClone0);
94 assertEquals(0, tst.compareTo(tttClone0));
96 JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate();
97 tst.copyTo(tttClone1);
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 assertThatThrownBy(() -> tst.copyTo(null)).hasMessage("target is marked @NonNull but is null");
122 assertEquals(6, tst.getKeys().size());
123 assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
125 new JpaToscaServiceTemplate().clean();
127 assertEquals(tttClone0, tst);
129 assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
130 assertTrue(tst.validate(new PfValidationResult()).isValid());
132 tst.setDescription(null);
133 assertTrue(tst.validate(new PfValidationResult()).isValid());
134 tst.setDescription("");
135 assertFalse(tst.validate(new PfValidationResult()).isValid());
136 tst.setDescription("A Description");
137 assertTrue(tst.validate(new PfValidationResult()).isValid());
139 assertThatThrownBy(() -> tst.validate(null)).hasMessage("resultIn is marked @NonNull but is null");