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);
107 public void testNotOrchestratedState() throws LifecycleException, NoTransitionDefinedException {
108 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
109 String nextState = lifecycleManager.getNextState(null,"NOT ORCHESTRATED",VNFOperation.Configure.toString());
110 Assert.assertEquals(nextState,"Configuring");
113 @Test(expected = NoTransitionDefinedException.class)
114 public void testBakckingUpState() throws LifecycleException, NoTransitionDefinedException {
115 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
116 String nextState = lifecycleManager.getNextState(null,"Software_Uploading",VNFOperation.Configure.toString());
119 private List<Event> getNegativeEvents(State state,Set<Event> events) {
120 List<Event> negativeEventList = new ArrayList<>();
121 negativeEventList.addAll(events);
123 for(Transition transition: state.getTransitions()){
124 negativeEventList.remove(transition.getEvent());
126 return negativeEventList;
130 public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
131 MetadataReader metadataReader = new MetadataReader();
132 StateMachineMetadata metadata = metadataReader.readMetadata(null);
133 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
134 for(State state: metadata.getStates()) {
135 if(isValidState(state, VALID_LOCK_STATES)) {
136 assertSameNextState(lifecycleManager, state, VNFOperation.Lock);
137 assertSameNextState(lifecycleManager, state, VNFOperation.Unlock);
138 assertSameNextState(lifecycleManager, state, VNFOperation.CheckLock);
140 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
141 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
142 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
147 private boolean isValidState(State state, State[] validStates) {
148 for(State validState: validStates) {
149 if(validState.equals(state)) {
156 private void assertSameNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException, NoTransitionDefinedException {
157 Assert.assertEquals(state.getStateName(), lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString()));
160 private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
162 lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString());
163 Assert.fail("lifecycleManager.getNextState() should fail for state [" + state + "], operation [" + operation + "]");
164 } catch(NoTransitionDefinedException e) {
165 // this exception is excepted