3e856911735e6f0dcdcac0a705bdba23d793679d
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaModelTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
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;
29
30 import java.util.Map;
31 import java.util.TreeMap;
32
33 import org.junit.Test;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfModelService;
36 import org.onap.policy.models.base.PfValidationResult;
37
38 /**
39  * DAO test for ToscaDatatype.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 public class JpaToscaModelTest {
44
45     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
46     private static final String VERSION_001 = "0.0.1";
47
48     @Test
49     public void testModelPojo() {
50         assertNotNull(new JpaToscaModel());
51         assertNotNull(new JpaToscaModel(new PfConceptKey()));
52         assertNotNull(new JpaToscaModel(new PfConceptKey(), new JpaToscaServiceTemplates()));
53         assertNotNull(new JpaToscaModel(new JpaToscaModel()));
54
55         assertThatThrownBy(() -> new JpaToscaModel((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
56
57         assertThatThrownBy(() -> new JpaToscaModel(null, null)).hasMessageMatching(KEY_IS_NULL);
58
59         assertThatThrownBy(() -> new JpaToscaModel(null, new JpaToscaServiceTemplates()))
60                 .hasMessageMatching(KEY_IS_NULL);
61
62         assertThatThrownBy(() -> new JpaToscaModel(new PfConceptKey(), null))
63                 .hasMessageMatching("serviceTemplates is marked .*on.*ull but is null");
64
65         assertThatThrownBy(() -> new JpaToscaModel((JpaToscaModel) null))
66                 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
67
68         PfConceptKey tstsKey = new PfConceptKey("tsts", VERSION_001);
69         Map<PfConceptKey, JpaToscaServiceTemplate> tstMap = new TreeMap<>();
70         JpaToscaServiceTemplates tsts = new JpaToscaServiceTemplates(tstsKey, tstMap);
71         PfConceptKey tmKey = new PfConceptKey("tst", VERSION_001);
72         JpaToscaModel tm = new JpaToscaModel(tmKey, tsts);
73
74         JpaToscaModel tttClone0 = new JpaToscaModel(tm);
75         assertEquals(tm, tttClone0);
76         assertEquals(0, tm.compareTo(tttClone0));
77
78         JpaToscaModel tttClone1 = new JpaToscaModel(tm);
79         assertEquals(tm, tttClone1);
80         assertEquals(0, tm.compareTo(tttClone1));
81
82         assertEquals(-1, tm.compareTo(null));
83         assertEquals(0, tm.compareTo(tm));
84         assertFalse(tm.compareTo(tm.getKey()) == 0);
85
86         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
87         JpaToscaModel otherDt = new JpaToscaModel(otherDtKey);
88
89         assertFalse(tm.compareTo(otherDt) == 0);
90         otherDt.setKey(tmKey);
91         assertFalse(tm.compareTo(otherDt) == 0);
92         otherDt.setServiceTemplates(tsts);
93         assertEquals(0, tm.compareTo(otherDt));
94
95         assertThatThrownBy(() -> new JpaToscaModel((JpaToscaModel) null)).isInstanceOf(NullPointerException.class);
96
97         assertEquals(2, tm.getKeys().size());
98         assertEquals(2, new JpaToscaModel().getKeys().size());
99
100         new JpaToscaModel().clean();
101         tm.clean();
102         assertEquals(tttClone0, tm);
103
104         assertFalse(new JpaToscaModel().validate(new PfValidationResult()).isValid());
105         assertTrue(tm.validate(new PfValidationResult()).isValid());
106
107         tm.register();
108         assertTrue(PfModelService.existsModel(tm.getServiceTemplates().getId()));
109         PfModelService.deregisterModel(tm.getServiceTemplates().getId());
110
111         assertThatThrownBy(() -> tm.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
112     }
113 }