Changes for checkstyle 8.32
[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
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         try {
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());
60         }
61
62         schema.setSchemaFlavour("NewSchemaFlavour");
63         assertEquals("NewSchemaFlavour", schema.getSchemaFlavour());
64
65         schema.setSchema("NewSchemaDefinition");
66         assertEquals("NewSchemaDefinition", schema.getSchema());
67
68         AxValidationResult result = new AxValidationResult();
69         result = schema.validate(result);
70         assertEquals(ValidationResult.VALID, result.getValidationResult());
71
72         schema.setKey(AxArtifactKey.getNullKey());
73         result = new AxValidationResult();
74         result = schema.validate(result);
75         assertEquals(ValidationResult.INVALID, result.getValidationResult());
76
77         schema.setKey(newKey);
78         result = new AxValidationResult();
79         result = schema.validate(result);
80         assertEquals(ValidationResult.VALID, result.getValidationResult());
81
82         schema.setSchemaFlavour("UNDEFINED");
83         result = new AxValidationResult();
84         result = schema.validate(result);
85         assertEquals(ValidationResult.INVALID, result.getValidationResult());
86
87         schema.setSchemaFlavour("NewSchemaFlavour");
88         result = new AxValidationResult();
89         result = schema.validate(result);
90         assertEquals(ValidationResult.VALID, result.getValidationResult());
91
92         schema.setSchema("");
93         result = new AxValidationResult();
94         result = schema.validate(result);
95         assertEquals(ValidationResult.INVALID, result.getValidationResult());
96
97         schema.setSchema("NewSchemaDefinition");
98         result = new AxValidationResult();
99         result = schema.validate(result);
100         assertEquals(ValidationResult.VALID, result.getValidationResult());
101
102         schema.clean();
103
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());
108
109         assertFalse(schema.hashCode() == 0);
110
111         assertTrue(schema.equals(schema));
112         assertTrue(schema.equals(clonedSchema));
113         assertFalse(schema.equals(null));
114         assertFalse(schema.equals((Object) "Hello"));
115         assertFalse(schema.equals(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
116         assertFalse(schema.equals(new AxContextSchema(newKey, "Flavour", "Def")));
117         assertFalse(schema.equals(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
118         assertTrue(schema.equals(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));
119
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")));
128
129         final AxContextSchemas schemas = new AxContextSchemas();
130         result = new AxValidationResult();
131         result = schemas.validate(result);
132         assertEquals(ValidationResult.INVALID, result.getValidationResult());
133
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());
139
140         schemas.getSchemasMap().put(newKey, schema);
141         result = new AxValidationResult();
142         result = schemas.validate(result);
143         assertEquals(ValidationResult.VALID, result.getValidationResult());
144
145         schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
146         result = new AxValidationResult();
147         result = schemas.validate(result);
148         assertEquals(ValidationResult.INVALID, result.getValidationResult());
149
150         schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
151         result = new AxValidationResult();
152         result = schemas.validate(result);
153         assertEquals(ValidationResult.VALID, result.getValidationResult());
154
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());
159
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());
164
165         schemas.clean();
166
167         final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
168         assertTrue(clonedSchemas.toString()
169                         .startsWith("AxContextSchemas:(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),"));
170
171         assertFalse(schemas.hashCode() == 0);
172
173         assertTrue(schemas.equals(schemas));
174         assertTrue(schemas.equals(clonedSchemas));
175         assertFalse(schemas.equals(null));
176         assertFalse(schemas.equals((Object) "Hello"));
177         assertFalse(schemas.equals(new AxContextSchemas(new AxArtifactKey())));
178
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())));
184
185         clonedSchemas.get(newKey).setSchemaFlavour("YetAnotherFlavour");
186         assertNotEquals(0, schemas.compareTo(clonedSchemas));
187
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());
192     }
193 }