af1c27633d508627452176399084daabe158a992
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc;
24
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
28 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
29 import org.openecomp.appc.lifecyclemanager.helper.MetadataReader;
30 import org.openecomp.appc.lifecyclemanager.impl.LifecycleManagerImpl;
31 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
32 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
33 import org.openecomp.appc.statemachine.objects.*;
34
35 import java.util.*;
36
37
38 public class TestLifecycleManager {
39
40     private static final State[] VALID_LOCK_STATES = new State[] {
41             new State("Instantiated"),
42             new State("Configured"),
43             new State("Tested"),
44             new State("Running"),
45             new State("Error"),
46             new State("Unknown"),
47             new State("Stopped"),
48     };
49
50     @Test
51     public void handleEvent() throws InvalidInputException, LifecycleException, NoTransitionDefinedException {
52
53         MetadataReader metadataReader = new MetadataReader();
54         StateMachineMetadata metadata = metadataReader.readMetadata(null);
55
56         LifecycleManagerImpl lifecycleManager = new LifecycleManagerImpl();
57
58         /*
59         Testing Positive Scenario passing the valid events and validating the StateMachineResponse
60          */
61         for(State state:metadata.getStates()){
62
63             for(Transition transition:state.getTransitions()){
64                 Event event = transition.getEvent();
65                 State nextStateFromMetadata = transition.getNextState();
66
67                 String expectedNextState = lifecycleManager.getNextState(null,state.toString(),event.toString());
68                 Assert.assertEquals(expectedNextState,nextStateFromMetadata.toString());
69             }
70         }
71
72         /*
73         Testing Negative Scenarios, 1. Passing the valid Events for which Transition is not defined in
74         Metadata and validating the StateMachineResponse 2. Passing the invalid events which are not
75         registered as events in the StateMachineMetadata and validating StateMachineResponse
76          */
77         for(State state:metadata.getStates()){
78
79             for(Transition transition:state.getTransitions()){
80                 List<Event> negativeEvents = getNegativeEvents(state,metadata.getEvents());
81
82                 for(Event negativeEvent:negativeEvents){
83                     boolean flag =false;
84                     try{
85                         String response = lifecycleManager.getNextState(null,state.toString(),negativeEvent.toString());
86
87                     }
88                     catch (NoTransitionDefinedException e){
89                         flag =true;
90                     }
91                     Assert.assertEquals(flag,true);
92
93                     flag =false;
94                     try{
95                         String response = lifecycleManager.getNextState(null,state.toString(),"PUT");
96                     }
97                     catch(LifecycleException e){
98                         flag = true;
99                     }
100                     Assert.assertTrue(flag);
101
102                 }
103             }
104         }
105     }
106
107     @Test
108     public void testNotOrchestratedState() throws LifecycleException, NoTransitionDefinedException {
109         LifecycleManager lifecycleManager = new LifecycleManagerImpl();
110         String nextState = lifecycleManager.getNextState(null,"NOT ORCHESTRATED",VNFOperation.Configure.toString());
111         Assert.assertEquals(nextState,"Configuring");
112     }
113
114     @Test(expected = NoTransitionDefinedException.class)
115     public void testBakckingUpState() throws LifecycleException, NoTransitionDefinedException {
116         LifecycleManager lifecycleManager = new LifecycleManagerImpl();
117         String nextState = lifecycleManager.getNextState(null,"Software_Uploading",VNFOperation.Configure.toString());
118     }
119
120     private List<Event> getNegativeEvents(State state,Set<Event> events) {
121         List<Event> negativeEventList = new ArrayList<>();
122         negativeEventList.addAll(events);
123
124         for(Transition transition: state.getTransitions()){
125             negativeEventList.remove(transition.getEvent());
126         }
127         return negativeEventList;
128     }
129
130     @Test
131     public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
132         MetadataReader metadataReader = new MetadataReader();
133         StateMachineMetadata metadata = metadataReader.readMetadata(null);
134         LifecycleManager lifecycleManager = new LifecycleManagerImpl();
135         for(State state: metadata.getStates()) {
136             if(isValidState(state, VALID_LOCK_STATES)) {
137                 assertSameNextState(lifecycleManager, state, VNFOperation.Lock);
138                 assertSameNextState(lifecycleManager, state, VNFOperation.Unlock);
139                 assertSameNextState(lifecycleManager, state, VNFOperation.CheckLock);
140             } else {
141                 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
142                 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
143                 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
144             }
145         }
146     }
147
148     private boolean isValidState(State state, State[] validStates) {
149         for(State validState: validStates) {
150             if(validState.equals(state)) {
151                 return true;
152             }
153         }
154         return false;
155     }
156
157     private void assertSameNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException, NoTransitionDefinedException {
158         Assert.assertEquals(state.getStateName(), lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString()));
159     }
160
161     private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
162         try {
163             lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString());
164             Assert.fail("lifecycleManager.getNextState() should fail for state [" + state + "], operation [" + operation + "]");
165         } catch(NoTransitionDefinedException e) {
166             // this exception is excepted
167         }
168     }
169 }