Merge "move actors code in drools-applications to policy/models"
[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 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.simple.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.ArrayList;
30 import java.util.List;
31
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfValidationResult;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntrySchema;
37
38 /**
39  * DAO test for ToscaEntrySchema.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 public class JpaToscaEntrySchemaTest {
44
45     @Test
46     public void testEntrySchemaPojo() {
47         assertNotNull(new JpaToscaEntrySchema(new PfConceptKey()));
48         assertNotNull(new JpaToscaEntrySchema(new JpaToscaEntrySchema(new PfConceptKey())));
49
50         try {
51             new JpaToscaEntrySchema((PfConceptKey) null);
52             fail("test should throw an exception");
53         } catch (Exception exc) {
54             assertEquals("type is marked @NonNull but is null", exc.getMessage());
55         }
56
57         try {
58             new JpaToscaEntrySchema((JpaToscaEntrySchema) 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         PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
65         JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
66
67         tes.setDescription("A Description");
68         assertEquals("A Description", tes.getDescription());
69
70         List<JpaToscaConstraint> constraints = new ArrayList<>();
71         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
72         constraints.add(lsc);
73         tes.setConstraints(constraints);
74         assertEquals(constraints, tes.getConstraints());
75
76         JpaToscaEntrySchema tdtClone0 = new JpaToscaEntrySchema(tes);
77         assertEquals(tes, tdtClone0);
78         assertEquals(0, tes.compareTo(tdtClone0));
79
80         JpaToscaEntrySchema tdtClone1 = new JpaToscaEntrySchema(tes);
81         assertEquals(tes, tdtClone1);
82         assertEquals(0, tes.compareTo(tdtClone1));
83
84         assertEquals(-1, tes.compareTo(null));
85         assertEquals(0, tes.compareTo(tes));
86
87         JpaToscaEntrySchema otherEs = new JpaToscaEntrySchema(typeKey);
88
89         assertFalse(tes.compareTo(otherEs) == 0);
90         otherEs.setType(typeKey);
91         assertFalse(tes.compareTo(otherEs) == 0);
92         otherEs.setDescription("A Description");
93         assertFalse(tes.compareTo(otherEs) == 0);
94         otherEs.setConstraints(constraints);
95         assertEquals(0, tes.compareTo(otherEs));
96
97         try {
98             tes.copyTo(null);
99             fail("test should throw an exception");
100         } catch (Exception exc) {
101             assertEquals("target is marked @NonNull but is null", exc.getMessage());
102         }
103
104         assertEquals(1, tes.getKeys().size());
105         assertEquals(1, new JpaToscaEntrySchema(typeKey).getKeys().size());
106
107         new JpaToscaEntrySchema(typeKey).clean();
108         tes.clean();
109         assertEquals(tdtClone0, tes);
110
111         assertTrue(new JpaToscaEntrySchema(typeKey).validate(new PfValidationResult()).isValid());
112         assertTrue(tes.validate(new PfValidationResult()).isValid());
113
114         tes.setType(PfConceptKey.getNullKey());
115         assertFalse(tes.validate(new PfValidationResult()).isValid());
116         tes.setType(null);
117         assertFalse(tes.validate(new PfValidationResult()).isValid());
118         tes.setType(typeKey);
119         assertTrue(tes.validate(new PfValidationResult()).isValid());
120
121         tes.setDescription("");;
122         assertFalse(tes.validate(new PfValidationResult()).isValid());
123         tes.setDescription("A Description");
124         assertTrue(tes.validate(new PfValidationResult()).isValid());
125
126         tes.getConstraints().add(null);
127         assertFalse(tes.validate(new PfValidationResult()).isValid());
128         tes.getConstraints().remove(null);
129         assertTrue(tes.validate(new PfValidationResult()).isValid());
130
131         try {
132             tes.validate(null);
133             fail("test should throw an exception");
134         } catch (Exception exc) {
135             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
136         }
137     }
138 }