3030import org .apache .cloudstack .api .command .user .iso .DeleteIsoCmd ;
3131import org .apache .cloudstack .api .command .user .iso .RegisterIsoCmd ;
3232import org .apache .cloudstack .api .command .user .kubernetes .version .ListKubernetesSupportedVersionsCmd ;
33+ import org .apache .cloudstack .api .response .KubernetesSupportedVersionResponse ;
34+ import org .apache .cloudstack .api .response .ListResponse ;
3335import org .apache .cloudstack .context .CallContext ;
3436import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine ;
3537import org .apache .cloudstack .framework .config .ConfigKey ;
3638import org .junit .After ;
39+ import org .junit .Assert ;
3740import org .junit .Before ;
3841import org .junit .Test ;
3942import org .junit .runner .RunWith ;
4043import org .mockito .InjectMocks ;
4144import org .mockito .Mock ;
45+ import org .mockito .MockedStatic ;
4246import org .mockito .Mockito ;
4347import org .mockito .MockitoAnnotations ;
44- import org .powermock .api .mockito .PowerMockito ;
45- import org .powermock .core .classloader .annotations .PrepareForTest ;
46- import org .powermock .modules .junit4 .PowerMockRunner ;
48+ import org .mockito .junit .MockitoJUnitRunner ;
4749
4850import com .cloud .api .query .dao .TemplateJoinDao ;
4951import com .cloud .api .query .vo .TemplateJoinVO ;
7072import com .cloud .utils .db .SearchCriteria ;
7173import com .cloud .utils .exception .CloudRuntimeException ;
7274
73- @ RunWith (PowerMockRunner .class )
74- @ PrepareForTest ({ComponentContext .class })
75+ @ RunWith (MockitoJUnitRunner .class )
7576public class KubernetesVersionServiceTest {
7677
7778 @ InjectMocks
@@ -92,6 +93,9 @@ public class KubernetesVersionServiceTest {
9293 @ Mock
9394 private TemplateApiService templateService ;
9495
96+ AutoCloseable closeable ;
97+
98+
9599 private void overrideDefaultConfigValue (final ConfigKey configKey , final String name , final Object o ) throws IllegalAccessException , NoSuchFieldException {
96100 Field f = ConfigKey .class .getDeclaredField (name );
97101 f .setAccessible (true );
@@ -100,7 +104,7 @@ private void overrideDefaultConfigValue(final ConfigKey configKey, final String
100104
101105 @ Before
102106 public void setUp () throws Exception {
103- MockitoAnnotations .initMocks (this );
107+ closeable = MockitoAnnotations .openMocks (this );
104108
105109 overrideDefaultConfigValue (KubernetesClusterService .KubernetesServiceEnabled , "_defaultValue" , "true" );
106110
@@ -114,12 +118,9 @@ public void setUp() throws Exception {
114118 when (kubernetesSupportedVersionDao .createSearchCriteria ()).thenReturn (versionSearchCriteria );
115119
116120 DataCenterVO zone = Mockito .mock (DataCenterVO .class );
117- when (zone .getId ()).thenReturn (1L );
118121 when (dataCenterDao .findById (Mockito .anyLong ())).thenReturn (zone );
119122
120123 TemplateJoinVO templateJoinVO = Mockito .mock (TemplateJoinVO .class );
121- when (templateJoinVO .getId ()).thenReturn (1L );
122- when (templateJoinVO .getUrl ()).thenReturn ("https://download.cloudstack.com" );
123124 when (templateJoinVO .getState ()).thenReturn (ObjectInDataStoreStateMachine .State .Ready );
124125 when (templateJoinDao .findById (Mockito .anyLong ())).thenReturn (templateJoinVO );
125126
@@ -130,6 +131,7 @@ public void setUp() throws Exception {
130131
131132 @ After
132133 public void tearDown () throws Exception {
134+ closeable .close ();
133135 }
134136
135137 @ Test
@@ -141,7 +143,13 @@ public void listKubernetesSupportedVersionsTest() {
141143 versionVOs .add (versionVO );
142144 when (kubernetesSupportedVersionDao .findById (Mockito .anyLong ())).thenReturn (versionVO );
143145 when (kubernetesSupportedVersionDao .search (Mockito .any (SearchCriteria .class ), Mockito .any (Filter .class ))).thenReturn (versionVOs );
144- kubernetesVersionService .listKubernetesSupportedVersions (cmd );
146+ ListResponse <KubernetesSupportedVersionResponse > response =
147+ kubernetesVersionService .listKubernetesSupportedVersions (
148+ cmd );
149+ Assert .assertNotNull (response );
150+ Assert .assertEquals (Integer .valueOf (1 ), response .getCount ());
151+ Assert .assertEquals (1 , response .getResponses ().size ());
152+ Assert .assertEquals (KubernetesVersionService .MIN_KUBERNETES_VERSION , response .getResponses ().get (0 ).getSemanticVersion ());
145153 }
146154
147155 @ Test (expected = InvalidParameterValueException .class )
@@ -206,13 +214,19 @@ public void addKubernetesSupportedVersionIsoUrlTest() throws ResourceAllocationE
206214 when (cmd .getMinimumRamSize ()).thenReturn (KubernetesClusterService .MIN_KUBERNETES_CLUSTER_NODE_RAM_SIZE );
207215 Account systemAccount = new AccountVO ("system" , 1L , "" , Account .Type .ADMIN , "uuid" );
208216 when (accountManager .getSystemAccount ()).thenReturn (systemAccount );
209- PowerMockito .mockStatic (ComponentContext .class );
210- when (ComponentContext .inject (Mockito .any (RegisterIsoCmd .class ))).thenReturn (new RegisterIsoCmd ());
211- when (templateService .registerIso (Mockito .any (RegisterIsoCmd .class ))).thenReturn (Mockito .mock (VirtualMachineTemplate .class ));
212- VMTemplateVO templateVO = Mockito .mock (VMTemplateVO .class );
213- when (templateVO .getId ()).thenReturn (1L );
214- when (templateDao .findById (Mockito .anyLong ())).thenReturn (templateVO );
215- kubernetesVersionService .addKubernetesSupportedVersion (cmd );
217+ try (MockedStatic <ComponentContext > mockedComponentContext = Mockito .mockStatic (ComponentContext .class )) {
218+ mockedComponentContext .when (() -> ComponentContext .inject (Mockito .any (RegisterIsoCmd .class ))).thenReturn (
219+ new RegisterIsoCmd ());
220+
221+ when (templateService .registerIso (Mockito .any (RegisterIsoCmd .class ))).thenReturn (
222+ Mockito .mock (VirtualMachineTemplate .class ));
223+ VMTemplateVO templateVO = Mockito .mock (VMTemplateVO .class );
224+ when (templateVO .getId ()).thenReturn (1L );
225+ when (templateDao .findById (Mockito .anyLong ())).thenReturn (templateVO );
226+ KubernetesSupportedVersionResponse response = kubernetesVersionService .addKubernetesSupportedVersion (cmd );
227+ Assert .assertNotNull (response );
228+ Mockito .verify (kubernetesSupportedVersionDao , Mockito .times (1 )).persist (Mockito .any (KubernetesSupportedVersionVO .class ));
229+ }
216230 }
217231
218232 @ Test (expected = CloudRuntimeException .class )
@@ -237,12 +251,11 @@ public void deleteKubernetesSupportedVersionTest() {
237251 when (kubernetesSupportedVersionDao .findById (Mockito .anyLong ())).thenReturn (Mockito .mock (KubernetesSupportedVersionVO .class ));
238252 List <KubernetesClusterVO > clusters = new ArrayList <>();
239253 when (kubernetesClusterDao .listAllByKubernetesVersion (Mockito .anyLong ())).thenReturn (clusters );
240- when (templateDao .findById (Mockito .anyLong ())).thenReturn (Mockito .mock (VMTemplateVO .class ));
241- PowerMockito .mockStatic (ComponentContext .class );
242- when (ComponentContext .inject (Mockito .any (DeleteIsoCmd .class ))).thenReturn (new DeleteIsoCmd ());
243- when (templateService .deleteIso (Mockito .any (DeleteIsoCmd .class ))).thenReturn (true );
244- when (kubernetesClusterDao .remove (Mockito .anyLong ())).thenReturn (true );
245- kubernetesVersionService .deleteKubernetesSupportedVersion (cmd );
254+ try (MockedStatic <ComponentContext > mockedComponentContext = Mockito .mockStatic (ComponentContext .class )) {
255+ mockedComponentContext .when (() -> ComponentContext .inject (Mockito .any (DeleteIsoCmd .class ))).thenReturn (new DeleteIsoCmd ());
256+ kubernetesVersionService .deleteKubernetesSupportedVersion (cmd );
257+ Mockito .verify (kubernetesSupportedVersionDao ).remove (Mockito .anyLong ());
258+ }
246259 }
247260
248261 @ Test
@@ -254,11 +267,12 @@ public void updateKubernetesSupportedVersionTest() {
254267 CallContext .register (user , account );
255268 when (kubernetesSupportedVersionDao .findById (Mockito .anyLong ())).thenReturn (Mockito .mock (KubernetesSupportedVersionVO .class ));
256269 KubernetesSupportedVersionVO version = Mockito .mock (KubernetesSupportedVersionVO .class );
257- when (kubernetesSupportedVersionDao .createForUpdate (Mockito .anyLong ())).thenReturn (version );
258- when (kubernetesSupportedVersionDao .update (Mockito .anyLong (), Mockito .any (KubernetesSupportedVersionVO .class ))).thenReturn (true );
259270 when (version .getState ()).thenReturn (KubernetesSupportedVersion .State .Disabled );
260271 when (version .getSemanticVersion ()).thenReturn (KubernetesVersionService .MIN_KUBERNETES_VERSION );
261272 when (kubernetesSupportedVersionDao .findById (Mockito .anyLong ())).thenReturn (version );
262- kubernetesVersionService .updateKubernetesSupportedVersion (cmd );
273+ KubernetesSupportedVersionResponse response = kubernetesVersionService .updateKubernetesSupportedVersion (cmd );
274+ Assert .assertNotNull (response );
275+ Assert .assertEquals (KubernetesSupportedVersion .State .Disabled .toString (), response .getState ());
276+ Assert .assertEquals (KubernetesVersionService .MIN_KUBERNETES_VERSION , response .getSemanticVersion ());
263277 }
264278}
0 commit comments