Applying license changes to all files
[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("Stopped"),
50     };
51
52     @Test
53     public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
54
55         MetadataReader metadataReader = new MetadataReader();
56         StateMachineMetadata metadata = metadataReader.readMetadata(null);
57
58         LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
59
60         /*
61         Testing Positive Scenario passing the valid events and validating the StateMachineResponse
62          */
63         for(State state:metadata.getStates()){
64
65             for(Transition transition:state.getTransitions()){
66                 Event event = transition.getEvent();
67                 State nextStateFromMetadata = transition.getNextState();
68
69                 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
70                 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
71             }
72         }
73
74         /*
75         Testing Negative Scenarios, 1. Passing the valid Events for which Transition is not defined in
76         Metadata and validating the StateMachineResponse 2. Passing the invalid events which are not
77         registered as events in the StateMachineMetadata and validating StateMachineResponse
78          */
79         for(State state:metadata.getStates()){
80
81             for(Transition transition:state.getTransitions()){
82                 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
83
84                 for(Event negativeEvent:negativeEvents){
85                     boolean flag =false;
86                     try{
87                         String response = lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
88
89                     }
90                     catch (NoTransitionDefinedException e){
91                         flag =true;
92                     }
93                     Assert.assertEquals(flag,true);
94
95                     flag =false;
96                     try{
97                         String response = lifecycleManager.getNextState(null,state.toString(),"PUT");
98                     }
99                     catch(LifecycleException e){
100                         flag = true;
101                     }
102                     Assert.assertTrue(flag);
103
104                 }
105             }
106         }
107     }
108
109     @Test
110     public void testNotOrchestratedState() throws LifecycleException, NoTransitionDefinedException {
111         LifecycleManager lifecycleManager = new LifecycleManagerImpl();
112         String nextState = lifecycleManager.getNextState(null,"NOT ORCHESTRATED",VNFOperation.Configure.toString());
113         Assert.assertEquals(nextState,"Configuring");
114     }
115
116     @Test(expected = NoTransitionDefinedException.class)
117     public void testBakckingUpState() throws LifecycleException, NoTransitionDefinedException {
118         LifecycleManager lifecycleManager = new LifecycleManagerImpl();
119         String nextState = lifecycleManager.getNextState(null,"Software_Uploading",VNFOperation.Configure.toString());
120     }
121
122     private List<Event> getNegativeEvents(State state,Set<Event> events) {
123         List<Event> negativeEventList = new ArrayList<>();
124         negativeEventList.addAll(events);
125
126         for(Transition transition: state.getTransitions()){
127             negativeEventList.remove(transition.getEvent());
128         }
129         return negativeEventList;
130     }
131
132     @Test
133     public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
134         MetadataReader metadataReader = new MetadataReader();
135         StateMachineMetadata metadata = metadataReader.readMetadata(null);
136         LifecycleManager lifecycleManager = new LifecycleManagerImpl();
137         for(State state: metadata.getStates()) {
138             if(isValidState(state, VALID_LOCK_STATES)) {
139                 assertSameNextState(lifecycleManager, state, VNFOperation.Lock);
140                 assertSameNextState(lifecycleManager, state, VNFOperation.Unlock);
141                 assertSameNextState(lifecycleManager, state, VNFOperation.CheckLock);
142             } else {
143                 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
144                 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
145                 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
146             }
147         }
148     }
149
150     private boolean isValidState(State state, State[] validStates) {
151         for(State validState: validStates) {
152             if(validState.equals(state)) {
153                 return true;
154             }
155         }
156         return false;
157     }
158
159     private void assertSameNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException, NoTransitionDefinedException {
160         Assert.assertEquals(state.getStateName(), lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString()));
161     }
162
163     private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
164         try {
165             lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString());
166             Assert.fail("lifecycleManager.getNextState() should fail for state [" + state + "], operation [" + operation + "]");
167         } catch(NoTransitionDefinedException e) {
168             // this exception is excepted
169         }
170     }
171 }