Change nexus values to properties
[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     private List<Event> getNegativeEvents(State state,Set<Event> events) {
107         List<Event> negativeEventList = new ArrayList<>();
108         negativeEventList.addAll(events);
109
110         for(Transition transition: state.getTransitions()){
111             negativeEventList.remove(transition.getEvent());
112         }
113         return negativeEventList;
114     }
115
116     @Test
117     public void testLockStates() throws LifecycleException, NoTransitionDefinedException {
118         MetadataReader metadataReader = new MetadataReader();
119         StateMachineMetadata metadata = metadataReader.readMetadata(null);
120         LifecycleManager lifecycleManager = new LifecycleManagerImpl();
121         for(State state: metadata.getStates()) {
122             if(isValidState(state, VALID_LOCK_STATES)) {
123                 assertSameNextState(lifecycleManager, state, VNFOperation.Lock);
124                 assertSameNextState(lifecycleManager, state, VNFOperation.Unlock);
125                 assertSameNextState(lifecycleManager, state, VNFOperation.CheckLock);
126             } else {
127                 assertNoNextState(lifecycleManager, state, VNFOperation.Lock);
128                 assertNoNextState(lifecycleManager, state, VNFOperation.Unlock);
129                 assertNoNextState(lifecycleManager, state, VNFOperation.CheckLock);
130             }
131         }
132     }
133
134     private boolean isValidState(State state, State[] validStates) {
135         for(State validState: validStates) {
136             if(validState.equals(state)) {
137                 return true;
138             }
139         }
140         return false;
141     }
142
143     private void assertSameNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException, NoTransitionDefinedException {
144         Assert.assertEquals(state.getStateName(), lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString()));
145     }
146
147     private void assertNoNextState(LifecycleManager lifecycleManager, State state, VNFOperation operation) throws LifecycleException {
148         try {
149             lifecycleManager.getNextState("no-matter", state.getStateName(), operation.toString());
150             Assert.fail("lifecycleManager.getNextState() should fail for state [" + state + "], operation [" + operation + "]");
151         } catch(NoTransitionDefinedException e) {
152             // this exception is excepted
153         }
154     }
155 }