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.exceptions.InvalidInputException;
30 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
31 import org.openecomp.appc.statemachine.StateMetaDataReader;
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.impl.readers.VnfMetaDataReader;
36 import org.openecomp.appc.statemachine.impl.readers.VnfMetaDataReader.VNFOperation;
37 import org.openecomp.appc.statemachine.objects.Event;
38 import org.openecomp.appc.statemachine.objects.State;
39 import org.openecomp.appc.statemachine.objects.StateMachineMetadata;
40 import org.openecomp.appc.statemachine.objects.Transition;
45 public class LifecycleManagerTest {
47 private static final State[] VALID_LOCK_STATES = new State[] {
48 new State("instantiated"),
49 new State("configured"),
55 new State("not orchestrated"),
60 public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
62 StateMachineMetadata metadata = getMetaDataReader().readMetadata();
63 LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
66 * Testing Positive Scenario passing the valid events and validating the StateMachineResponse
68 for(State state:metadata.getStates()){
70 for(Transition transition:state.getTransitions()){
71 Event event = transition.getEvent();
72 State nextStateFromMetadata = transition.getNextState();
74 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
75 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
80 Testing Negative Scenarios, 1. Passing the valid Events for which Transition is not defined in
81 Metadata and validating the StateMachineResponse 2. Passing the invalid events which are not
82 registered as events in the StateMachineMetadata and validating StateMachineResponse
84 for(State state:metadata.getStates()){
86 for(Transition transition:state.getTransitions()){
87 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
89 for(Event negativeEvent:negativeEvents){
92 lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
95 catch (NoTransitionDefinedException e){
98 Assert.assertEquals(flag,true);
102 lifecycleManager.getNextState(null,state.toString(),"PUT");
104 catch(LifecycleException e){
107 Assert.assertTrue(flag);
115 public void testNotOrchestratedState() throws LifecycleException, NoTransitionDefinedException {
116 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
117 String nextState = lifecycleManager.getNextState(
118 null,"NOT ORCHESTRATED",VNFOperation.Configure.toString());
119 Assert.assertEquals(nextState,"Configuring");
122 @Test(expected = NoTransitionDefinedException.class)
123 public void testBakckingUpState() throws LifecycleException, NoTransitionDefinedException {
124 LifecycleManager lifecycleManager = new LifecycleManagerImpl();
125 lifecycleManager.getNextState(null,"Software_Uploading",VNFOperation.Configure.toString());
128 private List<Event> getNegativeEvents(State state,Set<Event> events) {
129 List<Event> negativeEventList = new ArrayList<>();
130 negativeEventList.addAll(events);
132 for(Transition transition: state.getTransitions()){
133 negativeEventList.remove(transition.getEvent());
135 return negativeEventList;
139 public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
140 StateMachineMetadata metadata = getMetaDataReader().readMetadata();
141 LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
143 for(State state: metadata.getStates()) {
144 if(isValidState(state, VALID_LOCK_STATES)) {
145 assertSameNextState(lifecycleManager, state, VNFOperation.Lock);
146 assertSameNextState(lifecycleManager, state, VnfMetaDataReader.VNFOperation.Unlock);
147 assertSameNextState(lifecycleManager, state, VNFOperation.CheckLock);
149 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
150 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
151 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
156 private boolean isValidState(State state, State[] validStates) {
157 for(State validState: validStates) {
158 if(validState.equals(state)) {
165 private void assertSameNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation)
166 throws LifecycleException, NoTransitionDefinedException {
167 Assert.assertEquals(state.getStateName(),
168 lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString()));
171 private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation)
172 throws LifecycleException {
174 lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString());
175 Assert.fail("lifecycleManager.getNextState() should fail for state [" + state
176 + "], operation [" + operation + "]");
177 } catch(NoTransitionDefinedException e) {
178 // this exception is excepted
182 private StateMetaDataReader getMetaDataReader() {
183 return new VnfMetaDataReader();