Remove try/catch blocks with assertj - apex-pdp
[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  *  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         assertEquals(schema, schema);
107         assertEquals(schema, clonedSchema);
108         assertNotNull(schema);
109         assertNotEquals(schema, (Object) "Hello");
110         assertNotEquals(schema, new AxContextSchema(new AxArtifactKey(), "Flavour", "Def"));
111         assertNotEquals(schema, new AxContextSchema(newKey, "Flavour", "Def"));
112         assertNotEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "Def"));
113         assertEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition"));
114
115         assertEquals(0, schema.compareTo(schema));
116         assertEquals(0, schema.compareTo(clonedSchema));
117         assertNotEquals(0, schema.compareTo(null));
118         assertNotEquals(0, schema.compareTo(new AxArtifactKey()));
119         assertNotEquals(0, schema.compareTo(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
120         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "Flavour", "Def")));
121         assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
122         assertEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));
123
124         final AxContextSchemas schemas = new AxContextSchemas();
125         result = new AxValidationResult();
126         result = schemas.validate(result);
127         assertEquals(ValidationResult.INVALID, result.getValidationResult());
128
129         // Still invalid, no schemas in schema map
130         schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
131         result = new AxValidationResult();
132         result = schemas.validate(result);
133         assertEquals(ValidationResult.INVALID, result.getValidationResult());
134
135         schemas.getSchemasMap().put(newKey, schema);
136         result = new AxValidationResult();
137         result = schemas.validate(result);
138         assertEquals(ValidationResult.VALID, result.getValidationResult());
139
140         schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
141         result = new AxValidationResult();
142         result = schemas.validate(result);
143         assertEquals(ValidationResult.INVALID, result.getValidationResult());
144
145         schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
146         result = new AxValidationResult();
147         result = schemas.validate(result);
148         assertEquals(ValidationResult.VALID, result.getValidationResult());
149
150         schemas.getSchemasMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
151         result = new AxValidationResult();
152         result = schemas.validate(result);
153         assertEquals(ValidationResult.INVALID, result.getValidationResult());
154
155         schemas.getSchemasMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
156         result = new AxValidationResult();
157         result = schemas.validate(result);
158         assertEquals(ValidationResult.VALID, result.getValidationResult());
159
160         schemas.clean();
161
162         final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
163         assertTrue(clonedSchemas.toString()
164                         .startsWith("AxContextSchemas:(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),"));
165
166         assertNotEquals(0, schemas.hashCode());
167
168         assertEquals(schemas, schemas);
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 }