1+ // Licensed to the Apache Software Foundation (ASF) under one
2+ // or more contributor license agreements. See the NOTICE file
3+ // distributed with this work for additional information
4+ // regarding copyright ownership. The ASF licenses this file
5+ // to you under the Apache License, Version 2.0 (the
6+ // "License"); you may not use this file except in compliance
7+ // with the License. You may obtain a copy of the License at
8+ //
9+ // http://www.apache.org/licenses/LICENSE-2.0
10+ //
11+ // Unless required by applicable law or agreed to in writing,
12+ // software distributed under the License is distributed on an
13+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+ // KIND, either express or implied. See the License for the
15+ // specific language governing permissions and limitations
16+ // under the License.
17+ package org .apache .cloudstack .api .command .user .vm ;
18+
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertNull ;
21+ import static org .junit .Assert .assertTrue ;
22+ import static org .mockito .Mockito .anyList ;
23+ import static org .mockito .Mockito .anySet ;
24+ import static org .mockito .Mockito .doReturn ;
25+ import static org .mockito .Mockito .eq ;
26+ import static org .mockito .Mockito .mock ;
27+ import static org .mockito .Mockito .never ;
28+ import static org .mockito .Mockito .spy ;
29+ import static org .mockito .Mockito .verify ;
30+ import static org .mockito .Mockito .when ;
31+
32+ import java .util .Arrays ;
33+ import java .util .Collections ;
34+ import java .util .HashMap ;
35+ import java .util .List ;
36+ import java .util .Map ;
37+ import java .util .Set ;
38+ import java .util .UUID ;
39+
40+ import org .apache .cloudstack .api .ResponseGenerator ;
41+ import org .apache .cloudstack .api .response .ResourceIconResponse ;
42+ import org .apache .cloudstack .api .response .UserVmResponse ;
43+ import org .junit .Before ;
44+ import org .junit .Test ;
45+ import org .mockito .Mockito ;
46+
47+ import com .cloud .server .ResourceIcon ;
48+ import com .cloud .server .ResourceIconManager ;
49+ import com .cloud .server .ResourceTag ;
50+ import com .cloud .storage .GuestOS ;
51+ import com .cloud .utils .db .EntityManager ;
52+
53+ public class ListVMsCmdTest {
54+
55+ EntityManager _entityMgr ;
56+ ResourceIconManager resourceIconManager ;
57+ ResponseGenerator _responseGenerator ;
58+
59+ ListVMsCmd cmd ;
60+
61+ @ Before
62+ public void setup () {
63+ _entityMgr = mock (EntityManager .class );
64+ resourceIconManager = mock (ResourceIconManager .class );
65+ _responseGenerator = mock (ResponseGenerator .class );
66+ cmd = spy (ListVMsCmd .class );
67+ cmd ._entityMgr = _entityMgr ;
68+ cmd .resourceIconManager = resourceIconManager ;
69+ cmd ._responseGenerator = _responseGenerator ;
70+ }
71+
72+ @ Test
73+ public void testUpdateVMResponse_withMixedIcons () {
74+ String vm1Uuid = UUID .randomUUID ().toString ();
75+ UserVmResponse vm1 = mock (UserVmResponse .class );
76+ when (vm1 .getId ()).thenReturn (vm1Uuid );
77+ String vm2Uuid = UUID .randomUUID ().toString ();
78+ UserVmResponse vm2 = mock (UserVmResponse .class );
79+ when (vm2 .getId ()).thenReturn (vm2Uuid );
80+ List <UserVmResponse > responses = Arrays .asList (vm1 , vm2 );
81+ ResourceIcon icon1 = mock (ResourceIcon .class );
82+ ResourceIcon icon2 = mock (ResourceIcon .class );
83+ Map <String , ResourceIcon > initialIcons = new HashMap <>();
84+ initialIcons .put (vm1Uuid , icon1 );
85+ when (resourceIconManager .getByResourceTypeAndUuids (ResourceTag .ResourceObjectType .UserVm , Set .of (vm1Uuid , vm2Uuid )))
86+ .thenReturn (initialIcons );
87+ Map <String , ResourceIcon > fallbackIcons = Map .of (vm2Uuid , icon2 );
88+ doReturn (fallbackIcons ).when (cmd ).getResourceIconsForUsingTemplateIso (anyList ());
89+ ResourceIconResponse iconResponse1 = new ResourceIconResponse ();
90+ ResourceIconResponse iconResponse2 = new ResourceIconResponse ();
91+ when (_responseGenerator .createResourceIconResponse (icon1 )).thenReturn (iconResponse1 );
92+ when (_responseGenerator .createResourceIconResponse (icon2 )).thenReturn (iconResponse2 );
93+ cmd .updateVMResponse (responses );
94+ verify (vm1 ).setResourceIconResponse (iconResponse1 );
95+ verify (vm2 ).setResourceIconResponse (iconResponse2 );
96+ }
97+
98+ @ Test
99+ public void testUpdateVMResponse_withEmptyList () {
100+ cmd .updateVMResponse (Collections .emptyList ());
101+ verify (resourceIconManager , never ()).getByResourceTypeAndIds (Mockito .any (), Mockito .anyCollection ());
102+ }
103+
104+ @ Test
105+ public void testGetResourceIconsForUsingTemplateIso_withValidData () {
106+ String vm1Uuid = UUID .randomUUID ().toString ();
107+ String template1Uuid = UUID .randomUUID ().toString ();
108+ UserVmResponse vm1 = mock (UserVmResponse .class );
109+ when (vm1 .getId ()).thenReturn (vm1Uuid );
110+ when (vm1 .getTemplateId ()).thenReturn (template1Uuid );
111+ when (vm1 .getIsoId ()).thenReturn (null );
112+ String vm2Uuid = UUID .randomUUID ().toString ();
113+ String iso2Uuid = UUID .randomUUID ().toString ();
114+ UserVmResponse vm2 = mock (UserVmResponse .class );
115+ when (vm2 .getId ()).thenReturn (vm2Uuid );
116+ when (vm2 .getTemplateId ()).thenReturn (null );
117+ when (vm2 .getIsoId ()).thenReturn (iso2Uuid );
118+ List <UserVmResponse > responses = Arrays .asList (vm1 , vm2 );
119+ Map <String , ResourceIcon > templateIcons = new HashMap <>();
120+ templateIcons .put (template1Uuid , mock (ResourceIcon .class ));
121+ Map <String , ResourceIcon > isoIcons = new HashMap <>();
122+ isoIcons .put (iso2Uuid , mock (ResourceIcon .class ));
123+ when (resourceIconManager .getByResourceTypeAndUuids (ResourceTag .ResourceObjectType .Template , Set .of (template1Uuid )))
124+ .thenReturn (templateIcons );
125+ when (resourceIconManager .getByResourceTypeAndUuids (ResourceTag .ResourceObjectType .ISO , Set .of (iso2Uuid )))
126+ .thenReturn (isoIcons );
127+ doReturn (Collections .emptyMap ()).when (cmd ).getResourceIconsUsingOsCategory (anyList ());
128+ Map <String , ResourceIcon > result = cmd .getResourceIconsForUsingTemplateIso (responses );
129+ assertEquals (2 , result .size ());
130+ assertTrue (result .containsKey (vm1Uuid ));
131+ assertTrue (result .containsKey (vm2Uuid ));
132+ assertEquals (templateIcons .get (template1Uuid ), result .get (vm1Uuid ));
133+ assertEquals (isoIcons .get (iso2Uuid ), result .get (vm2Uuid ));
134+ }
135+
136+ @ Test
137+ public void testGetResourceIconsForUsingTemplateIso_withMissingIcons () {
138+ String vm1Uuid = UUID .randomUUID ().toString ();
139+ String template1Uuid = UUID .randomUUID ().toString ();
140+ UserVmResponse vm1 = mock (UserVmResponse .class );
141+ when (vm1 .getId ()).thenReturn (vm1Uuid );
142+ when (vm1 .getTemplateId ()).thenReturn (template1Uuid );
143+ when (vm1 .getIsoId ()).thenReturn (null );
144+ List <UserVmResponse > responses = List .of (vm1 );
145+ when (resourceIconManager .getByResourceTypeAndUuids (eq (ResourceTag .ResourceObjectType .Template ), anySet ()))
146+ .thenReturn (Collections .emptyMap ());
147+ when (resourceIconManager .getByResourceTypeAndUuids (eq (ResourceTag .ResourceObjectType .ISO ), anySet ()))
148+ .thenReturn (Collections .emptyMap ());
149+ Map <String , ResourceIcon > fallbackIcons = Map .of (vm1Uuid , mock (ResourceIcon .class ));
150+ doReturn (fallbackIcons ).when (cmd ).getResourceIconsUsingOsCategory (anyList ());
151+ Map <String , ResourceIcon > result = cmd .getResourceIconsForUsingTemplateIso (responses );
152+ assertEquals (1 , result .size ());
153+ assertEquals (fallbackIcons .get ("vm1" ), result .get ("vm1" ));
154+ }
155+
156+ @ Test
157+ public void testGetResourceIconsUsingOsCategory_withValidData () {
158+ String vm1Uuid = UUID .randomUUID ().toString ();
159+ String os1Uuid = UUID .randomUUID ().toString ();
160+ UserVmResponse vm1 = mock (UserVmResponse .class );
161+ when (vm1 .getGuestOsId ()).thenReturn (os1Uuid );
162+ when (vm1 .getId ()).thenReturn (vm1Uuid );
163+ String vm2Uuid = UUID .randomUUID ().toString ();
164+ String os2Uuid = UUID .randomUUID ().toString ();
165+ UserVmResponse vm2 = mock (UserVmResponse .class );
166+ when (vm2 .getGuestOsId ()).thenReturn (os2Uuid );
167+ when (vm2 .getId ()).thenReturn (vm2Uuid );
168+ List <UserVmResponse > responses = Arrays .asList (vm1 , vm2 );
169+ GuestOS guestOS1 = mock (GuestOS .class );
170+ when (guestOS1 .getUuid ()).thenReturn (os1Uuid );
171+ when (guestOS1 .getCategoryId ()).thenReturn (10L );
172+ GuestOS guestOS2 = mock (GuestOS .class );
173+ when (guestOS2 .getUuid ()).thenReturn (os2Uuid );
174+ when (guestOS2 .getCategoryId ()).thenReturn (20L );
175+ when (_entityMgr .listByUuids (eq (GuestOS .class ), anySet ()))
176+ .thenReturn (Arrays .asList (guestOS1 , guestOS2 ));
177+ ResourceIcon icon1 = mock (ResourceIcon .class );
178+ ResourceIcon icon2 = mock (ResourceIcon .class );
179+ Map <Long , ResourceIcon > categoryIcons = new HashMap <>();
180+ categoryIcons .put (10L , icon1 );
181+ categoryIcons .put (20L , icon2 );
182+ when (resourceIconManager .getByResourceTypeAndIds (eq (ResourceTag .ResourceObjectType .GuestOsCategory ), anySet ()))
183+ .thenReturn (categoryIcons );
184+ Map <String , ResourceIcon > result = cmd .getResourceIconsUsingOsCategory (responses );
185+ assertEquals (2 , result .size ());
186+ assertEquals (icon1 , result .get (vm1Uuid ));
187+ assertEquals (icon2 , result .get (vm2Uuid ));
188+ }
189+
190+ @ Test
191+ public void testGetResourceIconsUsingOsCategory_missingGuestOS () {
192+ String vm1Uuid = UUID .randomUUID ().toString ();
193+ String os1Uuid = UUID .randomUUID ().toString ();
194+ UserVmResponse vm1 = mock (UserVmResponse .class );
195+ when (vm1 .getGuestOsId ()).thenReturn (vm1Uuid );
196+ when (vm1 .getId ()).thenReturn (os1Uuid );
197+ List <UserVmResponse > responses = Collections .singletonList (vm1 );
198+ when (_entityMgr .listByUuids (eq (GuestOS .class ), anySet ()))
199+ .thenReturn (Collections .emptyList ());
200+ Map <String , ResourceIcon > result = cmd .getResourceIconsUsingOsCategory (responses );
201+ assertTrue (result .isEmpty ());
202+ }
203+
204+ @ Test
205+ public void testGetResourceIconsUsingOsCategory_missingIcon () {
206+ UserVmResponse vm1 = mock (UserVmResponse .class );
207+ String vmUuid = UUID .randomUUID ().toString ();
208+ String osUuid = UUID .randomUUID ().toString ();
209+ when (vm1 .getGuestOsId ()).thenReturn (osUuid );
210+ when (vm1 .getId ()).thenReturn (vmUuid );
211+ List <UserVmResponse > responses = Collections .singletonList (vm1 );
212+ GuestOS guestOS1 = mock (GuestOS .class );
213+ when (guestOS1 .getCategoryId ()).thenReturn (10L );
214+ when (guestOS1 .getUuid ()).thenReturn (osUuid );
215+ when (_entityMgr .listByUuids (eq (GuestOS .class ), anySet ()))
216+ .thenReturn (Collections .singletonList (guestOS1 ));
217+ when (resourceIconManager .getByResourceTypeAndIds (eq (ResourceTag .ResourceObjectType .GuestOsCategory ), anySet ()))
218+ .thenReturn (Collections .emptyMap ());
219+ Map <String , ResourceIcon > result = cmd .getResourceIconsUsingOsCategory (responses );
220+ assertTrue (result .containsKey (vmUuid ));
221+ assertNull (result .get (vmUuid ));
222+ }
223+
224+ }
0 commit comments