52cca14bf3a95ad76b9b7bc03af4f274d8d2b3f0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.contextmodel.concepts;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
35
36 /**
37  * Context schema tests.
38  */
39 public class ContextSchemasTest {
40
41     @Test
42     public void testNewAxContextSchema() {
43         assertNotNull(new AxContextSchema());
44         assertNotNull(new AxContextSchema(new AxArtifactKey(), "SchemaFlavour", "SchemaDefinition"));
45
46     }
47
48     @Test
49     public void testContextSchemas() {
50         final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
51                         "SchemaDefinition");
52         assertNotNull(schema);
53
54         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
55         schema.setKey(newKey);
56         assertEquals("NewSchemaName:0.0.1", schema.getKey().getId());
57         assertEquals("NewSchemaName:0.0.1", schema.getKeys().get(0).getId());
58
59         assertThatThrownBy(() -> schema.setSchemaFlavour(""))
60             .hasMessage("parameter \"schemaFlavour\": value \"\", "
61                             + "does not match regular expression \"[A-Za-z0-9\\-_]+\"");
62         schema.setSchemaFlavour("NewSchemaFlavour");
63         assertEquals("NewSchemaFlavour", schema.getSchemaFlavour());
64
65         schema.setSchema("NewSchemaDefinition");
66         assertEquals("NewSchemaDefinition", schema.getSchema());
67     }
68
69     private AxContextSchema setTestSchema() {
70         final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
71                 "SchemaDefinition");
72         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
73         schema.setKey(newKey);
74         schema.setSchemaFlavour("NewSchemaFlavour");
75         schema.setSchema("NewSchemaDefinition");
76
77         return schema;
78     }
79
80     @Test
81     public void testAxvalidationSchema() {
82         AxContextSchema schema = setTestSchema();
83         AxValidationResult result = new AxValidationResult();
84         result = schema.validate(result);
85         assertEquals(ValidationResult.VALID, result.getValidationResult());
86
87         schema.setKey(AxArtifactKey.getNullKey());
88         result = new AxValidationResult();
89         result = schema.validate(result);
90         assertEquals(ValidationResult.INVALID, result.getValidationResult());
91
92         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
93         schema.setKey(newKey);
94         result = new AxValidationResult();
95         result = schema.validate(result);
96         assertEquals(ValidationResult.VALID, result.getValidationResult());
97
98         schema.setSchemaFlavour("UNDEFINED");
99         result = new AxValidationResult();
100         result = schema.validate(result);
101         assertEquals(ValidationResult.INVALID, result.getValidationResult());
102
103         schema.setSchemaFlavour("NewSchemaFlavour");
104         result = new AxValidationResult();
105         result = schema.validate(result);
106         assertEquals(ValidationResult.VALID, result.getValidationResult());
107
108         schema.setSchema("");
109         result = new AxValidationResult();
110         result = schema.validate(result);
111         assertEquals(ValidationResult.INVALID, result.getValidationResult());
112
113         schema.setSchema("NewSchemaDefinition");
114         result = new AxValidationResult();
115         result = schema.validate(result);
116         assertEquals(ValidationResult.VALID, result.getValidationResult());
117     }
118
119     @Test
120     public void testEqualsSchema() {
121         AxContextSchema schema = setTestSchema();
122         schema.clean();
123
124         final AxContextSchema clonedSchema = new AxContextSchema(schema);
125         assertEquals("AxContextSchema(key=AxArtifactKey:(name=NewSchemaName,version=0.0.1), "
126                         + "schemaFlavour=NewSchemaFlavour, schemaDefinition=NewSchemaDefinition)",
127                         clonedSchema.toString());
128
129         assertNotEquals(0, schema.hashCode());
130
131         // disabling sonar because this code tests the equals() method
132         assertEquals(schema, schema); // NOSONAR
133         assertEquals(schema, clonedSchema);
134         assertNotNull(schema);
135         assertNotEquals(schema, (Object) "Hello");
136         assertNotEquals(schema, new AxContextSchema(new AxArtifactKey(), "Flavour", "Def"));
137
138         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
139         assertNotEquals(schema, new AxContextSchema(newKey, "Flavour", "Def"));
140         assertNotEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "Def"));
141         assertEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition"));
142
143         assertEquals(0, schema.compareTo(schema));
144         assertEquals(0, schema.compareTo(clonedSchema));
145         assertNotEquals(0, schema.compareTo(null));
146         assertNotEquals(0, schema.compareTo(new AxArtifactKey()));
147         assertNotEquals(0, schema.compareTo(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
148         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "Flavour", "Def")));
149         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
150         assertEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));
151     }
152
153     @Test
154     public void testMultipleSchemas() {
155         final AxContextSchemas schemas = new AxContextSchemas();
156         AxValidationResult result = new AxValidationResult();
157         result = schemas.validate(result);
158         assertEquals(ValidationResult.INVALID, result.getValidationResult());
159
160         // Still invalid, no schemas in schema map
161         schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
162         result = new AxValidationResult();
163         result = schemas.validate(result);
164         assertEquals(ValidationResult.INVALID, result.getValidationResult());
165
166         AxContextSchema schema = setTestSchema();
167         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
168         schemas.getSchemasMap().put(newKey, schema);
169         result = new AxValidationResult();
170         result = schemas.validate(result);
171         assertEquals(ValidationResult.VALID, result.getValidationResult());
172
173         schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
174         result = new AxValidationResult();
175         result = schemas.validate(result);
176         assertEquals(ValidationResult.INVALID, result.getValidationResult());
177
178         schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
179         result = new AxValidationResult();
180         result = schemas.validate(result);
181         assertEquals(ValidationResult.VALID, result.getValidationResult());
182
183         schemas.getSchemasMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
184         result = new AxValidationResult();
185         result = schemas.validate(result);
186         assertEquals(ValidationResult.INVALID, result.getValidationResult());
187
188         schemas.getSchemasMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
189         result = new AxValidationResult();
190         result = schemas.validate(result);
191         assertEquals(ValidationResult.VALID, result.getValidationResult());
192
193     }
194
195     @Test
196     public void testClonedSchemas() {
197         final AxContextSchemas schemas = new AxContextSchemas();
198         AxContextSchema schema = setTestSchema();
199         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
200         schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
201         schemas.getSchemasMap().put(newKey, schema);
202         schemas.clean();
203
204         final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
205         assertThat(clonedSchemas.toString())
206                         .startsWith("AxContextSchemas(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),");
207
208         assertNotEquals(0, schemas.hashCode());
209
210         assertEquals(schemas, clonedSchemas);
211         assertNotNull(schemas);
212         assertNotEquals(schemas, (Object) "Hello");
213         assertNotEquals(schemas, new AxContextSchemas(new AxArtifactKey()));
214
215         assertEquals(0, schemas.compareTo(schemas));
216         assertEquals(0, schemas.compareTo(clonedSchemas));
217         assertNotEquals(0, schemas.compareTo(null));
218         assertNotEquals(0, schemas.compareTo(new AxArtifactKey()));
219         assertNotEquals(0, schemas.compareTo(new AxContextSchemas(new AxArtifactKey())));
220
221         clonedSchemas.get(newKey).setSchemaFlavour("YetAnotherFlavour");
222         assertNotEquals(0, schemas.compareTo(clonedSchemas));
223
224         assertEquals("NewSchemaName", schemas.get("NewSchemaName").getKey().getName());
225         assertEquals("NewSchemaName", schemas.get("NewSchemaName", "0.0.1").getKey().getName());
226         assertEquals(1, schemas.getAll("NewSchemaName", "0.0.1").size());
227         assertEquals(0, schemas.getAll("NonExistantSchemaName").size());
228     }
229 }