7e243f5678e6324c5d17d3c2bd9b68b5ad00c895
[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  * ================================================================================
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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
34
35 /**
36  * Test state outputs.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class StateOutputTest {
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         assertThatThrownBy(() -> so.setKey(null))
56             .hasMessage("key may not be null");
57         so.setKey(soKey);
58         assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKey().getId());
59         assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKeys().get(0).getId());
60
61         assertThatThrownBy(() -> so.setNextState(null))
62             .hasMessage("nextState may not be null");
63         so.setNextState(nsKey);
64         assertEquals(nsKey, so.getNextState());
65
66         assertThatThrownBy(() -> so.setOutgoingEvent(null))
67             .hasMessage("outgoingEvent may not be null");
68         so.setOutgoingEvent(eKey);
69         assertEquals(eKey, so.getOutgingEvent());
70
71         AxValidationResult result = new AxValidationResult();
72         result = so.validate(result);
73         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
74
75         so.setKey(AxReferenceKey.getNullKey());
76         result = new AxValidationResult();
77         result = so.validate(result);
78         assertEquals(ValidationResult.INVALID, result.getValidationResult());
79
80         so.setKey(soKey);
81         result = new AxValidationResult();
82         result = so.validate(result);
83         assertEquals(ValidationResult.VALID, result.getValidationResult());
84
85         so.setOutgoingEvent(AxArtifactKey.getNullKey());
86         result = new AxValidationResult();
87         result = so.validate(result);
88         assertEquals(ValidationResult.INVALID, result.getValidationResult());
89
90         so.setOutgoingEvent(eKey);
91         result = new AxValidationResult();
92         result = so.validate(result);
93         assertEquals(ValidationResult.VALID, result.getValidationResult());
94
95         so.clean();
96
97         final AxStateOutput clonedPar = new AxStateOutput(so);
98         assertEquals("AxStateOutput:(stateKey=AxReferenceKey:(parentKeyN", clonedPar.toString().substring(0, 50));
99
100         assertNotEquals(0, so.hashCode());
101
102         // disabling sonar because this code tests the equals() method
103         assertEquals(so, so); // NOSONAR
104         assertEquals(so, clonedPar);
105         assertNotNull(so);
106         assertNotEquals(so, "Hello");
107         assertNotEquals(so, new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey));
108         assertNotEquals(so, new AxStateOutput(soKey, new AxArtifactKey(), nsKey));
109         assertNotEquals(so, new AxStateOutput(soKey, eKey, new AxReferenceKey()));
110         assertEquals(so, new AxStateOutput(soKey, eKey, nsKey));
111
112         assertEquals(0, so.compareTo(so));
113         assertEquals(0, so.compareTo(clonedPar));
114         assertNotEquals(0, so.compareTo(new AxArtifactKey()));
115         assertNotEquals(0, so.compareTo(null));
116         assertNotEquals(0, so.compareTo(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey)));
117         assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, new AxArtifactKey(), nsKey)));
118         assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, new AxReferenceKey())));
119         assertEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, nsKey)));
120
121         assertNotNull(so.getKeys());
122     }
123 }