2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.contextmodel.concepts;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
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;
36 * Context schema tests.
38 public class ContextSchemasTest {
41 public void testContextSchemas() {
42 assertNotNull(new AxContextSchema());
43 assertNotNull(new AxContextSchema(new AxArtifactKey(), "SchemaFlavour", "SchemaDefinition"));
45 final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
47 assertNotNull(schema);
49 final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
50 schema.setKey(newKey);
51 assertEquals("NewSchemaName:0.0.1", schema.getKey().getId());
52 assertEquals("NewSchemaName:0.0.1", schema.getKeys().get(0).getId());
55 schema.setSchemaFlavour("");
56 fail("test should throw an exception here");
57 } catch (final Exception e) {
58 assertEquals("parameter \"schemaFlavour\": value \"\", "
59 + "does not match regular expression \"[A-Za-z0-9\\-_]+\"", e.getMessage());
62 schema.setSchemaFlavour("NewSchemaFlavour");
63 assertEquals("NewSchemaFlavour", schema.getSchemaFlavour());
65 schema.setSchema("NewSchemaDefinition");
66 assertEquals("NewSchemaDefinition", schema.getSchema());
68 AxValidationResult result = new AxValidationResult();
69 result = schema.validate(result);
70 assertEquals(ValidationResult.VALID, result.getValidationResult());
72 schema.setKey(AxArtifactKey.getNullKey());
73 result = new AxValidationResult();
74 result = schema.validate(result);
75 assertEquals(ValidationResult.INVALID, result.getValidationResult());
77 schema.setKey(newKey);
78 result = new AxValidationResult();
79 result = schema.validate(result);
80 assertEquals(ValidationResult.VALID, result.getValidationResult());
82 schema.setSchemaFlavour("UNDEFINED");
83 result = new AxValidationResult();
84 result = schema.validate(result);
85 assertEquals(ValidationResult.INVALID, result.getValidationResult());
87 schema.setSchemaFlavour("NewSchemaFlavour");
88 result = new AxValidationResult();
89 result = schema.validate(result);
90 assertEquals(ValidationResult.VALID, result.getValidationResult());
93 result = new AxValidationResult();
94 result = schema.validate(result);
95 assertEquals(ValidationResult.INVALID, result.getValidationResult());
97 schema.setSchema("NewSchemaDefinition");
98 result = new AxValidationResult();
99 result = schema.validate(result);
100 assertEquals(ValidationResult.VALID, result.getValidationResult());
104 final AxContextSchema clonedSchema = new AxContextSchema(schema);
105 assertEquals("AxContextSchema:(key=AxArtifactKey:(name=NewSchemaName,version=0.0.1),"
106 + "schemaFlavour=NewSchemaFlavour,schemaDefinition=NewSchemaDefinition)",
107 clonedSchema.toString());
109 assertNotEquals(0, schema.hashCode());
111 assertEquals(schema, schema);
112 assertEquals(schema, clonedSchema);
113 assertNotNull(schema);
114 assertNotEquals(schema, (Object) "Hello");
115 assertNotEquals(schema, new AxContextSchema(new AxArtifactKey(), "Flavour", "Def"));
116 assertNotEquals(schema, new AxContextSchema(newKey, "Flavour", "Def"));
117 assertNotEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "Def"));
118 assertEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition"));
120 assertEquals(0, schema.compareTo(schema));
121 assertEquals(0, schema.compareTo(clonedSchema));
122 assertNotEquals(0, schema.compareTo(null));
123 assertNotEquals(0, schema.compareTo(new AxArtifactKey()));
124 assertNotEquals(0, schema.compareTo(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
125 assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "Flavour", "Def")));
126 assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
127 assertEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));
129 final AxContextSchemas schemas = new AxContextSchemas();
130 result = new AxValidationResult();
131 result = schemas.validate(result);
132 assertEquals(ValidationResult.INVALID, result.getValidationResult());
134 // Still invalid, no schemas in schema map
135 schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
136 result = new AxValidationResult();
137 result = schemas.validate(result);
138 assertEquals(ValidationResult.INVALID, result.getValidationResult());
140 schemas.getSchemasMap().put(newKey, schema);
141 result = new AxValidationResult();
142 result = schemas.validate(result);
143 assertEquals(ValidationResult.VALID, result.getValidationResult());
145 schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
146 result = new AxValidationResult();
147 result = schemas.validate(result);
148 assertEquals(ValidationResult.INVALID, result.getValidationResult());
150 schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
151 result = new AxValidationResult();
152 result = schemas.validate(result);
153 assertEquals(ValidationResult.VALID, result.getValidationResult());
155 schemas.getSchemasMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
156 result = new AxValidationResult();
157 result = schemas.validate(result);
158 assertEquals(ValidationResult.INVALID, result.getValidationResult());
160 schemas.getSchemasMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
161 result = new AxValidationResult();
162 result = schemas.validate(result);
163 assertEquals(ValidationResult.VALID, result.getValidationResult());
167 final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
168 assertTrue(clonedSchemas.toString()
169 .startsWith("AxContextSchemas:(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),"));
171 assertNotEquals(0, schemas.hashCode());
173 assertEquals(schemas, schemas);
174 assertEquals(schemas, clonedSchemas);
175 assertNotNull(schemas);
176 assertNotEquals(schemas, (Object) "Hello");
177 assertNotEquals(schemas, new AxContextSchemas(new AxArtifactKey()));
179 assertEquals(0, schemas.compareTo(schemas));
180 assertEquals(0, schemas.compareTo(clonedSchemas));
181 assertNotEquals(0, schemas.compareTo(null));
182 assertNotEquals(0, schemas.compareTo(new AxArtifactKey()));
183 assertNotEquals(0, schemas.compareTo(new AxContextSchemas(new AxArtifactKey())));
185 clonedSchemas.get(newKey).setSchemaFlavour("YetAnotherFlavour");
186 assertNotEquals(0, schemas.compareTo(clonedSchemas));
188 assertEquals("NewSchemaName", schemas.get("NewSchemaName").getKey().getName());
189 assertEquals("NewSchemaName", schemas.get("NewSchemaName", "0.0.1").getKey().getName());
190 assertEquals(1, schemas.getAll("NewSchemaName", "0.0.1").size());
191 assertEquals(0, schemas.getAll("NonExistantSchemaName").size());