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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.contextmodel.concepts;
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;
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;
37 * Context schema tests.
39 public class ContextSchemasTest {
42 public void testNewAxContextSchema() {
43 assertNotNull(new AxContextSchema());
44 assertNotNull(new AxContextSchema(new AxArtifactKey(), "SchemaFlavour", "SchemaDefinition"));
49 public void testContextSchemas() {
50 final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
52 assertNotNull(schema);
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());
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());
65 schema.setSchema("NewSchemaDefinition");
66 assertEquals("NewSchemaDefinition", schema.getSchema());
69 private AxContextSchema setTestSchema() {
70 final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
72 final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
73 schema.setKey(newKey);
74 schema.setSchemaFlavour("NewSchemaFlavour");
75 schema.setSchema("NewSchemaDefinition");
81 public void testAxvalidationSchema() {
82 AxContextSchema schema = setTestSchema();
83 AxValidationResult result = new AxValidationResult();
84 result = schema.validate(result);
85 assertEquals(ValidationResult.VALID, result.getValidationResult());
87 schema.setKey(AxArtifactKey.getNullKey());
88 result = new AxValidationResult();
89 result = schema.validate(result);
90 assertEquals(ValidationResult.INVALID, result.getValidationResult());
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());
98 schema.setSchemaFlavour("UNDEFINED");
99 result = new AxValidationResult();
100 result = schema.validate(result);
101 assertEquals(ValidationResult.INVALID, result.getValidationResult());
103 schema.setSchemaFlavour("NewSchemaFlavour");
104 result = new AxValidationResult();
105 result = schema.validate(result);
106 assertEquals(ValidationResult.VALID, result.getValidationResult());
108 schema.setSchema("");
109 result = new AxValidationResult();
110 result = schema.validate(result);
111 assertEquals(ValidationResult.INVALID, result.getValidationResult());
113 schema.setSchema("NewSchemaDefinition");
114 result = new AxValidationResult();
115 result = schema.validate(result);
116 assertEquals(ValidationResult.VALID, result.getValidationResult());
120 public void testEqualsSchema() {
121 AxContextSchema schema = setTestSchema();
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());
129 assertNotEquals(0, schema.hashCode());
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"));
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"));
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")));
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());
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());
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());
173 schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
174 result = new AxValidationResult();
175 result = schemas.validate(result);
176 assertEquals(ValidationResult.INVALID, result.getValidationResult());
178 schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
179 result = new AxValidationResult();
180 result = schemas.validate(result);
181 assertEquals(ValidationResult.VALID, result.getValidationResult());
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());
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());
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);
204 final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
205 assertThat(clonedSchemas.toString())
206 .startsWith("AxContextSchemas(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),");
208 assertNotEquals(0, schemas.hashCode());
210 assertEquals(schemas, clonedSchemas);
211 assertNotNull(schemas);
212 assertNotEquals(schemas, (Object) "Hello");
213 assertNotEquals(schemas, new AxContextSchemas(new AxArtifactKey()));
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())));
221 clonedSchemas.get(newKey).setSchemaFlavour("YetAnotherFlavour");
222 assertNotEquals(0, schemas.compareTo(clonedSchemas));
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());