049783a7fad11fd3689b274c8a1ab435e0ce14b9
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.policymodel.concepts;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
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
36 /**
37  * Test state outputs.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class StateOutputTest {
42
43     @Test
44     public void testStateOutput() {
45         assertNotNull(new AxStateOutput());
46         assertNotNull(new AxStateOutput(new AxReferenceKey()));
47         assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxReferenceKey(), new AxArtifactKey()));
48         assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxArtifactKey(), new AxReferenceKey()));
49
50         final AxStateOutput so = new AxStateOutput();
51
52         final AxReferenceKey soKey = new AxReferenceKey("SOStateParent", "0.0.1", "SOState", "SOName");
53         final AxReferenceKey nsKey = new AxReferenceKey("SOStateParent", "0.0.1", "NotUsed", "NextStateName");
54         final AxArtifactKey eKey = new AxArtifactKey("EventName", "0.0.1");
55
56         assertThatThrownBy(() -> so.setKey(null))
57             .hasMessage("key is marked non-null but is null");
58         so.setKey(soKey);
59         assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKey().getId());
60         assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKeys().get(0).getId());
61
62         assertThatThrownBy(() -> so.setNextState(null))
63             .hasMessage("nextState is marked non-null but is null");
64         so.setNextState(nsKey);
65         assertEquals(nsKey, so.getNextState());
66
67         assertThatThrownBy(() -> so.setOutgoingEvent(null))
68             .hasMessage("outgoingEvent is marked non-null but is null");
69         so.setOutgoingEvent(eKey);
70         assertEquals(eKey, so.getOutgoingEvent());
71
72         AxValidationResult result = new AxValidationResult();
73         result = so.validate(result);
74         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
75
76         so.setKey(AxReferenceKey.getNullKey());
77         result = new AxValidationResult();
78         result = so.validate(result);
79         assertEquals(ValidationResult.INVALID, result.getValidationResult());
80
81         so.setKey(soKey);
82         result = new AxValidationResult();
83         result = so.validate(result);
84         assertEquals(ValidationResult.VALID, result.getValidationResult());
85
86         so.setOutgoingEvent(AxArtifactKey.getNullKey());
87         result = new AxValidationResult();
88         result = so.validate(result);
89         assertEquals(ValidationResult.INVALID, result.getValidationResult());
90
91         so.setOutgoingEvent(eKey);
92         result = new AxValidationResult();
93         result = so.validate(result);
94         assertEquals(ValidationResult.VALID, result.getValidationResult());
95
96         so.clean();
97
98         final AxStateOutput clonedPar = new AxStateOutput(so);
99         assertEquals("AxStateOutput:(stateKey=AxReferenceKey:(parentKeyN", clonedPar.toString().substring(0, 50));
100
101         assertNotEquals(0, so.hashCode());
102
103         // disabling sonar because this code tests the equals() method
104         assertEquals(so, so); // NOSONAR
105         assertEquals(so, clonedPar);
106         assertNotNull(so);
107         assertNotEquals(so, (Object) "Hello");
108         assertNotEquals(so, new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey));
109         assertNotEquals(so, new AxStateOutput(soKey, new AxArtifactKey(), nsKey));
110         assertNotEquals(so, new AxStateOutput(soKey, eKey, new AxReferenceKey()));
111         assertEquals(so, new AxStateOutput(soKey, eKey, nsKey));
112
113         assertEquals(0, so.compareTo(so));
114         assertEquals(0, so.compareTo(clonedPar));
115         assertNotEquals(0, so.compareTo(new AxArtifactKey()));
116         assertNotEquals(0, so.compareTo(null));
117         assertNotEquals(0, so.compareTo(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey)));
118         assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, new AxArtifactKey(), nsKey)));
119         assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, new AxReferenceKey())));
120         assertEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, nsKey)));
121
122         assertNotNull(so.getKeys());
123     }
124 }