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