Add support for legacy guard policies
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / ToscaEntrySchemaTest.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.PfReferenceKey;
35 import org.onap.policy.models.base.PfValidationResult;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaConstraint;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaConstraintLogical.Operation;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaConstraintLogicalString;
39 import org.onap.policy.models.tosca.simple.concepts.ToscaEntrySchema;
40
41 /**
42  * DAO test for ToscaEntrySchema.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class ToscaEntrySchemaTest {
47
48     @Test
49     public void testEntrySchemaPojo() {
50         assertNotNull(new ToscaEntrySchema());
51         assertNotNull(new ToscaEntrySchema(new PfReferenceKey()));
52         assertNotNull(new ToscaEntrySchema(new PfReferenceKey(), new PfConceptKey()));
53         assertNotNull(new ToscaEntrySchema(new ToscaEntrySchema()));
54
55         try {
56             new ToscaEntrySchema((PfReferenceKey) null);
57             fail("test should throw an exception");
58         } catch (Exception exc) {
59             assertEquals("key is marked @NonNull but is null", exc.getMessage());
60         }
61
62         try {
63             new ToscaEntrySchema((ToscaEntrySchema) null);
64             fail("test should throw an exception");
65         } catch (Exception exc) {
66             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
67         }
68
69         PfReferenceKey esKey = new PfReferenceKey("entrySchemaParent", "0.0.1", "entrySchema");
70         PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
71         ToscaEntrySchema tes = new ToscaEntrySchema(esKey, typeKey);
72
73         tes.setDescription("A Description");
74         assertEquals("A Description", tes.getDescription());
75
76         List<ToscaConstraint> constraints = new ArrayList<>();
77         ToscaConstraintLogicalString lsc =
78                 new ToscaConstraintLogicalString(new PfReferenceKey(esKey, "sc"), Operation.EQ, "hello");
79         constraints.add(lsc);
80         tes.setConstraints(constraints);
81         assertEquals(constraints, tes.getConstraints());
82
83         ToscaEntrySchema tdtClone0 = new ToscaEntrySchema(tes);
84         assertEquals(tes, tdtClone0);
85         assertEquals(0, tes.compareTo(tdtClone0));
86
87         ToscaEntrySchema tdtClone1 = new ToscaEntrySchema();
88         tes.copyTo(tdtClone1);
89         assertEquals(tes, tdtClone1);
90         assertEquals(0, tes.compareTo(tdtClone1));
91
92         assertEquals(-1, tes.compareTo(null));
93         assertEquals(0, tes.compareTo(tes));
94         assertFalse(tes.compareTo(tes.getKey()) == 0);
95
96         PfReferenceKey otherEsKey = new PfReferenceKey("entrySchemaParent", "0.0.1", "otherEntrySchema");
97         ToscaEntrySchema otherEs = new ToscaEntrySchema(otherEsKey);
98
99         assertFalse(tes.compareTo(otherEs) == 0);
100         otherEs.setKey(esKey);
101         assertFalse(tes.compareTo(otherEs) == 0);
102         otherEs.setType(typeKey);
103         assertFalse(tes.compareTo(otherEs) == 0);
104         otherEs.setDescription("A Description");
105         assertFalse(tes.compareTo(otherEs) == 0);
106         otherEs.setConstraints(constraints);
107         assertEquals(0, tes.compareTo(otherEs));
108
109         try {
110             tes.copyTo(null);
111             fail("test should throw an exception");
112         } catch (Exception exc) {
113             assertEquals("target is marked @NonNull but is null", exc.getMessage());
114         }
115
116         assertEquals(3, tes.getKeys().size());
117         assertEquals(2, new ToscaEntrySchema().getKeys().size());
118
119         new ToscaEntrySchema().clean();
120         tes.clean();
121         assertEquals(tdtClone0, tes);
122
123         assertFalse(new ToscaEntrySchema().validate(new PfValidationResult()).isValid());
124         assertTrue(tes.validate(new PfValidationResult()).isValid());
125
126         tes.setType(PfConceptKey.getNullKey());
127         assertFalse(tes.validate(new PfValidationResult()).isValid());
128         tes.setType(null);
129         assertFalse(tes.validate(new PfValidationResult()).isValid());
130         tes.setType(typeKey);
131         assertTrue(tes.validate(new PfValidationResult()).isValid());
132
133         tes.setDescription("");;
134         assertFalse(tes.validate(new PfValidationResult()).isValid());
135         tes.setDescription("A Description");
136         assertTrue(tes.validate(new PfValidationResult()).isValid());
137
138         tes.getConstraints().add(null);
139         assertFalse(tes.validate(new PfValidationResult()).isValid());
140         tes.getConstraints().remove(null);
141         assertTrue(tes.validate(new PfValidationResult()).isValid());
142
143         try {
144             tes.validate(null);
145             fail("test should throw an exception");
146         } catch (Exception exc) {
147             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
148         }
149     }
150 }