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