fe3c9e6b7772935421e9371ef61aed93b96c19be
[policy/apex-pdp.git] / model / event-model / src / test / java / org / onap / policy / apex / model / eventmodel / concepts / FieldTest.java
1 /*-
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
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.eventmodel.concepts;
23
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;
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.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
35 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
36 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
37 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
38
39 /**
40  * Test fields.
41  * 
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class FieldTest {
45
46     @Test
47     public void testField() {
48         assertNotNull(new AxField());
49         assertNotNull(new AxField(new AxReferenceKey()));
50         assertNotNull(new AxField(new AxReferenceKey(), new AxArtifactKey()));
51         assertNotNull(new AxField(new AxReferenceKey(), new AxArtifactKey(), false));
52         assertNotNull(new AxField("LocalName", new AxArtifactKey(), false));
53         assertNotNull(new AxField("LocalName", new AxArtifactKey()));
54         assertNotNull(new AxField("LocalName", new AxArtifactKey(), false));
55
56         assertNotNull(new AxInputField());
57         assertNotNull(new AxInputField(new AxReferenceKey()));
58         assertNotNull(new AxInputField(new AxReferenceKey(), new AxArtifactKey()));
59         assertNotNull(new AxInputField(new AxReferenceKey(), new AxArtifactKey(), true));
60         assertNotNull(new AxInputField("LocalName", new AxArtifactKey()));
61         assertNotNull(new AxInputField(new AxInputField()));
62
63         assertNotNull(new AxOutputField());
64         assertNotNull(new AxOutputField(new AxReferenceKey()));
65         assertNotNull(new AxOutputField(new AxReferenceKey(), new AxArtifactKey()));
66         assertNotNull(new AxOutputField(new AxReferenceKey(), new AxArtifactKey(), false));
67         assertNotNull(new AxOutputField("LocalName", new AxArtifactKey()));
68         assertNotNull(new AxOutputField(new AxOutputField()));
69
70         final AxField field = new AxField();
71
72         final AxReferenceKey fieldKey = new AxReferenceKey("FieldName", "0.0.1", "PLN", "LN");
73         field.setKey(fieldKey);
74         assertEquals("FieldName:0.0.1:PLN:LN", field.getKey().getId());
75         assertEquals("FieldName:0.0.1:PLN:LN", field.getKeys().get(0).getId());
76
77         final AxArtifactKey schemaKey = new AxArtifactKey("SchemaName", "0.0.1");
78         field.setSchema(schemaKey);
79         assertEquals("SchemaName:0.0.1", field.getSchema().getId());
80
81         assertEquals(false, field.getOptional());
82         field.setOptional(true);
83         assertEquals(true, field.getOptional());
84
85         AxValidationResult result = new AxValidationResult();
86         result = field.validate(result);
87         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
88
89         field.setKey(AxReferenceKey.getNullKey());
90         result = new AxValidationResult();
91         result = field.validate(result);
92         assertEquals(ValidationResult.INVALID, result.getValidationResult());
93
94         field.setKey(fieldKey);
95         result = new AxValidationResult();
96         result = field.validate(result);
97         assertEquals(ValidationResult.VALID, result.getValidationResult());
98
99         field.setSchema(AxArtifactKey.getNullKey());
100         result = new AxValidationResult();
101         result = field.validate(result);
102         assertEquals(ValidationResult.INVALID, result.getValidationResult());
103
104         field.setSchema(schemaKey);
105         result = new AxValidationResult();
106         result = field.validate(result);
107         assertEquals(ValidationResult.VALID, result.getValidationResult());
108
109         field.clean();
110
111         final AxField clonedField = new AxField(field);
112         assertEquals("AxField:(key=AxReferenceKey:(parentKeyName=FieldName,parentKeyVersion=0.0.1,"
113                         + "parentLocalName=PLN,localName=LN),fieldSchemaKey="
114                         + "AxArtifactKey:(name=SchemaName,version=0.0.1),optional=true)", clonedField.toString());
115
116         assertFalse(field.hashCode() == 0);
117
118         assertTrue(field.equals(field));
119         assertTrue(field.equals(clonedField));
120         assertFalse(field.equals(null));
121         assertFalse(field.equals((Object)"Hello"));
122         assertFalse(field.equals(new AxField(AxReferenceKey.getNullKey(), AxArtifactKey.getNullKey(), false)));
123         assertFalse(field.equals(new AxField(fieldKey, AxArtifactKey.getNullKey(), false)));
124         assertFalse(field.equals(new AxField(fieldKey, schemaKey, false)));
125         assertTrue(field.equals(new AxField(fieldKey, schemaKey, true)));
126
127         assertEquals(0, field.compareTo(field));
128         assertEquals(0, field.compareTo(clonedField));
129         assertNotEquals(0, field.compareTo(new AxArtifactKey()));
130         assertNotEquals(0, field.compareTo(null));
131         assertNotEquals(0,
132                         field.compareTo(new AxField(AxReferenceKey.getNullKey(), AxArtifactKey.getNullKey(), false)));
133         assertNotEquals(0, field.compareTo(new AxField(fieldKey, AxArtifactKey.getNullKey(), false)));
134         assertNotEquals(0, field.compareTo(new AxField(fieldKey, schemaKey, false)));
135         assertEquals(0, field.compareTo(new AxField(fieldKey, schemaKey, true)));
136
137         assertNotNull(field.getKeys());
138     }
139 }