73f4c3086ea0830113fdd4490487a3cd76aaba2f
[policy/apex-pdp.git] / model / context-model / src / test / java / org / onap / policy / apex / model / contextmodel / concepts / ContextSchemasTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.contextmodel.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
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;
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 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
36
37 /**
38  * Context schema tests.
39  */
40 public class ContextSchemasTest {
41
42     @Test
43     public void testContextSchemas() {
44         assertNotNull(new AxContextSchema());
45         assertNotNull(new AxContextSchema(new AxArtifactKey(), "SchemaFlavour", "SchemaDefinition"));
46
47         final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
48                         "SchemaDefinition");
49         assertNotNull(schema);
50
51         final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
52         schema.setKey(newKey);
53         assertEquals("NewSchemaName:0.0.1", schema.getKey().getId());
54         assertEquals("NewSchemaName:0.0.1", schema.getKeys().get(0).getId());
55
56         try {
57             schema.setSchemaFlavour("");
58             fail("test should throw an exception here");
59         } catch (final Exception e) {
60             assertEquals("parameter \"schemaFlavour\": value \"\", "
61                             + "does not match regular expression \"[A-Za-z0-9\\-_]+\"", e.getMessage());
62         }
63
64         schema.setSchemaFlavour("NewSchemaFlavour");
65         assertEquals("NewSchemaFlavour", schema.getSchemaFlavour());
66
67         schema.setSchema("NewSchemaDefinition");
68         assertEquals("NewSchemaDefinition", schema.getSchema());
69
70         AxValidationResult result = new AxValidationResult();
71         result = schema.validate(result);
72         assertEquals(ValidationResult.VALID, result.getValidationResult());
73
74         schema.setKey(AxArtifactKey.getNullKey());
75         result = new AxValidationResult();
76         result = schema.validate(result);
77         assertEquals(ValidationResult.INVALID, result.getValidationResult());
78
79         schema.setKey(newKey);
80         result = new AxValidationResult();
81         result = schema.validate(result);
82         assertEquals(ValidationResult.VALID, result.getValidationResult());
83
84         schema.setSchemaFlavour("UNDEFINED");
85         result = new AxValidationResult();
86         result = schema.validate(result);
87         assertEquals(ValidationResult.INVALID, result.getValidationResult());
88
89         schema.setSchemaFlavour("NewSchemaFlavour");
90         result = new AxValidationResult();
91         result = schema.validate(result);
92         assertEquals(ValidationResult.VALID, result.getValidationResult());
93
94         schema.setSchema("");
95         result = new AxValidationResult();
96         result = schema.validate(result);
97         assertEquals(ValidationResult.INVALID, result.getValidationResult());
98
99         schema.setSchema("NewSchemaDefinition");
100         result = new AxValidationResult();
101         result = schema.validate(result);
102         assertEquals(ValidationResult.VALID, result.getValidationResult());
103
104         schema.clean();
105
106         final AxContextSchema clonedSchema = new AxContextSchema(schema);
107         assertEquals("AxContextSchema:(key=AxArtifactKey:(name=NewSchemaName,version=0.0.1),"
108                         + "schemaFlavour=NewSchemaFlavour,schemaDefinition=NewSchemaDefinition)",
109                         clonedSchema.toString());
110
111         assertFalse(schema.hashCode() == 0);
112
113         assertTrue(schema.equals(schema));
114         assertTrue(schema.equals(clonedSchema));
115         assertFalse(schema.equals(null));
116         assertFalse(schema.equals((Object) "Hello"));
117         assertFalse(schema.equals(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
118         assertFalse(schema.equals(new AxContextSchema(newKey, "Flavour", "Def")));
119         assertFalse(schema.equals(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
120         assertTrue(schema.equals(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));
121
122         assertEquals(0, schema.compareTo(schema));
123         assertEquals(0, schema.compareTo(clonedSchema));
124         assertNotEquals(0, schema.compareTo(null));
125         assertNotEquals(0, schema.compareTo(new AxArtifactKey()));
126         assertNotEquals(0, schema.compareTo(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
127         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "Flavour", "Def")));
128         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
129         assertEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));
130
131         final AxContextSchemas schemas = new AxContextSchemas();
132         result = new AxValidationResult();
133         result = schemas.validate(result);
134         assertEquals(ValidationResult.INVALID, result.getValidationResult());
135
136         // Still invalid, no schemas in schema map
137         schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
138         result = new AxValidationResult();
139         result = schemas.validate(result);
140         assertEquals(ValidationResult.INVALID, result.getValidationResult());
141
142         schemas.getSchemasMap().put(newKey, schema);
143         result = new AxValidationResult();
144         result = schemas.validate(result);
145         assertEquals(ValidationResult.VALID, result.getValidationResult());
146
147         schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
148         result = new AxValidationResult();
149         result = schemas.validate(result);
150         assertEquals(ValidationResult.INVALID, result.getValidationResult());
151
152         schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
153         result = new AxValidationResult();
154         result = schemas.validate(result);
155         assertEquals(ValidationResult.VALID, result.getValidationResult());
156
157         schemas.getSchemasMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
158         result = new AxValidationResult();
159         result = schemas.validate(result);
160         assertEquals(ValidationResult.INVALID, result.getValidationResult());
161
162         schemas.getSchemasMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
163         result = new AxValidationResult();
164         result = schemas.validate(result);
165         assertEquals(ValidationResult.VALID, result.getValidationResult());
166
167         schemas.clean();
168
169         final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
170         assertTrue(clonedSchemas.toString()
171                         .startsWith("AxContextSchemas:(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),"));
172
173         assertFalse(schemas.hashCode() == 0);
174
175         assertTrue(schemas.equals(schemas));
176         assertTrue(schemas.equals(clonedSchemas));
177         assertFalse(schemas.equals(null));
178         assertFalse(schemas.equals((Object) "Hello"));
179         assertFalse(schemas.equals(new AxContextSchemas(new AxArtifactKey())));
180
181         assertEquals(0, schemas.compareTo(schemas));
182         assertEquals(0, schemas.compareTo(clonedSchemas));
183         assertNotEquals(0, schemas.compareTo(null));
184         assertNotEquals(0, schemas.compareTo(new AxArtifactKey()));
185         assertNotEquals(0, schemas.compareTo(new AxContextSchemas(new AxArtifactKey())));
186
187         clonedSchemas.get(newKey).setSchemaFlavour("YetAnotherFlavour");
188         assertNotEquals(0, schemas.compareTo(clonedSchemas));
189
190         assertEquals("NewSchemaName", schemas.get("NewSchemaName").getKey().getName());
191         assertEquals("NewSchemaName", schemas.get("NewSchemaName", "0.0.1").getKey().getName());
192         assertEquals(1, schemas.getAll("NewSchemaName", "0.0.1").size());
193         assertEquals(0, schemas.getAll("NonExistantSchemaName").size());
194     }
195 }