First part of onap rename
[appc.git] / appc-lifecycle-management / state-machine-lib / src / test / java / org / openecomp / appc / statemachine / objects / StateTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.statemachine.objects;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.mockito.internal.util.reflection.Whitebox;
30
31 import java.util.List;
32
33 public class StateTest {
34     private final String STATE_NAME = "Starting";
35     private State state = new State(STATE_NAME);
36
37     @SuppressWarnings("unchecked")
38     @Test
39     public void testConstructor() {
40         State state = new State(STATE_NAME);
41         Assert.assertEquals("Should set stateName",
42                 STATE_NAME, Whitebox.getInternalState(state, "stateName"));
43         Assert.assertEquals("Should set hash code",
44                 STATE_NAME.toLowerCase().hashCode(), (int)Whitebox.getInternalState(state, "hashCode"));
45         List<Transition> transitions = (List<Transition>) Whitebox.getInternalState(state, "transitions");
46         Assert.assertTrue("Should initialized transtiions",
47                 transitions != null && transitions.isEmpty());
48     }
49
50     @Test
51     public void testHashCode() throws Exception {
52         Assert.assertEquals("Should return proper hash code",
53                 STATE_NAME.toLowerCase().hashCode(), state.hashCode());
54     }
55
56     @Test
57     public void testEquals() throws Exception {
58         Assert.assertFalse("should return false for null", state.equals(null));
59         Assert.assertFalse("should return false for object", state.equals(new Event(STATE_NAME)));
60         Assert.assertFalse("should return false for different event",
61                 state.equals(new Event("Another")));
62         Assert.assertTrue("should return true", state.equals(new State(STATE_NAME)));
63         Assert.assertTrue("should return true (lower case)", state.equals(new State(STATE_NAME.toLowerCase())));
64     }
65
66     @Test
67     public void testGetStateName() throws Exception {
68         Assert.assertEquals("Should return STATE_NAME", STATE_NAME, state.getStateName());
69     }
70
71     @SuppressWarnings("unchecked")
72     @Test
73     public void testAddAndGetTransition() throws Exception {
74         Transition transition1 = new Transition(new Event("event1"), new State("state2"));
75         List<Transition> transitions = (List<Transition>) Whitebox.getInternalState(state, "transitions");
76         Assert.assertFalse("should not have transition1", transitions.contains(transition1));
77         state.addTransition(transition1);
78         transitions = (List<Transition>) Whitebox.getInternalState(state, "transitions");
79         Assert.assertTrue("should have added transition1", transitions.contains(transition1));
80         Assert.assertEquals("Should return transitions", transitions, state.getTransitions());
81
82         state.addTransition(null);
83         Assert.assertEquals("Should not change transitions", transitions,
84                 Whitebox.getInternalState(state, "transitions"));
85     }
86
87     @Test
88     public void testToString() throws Exception {
89         Assert.assertEquals("Should return STATE_NAME", STATE_NAME, state.toString());
90     }
91 }