Skip to content

Commit eda7679

Browse files
committed
tests
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 6d2c124 commit eda7679

File tree

7 files changed

+742
-84
lines changed

7 files changed

+742
-84
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.admin.guest;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.junit.MockitoJUnitRunner;
23+
import org.springframework.test.util.ReflectionTestUtils;
24+
25+
@RunWith(MockitoJUnitRunner.class)
26+
public class AddGuestOsCategoryCmdTest {
27+
28+
@Test
29+
public void testGetArch() {
30+
AddGuestOsCategoryCmd cmd = new AddGuestOsCategoryCmd();
31+
Assert.assertNull(cmd.getName());
32+
String name = "Name";
33+
ReflectionTestUtils.setField(cmd, "name", name);
34+
Assert.assertEquals(name, cmd.getName());
35+
}
36+
37+
@Test
38+
public void testIsFeatured() {
39+
AddGuestOsCategoryCmd cmd = new AddGuestOsCategoryCmd();
40+
Assert.assertFalse(cmd.isFeatured());
41+
ReflectionTestUtils.setField(cmd, "featured", false);
42+
Assert.assertFalse(cmd.isFeatured());
43+
ReflectionTestUtils.setField(cmd, "featured", true);
44+
Assert.assertTrue(cmd.isFeatured());
45+
}
46+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.admin.guest;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.junit.MockitoJUnitRunner;
23+
import org.springframework.test.util.ReflectionTestUtils;
24+
25+
@RunWith(MockitoJUnitRunner.class)
26+
public class UpdateGuestOsCategoryCmdTest {
27+
28+
@Test
29+
public void testGetArch() {
30+
UpdateGuestOsCategoryCmd cmd = new UpdateGuestOsCategoryCmd();
31+
Assert.assertNull(cmd.getName());
32+
String name = "Name";
33+
ReflectionTestUtils.setField(cmd, "name", name);
34+
Assert.assertEquals(name, cmd.getName());
35+
}
36+
37+
@Test
38+
public void testIsFeatured() {
39+
UpdateGuestOsCategoryCmd cmd = new UpdateGuestOsCategoryCmd();
40+
Assert.assertNull(cmd.isFeatured());
41+
ReflectionTestUtils.setField(cmd, "featured", false);
42+
Assert.assertFalse(cmd.isFeatured());
43+
ReflectionTestUtils.setField(cmd, "featured", true);
44+
Assert.assertTrue(cmd.isFeatured());
45+
}
46+
47+
@Test
48+
public void testGetSortKey() {
49+
UpdateGuestOsCategoryCmd cmd = new UpdateGuestOsCategoryCmd();
50+
Assert.assertNull(cmd.getSortKey());
51+
Integer sortKey = 100;
52+
ReflectionTestUtils.setField(cmd, "sortKey", sortKey);
53+
Assert.assertEquals(sortKey, cmd.getSortKey());
54+
}
55+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.admin.guest;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.junit.MockitoJUnitRunner;
23+
import org.springframework.test.util.ReflectionTestUtils;
24+
25+
@RunWith(MockitoJUnitRunner.class)
26+
public class UpdateGuestOsCmdTest {
27+
28+
29+
@Test
30+
public void testGetZoneId() {
31+
UpdateGuestOsCmd cmd = new UpdateGuestOsCmd();
32+
Assert.assertNull(cmd.getOsCategoryId());
33+
Long osCategoryId = 100L;
34+
ReflectionTestUtils.setField(cmd, "osCategoryId", osCategoryId);
35+
Assert.assertEquals(osCategoryId, cmd.getOsCategoryId());
36+
}
37+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.guest;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.junit.MockitoJUnitRunner;
23+
import org.springframework.test.util.ReflectionTestUtils;
24+
25+
import com.cloud.cpu.CPU;
26+
27+
@RunWith(MockitoJUnitRunner.class)
28+
public class ListGuestOsCategoriesCmdTest {
29+
30+
@Test
31+
public void testIsFeatured() {
32+
ListGuestOsCategoriesCmd cmd = new ListGuestOsCategoriesCmd();
33+
Assert.assertNull(cmd.isFeatured());
34+
ReflectionTestUtils.setField(cmd, "featured", false);
35+
Assert.assertFalse(cmd.isFeatured());
36+
ReflectionTestUtils.setField(cmd, "featured", true);
37+
Assert.assertTrue(cmd.isFeatured());
38+
}
39+
40+
@Test
41+
public void testIsIso() {
42+
ListGuestOsCategoriesCmd cmd = new ListGuestOsCategoriesCmd();
43+
Assert.assertNull(cmd.isIso());
44+
ReflectionTestUtils.setField(cmd, "iso", false);
45+
Assert.assertFalse(cmd.isIso());
46+
ReflectionTestUtils.setField(cmd, "iso", true);
47+
Assert.assertTrue(cmd.isIso());
48+
}
49+
50+
@Test
51+
public void testGetZoneId() {
52+
ListGuestOsCategoriesCmd cmd = new ListGuestOsCategoriesCmd();
53+
Assert.assertNull(cmd.getZoneId());
54+
Long zoneId = 100L;
55+
ReflectionTestUtils.setField(cmd, "zoneId", zoneId);
56+
Assert.assertEquals(zoneId, cmd.getZoneId());
57+
}
58+
59+
@Test
60+
public void testGetArch() {
61+
ListGuestOsCategoriesCmd cmd = new ListGuestOsCategoriesCmd();
62+
Assert.assertNull(cmd.getArch());
63+
CPU.CPUArch arch = CPU.CPUArch.getDefault();
64+
ReflectionTestUtils.setField(cmd, "arch", arch.getType());
65+
Assert.assertEquals(arch, cmd.getArch());
66+
}
67+
}

engine/schema/src/test/java/com/cloud/storage/dao/VMTemplateDaoImplTest.java

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
import static org.junit.Assert.assertNull;
2222
import static org.mockito.ArgumentMatchers.any;
2323
import static org.mockito.Mockito.doReturn;
24+
import static org.mockito.Mockito.eq;
2425
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.never;
27+
import static org.mockito.Mockito.times;
28+
import static org.mockito.Mockito.verify;
2529
import static org.mockito.Mockito.when;
2630

2731
import java.util.ArrayList;
@@ -44,8 +48,11 @@
4448
import com.cloud.hypervisor.Hypervisor;
4549
import com.cloud.storage.Storage;
4650
import com.cloud.storage.VMTemplateVO;
51+
import com.cloud.storage.VMTemplateZoneVO;
4752
import com.cloud.utils.Pair;
4853
import com.cloud.utils.db.Filter;
54+
import com.cloud.utils.db.GenericSearchBuilder;
55+
import com.cloud.utils.db.JoinBuilder;
4956
import com.cloud.utils.db.SearchBuilder;
5057
import com.cloud.utils.db.SearchCriteria;
5158

@@ -55,6 +62,9 @@ public class VMTemplateDaoImplTest {
5562
@Mock
5663
HostDao hostDao;
5764

65+
@Mock
66+
VMTemplateZoneDao templateZoneDao;
67+
5868
@Spy
5969
@InjectMocks
6070
VMTemplateDaoImpl templateDao = new VMTemplateDaoImpl();
@@ -186,4 +196,107 @@ public void testFindLatestTemplateByTypeAndHypervisorAndArch_NotFound() {
186196
VMTemplateVO result = templateDao.findLatestTemplateByTypeAndHypervisorAndArch(hypervisorType, arch, type);
187197
assertNull(result);
188198
}
199+
200+
private void mockTemplateZoneJoin() {
201+
VMTemplateZoneVO templateZoneVO = mock(VMTemplateZoneVO.class);
202+
SearchBuilder<VMTemplateZoneVO> templateZoneVOSearchBuilder = mock(SearchBuilder.class);
203+
when(templateZoneVOSearchBuilder.entity()).thenReturn(templateZoneVO);
204+
when(templateZoneDao.createSearchBuilder()).thenReturn(templateZoneVOSearchBuilder);
205+
}
206+
207+
@Test
208+
public void testListTemplateIsoByArchAndZone_WithDataCenterId() {
209+
Long dataCenterId = 1L;
210+
CPU.CPUArch arch = CPU.CPUArch.getDefault();
211+
Boolean isIso = true;
212+
VMTemplateVO templateVO = mock(VMTemplateVO.class);
213+
GenericSearchBuilder<VMTemplateVO, Long> searchBuilder = mock(GenericSearchBuilder.class);
214+
when(searchBuilder.entity()).thenReturn(templateVO);
215+
SearchCriteria<Long>searchCriteria = mock(SearchCriteria.class);
216+
when(templateDao.createSearchBuilder(Long.class)).thenReturn(searchBuilder);
217+
when(searchBuilder.create()).thenReturn(searchCriteria);
218+
mockTemplateZoneJoin();
219+
doReturn(new ArrayList<>()).when(templateDao).customSearch(searchCriteria, null);
220+
List<Long> result = templateDao.listTemplateIsoByArchAndZone(dataCenterId, arch, isIso);
221+
assertNotNull(result);
222+
verify(searchBuilder, times(1)).select(null, SearchCriteria.Func.DISTINCT, templateVO.getGuestOSId());
223+
verify(searchBuilder, times(1)).and(eq("state"), any(), eq(SearchCriteria.Op.IN));
224+
verify(searchBuilder, times(1)).and(eq("type"), any(), eq(SearchCriteria.Op.IN));
225+
verify(searchBuilder, times(1)).and(eq("arch"), any(), eq(SearchCriteria.Op.EQ));
226+
verify(searchBuilder, times(1)).and(eq("isIso"), any(), eq(SearchCriteria.Op.EQ));
227+
verify(searchBuilder, times(1)).join(eq("templateZoneSearch"), any(), any(), any(), eq(JoinBuilder.JoinType.INNER));
228+
verify(templateDao, times(1)).customSearch(searchCriteria, null);
229+
}
230+
231+
@Test
232+
public void testListTemplateIsoByArchAndZone_WithoutDataCenterId() {
233+
Long dataCenterId = null;
234+
CPU.CPUArch arch = CPU.CPUArch.getDefault();
235+
Boolean isIso = false;
236+
VMTemplateVO templateVO = mock(VMTemplateVO.class);
237+
GenericSearchBuilder<VMTemplateVO, Long> searchBuilder = mock(GenericSearchBuilder.class);
238+
when(searchBuilder.entity()).thenReturn(templateVO);
239+
SearchCriteria<Long>searchCriteria = mock(SearchCriteria.class);
240+
when(templateDao.createSearchBuilder(Long.class)).thenReturn(searchBuilder);
241+
when(searchBuilder.create()).thenReturn(searchCriteria);
242+
doReturn(new ArrayList<>()).when(templateDao).customSearch(searchCriteria, null);
243+
List<Long> result = templateDao.listTemplateIsoByArchAndZone(dataCenterId, arch, isIso);
244+
assertNotNull(result);
245+
verify(searchBuilder, times(1)).select(null, SearchCriteria.Func.DISTINCT, templateVO.getGuestOSId());
246+
verify(searchBuilder, times(1)).and(eq("state"), any(), eq(SearchCriteria.Op.IN));
247+
verify(searchBuilder, times(1)).and(eq("type"), any(), eq(SearchCriteria.Op.IN));
248+
verify(searchBuilder, times(1)).and(eq("arch"), any(), eq(SearchCriteria.Op.EQ));
249+
verify(searchBuilder, times(1)).and(eq("isIso"), any(), eq(SearchCriteria.Op.NEQ));
250+
verify(searchBuilder, never()).join(eq("templateZoneSearch"), any(), any(), any(), eq(JoinBuilder.JoinType.INNER));
251+
verify(templateDao, times(1)).customSearch(searchCriteria, null);
252+
}
253+
254+
@Test
255+
public void testListTemplateIsoByArchAndZone_WithoutArch() {
256+
Long dataCenterId = 1L;
257+
CPU.CPUArch arch = null;
258+
Boolean isIso = true;
259+
VMTemplateVO templateVO = mock(VMTemplateVO.class);
260+
GenericSearchBuilder<VMTemplateVO, Long> searchBuilder = mock(GenericSearchBuilder.class);
261+
when(searchBuilder.entity()).thenReturn(templateVO);
262+
SearchCriteria<Long>searchCriteria = mock(SearchCriteria.class);
263+
when(templateDao.createSearchBuilder(Long.class)).thenReturn(searchBuilder);
264+
when(searchBuilder.create()).thenReturn(searchCriteria);
265+
mockTemplateZoneJoin();
266+
doReturn(new ArrayList<>()).when(templateDao).customSearch(searchCriteria, null);
267+
List<Long> result = templateDao.listTemplateIsoByArchAndZone(dataCenterId, arch, isIso);
268+
assertNotNull(result);
269+
verify(searchBuilder, times(1)).select(null, SearchCriteria.Func.DISTINCT, templateVO.getGuestOSId());
270+
verify(searchBuilder, times(1)).and(eq("state"), any(), eq(SearchCriteria.Op.IN));
271+
verify(searchBuilder, times(1)).and(eq("type"), any(), eq(SearchCriteria.Op.IN));
272+
verify(searchBuilder, times(1)).and(eq("arch"), any(), eq(SearchCriteria.Op.EQ));
273+
verify(searchBuilder, times(1)).and(eq("isIso"), any(), eq(SearchCriteria.Op.EQ));
274+
verify(searchBuilder, times(1)).join(eq("templateZoneSearch"), any(), any(), any(), eq(JoinBuilder.JoinType.INNER));
275+
verify(templateDao, times(1)).customSearch(searchCriteria, null);
276+
}
277+
278+
@Test
279+
public void testListTemplateIsoByArchAndZone_WithoutIsIso() {
280+
Long dataCenterId = 1L;
281+
CPU.CPUArch arch = CPU.CPUArch.getDefault();
282+
Boolean isIso = null;
283+
VMTemplateVO templateVO = mock(VMTemplateVO.class);
284+
GenericSearchBuilder<VMTemplateVO, Long> searchBuilder = mock(GenericSearchBuilder.class);
285+
when(searchBuilder.entity()).thenReturn(templateVO);
286+
SearchCriteria<Long>searchCriteria = mock(SearchCriteria.class);
287+
when(templateDao.createSearchBuilder(Long.class)).thenReturn(searchBuilder);
288+
when(searchBuilder.create()).thenReturn(searchCriteria);
289+
mockTemplateZoneJoin();
290+
doReturn(new ArrayList<>()).when(templateDao).customSearch(searchCriteria, null);
291+
List<Long> result = templateDao.listTemplateIsoByArchAndZone(dataCenterId, arch, isIso);
292+
assertNotNull(result);
293+
verify(searchBuilder, times(1)).select(null, SearchCriteria.Func.DISTINCT, templateVO.getGuestOSId());
294+
verify(searchBuilder, times(1)).and(eq("state"), any(), eq(SearchCriteria.Op.IN));
295+
verify(searchBuilder, times(1)).and(eq("type"), any(), eq(SearchCriteria.Op.IN));
296+
verify(searchBuilder, times(1)).and(eq("arch"), any(), eq(SearchCriteria.Op.EQ));
297+
verify(searchBuilder, never()).and(eq("isIso"), any(), eq(SearchCriteria.Op.NEQ));
298+
verify(searchBuilder, never()).and(eq("isIso"), any(), eq(SearchCriteria.Op.EQ));
299+
verify(searchBuilder, times(1)).join(eq("templateZoneSearch"), any(), any(), any(), eq(JoinBuilder.JoinType.INNER));
300+
verify(templateDao, times(1)).customSearch(searchCriteria, null);
301+
}
189302
}

0 commit comments

Comments
 (0)