2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.oam.util;
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.openecomp.appc.oam.rev170303.AppcState;
34 import org.openecomp.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;
41 import java.util.HashMap;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.times;
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({StateHelper.class})
49 public class StateHelperTest {
50 private StateHelper stateHelper;
53 public void setUp() throws Exception {
54 stateHelper = PowerMockito.spy(new StateHelper(null, null));
56 // to avoid operation on logger fail, mock up the logger
57 EELFLogger mockLogger = mock(EELFLogger.class);
58 Whitebox.setInternalState(stateHelper, "logger", mockLogger);
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);
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"));
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);
86 Assert.assertFalse("Should not be the same", isSame);
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");
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();
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();
108 Whitebox.setInternalState(stateHelper, "appcOamCurrentState", mockResult);
109 Assert.assertEquals("Should just return mockeResult", mockResult, stateHelper.getCurrentOamState());
110 Mockito.verify(stateHelper, times(2)).getBundlesState();
114 public void testGetCurrentOamYangState() throws Exception {
115 Map<AppcOamStates, AppcState> stateMap = new HashMap<AppcOamStates, AppcState>() {
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);
130 for (Map.Entry<AppcOamStates, AppcState> aEntry : stateMap.entrySet()) {
131 AppcOamStates aState = aEntry.getKey();
132 AppcState appcState = aEntry.getValue();
134 PowerMockito.doReturn(aState).when(stateHelper, "getCurrentOamState");
136 AppcState resultState = stateHelper.getCurrentOamYangState();
138 String.format("%s state, returned(%s),should return(%s) state", aState, resultState, appcState),
139 appcState, resultState);
145 public void testGetBundlesState() throws Exception {
146 BundleHelper mockBundlerHelper = mock(BundleHelper.class);
147 PowerMockito.whenNew(BundleHelper.class).withAnyArguments().thenReturn(mockBundlerHelper);
149 // test null bundle map
150 Mockito.when(mockBundlerHelper.getAppcLcmBundles()).thenReturn(null);
151 Assert.assertEquals("Should return unknown state", AppcOamStates.Unknown, stateHelper.getBundlesState());
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());
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);
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());
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());