forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceIconManagerImplTest.java
More file actions
132 lines (122 loc) · 6.11 KB
/
ResourceIconManagerImplTest.java
File metadata and controls
132 lines (122 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.resourceicon;
import com.cloud.network.dao.NetworkVO;
import com.cloud.resource.icon.ResourceIconVO;
import com.cloud.resource.icon.dao.ResourceIconDao;
import com.cloud.server.ResourceManagerUtil;
import com.cloud.server.ResourceTag;
import com.cloud.storage.VMTemplateVO;
import com.cloud.user.Account;
import com.cloud.user.AccountVO;
import com.cloud.user.User;
import com.cloud.user.UserVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.db.EntityManager;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.context.CallContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ResourceIconManagerImplTest {
public static final long ACCOUNT_ID = 1L;
public static final Long RESOURCE_ID = 1L;
@Mock
protected AccountDao accountDao;
@Mock
protected UserDao userDao;
@Mock
ResourceManagerUtil resourceManagerUtil;
@Mock
ResourceIconDao resourceIconDao;
@Mock
EntityManager entityMgr;
@Spy
@InjectMocks
private ResourceIconManagerImpl resourceIconManager = new ResourceIconManagerImpl();
private AccountVO account;
private UserVO user;
@Before
public void setup() throws Exception {
account = new AccountVO("testaccount", 1L, "networkdomain", Account.Type.NORMAL, "uuid");
account.setId(ACCOUNT_ID);
user = new UserVO(ACCOUNT_ID, "testuser", "password", "firstname", "lastName", "email", "timezone",
UUID.randomUUID().toString(), User.Source.UNKNOWN);
}
@Test
public void testUploadIconActionEventResourceDetailsUpdate() {
CallContext.register(user, account);
String uuid = UUID.randomUUID().toString();
ResourceTag.ResourceObjectType resourceType = ResourceTag.ResourceObjectType.Network;
List<String> resourceIds = new ArrayList<>();
resourceIds.add(uuid);
Mockito.when(resourceManagerUtil.getResourceId(uuid, resourceType)).thenReturn(RESOURCE_ID);
NetworkVO network = Mockito.mock(NetworkVO.class);
Mockito.when(network.getAccountId()).thenReturn(1L);
Mockito.when((network.getDomainId())).thenReturn(1L);
Mockito.when(entityMgr.findById(NetworkVO.class, RESOURCE_ID)).thenReturn(network);
Mockito.doNothing().when(resourceManagerUtil).checkResourceAccessible(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString());
Mockito.when(resourceIconDao.persist(Mockito.any(ResourceIconVO.class))).thenReturn(Mockito.mock(ResourceIconVO.class));
resourceIconManager.uploadResourceIcon(resourceIds, resourceType, "Something");
Assert.assertEquals(RESOURCE_ID, CallContext.current().getEventResourceId());
Assert.assertEquals(ApiCommandResourceType.Network, CallContext.current().getEventResourceType());
}
@Test
public void testDeleteIconActionEventResourceDetailsUpdate() {
CallContext.register(user, account);
String uuid = UUID.randomUUID().toString();
String iconUuid = UUID.randomUUID().toString();
ResourceTag.ResourceObjectType resourceType = ResourceTag.ResourceObjectType.Template;
List<String> resourceIds = new ArrayList<>();
resourceIds.add(uuid);
List<ResourceIconVO> resourceIcons = new ArrayList<>();
ResourceIconVO resourceIcon = Mockito.mock(ResourceIconVO.class);
Mockito.when(resourceIcon.getResourceUuid()).thenReturn(uuid);
resourceIcons.add(resourceIcon);
Mockito.when(resourceManagerUtil.getResourceId(uuid, resourceType)).thenReturn(RESOURCE_ID);
Mockito.when(resourceManagerUtil.getUuid(uuid, resourceType)).thenReturn(iconUuid);
SearchBuilder<ResourceIconVO> searchBuilder = Mockito.mock(SearchBuilder.class);
SearchCriteria<ResourceIconVO> searchCriteria = Mockito.mock(SearchCriteria.class);
when(searchBuilder.create()).thenReturn(searchCriteria);
when(searchBuilder.entity()).thenReturn(resourceIcon);
Mockito.when(resourceIconDao.createSearchBuilder()).thenReturn(searchBuilder);
Mockito.when(resourceIconDao.search(Mockito.any(), Mockito.any())).thenReturn(resourceIcons);
VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
Mockito.when(template.getAccountId()).thenReturn(1L);
Mockito.when((template.getDomainId())).thenReturn(1L);
Mockito.when(entityMgr.findById(VMTemplateVO.class, RESOURCE_ID)).thenReturn(template);
Mockito.doNothing().when(resourceManagerUtil).checkResourceAccessible(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString());
Mockito.when(resourceIconDao.remove(Mockito.anyLong())).thenReturn(true);
resourceIconManager.deleteResourceIcon(resourceIds, resourceType);
Assert.assertEquals(RESOURCE_ID, CallContext.current().getEventResourceId());
Assert.assertEquals(ApiCommandResourceType.Template, CallContext.current().getEventResourceType());
}
}