Merging in bug fixes
[appc.git] / appc-dispatcher / appc-lifecycle-management / appc-lifecycle-management-core / src / test / java / org / openecomp / appc / TestLifecycleManager.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.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.*;
36
37 import java.util.*;
38
39
40 public class TestLifecycleManager {
41
42     private static final State[] VALID_LOCK_STATES = new State[] {
43             new State("instantiated"),
44             new State("configured"),
45             new State("tested"),
46             new State("running"),
47             new State("error"),
48             new State("unknown"),
49             new State("created"),
50             new State("not orchestrated"),
51             new State("stopped"),
52     };
53
54     @Test
55     public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
56
57         MetadataReader metadataReader = new MetadataReader();
58         StateMachineMetadata metadata = metadataReader.readMetadata(null);
59
60         LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
61
62         /*
63         Testing Positive Scenario passing the valid events and validating the StateMachineResponse
64          */
65         for(State state:metadata.getStates()){
66
67             for(Transition transition:state.getTransitions()){
68                 Event event = transition.getEvent();
69                 State nextStateFromMetadata = transition.getNextState();
70
71                 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
72                 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
73             }
74         }
75
76         /*
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
80          */
81         for(State state:metadata.getStates()){
82
83             for(Transition transition:state.getTransitions()){
84                 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
85
86                 for(Event negativeEvent:negativeEvents){
87                     boolean flag =false;
88                     try{
89                         String response = lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
90
91                     }
92                     catch (NoTransitionDefinedException e){
93                         flag =true;
94                     }
95                     Assert.assertEquals(flag,true);
96
97                     flag =false;
98                     try{
99                         String response = lifecycleManager.getNextState(null,state.toString(),"PUT");
100                     }
101                     catch(LifecycleException e){
102                         flag = true;
103                     }
104                     Assert.assertTrue(flag);
105
106                 }
107             }
108         }
109     }
110
111     @Test
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");
116     }
117
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());
122     }
123
124     private List<Event> getNegativeEvents(State state,Set<Event> events) {
125         List<Event> negativeEventList = new ArrayList<>();
126         negativeEventList.addAll(events);
127
128         for(Transition transition: state.getTransitions()){
129             negativeEventList.remove(transition.getEvent());
130         }
131         return negativeEventList;
132     }
133
134     @Test
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);
144             } else {
145                 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
146                 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
147                 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
148             }
149         }
150     }
151
152     private boolean isValidState(State state, State[] validStates) {
153         for(State validState: validStates) {
154             if(validState.equals(state)) {
155                 return true;
156             }
157         }
158         return false;
159     }
160
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()));
163     }
164
165     private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
166         try {
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
171         }
172     }
173 }