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