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 .isInstanceOf(NullPointerException.class);
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(tst);
97 assertEquals(tst, tttClone1);
98 assertEquals(0, tst.compareTo(tttClone1));
100 assertEquals(-1, tst.compareTo(null));
101 assertEquals(0, tst.compareTo(tst));
102 assertFalse(tst.compareTo(tst.getKey()) == 0);
104 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
105 JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
107 assertFalse(tst.compareTo(otherDt) == 0);
108 otherDt.setKey(tstKey);
109 assertFalse(tst.compareTo(otherDt) == 0);
110 otherDt.setToscaDefinitionsVersion("Tosca Version");
111 assertFalse(tst.compareTo(otherDt) == 0);
112 otherDt.setDataTypes(dataTypes);
113 assertFalse(tst.compareTo(otherDt) == 0);
114 otherDt.setPolicyTypes(policyTypes);
115 assertFalse(tst.compareTo(otherDt) == 0);
116 otherDt.setTopologyTemplate(ttt);
117 assertEquals(0, tst.compareTo(otherDt));
119 assertEquals(6, tst.getKeys().size());
120 assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
122 new JpaToscaServiceTemplate().clean();
124 assertEquals(tttClone0, tst);
126 assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
127 assertTrue(tst.validate(new PfValidationResult()).isValid());
129 tst.setDescription(null);
130 assertTrue(tst.validate(new PfValidationResult()).isValid());
131 tst.setDescription("");
132 assertFalse(tst.validate(new PfValidationResult()).isValid());
133 tst.setDescription("A Description");
134 assertTrue(tst.validate(new PfValidationResult()).isValid());
136 assertThatThrownBy(() -> tst.validate(null)).hasMessage("resultIn is marked @NonNull but is null");