5911d41be0de51205b6848ea2a695272d9fd1b33
[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.AxStateOutput;
36
37 /**
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class TestStateOutput {
41
42     @Test
43     public void testStateOutput() {
44         assertNotNull(new AxStateOutput());
45         assertNotNull(new AxStateOutput(new AxReferenceKey()));
46         assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxReferenceKey(), new AxArtifactKey()));
47         assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxArtifactKey(), new AxReferenceKey()));
48
49         final AxStateOutput so = new AxStateOutput();
50
51         final AxReferenceKey soKey = new AxReferenceKey("SOStateParent", "0.0.1", "SOState", "SOName");
52         final AxReferenceKey nsKey = new AxReferenceKey("SOStateParent", "0.0.1", "NotUsed", "NextStateName");
53         final AxArtifactKey eKey = new AxArtifactKey("EventName", "0.0.1");
54
55         try {
56             so.setKey(null);
57             fail("test should throw an exception here");
58         } catch (final Exception e) {
59             assertEquals("key may not be null", e.getMessage());
60         }
61
62         so.setKey(soKey);
63         assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKey().getID());
64         assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKeys().get(0).getID());
65
66         try {
67             so.setNextState(null);
68             fail("test should throw an exception here");
69         } catch (final Exception e) {
70             assertEquals("nextState may not be null", e.getMessage());
71         }
72
73         so.setNextState(nsKey);
74         assertEquals(nsKey, so.getNextState());
75
76         try {
77             so.setOutgoingEvent(null);
78             fail("test should throw an exception here");
79         } catch (final Exception e) {
80             assertEquals("outgoingEvent may not be null", e.getMessage());
81         }
82
83         so.setOutgoingEvent(eKey);
84         assertEquals(eKey, so.getOutgingEvent());
85
86         AxValidationResult result = new AxValidationResult();
87         result = so.validate(result);
88         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
89
90         so.setKey(AxReferenceKey.getNullKey());
91         result = new AxValidationResult();
92         result = so.validate(result);
93         assertEquals(ValidationResult.INVALID, result.getValidationResult());
94
95         so.setKey(soKey);
96         result = new AxValidationResult();
97         result = so.validate(result);
98         assertEquals(ValidationResult.VALID, result.getValidationResult());
99
100         so.setOutgoingEvent(AxArtifactKey.getNullKey());
101         result = new AxValidationResult();
102         result = so.validate(result);
103         assertEquals(ValidationResult.INVALID, result.getValidationResult());
104
105         so.setOutgoingEvent(eKey);
106         result = new AxValidationResult();
107         result = so.validate(result);
108         assertEquals(ValidationResult.VALID, result.getValidationResult());
109
110         so.clean();
111
112         final AxStateOutput clonedPar = new AxStateOutput(so);
113         assertEquals("AxStateOutput:(stateKey=AxReferenceKey:(parentKeyN", clonedPar.toString().substring(0, 50));
114
115         assertFalse(so.hashCode() == 0);
116
117         assertTrue(so.equals(so));
118         assertTrue(so.equals(clonedPar));
119         assertFalse(so.equals(null));
120         assertFalse(so.equals("Hello"));
121         assertFalse(so.equals(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey)));
122         assertFalse(so.equals(new AxStateOutput(soKey, new AxArtifactKey(), nsKey)));
123         assertFalse(so.equals(new AxStateOutput(soKey, eKey, new AxReferenceKey())));
124         assertTrue(so.equals(new AxStateOutput(soKey, eKey, nsKey)));
125
126         assertEquals(0, so.compareTo(so));
127         assertEquals(0, so.compareTo(clonedPar));
128         assertNotEquals(0, so.compareTo(new AxArtifactKey()));
129         assertNotEquals(0, so.compareTo(null));
130         assertNotEquals(0, so.compareTo(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey)));
131         assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, new AxArtifactKey(), nsKey)));
132         assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, new AxReferenceKey())));
133         assertEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, nsKey)));
134
135         assertNotNull(so.getKeys());
136     }
137 }