aboutsummaryrefslogtreecommitdiffstats
path: root/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextSchemasTest.java
blob: 55844d9d9fe4091822464fa497c9a4cf026f55ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.model.contextmodel.concepts;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;

/**
 * Context schema tests.
 */
public class ContextSchemasTest {

    @Test
    public void testContextSchemas() {
        assertNotNull(new AxContextSchema());
        assertNotNull(new AxContextSchema(new AxArtifactKey(), "SchemaFlavour", "SchemaDefinition"));

        final AxContextSchema schema = new AxContextSchema(new AxArtifactKey("SchemaName", "0.0.1"), "SchemaFlavour",
                        "SchemaDefinition");
        assertNotNull(schema);

        final AxArtifactKey newKey = new AxArtifactKey("NewSchemaName", "0.0.1");
        schema.setKey(newKey);
        assertEquals("NewSchemaName:0.0.1", schema.getKey().getId());
        assertEquals("NewSchemaName:0.0.1", schema.getKeys().get(0).getId());

        assertThatThrownBy(() -> schema.setSchemaFlavour(""))
            .hasMessage("parameter \"schemaFlavour\": value \"\", "
                            + "does not match regular expression \"[A-Za-z0-9\\-_]+\"");
        schema.setSchemaFlavour("NewSchemaFlavour");
        assertEquals("NewSchemaFlavour", schema.getSchemaFlavour());

        schema.setSchema("NewSchemaDefinition");
        assertEquals("NewSchemaDefinition", schema.getSchema());

        AxValidationResult result = new AxValidationResult();
        result = schema.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        schema.setKey(AxArtifactKey.getNullKey());
        result = new AxValidationResult();
        result = schema.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        schema.setKey(newKey);
        result = new AxValidationResult();
        result = schema.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        schema.setSchemaFlavour("UNDEFINED");
        result = new AxValidationResult();
        result = schema.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        schema.setSchemaFlavour("NewSchemaFlavour");
        result = new AxValidationResult();
        result = schema.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        schema.setSchema("");
        result = new AxValidationResult();
        result = schema.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        schema.setSchema("NewSchemaDefinition");
        result = new AxValidationResult();
        result = schema.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        schema.clean();

        final AxContextSchema clonedSchema = new AxContextSchema(schema);
        assertEquals("AxContextSchema:(key=AxArtifactKey:(name=NewSchemaName,version=0.0.1),"
                        + "schemaFlavour=NewSchemaFlavour,schemaDefinition=NewSchemaDefinition)",
                        clonedSchema.toString());

        assertNotEquals(0, schema.hashCode());

        // disabling sonar because this code tests the equals() method
        assertEquals(schema, schema); // NOSONAR
        assertEquals(schema, clonedSchema);
        assertNotNull(schema);
        assertNotEquals(schema, "Hello");
        assertNotEquals(schema, new AxContextSchema(new AxArtifactKey(), "Flavour", "Def"));
        assertNotEquals(schema, new AxContextSchema(newKey, "Flavour", "Def"));
        assertNotEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "Def"));
        assertEquals(schema, new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition"));

        assertEquals(0, schema.compareTo(schema));
        assertEquals(0, schema.compareTo(clonedSchema));
        assertNotEquals(0, schema.compareTo(null));
        assertNotEquals(0, schema.compareTo(new AxArtifactKey()));
        assertNotEquals(0, schema.compareTo(new AxContextSchema(new AxArtifactKey(), "Flavour", "Def")));
        assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "Flavour", "Def")));
        assertNotEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "Def")));
        assertEquals(0, schema.compareTo(new AxContextSchema(newKey, "NewSchemaFlavour", "NewSchemaDefinition")));

        final AxContextSchemas schemas = new AxContextSchemas();
        result = new AxValidationResult();
        result = schemas.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        // Still invalid, no schemas in schema map
        schemas.setKey(new AxArtifactKey("SchemasKey", "0.0.1"));
        result = new AxValidationResult();
        result = schemas.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        schemas.getSchemasMap().put(newKey, schema);
        result = new AxValidationResult();
        result = schemas.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        schemas.getSchemasMap().put(AxArtifactKey.getNullKey(), null);
        result = new AxValidationResult();
        result = schemas.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        schemas.getSchemasMap().remove(AxArtifactKey.getNullKey());
        result = new AxValidationResult();
        result = schemas.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        schemas.getSchemasMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
        result = new AxValidationResult();
        result = schemas.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        schemas.getSchemasMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
        result = new AxValidationResult();
        result = schemas.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        schemas.clean();

        final AxContextSchemas clonedSchemas = new AxContextSchemas(schemas);
        assertTrue(clonedSchemas.toString()
                        .startsWith("AxContextSchemas:(key=AxArtifactKey:(name=SchemasKey,version=0.0.1),"));

        assertNotEquals(0, schemas.hashCode());

        assertEquals(schemas, clonedSchemas);
        assertNotNull(schemas);
        assertNotEquals(schemas, "Hello");
        assertNotEquals(schemas, new AxContextSchemas(new AxArtifactKey()));

        assertEquals(0, schemas.compareTo(schemas));
        assertEquals(0, schemas.compareTo(clonedSchemas));
        assertNotEquals(0, schemas.compareTo(null));
        assertNotEquals(0, schemas.compareTo(new AxArtifactKey()));
        assertNotEquals(0, schemas.compareTo(new AxContextSchemas(new AxArtifactKey())));

        clonedSchemas.get(newKey).setSchemaFlavour("YetAnotherFlavour");
        assertNotEquals(0, schemas.compareTo(clonedSchemas));

        assertEquals("NewSchemaName", schemas.get("NewSchemaName").getKey().getName());
        assertEquals("NewSchemaName", schemas.get("NewSchemaName", "0.0.1").getKey().getName());
        assertEquals(1, schemas.getAll("NewSchemaName", "0.0.1").size());
        assertEquals(0, schemas.getAll("NonExistantSchemaName").size());
    }
}