First part of onap rename
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / openecomp / appc / oam / util / StateHelperTest.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.oam.util;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.AppcState;
34 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
35 import org.osgi.framework.Bundle;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
39 import org.powermock.reflect.Whitebox;
40
41 import java.util.HashMap;
42 import java.util.Map;
43
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.times;
46
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({StateHelper.class})
49 public class StateHelperTest {
50     private StateHelper stateHelper;
51
52     @Before
53     public void setUp() throws Exception {
54         stateHelper = PowerMockito.spy(new StateHelper(null, null));
55
56         // to avoid operation on logger fail, mock up the logger
57         EELFLogger mockLogger = mock(EELFLogger.class);
58         Whitebox.setInternalState(stateHelper, "logger", mockLogger);
59     }
60
61     @Test
62     public void testSetState() throws Exception {
63         AppcOamStates appcOamStates = AppcOamStates.Started;
64         stateHelper.setState(appcOamStates);
65         Assert.assertEquals("Should have the new value", appcOamStates,
66                 Whitebox.getInternalState(stateHelper, "appcOamCurrentState"));
67         // reest to default value
68         stateHelper.setState(AppcOamStates.Unknown);
69     }
70
71     @Test
72     public void testGetState() throws Exception {
73         AppcOamStates appcOamStates = stateHelper.getState();
74         Assert.assertEquals("Should have the class value", appcOamStates,
75                 Whitebox.getInternalState(stateHelper, "appcOamCurrentState"));
76     }
77
78     @Test
79     public void testIsSameState() throws Exception {
80         AppcOamStates classValue = Whitebox.getInternalState(stateHelper, "appcOamCurrentState");
81         for (AppcOamStates appcOamStates : AppcOamStates.values()) {
82             boolean isSame = stateHelper.isSameState(appcOamStates);
83             if (appcOamStates == classValue) {
84                 Assert.assertTrue("Should be the same", isSame);
85             } else {
86                 Assert.assertFalse("Should not be the same", isSame);
87             }
88         }
89     }
90
91     @Test
92     public void testGetCurrentOamState() throws Exception {
93         AppcOamStates mockResult = AppcOamStates.Started;
94         // mock getBundlesState, as we are testin it separately
95         PowerMockito.doReturn(mockResult).when(stateHelper, "getBundlesState");
96
97         Whitebox.setInternalState(stateHelper, "appcOamCurrentState", AppcOamStates.Unknown);
98         Assert.assertEquals("Should call deriveStatte and return mockeResult",
99                 mockResult, stateHelper.getCurrentOamState());
100         Mockito.verify(stateHelper, times(1)).getBundlesState();
101
102
103         Whitebox.setInternalState(stateHelper, "appcOamCurrentState", AppcOamStates.Unknown);
104         Assert.assertEquals("Should call deriveStatte and return mockeResult",
105                 mockResult, stateHelper.getCurrentOamState());
106         Mockito.verify(stateHelper, times(2)).getBundlesState();
107
108         Whitebox.setInternalState(stateHelper, "appcOamCurrentState", mockResult);
109         Assert.assertEquals("Should just return mockeResult", mockResult, stateHelper.getCurrentOamState());
110         Mockito.verify(stateHelper, times(2)).getBundlesState();
111     }
112
113     @Test
114     public void testGetCurrentOamYangState() throws Exception {
115         Map<AppcOamStates, AppcState> stateMap = new HashMap<AppcOamStates, AppcState>() {
116             {
117                 put(AppcOamStates.EnteringMaintenanceMode, AppcState.EnteringMaintenanceMode);
118                 put(AppcOamStates.MaintenanceMode,         AppcState.MaintenanceMode);
119                 put(AppcOamStates.Instantiated,            AppcState.Instantiated);
120                 put(AppcOamStates.NotInstantiated,         AppcState.NotInstantiated);
121                 put(AppcOamStates.Restarting,              AppcState.Restarting);
122                 put(AppcOamStates.Started,                 AppcState.Started);
123                 put(AppcOamStates.Starting,                AppcState.Starting);
124                 put(AppcOamStates.Stopped,                 AppcState.Stopped);
125                 put(AppcOamStates.Stopping,                AppcState.Stopping);
126                 put(AppcOamStates.Error,                   AppcState.Error);
127                 put(AppcOamStates.Unknown,                 AppcState.Unknown);
128             }
129         };
130         for (Map.Entry<AppcOamStates, AppcState> aEntry : stateMap.entrySet()) {
131             AppcOamStates aState = aEntry.getKey();
132             AppcState appcState = aEntry.getValue();
133
134             PowerMockito.doReturn(aState).when(stateHelper, "getCurrentOamState");
135
136             AppcState resultState = stateHelper.getCurrentOamYangState();
137             Assert.assertEquals(
138                     String.format("%s state, returned(%s),should return(%s) state", aState, resultState, appcState),
139                     appcState, resultState);
140
141         }
142     }
143
144     @Test
145     public void testGetBundlesState() throws Exception {
146         BundleHelper mockBundlerHelper = mock(BundleHelper.class);
147         PowerMockito.whenNew(BundleHelper.class).withAnyArguments().thenReturn(mockBundlerHelper);
148
149         // test null bundle map
150         Mockito.when(mockBundlerHelper.getAppcLcmBundles()).thenReturn(null);
151         Assert.assertEquals("Should return unknown state", AppcOamStates.Unknown, stateHelper.getBundlesState());
152
153         // tet empty bundle map
154         Map<String, Bundle> bundleMap = new HashMap<>();
155         Mockito.when(mockBundlerHelper.getAppcLcmBundles()).thenReturn(bundleMap);
156         Assert.assertEquals("Should return unknown state", AppcOamStates.Unknown, stateHelper.getBundlesState());
157
158         Bundle mockBundle1 = mock(Bundle.class);
159         Bundle mockBundle2 = mock(Bundle.class);
160         bundleMap.put("1", mockBundle1);
161         bundleMap.put("2", mockBundle2);
162         Mockito.when(mockBundlerHelper.getAppcLcmBundles()).thenReturn(bundleMap);
163
164         // test bundles have differnt states
165         Mockito.doReturn(Bundle.RESOLVED).when(mockBundle1).getState();
166         Mockito.doReturn(Bundle.ACTIVE).when(mockBundle2).getState();
167         Assert.assertEquals("Should return lower state", AppcOamStates.Stopped, stateHelper.getBundlesState());
168
169         // test bundles have the same state
170         Mockito.doReturn(Bundle.ACTIVE).when(mockBundle1).getState();
171         Assert.assertEquals("Should return the state", AppcOamStates.Started, stateHelper.getBundlesState());
172     }
173 }