Fix simple sonar issues in models-tosca
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTypeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.ArrayList;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.Test;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfReferenceKey;
38 import org.onap.policy.models.base.PfValidationResult;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
41
42 /**
43  * DAO test for ToscaPolicyType.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaToscaPolicyTypeTest {
48     private static final String A_DESCRIPTION = "A Description";
49     private static final String VERSION_001 = "0.0.1";
50
51     @Test
52     public void testPolicyTypePojo() {
53         assertNotNull(new JpaToscaPolicyType());
54         assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
55         assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
56
57         assertThatThrownBy(() -> new JpaToscaPolicyType((PfConceptKey) null))
58                         .hasMessage("key is marked @NonNull but is null");
59
60         assertThatThrownBy(() -> new JpaToscaPolicyType((JpaToscaPolicyType) null))
61                         .hasMessage("copyConcept is marked @NonNull but is null");
62
63         PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
64         JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
65
66         PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
67         tpt.setDerivedFrom(derivedFromKey);
68
69         Map<String, String> metadata = new HashMap<>();
70         metadata.put("key", "value");
71         tpt.setMetadata(metadata);
72         assertEquals(metadata, tpt.getMetadata());
73
74         tpt.setDescription(A_DESCRIPTION);
75
76         PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
77         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
78         JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
79         properties.put(tp.getKey().getLocalName(), tp);
80         tpt.setProperties(properties);
81         assertEquals(properties, tpt.getProperties());
82
83         List<PfConceptKey> targets = new ArrayList<>();
84         PfConceptKey target = new PfConceptKey("target", VERSION_001);
85         targets.add(target);
86         tpt.setTargets(targets);
87         assertEquals(targets, tpt.getTargets());
88
89         List<JpaToscaTrigger> triggers = new ArrayList<>();
90         JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
91         triggers.add(trigger);
92         tpt.setTriggers(triggers);
93         assertEquals(triggers, tpt.getTriggers());
94
95         JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
96         assertEquals(tpt, tdtClone0);
97         assertEquals(0, tpt.compareTo(tdtClone0));
98
99         JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType();
100         tpt.copyTo(tdtClone1);
101         assertEquals(tpt, tdtClone1);
102         assertEquals(0, tpt.compareTo(tdtClone1));
103
104         assertEquals(-1, tpt.compareTo(null));
105         assertEquals(0, tpt.compareTo(tpt));
106         assertFalse(tpt.compareTo(tpt.getKey()) == 0);
107
108         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
109         JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
110
111         assertFalse(tpt.compareTo(otherDt) == 0);
112         otherDt.setKey(ptKey);
113         assertFalse(tpt.compareTo(otherDt) == 0);
114         otherDt.setDerivedFrom(derivedFromKey);
115         assertFalse(tpt.compareTo(otherDt) == 0);
116         otherDt.setMetadata(metadata);
117         assertFalse(tpt.compareTo(otherDt) == 0);
118         otherDt.setDescription(A_DESCRIPTION);
119         assertFalse(tpt.compareTo(otherDt) == 0);
120         otherDt.setProperties(properties);
121         assertFalse(tpt.compareTo(otherDt) == 0);
122         otherDt.setTargets(targets);
123         assertFalse(tpt.compareTo(otherDt) == 0);
124         otherDt.setTriggers(triggers);
125         assertEquals(0, tpt.compareTo(otherDt));
126
127         assertThatThrownBy(() -> tpt.copyTo(null)).hasMessage("target is marked @NonNull but is null");
128
129         assertEquals(6, tpt.getKeys().size());
130         assertEquals(1, new JpaToscaPolicyType().getKeys().size());
131
132         new JpaToscaPolicyType().clean();
133         tpt.clean();
134         assertEquals(tdtClone0, tpt);
135
136         assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
137         assertTrue(tpt.validate(new PfValidationResult()).isValid());
138
139         tpt.getProperties().put(null, null);
140         assertFalse(tpt.validate(new PfValidationResult()).isValid());
141         tpt.getProperties().remove(null);
142         assertTrue(tpt.validate(new PfValidationResult()).isValid());
143
144         tpt.getTargets().add(null);
145         assertFalse(tpt.validate(new PfValidationResult()).isValid());
146         tpt.getTargets().remove(null);
147         assertTrue(tpt.validate(new PfValidationResult()).isValid());
148
149         tpt.getTriggers().add(null);
150         assertFalse(tpt.validate(new PfValidationResult()).isValid());
151         tpt.getTriggers().remove(null);
152         assertTrue(tpt.validate(new PfValidationResult()).isValid());
153
154         tpt.getMetadata().put(null, null);
155         assertFalse(tpt.validate(new PfValidationResult()).isValid());
156         tpt.getMetadata().remove(null);
157         assertTrue(tpt.validate(new PfValidationResult()).isValid());
158
159         tpt.getMetadata().put("nullKey", null);
160         assertFalse(tpt.validate(new PfValidationResult()).isValid());
161         tpt.getMetadata().remove("nullKey");
162         assertTrue(tpt.validate(new PfValidationResult()).isValid());
163
164         tpt.setDescription("");;
165         assertFalse(tpt.validate(new PfValidationResult()).isValid());
166         tpt.setDescription(A_DESCRIPTION);
167         assertTrue(tpt.validate(new PfValidationResult()).isValid());
168
169         tpt.setDerivedFrom(PfConceptKey.getNullKey());
170         assertFalse(tpt.validate(new PfValidationResult()).isValid());
171         tpt.setDerivedFrom(derivedFromKey);
172         assertTrue(tpt.validate(new PfValidationResult()).isValid());
173
174         assertThatThrownBy(() -> tpt.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
175
176         assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null))
177                         .hasMessage("key is marked @NonNull but is null");
178
179         assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null))
180                         .hasMessage("copyConcept is marked @NonNull but is null");
181
182         JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey());
183         assertEquals(-1, tet.compareTo(null));
184         assertEquals(0, tet.compareTo(tet));
185         assertFalse(tet.compareTo(tet.getKey()) == 0);
186
187         assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
188
189         assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));
190     }
191 }