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.openecomp.appc;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
30 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
31 import org.openecomp.appc.lifecyclemanager.helper.MetadataReader;
32 import org.openecomp.appc.lifecyclemanager.impl.LifecycleManagerImpl;
33 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
34 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
35 import org.openecomp.appc.statemachine.objects.*;
40 public class TestLifecycleManager {
42 private static final State[] VALID_LOCK_STATES = new State[] {
43 new State("instantiated"),
44 new State("configured"),
50 new State("not orchestrated"),
55 public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
57 MetadataReader metadataReader = new MetadataReader();
58 StateMachineMetadata metadata = metadataReader.readMetadata(null);
60 LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
63 Testing Positive Scenario passing the valid events and validating the StateMachineResponse
65 for(State state:metadata.getStates()){
67 for(Transition transition:state.getTransitions()){
68 Event event = transition.getEvent();
69 State nextStateFromMetadata = transition.getNextState();
71 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
72 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
77 Testing Negative Scenarios, 1. Passing the valid Events for which Transition is not defined in
78 Metadata and validating the StateMachineResponse 2. Passing the invalid events which are not
79 registered as events in the StateMachineMetadata and validating StateMachineResponse
81 for(State state:metadata.getStates()){
83 for(Transition transition:state.getTransitions()){
84 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
86 for(Event negativeEvent:negativeEvents){
89 String response = lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
92 catch (NoTransitionDefinedException e){
95 Assert.assertEquals(flag,true);
99 String response = lifecycleManager.getNextState(null,state.toString(),"PUT");
101 catch(LifecycleException e){
104 Assert.assertTrue(flag);
112 public void testNotOrchestratedState() throws LifecycleException, NoTransitionDefinedException {
113 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
114 String nextState = lifecycleManager.getNextState(null,"NOT ORCHESTRATED",VNFOperation.Configure.toString());
115 Assert.assertEquals(nextState,"configuring");
118 @Test(expected = NoTransitionDefinedException.class)
119 public void testBakckingUpState() throws LifecycleException, NoTransitionDefinedException {
120 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
121 String nextState = lifecycleManager.getNextState(null,"Software_Uploading",VNFOperation.Configure.toString());
124 private List<Event> getNegativeEvents(State state,Set<Event> events) {
125 List<Event> negativeEventList = new ArrayList<>();
126 negativeEventList.addAll(events);
128 for(Transition transition: state.getTransitions()){
129 negativeEventList.remove(transition.getEvent());
131 return negativeEventList;
135 public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
136 MetadataReader metadataReader = new MetadataReader();
137 StateMachineMetadata metadata = metadataReader.readMetadata(null);
138 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
139 for(State state: metadata.getStates()) {
140 if(isValidState(state, VALID_LOCK_STATES)) {
141 assertSameNextState(lifecycleManager, state, VNFOperation.Lock);
142 assertSameNextState(lifecycleManager, state, VNFOperation.Unlock);
143 assertSameNextState(lifecycleManager, state, VNFOperation.CheckLock);
145 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
146 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
147 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
152 private boolean isValidState(State state, State[] validStates) {
153 for(State validState: validStates) {
154 if(validState.equals(state)) {
161 private void assertSameNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException, NoTransitionDefinedException {
162 Assert.assertEquals(state.getStateName(), lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString()));
165 private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
167 lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString());
168 Assert.fail("lifecycleManager.getNextState() should fail for state [" + state + "], operation [" + operation + "]");
169 } catch(NoTransitionDefinedException e) {
170 // this exception is excepted