2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc;
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
27 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
28 import org.openecomp.appc.lifecyclemanager.helper.MetadataReader;
29 import org.openecomp.appc.lifecyclemanager.impl.LifecycleManagerImpl;
30 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
31 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
32 import org.openecomp.appc.statemachine.objects.*;
37 public class TestLifecycleManager {
39 private static final State[] VALID_LOCK_STATES = new State[] {
40 new State("Instantiated"),
41 new State("Configured"),
50 public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
52 MetadataReader metadataReader = new MetadataReader();
53 StateMachineMetadata metadata = metadataReader.readMetadata(null);
55 LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
58 Testing Positive Scenario passing the valid events and validating the StateMachineResponse
60 for(State state:metadata.getStates()){
62 for(Transition transition:state.getTransitions()){
63 Event event = transition.getEvent();
64 State nextStateFromMetadata = transition.getNextState();
66 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
67 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
72 Testing Negative Scenarios, 1. Passing the valid Events for which Transition is not defined in
73 Metadata and validating the StateMachineResponse 2. Passing the invalid events which are not
74 registered as events in the StateMachineMetadata and validating StateMachineResponse
76 for(State state:metadata.getStates()){
78 for(Transition transition:state.getTransitions()){
79 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
81 for(Event negativeEvent:negativeEvents){
84 String response = lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
87 catch (NoTransitionDefinedException e){
90 Assert.assertEquals(flag,true);
94 String response = lifecycleManager.getNextState(null,state.toString(),"PUT");
96 catch(LifecycleException e){
99 Assert.assertTrue(flag);
106 private List<Event> getNegativeEvents(State state,Set<Event> events) {
107 List<Event> negativeEventList = new ArrayList<>();
108 negativeEventList.addAll(events);
110 for(Transition transition: state.getTransitions()){
111 negativeEventList.remove(transition.getEvent());
113 return negativeEventList;
117 public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
118 MetadataReader metadataReader = new MetadataReader();
119 StateMachineMetadata metadata = metadataReader.readMetadata(null);
120 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
121 for(State state: metadata.getStates()) {
122 if(isValidState(state, VALID_LOCK_STATES)) {
123 assertSameNextState(lifecycleManager, state, VNFOperation.Lock);
124 assertSameNextState(lifecycleManager, state, VNFOperation.Unlock);
125 assertSameNextState(lifecycleManager, state, VNFOperation.CheckLock);
127 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
128 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
129 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
134 private boolean isValidState(State state, State[] validStates) {
135 for(State validState: validStates) {
136 if(validState.equals(state)) {
143 private void assertSameNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException, NoTransitionDefinedException {
144 Assert.assertEquals(state.getStateName(), lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString()));
147 private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
149 lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString());
150 Assert.fail("lifecycleManager.getNextState() should fail for state [" + state + "], operation [" + operation + "]");
151 } catch(NoTransitionDefinedException e) {
152 // this exception is excepted