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
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.policymodel.concepts;
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;
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;
38 * Test state task references.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class StateTaskReferenceTest {
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()));
53 AxStateTaskReference stRef = new AxStateTaskReference();
55 AxReferenceKey stRefKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "SOName");
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());
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());
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());
78 AxValidationResult result = new AxValidationResult();
79 result = stRef.validate(result);
80 assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
82 stRef.setKey(AxReferenceKey.getNullKey());
83 result = new AxValidationResult();
84 result = stRef.validate(result);
85 assertEquals(ValidationResult.INVALID, result.getValidationResult());
87 stRef.setKey(stRefKey);
88 result = new AxValidationResult();
89 result = stRef.validate(result);
90 assertEquals(ValidationResult.VALID, result.getValidationResult());
92 stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED);
93 result = new AxValidationResult();
94 result = stRef.validate(result);
95 assertEquals(ValidationResult.INVALID, result.getValidationResult());
97 stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC);
98 result = new AxValidationResult();
99 result = stRef.validate(result);
100 assertEquals(ValidationResult.VALID, result.getValidationResult());
102 stRef.setOutput(AxReferenceKey.getNullKey());
103 result = new AxValidationResult();
104 result = stRef.validate(result);
105 assertEquals(ValidationResult.INVALID, result.getValidationResult());
107 stRef.setOutput(soKey);
108 result = new AxValidationResult();
109 result = stRef.validate(result);
110 assertEquals(ValidationResult.VALID, result.getValidationResult());
114 AxStateTaskReference clonedStRef = new AxStateTaskReference(stRef);
115 assertEquals("AxStateTaskReference:(stateKey=AxReferenceKey:(par", clonedStRef.toString().substring(0, 50));
117 assertFalse(stRef.hashCode() == 0);
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)));
127 .equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey())));
128 assertTrue(stRef.equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey)));
130 assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(),
131 AxStateTaskOutputType.UNDEFINED, new AxReferenceKey()));
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)));
144 assertNotNull(stRef.getKeys());