2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.eventmodel.concepts;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class FieldTest {
44 public void testField() {
45 assertNotNull(new AxField());
46 assertNotNull(new AxField(new AxReferenceKey()));
47 assertNotNull(new AxField(new AxReferenceKey(), new AxArtifactKey()));
48 assertNotNull(new AxField(new AxReferenceKey(), new AxArtifactKey(), false));
49 assertNotNull(new AxField("LocalName", new AxArtifactKey(), false));
50 assertNotNull(new AxField("LocalName", new AxArtifactKey()));
51 assertNotNull(new AxField("LocalName", new AxArtifactKey(), false));
53 assertNotNull(new AxInputField());
54 assertNotNull(new AxInputField(new AxReferenceKey()));
55 assertNotNull(new AxInputField(new AxReferenceKey(), new AxArtifactKey()));
56 assertNotNull(new AxInputField(new AxReferenceKey(), new AxArtifactKey(), true));
57 assertNotNull(new AxInputField("LocalName", new AxArtifactKey()));
58 assertNotNull(new AxInputField(new AxInputField()));
60 assertNotNull(new AxOutputField());
61 assertNotNull(new AxOutputField(new AxReferenceKey()));
62 assertNotNull(new AxOutputField(new AxReferenceKey(), new AxArtifactKey()));
63 assertNotNull(new AxOutputField(new AxReferenceKey(), new AxArtifactKey(), false));
64 assertNotNull(new AxOutputField("LocalName", new AxArtifactKey()));
65 assertNotNull(new AxOutputField(new AxOutputField()));
67 final AxField field = new AxField();
69 final AxReferenceKey fieldKey = new AxReferenceKey("FieldName", "0.0.1", "PLN", "LN");
70 field.setKey(fieldKey);
71 assertEquals("FieldName:0.0.1:PLN:LN", field.getKey().getId());
72 assertEquals("FieldName:0.0.1:PLN:LN", field.getKeys().get(0).getId());
74 final AxArtifactKey schemaKey = new AxArtifactKey("SchemaName", "0.0.1");
75 field.setSchema(schemaKey);
76 assertEquals("SchemaName:0.0.1", field.getSchema().getId());
78 assertEquals(false, field.getOptional());
79 field.setOptional(true);
80 assertEquals(true, field.getOptional());
82 AxValidationResult result = new AxValidationResult();
83 result = field.validate(result);
84 assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
86 field.setKey(AxReferenceKey.getNullKey());
87 result = new AxValidationResult();
88 result = field.validate(result);
89 assertEquals(ValidationResult.INVALID, result.getValidationResult());
91 field.setKey(fieldKey);
92 result = new AxValidationResult();
93 result = field.validate(result);
94 assertEquals(ValidationResult.VALID, result.getValidationResult());
96 field.setSchema(AxArtifactKey.getNullKey());
97 result = new AxValidationResult();
98 result = field.validate(result);
99 assertEquals(ValidationResult.INVALID, result.getValidationResult());
101 field.setSchema(schemaKey);
102 result = new AxValidationResult();
103 result = field.validate(result);
104 assertEquals(ValidationResult.VALID, result.getValidationResult());
108 final AxField clonedField = new AxField(field);
109 assertEquals("AxField:(key=AxReferenceKey:(parentKeyName=FieldName,parentKeyVersion=0.0.1,"
110 + "parentLocalName=PLN,localName=LN),fieldSchemaKey="
111 + "AxArtifactKey:(name=SchemaName,version=0.0.1),optional=true)", clonedField.toString());
113 assertFalse(field.hashCode() == 0);
115 assertTrue(field.equals(field));
116 assertTrue(field.equals(clonedField));
117 assertFalse(field.equals(null));
118 assertFalse(field.equals((Object) "Hello"));
119 assertFalse(field.equals(new AxField(AxReferenceKey.getNullKey(), AxArtifactKey.getNullKey(), false)));
120 assertFalse(field.equals(new AxField(fieldKey, AxArtifactKey.getNullKey(), false)));
121 assertFalse(field.equals(new AxField(fieldKey, schemaKey, false)));
122 assertTrue(field.equals(new AxField(fieldKey, schemaKey, true)));
124 assertEquals(0, field.compareTo(field));
125 assertEquals(0, field.compareTo(clonedField));
126 assertNotEquals(0, field.compareTo(new AxArtifactKey()));
127 assertNotEquals(0, field.compareTo(null));
129 field.compareTo(new AxField(AxReferenceKey.getNullKey(), AxArtifactKey.getNullKey(), false)));
130 assertNotEquals(0, field.compareTo(new AxField(fieldKey, AxArtifactKey.getNullKey(), false)));
131 assertNotEquals(0, field.compareTo(new AxField(fieldKey, schemaKey, false)));
132 assertEquals(0, field.compareTo(new AxField(fieldKey, schemaKey, true)));
134 assertNotNull(field.getKeys());