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