6de0a52144cc2afac606bdee3e04fe10a9f4a685
[policy/apex-pdp.git] /
1 /*-
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
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 testContextSchemas() {
42         assertNotNull(new AxContextSchema());
43         assertNotNull(new AxContextSchema(new AxArtifactKey(), "SchemaFlavour", "SchemaDefinition"));
44
45         final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
46                         "SchemaDefinition");
47         assertNotNull(schema);
48
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());
53
54         assertThatThrownBy(() -> schema.setSchemaFlavour(""))
55             .hasMessage("parameter \"schemaFlavour\": value \"\", "
56                             + "does not match regular expression \"[A-Za-z0-9\\-_]+\"");
57         schema.setSchemaFlavour("NewSchemaFlavour");
58         assertEquals("NewSchemaFlavour", schema.getSchemaFlavour());
59
60         schema.setSchema("NewSchemaDefinition");
61         assertEquals("NewSchemaDefinition", schema.getSchema());
62
63         AxValidationResult result = new AxValidationResult();
64         result = schema.validate(result);
65         assertEquals(ValidationResult.VALID, result.getValidationResult());
66
67         schema.setKey(AxArtifactKey.getNullKey());
68         result = new AxValidationResult();
69         result = schema.validate(result);
70         assertEquals(ValidationResult.INVALID, result.getValidationResult());
71
72         schema.setKey(newKey);
73         result = new AxValidationResult();
74         result = schema.validate(result);
75         assertEquals(ValidationResult.VALID, result.getValidationResult());
76
77         schema.setSchemaFlavour("UNDEFINED");
78         result = new AxValidationResult();
79         result = schema.validate(result);
80         assertEquals(ValidationResult.INVALID, result.getValidationResult());
81
82         schema.setSchemaFlavour("NewSchemaFlavour");
83         result = new AxValidationResult();
84         result = schema.validate(result);
85         assertEquals(ValidationResult.VALID, result.getValidationResult());
86
87         schema.setSchema("");
88         result = new AxValidationResult();
89         result = schema.validate(result);
90         assertEquals(ValidationResult.INVALID, result.getValidationResult());
91
92         schema.setSchema("NewSchemaDefinition");
93         result = new AxValidationResult();
94         result = schema.validate(result);
95         assertEquals(ValidationResult.VALID, result.getValidationResult());
96
97         schema.clean();
98
99         final AxContextSchema clonedSchema = new AxContextSchema(schema);
100         assertEquals("AxContextSchema:(key=AxArtifactKey:(name=NewSchemaName,version=0.0.1),"
101                         + "schemaFlavour=NewSchemaFlavour,schemaDefinition=NewSchemaDefinition)",
102                         clonedSchema.toString());
103
104         assertNotEquals(0, schema.hashCode());
105
106         // disabling sonar because this code tests the equals() method
107         assertEquals(schema, schema); // NOSONAR
108         assertEquals(schema, clonedSchema);
109         assertNotNull(schema);
110         assertNotEquals(schema, (Object) "Hello");
111         assertNotEquals(schema, new AxContextSchema(new AxArtifactKey(), "Flavour", "Def"));
112         assertNotEquals(schema, new AxContextSchema(newKey, "Flavour", "Def"));
113         assertNotEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "Def"));
114         assertEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition"));
115
116         assertEquals(0, schema.compareTo(schema));
117         assertEquals(0, schema.compareTo(clonedSchema));
118         assertNotEquals(0, schema.compareTo(null));
119         assertNotEquals(0, schema.compareTo(new AxArtifactKey()));
120         assertNotEquals(0, schema.compareTo(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
121         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "Flavour", "Def")));
122         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
123         assertEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));
124
125         final AxContextSchemas schemas = new AxContextSchemas();
126         result = new AxValidationResult();
127         result = schemas.validate(result);
128         assertEquals(ValidationResult.INVALID, result.getValidationResult());
129
130         // Still invalid, no schemas in schema map
131         schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
132         result = new AxValidationResult();
133         result = schemas.validate(result);
134         assertEquals(ValidationResult.INVALID, result.getValidationResult());
135
136         schemas.getSchemasMap().put(newKey, schema);
137         result = new AxValidationResult();
138         result = schemas.validate(result);
139         assertEquals(ValidationResult.VALID, result.getValidationResult());
140
141         schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
142         result = new AxValidationResult();
143         result = schemas.validate(result);
144         assertEquals(ValidationResult.INVALID, result.getValidationResult());
145
146         schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
147         result = new AxValidationResult();
148         result = schemas.validate(result);
149         assertEquals(ValidationResult.VALID, result.getValidationResult());
150
151         schemas.getSchemasMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
152         result = new AxValidationResult();
153         result = schemas.validate(result);
154         assertEquals(ValidationResult.INVALID, result.getValidationResult());
155
156         schemas.getSchemasMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
157         result = new AxValidationResult();
158         result = schemas.validate(result);
159         assertEquals(ValidationResult.VALID, result.getValidationResult());
160
161         schemas.clean();
162
163         final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
164         assertTrue(clonedSchemas.toString()
165                         .startsWith("AxContextSchemas:(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),"));
166
167         assertNotEquals(0, schemas.hashCode());
168
169         assertEquals(schemas, clonedSchemas);
170         assertNotNull(schemas);
171         assertNotEquals(schemas, (Object) "Hello");
172         assertNotEquals(schemas, new AxContextSchemas(new AxArtifactKey()));
173
174         assertEquals(0, schemas.compareTo(schemas));
175         assertEquals(0, schemas.compareTo(clonedSchemas));
176         assertNotEquals(0, schemas.compareTo(null));
177         assertNotEquals(0, schemas.compareTo(new AxArtifactKey()));
178         assertNotEquals(0, schemas.compareTo(new AxContextSchemas(new AxArtifactKey())));
179
180         clonedSchemas.get(newKey).setSchemaFlavour("YetAnotherFlavour");
181         assertNotEquals(0, schemas.compareTo(clonedSchemas));
182
183         assertEquals("NewSchemaName", schemas.get("NewSchemaName").getKey().getName());
184         assertEquals("NewSchemaName", schemas.get("NewSchemaName", "0.0.1").getKey().getName());
185         assertEquals(1, schemas.getAll("NewSchemaName", "0.0.1").size());
186         assertEquals(0, schemas.getAll("NonExistantSchemaName").size());
187     }
188 }