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