77ce93d4c25edfed020e40cddaac741270d9b380
[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.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
36
37 /**
38  * Test state outputs.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class StateOutputTest {
43
44     @Test
45     public void testStateOutput() {
46         assertNotNull(new AxStateOutput());
47         assertNotNull(new AxStateOutput(new AxReferenceKey()));
48         assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxReferenceKey(), new AxArtifactKey()));
49         assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxArtifactKey(), new AxReferenceKey()));
50
51         final AxStateOutput so = new AxStateOutput();
52
53         final AxReferenceKey soKey = new AxReferenceKey("SOStateParent", "0.0.1", "SOState", "SOName");
54         final AxReferenceKey nsKey = new AxReferenceKey("SOStateParent", "0.0.1", "NotUsed", "NextStateName");
55         final AxArtifactKey eKey = new AxArtifactKey("EventName", "0.0.1");
56
57         assertThatThrownBy(() -> so.setKey(null))
58             .hasMessage("key may not be null");
59         so.setKey(soKey);
60         assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKey().getId());
61         assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKeys().get(0).getId());
62
63         assertThatThrownBy(() -> so.setNextState(null))
64             .hasMessage("nextState may not be null");
65         so.setNextState(nsKey);
66         assertEquals(nsKey, so.getNextState());
67
68         assertThatThrownBy(() -> so.setOutgoingEvent(null))
69             .hasMessage("outgoingEvent may not be null");
70         so.setOutgoingEvent(eKey);
71         assertEquals(eKey, so.getOutgingEvent());
72
73         AxValidationResult result = new AxValidationResult();
74         result = so.validate(result);
75         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
76
77         so.setKey(AxReferenceKey.getNullKey());
78         result = new AxValidationResult();
79         result = so.validate(result);
80         assertEquals(ValidationResult.INVALID, result.getValidationResult());
81
82         so.setKey(soKey);
83         result = new AxValidationResult();
84         result = so.validate(result);
85         assertEquals(ValidationResult.VALID, result.getValidationResult());
86
87         so.setOutgoingEvent(AxArtifactKey.getNullKey());
88         result = new AxValidationResult();
89         result = so.validate(result);
90         assertEquals(ValidationResult.INVALID, result.getValidationResult());
91
92         so.setOutgoingEvent(eKey);
93         result = new AxValidationResult();
94         result = so.validate(result);
95         assertEquals(ValidationResult.VALID, result.getValidationResult());
96
97         so.clean();
98
99         final AxStateOutput clonedPar = new AxStateOutput(so);
100         assertEquals("AxStateOutput:(stateKey=AxReferenceKey:(parentKeyN", clonedPar.toString().substring(0, 50));
101
102         assertFalse(so.hashCode() == 0);
103
104         assertTrue(so.equals(so));
105         assertTrue(so.equals(clonedPar));
106         assertFalse(so.equals(null));
107         assertFalse(so.equals((Object) "Hello"));
108         assertFalse(so.equals(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey)));
109         assertFalse(so.equals(new AxStateOutput(soKey, new AxArtifactKey(), nsKey)));
110         assertFalse(so.equals(new AxStateOutput(soKey, eKey, new AxReferenceKey())));
111         assertTrue(so.equals(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 }