eeaf19e0795d0e48213b848a4c1fa730a80b0c35
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.policymodel.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
35 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType;
36 import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference;
37
38 /**
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class TestStateTaskReference {
42
43     @Test
44     public void testStateTaskReference() {
45         assertNotNull(new AxStateTaskReference());
46         assertNotNull(new AxStateTaskReference(new AxReferenceKey()));
47         assertNotNull(
48                 new AxStateTaskReference(new AxReferenceKey(), AxStateTaskOutputType.UNDEFINED, new AxReferenceKey()));
49         assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(),
50                 AxStateTaskOutputType.UNDEFINED, new AxReferenceKey()));
51
52         AxStateTaskReference stRef = new AxStateTaskReference();
53
54         AxReferenceKey stRefKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "SOName");
55         AxReferenceKey soKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "STRef0");
56
57         try {
58             stRef.setKey(null);
59             fail("test should throw an exception here");
60         } catch (Exception e) {
61             assertEquals("key may not be null", e.getMessage());
62         }
63
64         stRef.setKey(stRefKey);
65         assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKey().getId());
66         assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKeys().get(0).getId());
67
68         try {
69             stRef.setStateTaskOutputType(null);
70             fail("test should throw an exception here");
71         } catch (Exception e) {
72             assertEquals("outputType may not be null", e.getMessage());
73         }
74
75         stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED);
76         assertEquals(AxStateTaskOutputType.UNDEFINED, stRef.getStateTaskOutputType());
77         stRef.setStateTaskOutputType(AxStateTaskOutputType.DIRECT);
78         assertEquals(AxStateTaskOutputType.DIRECT, stRef.getStateTaskOutputType());
79         stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC);
80         assertEquals(AxStateTaskOutputType.LOGIC, stRef.getStateTaskOutputType());
81
82         try {
83             stRef.setOutput(null);
84             fail("test should throw an exception here");
85         } catch (Exception e) {
86             assertEquals("output may not be null", e.getMessage());
87         }
88
89         stRef.setOutput(soKey);
90         assertEquals(soKey, stRef.getOutput());
91
92         AxValidationResult result = new AxValidationResult();
93         result = stRef.validate(result);
94         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
95
96         stRef.setKey(AxReferenceKey.getNullKey());
97         result = new AxValidationResult();
98         result = stRef.validate(result);
99         assertEquals(ValidationResult.INVALID, result.getValidationResult());
100
101         stRef.setKey(stRefKey);
102         result = new AxValidationResult();
103         result = stRef.validate(result);
104         assertEquals(ValidationResult.VALID, result.getValidationResult());
105
106         stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED);
107         result = new AxValidationResult();
108         result = stRef.validate(result);
109         assertEquals(ValidationResult.INVALID, result.getValidationResult());
110
111         stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC);
112         result = new AxValidationResult();
113         result = stRef.validate(result);
114         assertEquals(ValidationResult.VALID, result.getValidationResult());
115
116         stRef.setOutput(AxReferenceKey.getNullKey());
117         result = new AxValidationResult();
118         result = stRef.validate(result);
119         assertEquals(ValidationResult.INVALID, result.getValidationResult());
120
121         stRef.setOutput(soKey);
122         result = new AxValidationResult();
123         result = stRef.validate(result);
124         assertEquals(ValidationResult.VALID, result.getValidationResult());
125
126         stRef.clean();
127
128         AxStateTaskReference clonedStRef = new AxStateTaskReference(stRef);
129         assertEquals("AxStateTaskReference:(stateKey=AxReferenceKey:(par", clonedStRef.toString().substring(0, 50));
130
131         assertFalse(stRef.hashCode() == 0);
132
133         assertTrue(stRef.equals(stRef));
134         assertTrue(stRef.equals(clonedStRef));
135         assertFalse(stRef.equals(null));
136         assertFalse(stRef.equals("Hello"));
137         assertFalse(stRef
138                 .equals(new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC, soKey)));
139         assertFalse(stRef.equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey)));
140         assertFalse(
141                 stRef.equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey())));
142         assertTrue(stRef.equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey)));
143
144         assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(),
145                 AxStateTaskOutputType.UNDEFINED, new AxReferenceKey()));
146
147         assertEquals(0, stRef.compareTo(stRef));
148         assertEquals(0, stRef.compareTo(clonedStRef));
149         assertNotEquals(0, stRef.compareTo(new AxArtifactKey()));
150         assertNotEquals(0, stRef.compareTo(null));
151         assertNotEquals(0, stRef
152                 .compareTo(new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC, soKey)));
153         assertNotEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey)));
154         assertNotEquals(0,
155                 stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey())));
156         assertEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey)));
157
158         assertNotNull(stRef.getKeys());
159     }
160 }