Increase sonar coverage for LifecycleManagement
[appc.git] / appc-lifecycle-management / state-machine-lib / src / test / java / org / openecomp / appc / statemachine / impl / StateMachineImplTest.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.openecomp.appc.statemachine.impl;
26
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.openecomp.appc.exceptions.InvalidInputException;
32 import org.openecomp.appc.statemachine.objects.Event;
33 import org.openecomp.appc.statemachine.objects.Response;
34 import org.openecomp.appc.statemachine.objects.State;
35 import org.openecomp.appc.statemachine.objects.StateMachineMetadata;
36 import org.openecomp.appc.statemachine.objects.StateMachineResponse;
37
38 public class StateMachineImplTest {
39     private StateMachineMetadata metadata;
40     private StateMachineImpl stateMachine;
41
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");
46
47     @Before
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);
57
58         metadata = builder.build();
59
60         stateMachine = new StateMachineImpl(metadata);
61     }
62
63     @Test
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"));
70     }
71
72     @Test(expected = InvalidInputException.class)
73     public void testHandleEventThrowsInvalidInputException() throws Exception {
74         stateMachine.handleEvent(null, null);
75     }
76
77     @Test
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());
82
83         response = stateMachine.handleEvent(state2, event1);
84         Assert.assertEquals(Response.NO_TRANSITION_DEFINED, response.getResponse());
85         Assert.assertTrue(response.getNextState() == null);
86     }
87
88     @Test
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));
97
98     }
99
100     @Test
101     public void testToString() throws Exception {
102         Assert.assertEquals(
103                 String.format(stateMachine.toStringFormat, metadata.getStates(), metadata.getEvents()),
104                 stateMachine.toString());
105     }
106
107 }