Add support for legacy guard policies
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / ToscaModelTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.util.Map;
30 import java.util.TreeMap;
31
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfModelService;
35 import org.onap.policy.models.base.PfValidationResult;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaModel;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplates;
39
40 /**
41  * DAO test for ToscaDatatype.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class ToscaModelTest {
46
47     @Test
48     public void testModelPojo() {
49         assertNotNull(new ToscaModel());
50         assertNotNull(new ToscaModel(new PfConceptKey()));
51         assertNotNull(new ToscaModel(new PfConceptKey(), new ToscaServiceTemplates()));
52         assertNotNull(new ToscaModel(new ToscaModel()));
53
54         try {
55             new ToscaModel((PfConceptKey) null);
56             fail("test should throw an exception");
57         } catch (Exception exc) {
58             assertEquals("key is marked @NonNull but is null", exc.getMessage());
59         }
60
61         try {
62             new ToscaModel(null, null);
63             fail("test should throw an exception");
64         } catch (Exception exc) {
65             assertEquals("key is marked @NonNull but is null", exc.getMessage());
66         }
67
68         try {
69             new ToscaModel(null, new ToscaServiceTemplates());
70             fail("test should throw an exception");
71         } catch (Exception exc) {
72             assertEquals("key is marked @NonNull but is null", exc.getMessage());
73         }
74
75         try {
76             new ToscaModel(new PfConceptKey(), null);
77             fail("test should throw an exception");
78         } catch (Exception exc) {
79             assertEquals("serviceTemplates is marked @NonNull but is null", exc.getMessage());
80         }
81
82         try {
83             new ToscaModel((ToscaModel) null);
84             fail("test should throw an exception");
85         } catch (Exception exc) {
86             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
87         }
88
89         PfConceptKey tstsKey = new PfConceptKey("tsts", "0.0.1");
90         Map<PfConceptKey, ToscaServiceTemplate> tstMap = new TreeMap<>();
91         ToscaServiceTemplates tsts = new ToscaServiceTemplates(tstsKey, tstMap);
92         PfConceptKey tmKey = new PfConceptKey("tst", "0.0.1");
93         ToscaModel tm = new ToscaModel(tmKey, tsts);
94
95         ToscaModel tttClone0 = new ToscaModel(tm);
96         assertEquals(tm, tttClone0);
97         assertEquals(0, tm.compareTo(tttClone0));
98
99         ToscaModel tttClone1 = new ToscaModel();
100         tm.copyTo(tttClone1);
101         assertEquals(tm, tttClone1);
102         assertEquals(0, tm.compareTo(tttClone1));
103
104         assertEquals(-1, tm.compareTo(null));
105         assertEquals(0, tm.compareTo(tm));
106         assertFalse(tm.compareTo(tm.getKey()) == 0);
107
108         PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
109         ToscaModel otherDt = new ToscaModel(otherDtKey);
110
111         assertFalse(tm.compareTo(otherDt) == 0);
112         otherDt.setKey(tmKey);
113         assertFalse(tm.compareTo(otherDt) == 0);
114         otherDt.setServiceTemplates(tsts);
115         assertEquals(0, tm.compareTo(otherDt));
116
117         try {
118             tm.copyTo(null);
119             fail("test should throw an exception");
120         } catch (Exception exc) {
121             assertEquals("targetObject is marked @NonNull but is null", exc.getMessage());
122         }
123
124         assertEquals(2, tm.getKeys().size());
125         assertEquals(2, new ToscaModel().getKeys().size());
126
127         new ToscaModel().clean();
128         tm.clean();
129         assertEquals(tttClone0, tm);
130
131         assertFalse(new ToscaModel().validate(new PfValidationResult()).isValid());
132         assertFalse(tm.validate(new PfValidationResult()).isValid());
133
134         tm.register();
135         assertTrue(PfModelService.existsModel(tm.getServiceTemplates().getId()));
136         PfModelService.deregisterModel(tm.getServiceTemplates().getId());
137
138         try {
139             tm.validate(null);
140             fail("test should throw an exception");
141         } catch (Exception exc) {
142             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
143         }
144     }
145 }