2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.statemachine.impl;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.internal.util.reflection.Whitebox;
31 import org.onap.appc.exceptions.InvalidInputException;
32 import org.onap.appc.statemachine.objects.Event;
33 import org.onap.appc.statemachine.objects.Response;
34 import org.onap.appc.statemachine.objects.State;
35 import org.onap.appc.statemachine.objects.StateMachineMetadata;
36 import org.onap.appc.statemachine.objects.StateMachineResponse;
38 public class StateMachineImplTest {
39 private StateMachineMetadata metadata;
40 private StateMachineImpl stateMachine;
42 private State state1 = new State("TestingState1");
43 private State state2 = new State("TestingState2");
44 private Event event1 = new Event("TestingEvent1");
45 private Event event2 = new Event("TestingEvent2");
48 public void setUp() throws Exception {
49 StateMachineMetadata.StateMachineMetadataBuilder builder =
50 new StateMachineMetadata.StateMachineMetadataBuilder();
51 builder.addEvent(event1);
52 builder.addEvent(event2);
53 builder.addState(state1);
54 builder.addState(state2);
55 builder.addState(new State("TestingState3"));
56 builder.addTransition(state1, event1, state2);
58 metadata = builder.build();
60 stateMachine = new StateMachineImpl(metadata);
64 public void testConstructor() throws Exception {
65 StateMachineImpl stateMachine = new StateMachineImpl(metadata);
66 Assert.assertEquals("Should have set internal states",
67 metadata.getStates(), Whitebox.getInternalState(stateMachine, "states"));
68 Assert.assertEquals("Should have set internal events",
69 metadata.getEvents(), Whitebox.getInternalState(stateMachine, "events"));
72 @Test(expected = InvalidInputException.class)
73 public void testHandleEventThrowsInvalidInputException() throws Exception {
74 stateMachine.handleEvent(null, null);
78 public void testHandleEvent() throws Exception {
79 StateMachineResponse response = stateMachine.handleEvent(state1, event1);
80 Assert.assertEquals(Response.VALID_TRANSITION, response.getResponse());
81 Assert.assertEquals(state2, response.getNextState());
83 response = stateMachine.handleEvent(state2, event1);
84 Assert.assertEquals(Response.NO_TRANSITION_DEFINED, response.getResponse());
85 Assert.assertTrue(response.getNextState() == null);
89 public void testValidateInputs() {
90 Assert.assertFalse(stateMachine.validateInputs(null, null));
91 Assert.assertFalse(stateMachine.validateInputs(new State("state1"), null));
92 Assert.assertFalse(stateMachine.validateInputs(null, new Event("event1")));
93 Assert.assertFalse(stateMachine.validateInputs(new State("state1"), new Event("event1")));
94 Assert.assertFalse(stateMachine.validateInputs(state1, new Event("event1")));
95 Assert.assertFalse(stateMachine.validateInputs(new State("state1"), event1));
96 Assert.assertTrue(stateMachine.validateInputs(state1, event1));
101 public void testToString() throws Exception {
103 String.format(stateMachine.toStringFormat, metadata.getStates(), metadata.getEvents()),
104 stateMachine.toString());