fbca04d7ec90d5d06d0798a4f95f98eab441eaba
[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  * ================================================================================
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.apex.model.contextmodel.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
34
35 /**
36  * Context schema tests.
37  */
38 public class ContextSchemasTest {
39
40     @Test
41     public void testNewAxContextSchema() {
42         assertNotNull(new AxContextSchema());
43         assertNotNull(new AxContextSchema(new AxArtifactKey(), "SchemaFlavour", "SchemaDefinition"));
44
45     }
46
47     @Test
48     public void testContextSchemas() {
49         final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
50                         "SchemaDefinition");
51         assertNotNull(schema);
52
53         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
54         schema.setKey(newKey);
55         assertEquals("NewSchemaName:0.0.1", schema.getKey().getId());
56         assertEquals("NewSchemaName:0.0.1", schema.getKeys().get(0).getId());
57
58         assertThatThrownBy(() -> schema.setSchemaFlavour(""))
59             .hasMessage("parameter \"schemaFlavour\": value \"\", "
60                             + "does not match regular expression \"[A-Za-z0-9\\-_]+\"");
61         schema.setSchemaFlavour("NewSchemaFlavour");
62         assertEquals("NewSchemaFlavour", schema.getSchemaFlavour());
63
64         schema.setSchema("NewSchemaDefinition");
65         assertEquals("NewSchemaDefinition", schema.getSchema());
66     }
67
68     private AxContextSchema setTestSchema() {
69         final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
70                 "SchemaDefinition");
71         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
72         schema.setKey(newKey);
73         schema.setSchemaFlavour("NewSchemaFlavour");
74         schema.setSchema("NewSchemaDefinition");
75
76         return schema;
77     }
78
79     @Test
80     public void testAxvalidationSchema() {
81         AxContextSchema schema = setTestSchema();
82         AxValidationResult result = new AxValidationResult();
83         result = schema.validate(result);
84         assertEquals(ValidationResult.VALID, result.getValidationResult());
85
86         schema.setKey(AxArtifactKey.getNullKey());
87         result = new AxValidationResult();
88         result = schema.validate(result);
89         assertEquals(ValidationResult.INVALID, result.getValidationResult());
90
91         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
92         schema.setKey(newKey);
93         result = new AxValidationResult();
94         result = schema.validate(result);
95         assertEquals(ValidationResult.VALID, result.getValidationResult());
96
97         schema.setSchemaFlavour("UNDEFINED");
98         result = new AxValidationResult();
99         result = schema.validate(result);
100         assertEquals(ValidationResult.INVALID, result.getValidationResult());
101
102         schema.setSchemaFlavour("NewSchemaFlavour");
103         result = new AxValidationResult();
104         result = schema.validate(result);
105         assertEquals(ValidationResult.VALID, result.getValidationResult());
106
107         schema.setSchema("");
108         result = new AxValidationResult();
109         result = schema.validate(result);
110         assertEquals(ValidationResult.INVALID, result.getValidationResult());
111
112         schema.setSchema("NewSchemaDefinition");
113         result = new AxValidationResult();
114         result = schema.validate(result);
115         assertEquals(ValidationResult.VALID, result.getValidationResult());
116     }
117
118     @Test
119     public void testEqualsSchema() {
120         AxContextSchema schema = setTestSchema();
121         schema.clean();
122
123         final AxContextSchema clonedSchema = new AxContextSchema(schema);
124         assertEquals("AxContextSchema:(key=AxArtifactKey:(name=NewSchemaName,version=0.0.1),"
125                         + "schemaFlavour=NewSchemaFlavour,schemaDefinition=NewSchemaDefinition)",
126                         clonedSchema.toString());
127
128         assertNotEquals(0, schema.hashCode());
129
130         // disabling sonar because this code tests the equals() method
131         assertEquals(schema, schema); // NOSONAR
132         assertEquals(schema, clonedSchema);
133         assertNotNull(schema);
134         assertNotEquals(schema, (Object) "Hello");
135         assertNotEquals(schema, new AxContextSchema(new AxArtifactKey(), "Flavour", "Def"));
136
137         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
138         assertNotEquals(schema, new AxContextSchema(newKey, "Flavour", "Def"));
139         assertNotEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "Def"));
140         assertEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition"));
141
142         assertEquals(0, schema.compareTo(schema));
143         assertEquals(0, schema.compareTo(clonedSchema));
144         assertNotEquals(0, schema.compareTo(null));
145         assertNotEquals(0, schema.compareTo(new AxArtifactKey()));
146         assertNotEquals(0, schema.compareTo(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
147         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "Flavour", "Def")));
148         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
149         assertEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));
150     }
151
152     @Test
153     public void testMultipleSchemas() {
154         final AxContextSchemas schemas = new AxContextSchemas();
155         AxValidationResult result = new AxValidationResult();
156         result = schemas.validate(result);
157         assertEquals(ValidationResult.INVALID, result.getValidationResult());
158
159         // Still invalid, no schemas in schema map
160         schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
161         result = new AxValidationResult();
162         result = schemas.validate(result);
163         assertEquals(ValidationResult.INVALID, result.getValidationResult());
164
165         AxContextSchema schema = setTestSchema();
166         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
167         schemas.getSchemasMap().put(newKey, schema);
168         result = new AxValidationResult();
169         result = schemas.validate(result);
170         assertEquals(ValidationResult.VALID, result.getValidationResult());
171
172         schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
173         result = new AxValidationResult();
174         result = schemas.validate(result);
175         assertEquals(ValidationResult.INVALID, result.getValidationResult());
176
177         schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
178         result = new AxValidationResult();
179         result = schemas.validate(result);
180         assertEquals(ValidationResult.VALID, result.getValidationResult());
181
182         schemas.getSchemasMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
183         result = new AxValidationResult();
184         result = schemas.validate(result);
185         assertEquals(ValidationResult.INVALID, result.getValidationResult());
186
187         schemas.getSchemasMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
188         result = new AxValidationResult();
189         result = schemas.validate(result);
190         assertEquals(ValidationResult.VALID, result.getValidationResult());
191
192     }
193
194     @Test
195     public void testClonedSchemas() {
196         final AxContextSchemas schemas = new AxContextSchemas();
197         AxContextSchema schema = setTestSchema();
198         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
199         schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
200         schemas.getSchemasMap().put(newKey, schema);
201         schemas.clean();
202
203         final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
204         assertTrue(clonedSchemas.toString()
205                         .startsWith("AxContextSchemas:(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),"));
206
207         assertNotEquals(0, schemas.hashCode());
208
209         assertEquals(schemas, clonedSchemas);
210         assertNotNull(schemas);
211         assertNotEquals(schemas, (Object) "Hello");
212         assertNotEquals(schemas, new AxContextSchemas(new AxArtifactKey()));
213
214         assertEquals(0, schemas.compareTo(schemas));
215         assertEquals(0, schemas.compareTo(clonedSchemas));
216         assertNotEquals(0, schemas.compareTo(null));
217         assertNotEquals(0, schemas.compareTo(new AxArtifactKey()));
218         assertNotEquals(0, schemas.compareTo(new AxContextSchemas(new AxArtifactKey())));
219
220         clonedSchemas.get(newKey).setSchemaFlavour("YetAnotherFlavour");
221         assertNotEquals(0, schemas.compareTo(clonedSchemas));
222
223         assertEquals("NewSchemaName", schemas.get("NewSchemaName").getKey().getName());
224         assertEquals("NewSchemaName", schemas.get("NewSchemaName", "0.0.1").getKey().getName());
225         assertEquals(1, schemas.getAll("NewSchemaName", "0.0.1").size());
226         assertEquals(0, schemas.getAll("NonExistantSchemaName").size());
227     }
228 }