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