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.PfModelService;
38 * DAO test for ToscaDatatype.
40 * @author Liam Fallon (liam.fallon@est.tech)
42 public class JpaToscaModelTest {
44 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
45 private static final String VERSION_001 = "0.0.1";
48 public void testModelPojo() {
49 assertNotNull(new JpaToscaModel());
50 assertNotNull(new JpaToscaModel(new PfConceptKey()));
51 assertNotNull(new JpaToscaModel(new PfConceptKey(), new JpaToscaServiceTemplates()));
52 assertNotNull(new JpaToscaModel(new JpaToscaModel()));
54 assertThatThrownBy(() -> new JpaToscaModel((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
56 assertThatThrownBy(() -> new JpaToscaModel(null, null)).hasMessageMatching(KEY_IS_NULL);
58 assertThatThrownBy(() -> new JpaToscaModel(null, new JpaToscaServiceTemplates()))
59 .hasMessageMatching(KEY_IS_NULL);
61 assertThatThrownBy(() -> new JpaToscaModel(new PfConceptKey(), null))
62 .hasMessageMatching("serviceTemplates is marked .*on.*ull but is null");
64 assertThatThrownBy(() -> new JpaToscaModel((JpaToscaModel) null))
65 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
69 public void testJpaToscaModel() {
70 PfConceptKey tstsKey = new PfConceptKey("tsts", VERSION_001);
71 Map<PfConceptKey, JpaToscaServiceTemplate> tstMap = new TreeMap<>();
72 JpaToscaServiceTemplates tsts = new JpaToscaServiceTemplates(tstsKey, tstMap);
73 PfConceptKey tmKey = new PfConceptKey("tst", VERSION_001);
74 JpaToscaModel tm = new JpaToscaModel(tmKey, tsts);
76 JpaToscaModel tttClone0 = new JpaToscaModel(tm);
77 assertEquals(tm, tttClone0);
78 assertEquals(0, tm.compareTo(tttClone0));
80 JpaToscaModel tttClone1 = new JpaToscaModel(tm);
81 assertEquals(tm, tttClone1);
82 assertEquals(0, tm.compareTo(tttClone1));
84 assertEquals(-1, tm.compareTo(null));
85 assertEquals(0, tm.compareTo(tm));
86 assertNotEquals(0, tm.compareTo(tm.getKey()));
88 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
89 JpaToscaModel otherDt = new JpaToscaModel(otherDtKey);
91 assertNotEquals(0, tm.compareTo(otherDt));
92 otherDt.setKey(tmKey);
93 assertNotEquals(0, tm.compareTo(otherDt));
94 otherDt.setServiceTemplates(tsts);
95 assertEquals(0, tm.compareTo(otherDt));
97 assertThatThrownBy(() -> new JpaToscaModel((JpaToscaModel) null)).isInstanceOf(NullPointerException.class);
99 assertEquals(2, tm.getKeys().size());
100 assertEquals(2, new JpaToscaModel().getKeys().size());
102 new JpaToscaModel().clean();
104 assertEquals(tttClone0, tm);
106 assertFalse(new JpaToscaModel().validate("").isValid());
107 assertTrue(tm.validate("").isValid());
110 assertTrue(PfModelService.existsModel(tm.getServiceTemplates().getId()));
111 PfModelService.deregisterModel(tm.getServiceTemplates().getId());
113 assertThatThrownBy(() -> tm.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");