299c3254654e75d02cdc573ec682803cddf3c01c
[appc.git] / appc-lifecycle-management / appc-lifecycle-management-core / src / test / java / org / openecomp / appc / LifecycleManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc;
26
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;
41
42 import java.util.*;
43
44
45 public class LifecycleManagerTest {
46
47     private static final State[] VALID_LOCK_STATES = new State[] {
48             new State("instantiated"),
49             new State("configured"),
50             new State("tested"),
51             new State("running"),
52             new State("error"),
53             new State("unknown"),
54             new State("created"),
55             new State("not orchestrated"),
56             new State("stopped"),
57     };
58
59     @Test
60     public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
61
62         StateMachineMetadata metadata = getMetaDataReader().readMetadata();
63         LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
64
65         /*
66          * Testing Positive Scenario passing the valid events and validating the StateMachineResponse
67          */
68         for(State state:metadata.getStates()){
69
70             for(Transition transition:state.getTransitions()){
71                 Event event = transition.getEvent();
72                 State nextStateFromMetadata = transition.getNextState();
73
74                 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
75                 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
76             }
77         }
78
79         /*
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
83          */
84         for(State state:metadata.getStates()){
85
86             for(Transition transition:state.getTransitions()){
87                 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
88
89                 for(Event negativeEvent:negativeEvents){
90                     boolean flag =false;
91                     try{
92                         lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
93
94                     }
95                     catch (NoTransitionDefinedException e){
96                         flag =true;
97                     }
98                     Assert.assertEquals(flag,true);
99
100                     flag = false;
101                     try{
102                         lifecycleManager.getNextState(null,state.toString(),"PUT");
103                     }
104                     catch(LifecycleException e){
105                         flag = true;
106                     }
107                     Assert.assertTrue(flag);
108
109                 }
110             }
111         }
112     }
113
114     @Test
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");
120     }
121
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());
126     }
127
128     private List<Event> getNegativeEvents(State state,Set<Event> events) {
129         List<Event> negativeEventList = new ArrayList<>();
130         negativeEventList.addAll(events);
131
132         for(Transition transition: state.getTransitions()){
133             negativeEventList.remove(transition.getEvent());
134         }
135         return negativeEventList;
136     }
137
138     @Test
139     public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
140         StateMachineMetadata metadata = getMetaDataReader().readMetadata();
141         LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
142
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);
148             } else {
149                 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
150                 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
151                 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
152             }
153         }
154     }
155
156     private boolean isValidState(State state, State[] validStates) {
157         for(State validState: validStates) {
158             if(validState.equals(state)) {
159                 return true;
160             }
161         }
162         return false;
163     }
164
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()));
169     }
170
171     private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation)
172             throws LifecycleException {
173         try {
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
179         }
180     }
181
182     private StateMetaDataReader getMetaDataReader() {
183         return new VnfMetaDataReader();
184     }
185 }