Add support for legacy guard policies
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / ToscaTopologyTemplateTest.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.PfReferenceKey;
35 import org.onap.policy.models.base.PfValidationResult;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
39
40 /**
41  * DAO test for ToscaDatatype.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class ToscaTopologyTemplateTest {
46
47     @Test
48     public void testTopologyTemplatePojo() {
49         assertNotNull(new ToscaTopologyTemplate());
50         assertNotNull(new ToscaTopologyTemplate(new PfReferenceKey()));
51         assertNotNull(new ToscaTopologyTemplate(new ToscaTopologyTemplate()));
52
53         try {
54             new ToscaTopologyTemplate((PfReferenceKey) null);
55             fail("test should throw an exception");
56         } catch (Exception exc) {
57             assertEquals("key is marked @NonNull but is null", exc.getMessage());
58         }
59
60         try {
61             new ToscaTopologyTemplate((ToscaTopologyTemplate) null);
62             fail("test should throw an exception");
63         } catch (Exception exc) {
64             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
65         }
66
67         PfReferenceKey tttKey = new PfReferenceKey("tst", "0.0.1", "ttt");
68         ToscaTopologyTemplate ttt = new ToscaTopologyTemplate(tttKey);
69
70         ttt.setDescription("A Description");
71         assertEquals("A Description", ttt.getDescription());
72
73         PfConceptKey policy0TypeKey = new PfConceptKey("Policy0Type", "0.0.1");
74         PfConceptKey policy0Key = new PfConceptKey("Policy0", "0.0.1");
75
76         ToscaPolicy policy0 = new ToscaPolicy(policy0Key, policy0TypeKey);
77         PfConceptKey polsKey = new PfConceptKey("pols", "0.0.1");
78         Map<PfConceptKey, ToscaPolicy> policyMap = new TreeMap<>();
79         policyMap.put(policy0Key, policy0);
80         ToscaPolicies policies = new ToscaPolicies(polsKey, policyMap);
81         ttt.setPolicies(policies);
82
83         ToscaTopologyTemplate tttClone0 = new ToscaTopologyTemplate(ttt);
84         assertEquals(ttt, tttClone0);
85         assertEquals(0, ttt.compareTo(tttClone0));
86
87         ToscaTopologyTemplate tttClone1 = new ToscaTopologyTemplate();
88         ttt.copyTo(tttClone1);
89         assertEquals(ttt, tttClone1);
90         assertEquals(0, ttt.compareTo(tttClone1));
91
92         assertEquals(-1, ttt.compareTo(null));
93         assertEquals(0, ttt.compareTo(ttt));
94         assertFalse(ttt.compareTo(ttt.getKey()) == 0);
95
96         PfReferenceKey otherDtKey = new PfReferenceKey("otherSt", "0.0.1", "otherDt");
97         ToscaTopologyTemplate otherDt = new ToscaTopologyTemplate(otherDtKey);
98
99         assertFalse(ttt.compareTo(otherDt) == 0);
100         otherDt.setKey(tttKey);
101         assertFalse(ttt.compareTo(otherDt) == 0);
102         otherDt.setDescription("A Description");
103         assertFalse(ttt.compareTo(otherDt) == 0);
104         otherDt.setPolicies(policies);
105         assertEquals(0, ttt.compareTo(otherDt));
106
107         try {
108             ttt.copyTo(null);
109             fail("test should throw an exception");
110         } catch (Exception exc) {
111             assertEquals("target is marked @NonNull but is null", exc.getMessage());
112         }
113
114         assertEquals(4, ttt.getKeys().size());
115         assertEquals(1, new ToscaTopologyTemplate().getKeys().size());
116
117         new ToscaTopologyTemplate().clean();
118         ttt.clean();
119         assertEquals(tttClone0, ttt);
120
121         assertTrue(new ToscaTopologyTemplate().validate(new PfValidationResult()).isValid());
122         assertTrue(ttt.validate(new PfValidationResult()).isValid());
123
124         ttt.setDescription(null);
125         assertTrue(ttt.validate(new PfValidationResult()).isValid());
126         ttt.setDescription("");
127         assertFalse(ttt.validate(new PfValidationResult()).isValid());
128         ttt.setDescription("A Description");
129         assertTrue(ttt.validate(new PfValidationResult()).isValid());
130
131         try {
132             ttt.validate(null);
133             fail("test should throw an exception");
134         } catch (Exception exc) {
135             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
136         }
137     }
138 }