Update license header in appc lifecycle and metric
[appc.git] / appc-lifecycle-management / state-machine-lib / src / test / java / org / onap / appc / statemachine / objects / StateTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.statemachine.objects;
25
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.mockito.internal.util.reflection.Whitebox;
29
30 import java.util.List;
31
32 public class StateTest {
33     private final String STATE_NAME = "Starting";
34     private State state = new State(STATE_NAME);
35
36     @SuppressWarnings("unchecked")
37     @Test
38     public void testConstructor() {
39         State state = new State(STATE_NAME);
40         Assert.assertEquals("Should set stateName",
41                 STATE_NAME, Whitebox.getInternalState(state, "stateName"));
42         Assert.assertEquals("Should set hash code",
43                 STATE_NAME.toLowerCase().hashCode(), (int)Whitebox.getInternalState(state, "hashCode"));
44         List<Transition> transitions = (List<Transition>) Whitebox.getInternalState(state, "transitions");
45         Assert.assertTrue("Should initialized transtiions",
46                 transitions != null && transitions.isEmpty());
47     }
48
49     @Test
50     public void testHashCode() throws Exception {
51         Assert.assertEquals("Should return proper hash code",
52                 STATE_NAME.toLowerCase().hashCode(), state.hashCode());
53     }
54
55     @Test
56     public void testEquals() throws Exception {
57         Assert.assertFalse("should return false for null", state.equals(null));
58         Assert.assertFalse("should return false for object", state.equals(new Event(STATE_NAME)));
59         Assert.assertFalse("should return false for different event",
60                 state.equals(new Event("Another")));
61         Assert.assertTrue("should return true", state.equals(new State(STATE_NAME)));
62         Assert.assertTrue("should return true (lower case)", state.equals(new State(STATE_NAME.toLowerCase())));
63     }
64
65     @Test
66     public void testGetStateName() throws Exception {
67         Assert.assertEquals("Should return STATE_NAME", STATE_NAME, state.getStateName());
68     }
69
70     @SuppressWarnings("unchecked")
71     @Test
72     public void testAddAndGetTransition() throws Exception {
73         Transition transition1 = new Transition(new Event("event1"), new State("state2"));
74         List<Transition> transitions = (List<Transition>) Whitebox.getInternalState(state, "transitions");
75         Assert.assertFalse("should not have transition1", transitions.contains(transition1));
76         state.addTransition(transition1);
77         transitions = (List<Transition>) Whitebox.getInternalState(state, "transitions");
78         Assert.assertTrue("should have added transition1", transitions.contains(transition1));
79         Assert.assertEquals("Should return transitions", transitions, state.getTransitions());
80
81         state.addTransition(null);
82         Assert.assertEquals("Should not change transitions", transitions,
83                 Whitebox.getInternalState(state, "transitions"));
84     }
85
86     @Test
87     public void testToString() throws Exception {
88         Assert.assertEquals("Should return STATE_NAME", STATE_NAME, state.toString());
89     }
90 }