403a9f39ee2df686786c0f1fe3cadfc01222f3bc
[appc.git] / appc-lifecycle-management / appc-lifecycle-management-core / src / test / java / org / onap / appc / OamLifeCycleManagerTest.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.onap.appc;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.onap.appc.lifecyclemanager.impl.LifecycleManagerImpl;
33 import org.onap.appc.lifecyclemanager.objects.LifecycleException;
34 import org.onap.appc.lifecyclemanager.objects.NoTransitionDefinedException;
35 import org.onap.appc.statemachine.impl.readers.AppcOamMetaDataReader;
36 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
37
38 import java.util.Arrays;
39 import java.util.List;
40
41
42 public class OamLifeCycleManagerTest {
43     private static final String VNF_TYPE_APPC = "APPC";
44     private static final String NO_DEFINITION_FORMAT = "No Transition Defined for currentState = %s, event = %s";
45
46     private LifecycleManagerImpl lifecycleManager;
47
48     @Rule
49     public ExpectedException expectedException = ExpectedException.none();
50
51     @Before
52     public void setUp() throws Exception {
53         lifecycleManager = new LifecycleManagerImpl();
54     }
55
56     private void validateProper(String state, String event, String expectedResult)
57             throws LifecycleException, NoTransitionDefinedException {
58         String nextState = lifecycleManager.getNextState(VNF_TYPE_APPC, state, event);
59         Assert.assertEquals(String.format("Should return %s", expectedResult), expectedResult, nextState);
60     }
61
62     private void validateException(String state, String event) throws LifecycleException, NoTransitionDefinedException {
63         expectedException.expect(NoTransitionDefinedException.class);
64         expectedException.expectMessage(String.format(NO_DEFINITION_FORMAT, state, event));
65         lifecycleManager.getNextState(VNF_TYPE_APPC, state, event);
66
67         // Reset to no expectation
68         expectedException = ExpectedException.none();
69     }
70
71     @Test
72     public void testOamStateTransitionForMaintenanceMode() throws Exception {
73         String event = AppcOamMetaDataReader.AppcOperation.MaintenanceMode.name();
74         String expecteResult = AppcOamStates.EnteringMaintenanceMode.toString();
75
76         for (AppcOamStates appcOamStates : AppcOamStates.values()) {
77             String state = appcOamStates.toString();
78             if (appcOamStates == AppcOamStates.Started) {
79                 validateProper(state, event, expecteResult);
80             } else {
81                 validateException(state, event);
82             }
83         }
84     }
85
86     @Test
87     public void testOamStateTransitionForStart() throws Exception {
88         String event = AppcOamMetaDataReader.AppcOperation.Start.name();
89         String expectResult = AppcOamStates.Starting.toString();
90
91         List<AppcOamStates> goodStates = Arrays.asList(
92                 AppcOamStates.MaintenanceMode,
93                 AppcOamStates.Stopped,
94                 AppcOamStates.Stopping);
95
96         for (AppcOamStates appcOamStates : AppcOamStates.values()) {
97             String state = appcOamStates.toString();
98             if (goodStates.contains(appcOamStates)) {
99                 validateProper(state, event, expectResult);
100             } else {
101                 validateException(state, event);
102             }
103         }
104     }
105
106     @Test
107     public void testOamStateTransitionForStop() throws Exception {
108         String event = AppcOamMetaDataReader.AppcOperation.Stop.name();
109         String expectResult = AppcOamStates.Stopping.toString();
110
111         List<AppcOamStates> goodStates = Arrays.asList(
112                 AppcOamStates.Error,
113                 AppcOamStates.EnteringMaintenanceMode,
114                 AppcOamStates.MaintenanceMode,
115                 AppcOamStates.Started,
116                 AppcOamStates.Starting);
117
118         for (AppcOamStates appcOamStates : AppcOamStates.values()) {
119             String state = appcOamStates.toString();
120             if (goodStates.contains(appcOamStates)) {
121                 validateProper(state, event, expectResult);
122             } else {
123                 validateException(state, event);
124             }
125         }
126     }
127
128
129     @Test
130     public void testOamStateTransitionForRestart() throws Exception {
131         String event = AppcOamMetaDataReader.AppcOperation.Restart.name();
132         String expectResult = AppcOamStates.Restarting.toString();
133
134         List<AppcOamStates> goodStates = Arrays.asList(
135                 AppcOamStates.Error,
136                 AppcOamStates.EnteringMaintenanceMode,
137                 AppcOamStates.MaintenanceMode,
138                 AppcOamStates.Started,
139                 AppcOamStates.Starting,
140                 AppcOamStates.Stopped,
141                 AppcOamStates.Stopping);
142
143         for (AppcOamStates appcOamStates : AppcOamStates.values()) {
144             String state = appcOamStates.toString();
145             if (goodStates.contains(appcOamStates)) {
146                 validateProper(state, event, expectResult);
147             } else {
148                 validateException(state, event);
149             }
150         }
151     }
152 }