Fix Sonar Issues models-tosca-simple
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaSchemaDefinitionTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 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.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import org.junit.Test;
34 import org.onap.policy.models.base.PfConceptKey;
35
36 /**
37  * DAO test for ToscaEntrySchema.
38  *
39  * @author Liam Fallon (liam.fallon@est.tech)
40  */
41 public class JpaToscaSchemaDefinitionTest {
42
43     private static final String A_DESCRIPTION = "A Description";
44
45     @Test
46     public void testEntrySchemaNull() {
47         assertNotNull(new JpaToscaSchemaDefinition(new PfConceptKey()));
48         assertNotNull(new JpaToscaSchemaDefinition(new JpaToscaSchemaDefinition(new PfConceptKey())));
49
50         assertThatThrownBy(() -> new JpaToscaSchemaDefinition((PfConceptKey) null))
51                 .hasMessageMatching("type is marked .*on.*ull but is null");
52
53         assertThatThrownBy(() -> new JpaToscaSchemaDefinition((JpaToscaSchemaDefinition) null))
54                 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
55     }
56
57     @Test
58     public void testEntrySchema() {
59         PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
60         JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
61
62         tes.setDescription(A_DESCRIPTION);
63         assertEquals(A_DESCRIPTION, tes.getDescription());
64
65         List<JpaToscaConstraint> constraints = new ArrayList<>();
66         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
67         constraints.add(lsc);
68         tes.setConstraints(constraints);
69         assertEquals(constraints, tes.getConstraints());
70
71         JpaToscaSchemaDefinition tdtClone0 = new JpaToscaSchemaDefinition(tes);
72         assertEquals(tes, tdtClone0);
73         assertEquals(0, tes.compareTo(tdtClone0));
74
75         JpaToscaSchemaDefinition tdtClone1 = new JpaToscaSchemaDefinition(tes);
76         assertEquals(tes, tdtClone1);
77         assertEquals(0, tes.compareTo(tdtClone1));
78
79         assertEquals(-1, tes.compareTo(null));
80         assertEquals(0, tes.compareTo(tes));
81
82         JpaToscaSchemaDefinition otherEs = new JpaToscaSchemaDefinition(typeKey);
83
84         assertNotEquals(0, tes.compareTo(otherEs));
85         otherEs.setType(typeKey);
86         assertNotEquals(0, tes.compareTo(otherEs));
87         otherEs.setDescription(A_DESCRIPTION);
88         assertNotEquals(0, tes.compareTo(otherEs));
89         otherEs.setConstraints(constraints);
90         assertEquals(0, tes.compareTo(otherEs));
91
92         assertThatThrownBy(() -> tes.copyTo(null)).hasMessageMatching("target is marked .*on.*ull but is null");
93
94         assertEquals(1, tes.getKeys().size());
95         assertEquals(1, new JpaToscaSchemaDefinition(typeKey).getKeys().size());
96
97         new JpaToscaSchemaDefinition(typeKey).clean();
98         tes.clean();
99         assertEquals(tdtClone0, tes);
100     }
101
102     @Test
103     public void testEntrySchemaValidation() {
104         PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
105         JpaToscaSchemaDefinition tes = setUpJpaToscaSchemaDefinition(typeKey);
106
107         assertTrue(new JpaToscaSchemaDefinition(typeKey).validate("").isValid());
108         assertTrue(tes.validate("").isValid());
109
110         tes.setType(PfConceptKey.getNullKey());
111         assertFalse(tes.validate("").isValid());
112         tes.setType(null);
113         assertFalse(tes.validate("").isValid());
114         tes.setType(typeKey);
115         assertTrue(tes.validate("").isValid());
116
117         tes.setDescription("");
118
119         assertFalse(tes.validate("").isValid());
120         tes.setDescription(A_DESCRIPTION);
121         assertTrue(tes.validate("").isValid());
122
123         tes.getConstraints().add(null);
124         assertFalse(tes.validate("").isValid());
125         tes.getConstraints().remove(null);
126         assertTrue(tes.validate("").isValid());
127
128         assertThatThrownBy(() -> tes.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
129     }
130
131     private JpaToscaSchemaDefinition setUpJpaToscaSchemaDefinition(PfConceptKey typeKey) {
132         JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
133         tes.setDescription(A_DESCRIPTION);
134
135         List<JpaToscaConstraint> constraints = new ArrayList<>();
136         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
137         constraints.add(lsc);
138         tes.setConstraints(constraints);
139
140         return tes;
141     }
142 }