Add functional method for mapping maps to PfUtils
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaEntrySchemaTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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 import org.onap.policy.models.base.PfValidationResult;
36
37 /**
38  * DAO test for ToscaEntrySchema.
39  *
40  * @author Liam Fallon (liam.fallon@est.tech)
41  */
42 public class JpaToscaEntrySchemaTest {
43
44     private static final String A_DESCRIPTION = "A Description";
45
46     @Test
47     public void testEntrySchemaPojo() {
48         assertNotNull(new JpaToscaEntrySchema(new PfConceptKey()));
49         assertNotNull(new JpaToscaEntrySchema(new JpaToscaEntrySchema(new PfConceptKey())));
50
51         assertThatThrownBy(() -> new JpaToscaEntrySchema((PfConceptKey) null))
52                 .hasMessageMatching("type is marked .*on.*ull but is null");
53
54         assertThatThrownBy(() -> new JpaToscaEntrySchema((JpaToscaEntrySchema) null))
55                 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
56
57         PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
58         JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
59
60         tes.setDescription(A_DESCRIPTION);
61         assertEquals(A_DESCRIPTION, tes.getDescription());
62
63         List<JpaToscaConstraint> constraints = new ArrayList<>();
64         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
65         constraints.add(lsc);
66         tes.setConstraints(constraints);
67         assertEquals(constraints, tes.getConstraints());
68
69         JpaToscaEntrySchema tdtClone0 = new JpaToscaEntrySchema(tes);
70         assertEquals(tes, tdtClone0);
71         assertEquals(0, tes.compareTo(tdtClone0));
72
73         JpaToscaEntrySchema tdtClone1 = new JpaToscaEntrySchema(tes);
74         assertEquals(tes, tdtClone1);
75         assertEquals(0, tes.compareTo(tdtClone1));
76
77         assertEquals(-1, tes.compareTo(null));
78         assertEquals(0, tes.compareTo(tes));
79
80         JpaToscaEntrySchema otherEs = new JpaToscaEntrySchema(typeKey);
81
82         assertNotEquals(0, tes.compareTo(otherEs));
83         otherEs.setType(typeKey);
84         assertNotEquals(0, tes.compareTo(otherEs));
85         otherEs.setDescription(A_DESCRIPTION);
86         assertNotEquals(0, tes.compareTo(otherEs));
87         otherEs.setConstraints(constraints);
88         assertEquals(0, tes.compareTo(otherEs));
89
90         assertThatThrownBy(() -> tes.copyTo(null)).hasMessageMatching("target is marked .*on.*ull but is null");
91
92         assertEquals(1, tes.getKeys().size());
93         assertEquals(1, new JpaToscaEntrySchema(typeKey).getKeys().size());
94
95         new JpaToscaEntrySchema(typeKey).clean();
96         tes.clean();
97         assertEquals(tdtClone0, tes);
98
99         assertTrue(new JpaToscaEntrySchema(typeKey).validate(new PfValidationResult()).isValid());
100         assertTrue(tes.validate(new PfValidationResult()).isValid());
101
102         tes.setType(PfConceptKey.getNullKey());
103         assertFalse(tes.validate(new PfValidationResult()).isValid());
104         tes.setType(null);
105         assertFalse(tes.validate(new PfValidationResult()).isValid());
106         tes.setType(typeKey);
107         assertTrue(tes.validate(new PfValidationResult()).isValid());
108
109         tes.setDescription("");
110
111         assertFalse(tes.validate(new PfValidationResult()).isValid());
112         tes.setDescription(A_DESCRIPTION);
113         assertTrue(tes.validate(new PfValidationResult()).isValid());
114
115         tes.getConstraints().add(null);
116         assertFalse(tes.validate(new PfValidationResult()).isValid());
117         tes.getConstraints().remove(null);
118         assertTrue(tes.validate(new PfValidationResult()).isValid());
119
120         assertThatThrownBy(() -> tes.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
121     }
122 }