31012c68cb0dfd9ebfa8245d40052a1617672f8a
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-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.policymodel.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
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
34
35 /**
36  * Test state task references.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class StateTaskReferenceTest {
41
42     @Test
43     public void testStateTaskReference() {
44         assertNotNull(new AxStateTaskReference());
45         assertNotNull(new AxStateTaskReference(new AxReferenceKey()));
46         assertNotNull(new AxStateTaskReference(new AxReferenceKey(), AxStateTaskOutputType.UNDEFINED,
47                         new AxReferenceKey()));
48         assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(),
49                         AxStateTaskOutputType.UNDEFINED, new AxReferenceKey()));
50
51         AxStateTaskReference stRef = new AxStateTaskReference();
52
53         AxReferenceKey stRefKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "SOName");
54
55         assertThatThrownBy(() -> stRef.setKey(null))
56             .hasMessage("key may not be null");
57         stRef.setKey(stRefKey);
58         assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKey().getId());
59         assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKeys().get(0).getId());
60
61         assertThatThrownBy(() -> stRef.setStateTaskOutputType(null))
62             .hasMessage("outputType may not be null");
63         stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED);
64         assertEquals(AxStateTaskOutputType.UNDEFINED, stRef.getStateTaskOutputType());
65         stRef.setStateTaskOutputType(AxStateTaskOutputType.DIRECT);
66         assertEquals(AxStateTaskOutputType.DIRECT, stRef.getStateTaskOutputType());
67         stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC);
68         assertEquals(AxStateTaskOutputType.LOGIC, stRef.getStateTaskOutputType());
69
70         assertThatThrownBy(() -> stRef.setOutput(null))
71             .hasMessage("output may not be null");
72         AxReferenceKey soKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "STRef0");
73         stRef.setOutput(soKey);
74         assertEquals(soKey, stRef.getOutput());
75
76         AxValidationResult result = new AxValidationResult();
77         result = stRef.validate(result);
78         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
79
80         stRef.setKey(AxReferenceKey.getNullKey());
81         result = new AxValidationResult();
82         result = stRef.validate(result);
83         assertEquals(ValidationResult.INVALID, result.getValidationResult());
84
85         stRef.setKey(stRefKey);
86         result = new AxValidationResult();
87         result = stRef.validate(result);
88         assertEquals(ValidationResult.VALID, result.getValidationResult());
89
90         stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED);
91         result = new AxValidationResult();
92         result = stRef.validate(result);
93         assertEquals(ValidationResult.INVALID, result.getValidationResult());
94
95         stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC);
96         result = new AxValidationResult();
97         result = stRef.validate(result);
98         assertEquals(ValidationResult.VALID, result.getValidationResult());
99
100         stRef.setOutput(AxReferenceKey.getNullKey());
101         result = new AxValidationResult();
102         result = stRef.validate(result);
103         assertEquals(ValidationResult.INVALID, result.getValidationResult());
104
105         stRef.setOutput(soKey);
106         result = new AxValidationResult();
107         result = stRef.validate(result);
108         assertEquals(ValidationResult.VALID, result.getValidationResult());
109
110         stRef.clean();
111
112         AxStateTaskReference clonedStRef = new AxStateTaskReference(stRef);
113         assertEquals("AxStateTaskReference:(stateKey=AxReferenceKey:(par", clonedStRef.toString().substring(0, 50));
114
115         assertNotEquals(0, stRef.hashCode());
116
117         assertEquals(stRef, stRef);
118         assertEquals(stRef, clonedStRef);
119         assertNotNull(stRef);
120         assertNotEquals(stRef, (Object) "Hello");
121         assertNotEquals(stRef, new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC,
122                         soKey));
123         assertNotEquals(stRef, new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey));
124         assertNotEquals(stRef, new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey()));
125         assertEquals(stRef, new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey));
126
127         assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(),
128                         AxStateTaskOutputType.UNDEFINED, new AxReferenceKey()));
129
130         assertEquals(0, stRef.compareTo(stRef));
131         assertEquals(0, stRef.compareTo(clonedStRef));
132         assertNotEquals(0, stRef.compareTo(new AxArtifactKey()));
133         assertNotEquals(0, stRef.compareTo(null));
134         assertNotEquals(0, stRef.compareTo(
135                         new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC, soKey)));
136         assertNotEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey)));
137         assertNotEquals(0, stRef.compareTo(
138                         new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey())));
139         assertEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey)));
140
141         assertNotNull(stRef.getKeys());
142     }
143 }