2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
28 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
29 import org.openecomp.appc.lifecyclemanager.helper.MetadataReader;
30 import org.openecomp.appc.lifecyclemanager.impl.LifecycleManagerImpl;
31 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
32 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
33 import org.openecomp.appc.statemachine.objects.*;
38 public class TestLifecycleManager {
40 private static final State[] VALID_LOCK_STATES = new State[] {
41 new State("Instantiated"),
42 new State("Configured"),
51 public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
53 MetadataReader metadataReader = new MetadataReader();
54 StateMachineMetadata metadata = metadataReader.readMetadata(null);
56 LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
59 Testing Positive Scenario passing the valid events and validating the StateMachineResponse
61 for(State state:metadata.getStates()){
63 for(Transition transition:state.getTransitions()){
64 Event event = transition.getEvent();
65 State nextStateFromMetadata = transition.getNextState();
67 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
68 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
73 Testing Negative Scenarios, 1. Passing the valid Events for which Transition is not defined in
74 Metadata and validating the StateMachineResponse 2. Passing the invalid events which are not
75 registered as events in the StateMachineMetadata and validating StateMachineResponse
77 for(State state:metadata.getStates()){
79 for(Transition transition:state.getTransitions()){
80 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
82 for(Event negativeEvent:negativeEvents){
85 String response = lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
88 catch (NoTransitionDefinedException e){
91 Assert.assertEquals(flag,true);
95 String response = lifecycleManager.getNextState(null,state.toString(),"PUT");
97 catch(LifecycleException e){
100 Assert.assertTrue(flag);
108 public void testNotOrchestratedState() throws LifecycleException, NoTransitionDefinedException {
109 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
110 String nextState = lifecycleManager.getNextState(null,"NOT ORCHESTRATED",VNFOperation.Configure.toString());
111 Assert.assertEquals(nextState,"Configuring");
114 @Test(expected = NoTransitionDefinedException.class)
115 public void testBakckingUpState() throws LifecycleException, NoTransitionDefinedException {
116 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
117 String nextState = lifecycleManager.getNextState(null,"Software_Uploading",VNFOperation.Configure.toString());
120 private List<Event> getNegativeEvents(State state,Set<Event> events) {
121 List<Event> negativeEventList = new ArrayList<>();
122 negativeEventList.addAll(events);
124 for(Transition transition: state.getTransitions()){
125 negativeEventList.remove(transition.getEvent());
127 return negativeEventList;
131 public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
132 MetadataReader metadataReader = new MetadataReader();
133 StateMachineMetadata metadata = metadataReader.readMetadata(null);
134 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
135 for(State state: metadata.getStates()) {
136 if(isValidState(state, VALID_LOCK_STATES)) {
137 assertSameNextState(lifecycleManager, state, VNFOperation.Lock);
138 assertSameNextState(lifecycleManager, state, VNFOperation.Unlock);
139 assertSameNextState(lifecycleManager, state, VNFOperation.CheckLock);
141 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
142 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
143 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
148 private boolean isValidState(State state, State[] validStates) {
149 for(State validState: validStates) {
150 if(validState.equals(state)) {
157 private void assertSameNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException, NoTransitionDefinedException {
158 Assert.assertEquals(state.getStateName(), lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString()));
161 private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
163 lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString());
164 Assert.fail("lifecycleManager.getNextState() should fail for state [" + state + "], operation [" + operation + "]");
165 } catch(NoTransitionDefinedException e) {
166 // this exception is excepted