Merge of new rebased code
[appc.git] / appc-dispatcher / appc-lifecycle-management / appc-lifecycle-management-core / src / test / java / org / openecomp / appc / TestLifecycleManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc;
23
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.*;
33
34 import java.util.*;
35
36
37 public class TestLifecycleManager {
38
39     private static final State[] VALID_LOCK_STATES = new State[] {
40             new State("Instantiated"),
41             new State("Configured"),
42             new State("Tested"),
43             new State("Running"),
44             new State("Error"),
45             new State("Unknown"),
46             new State("Stopped"),
47     };
48
49     @Test
50     public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
51
52         MetadataReader metadataReader = new MetadataReader();
53         StateMachineMetadata metadata = metadataReader.readMetadata(null);
54
55         LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
56
57         /*
58         Testing Positive Scenario passing the valid events and validating the StateMachineResponse
59          */
60         for(State state:metadata.getStates()){
61
62             for(Transition transition:state.getTransitions()){
63                 Event event = transition.getEvent();
64                 State nextStateFromMetadata = transition.getNextState();
65
66                 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
67                 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
68             }
69         }
70
71         /*
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
75          */
76         for(State state:metadata.getStates()){
77
78             for(Transition transition:state.getTransitions()){
79                 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
80
81                 for(Event negativeEvent:negativeEvents){
82                     boolean flag =false;
83                     try{
84                         String response = lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
85
86                     }
87                     catch (NoTransitionDefinedException e){
88                         flag =true;
89                     }
90                     Assert.assertEquals(flag,true);
91
92                     flag =false;
93                     try{
94                         String response = lifecycleManager.getNextState(null,state.toString(),"PUT");
95                     }
96                     catch(LifecycleException e){
97                         flag = true;
98                     }
99                     Assert.assertTrue(flag);
100
101                 }
102             }
103         }
104     }
105
106     @Test
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");
111     }
112
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());
117     }
118
119     private List<Event> getNegativeEvents(State state,Set<Event> events) {
120         List<Event> negativeEventList = new ArrayList<>();
121         negativeEventList.addAll(events);
122
123         for(Transition transition: state.getTransitions()){
124             negativeEventList.remove(transition.getEvent());
125         }
126         return negativeEventList;
127     }
128
129     @Test
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);
139             } else {
140                 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
141                 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
142                 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
143             }
144         }
145     }
146
147     private boolean isValidState(State state, State[] validStates) {
148         for(State validState: validStates) {
149             if(validState.equals(state)) {
150                 return true;
151             }
152         }
153         return false;
154     }
155
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()));
158     }
159
160     private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
161         try {
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
166         }
167     }
168 }