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.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
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;
36 * Test state task references.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class StateTaskReferenceTest {
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()));
51 AxStateTaskReference stRef = new AxStateTaskReference();
53 AxReferenceKey stRefKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "SOName");
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());
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());
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());
76 AxValidationResult result = new AxValidationResult();
77 result = stRef.validate(result);
78 assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
80 stRef.setKey(AxReferenceKey.getNullKey());
81 result = new AxValidationResult();
82 result = stRef.validate(result);
83 assertEquals(ValidationResult.INVALID, result.getValidationResult());
85 stRef.setKey(stRefKey);
86 result = new AxValidationResult();
87 result = stRef.validate(result);
88 assertEquals(ValidationResult.VALID, result.getValidationResult());
90 stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED);
91 result = new AxValidationResult();
92 result = stRef.validate(result);
93 assertEquals(ValidationResult.INVALID, result.getValidationResult());
95 stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC);
96 result = new AxValidationResult();
97 result = stRef.validate(result);
98 assertEquals(ValidationResult.VALID, result.getValidationResult());
100 stRef.setOutput(AxReferenceKey.getNullKey());
101 result = new AxValidationResult();
102 result = stRef.validate(result);
103 assertEquals(ValidationResult.INVALID, result.getValidationResult());
105 stRef.setOutput(soKey);
106 result = new AxValidationResult();
107 result = stRef.validate(result);
108 assertEquals(ValidationResult.VALID, result.getValidationResult());
112 AxStateTaskReference clonedStRef = new AxStateTaskReference(stRef);
113 assertEquals("AxStateTaskReference:(stateKey=AxReferenceKey:(par", clonedStRef.toString().substring(0, 50));
115 assertNotEquals(0, stRef.hashCode());
117 // disabling sonar because this code tests the equals() method
118 assertEquals(stRef, stRef); // NOSONAR
119 assertEquals(stRef, clonedStRef);
120 assertNotNull(stRef);
121 assertNotEquals(stRef, "Hello");
122 assertNotEquals(stRef, new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC,
124 assertNotEquals(stRef, new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey));
125 assertNotEquals(stRef, new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey()));
126 assertEquals(stRef, new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey));
128 assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(),
129 AxStateTaskOutputType.UNDEFINED, new AxReferenceKey()));
131 assertEquals(0, stRef.compareTo(stRef));
132 assertEquals(0, stRef.compareTo(clonedStRef));
133 assertNotEquals(0, stRef.compareTo(new AxArtifactKey()));
134 assertNotEquals(0, stRef.compareTo(null));
135 assertNotEquals(0, stRef.compareTo(
136 new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC, soKey)));
137 assertNotEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey)));
138 assertNotEquals(0, stRef.compareTo(
139 new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey())));
140 assertEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey)));
142 assertNotNull(stRef.getKeys());