Changes for Checkstyle 8.32
[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-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 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
38 /**
39  * DAO test for ToscaDatatype.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 public class JpaToscaTopologyTemplateTest {
44
45     private static final String A_DESCRIPTION = "A Description";
46     private static final String VERSION_001 = "0.0.1";
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         assertThatThrownBy(() -> new JpaToscaTopologyTemplate((PfReferenceKey) null))
56                 .hasMessageMatching("key is marked .*on.*ull but is null");
57
58         assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
59                 .isInstanceOf(NullPointerException.class);
60
61         PfReferenceKey tttKey = new PfReferenceKey("tst", VERSION_001, "ttt");
62         JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
63
64         ttt.setDescription(A_DESCRIPTION);
65         assertEquals(A_DESCRIPTION, ttt.getDescription());
66
67         PfConceptKey policy0TypeKey = new PfConceptKey("Policy0Type", VERSION_001);
68         PfConceptKey policy0Key = new PfConceptKey("Policy0", VERSION_001);
69
70         JpaToscaPolicy policy0 = new JpaToscaPolicy(policy0Key, policy0TypeKey);
71         PfConceptKey polsKey = new PfConceptKey("pols", VERSION_001);
72         Map<PfConceptKey, JpaToscaPolicy> policyMap = new TreeMap<>();
73         policyMap.put(policy0Key, policy0);
74         JpaToscaPolicies policies = new JpaToscaPolicies(polsKey, policyMap);
75         ttt.setPolicies(policies);
76
77         JpaToscaTopologyTemplate tttClone0 = new JpaToscaTopologyTemplate(ttt);
78         assertEquals(ttt, tttClone0);
79         assertEquals(0, ttt.compareTo(tttClone0));
80
81         JpaToscaTopologyTemplate tttClone1 = new JpaToscaTopologyTemplate(ttt);
82         assertEquals(ttt, tttClone1);
83         assertEquals(0, ttt.compareTo(tttClone1));
84
85         assertEquals(-1, ttt.compareTo(null));
86         assertEquals(0, ttt.compareTo(ttt));
87         assertFalse(ttt.compareTo(ttt.getKey()) == 0);
88
89         PfReferenceKey otherDtKey = new PfReferenceKey("otherSt", VERSION_001, "otherDt");
90         JpaToscaTopologyTemplate otherDt = new JpaToscaTopologyTemplate(otherDtKey);
91
92         assertFalse(ttt.compareTo(otherDt) == 0);
93         otherDt.setKey(tttKey);
94         assertFalse(ttt.compareTo(otherDt) == 0);
95         otherDt.setDescription(A_DESCRIPTION);
96         assertFalse(ttt.compareTo(otherDt) == 0);
97         otherDt.setPolicies(policies);
98         assertEquals(0, ttt.compareTo(otherDt));
99
100         assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null))
101                 .isInstanceOf(NullPointerException.class);
102
103         assertEquals(4, ttt.getKeys().size());
104         assertEquals(1, new JpaToscaTopologyTemplate().getKeys().size());
105
106         new JpaToscaTopologyTemplate().clean();
107         ttt.clean();
108         assertEquals(tttClone0, ttt);
109
110         assertTrue(new JpaToscaTopologyTemplate().validate(new PfValidationResult()).isValid());
111         assertTrue(ttt.validate(new PfValidationResult()).isValid());
112
113         ttt.setKey(PfReferenceKey.getNullKey());
114         assertFalse(ttt.validate(new PfValidationResult()).isValid());
115         ttt.setKey(tttKey);
116         assertTrue(ttt.validate(new PfValidationResult()).isValid());
117
118         ttt.setDescription(null);
119         assertTrue(ttt.validate(new PfValidationResult()).isValid());
120         ttt.setDescription("");
121         assertFalse(ttt.validate(new PfValidationResult()).isValid());
122         ttt.setDescription(A_DESCRIPTION);
123         assertTrue(ttt.validate(new PfValidationResult()).isValid());
124
125         assertThatThrownBy(() -> ttt.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
126     }
127 }