2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.statemachine.objects;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.mockito.internal.util.reflection.Whitebox;
30 import java.util.List;
32 public class StateTest {
33 private final String STATE_NAME = "Starting";
34 private State state = new State(STATE_NAME);
36 @SuppressWarnings("unchecked")
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());
50 public void testHashCode() throws Exception {
51 Assert.assertEquals("Should return proper hash code",
52 STATE_NAME.toLowerCase().hashCode(), state.hashCode());
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())));
66 public void testGetStateName() throws Exception {
67 Assert.assertEquals("Should return STATE_NAME", STATE_NAME, state.getStateName());
70 @SuppressWarnings("unchecked")
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());
81 state.addTransition(null);
82 Assert.assertEquals("Should not change transitions", transitions,
83 Whitebox.getInternalState(state, "transitions"));
87 public void testToString() throws Exception {
88 Assert.assertEquals("Should return STATE_NAME", STATE_NAME, state.toString());