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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
package org.openecomp.portalapp.portal.test.controller;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.openecomp.portalapp.portal.controller.PortalAdminController;
import org.openecomp.portalapp.portal.domain.EPRole;
import org.openecomp.portalapp.portal.domain.EPUser;
import org.openecomp.portalapp.portal.exceptions.NoHealthyServiceException;
import org.openecomp.portalapp.portal.service.AdminRolesService;
import org.openecomp.portalapp.portal.service.AdminRolesServiceImpl;
import org.openecomp.portalapp.portal.service.PortalAdminService;
import org.openecomp.portalapp.portal.service.PortalAdminServiceImpl;
import org.openecomp.portalapp.portal.test.core.MockEPUser;
import org.openecomp.portalapp.portal.transport.FieldsValidator;
import org.openecomp.portalapp.portal.transport.PortalAdmin;
import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
import org.openecomp.portalapp.test.framework.MockitoTestSuite;
import org.openecomp.portalapp.util.EPUserUtils;
import org.openecomp.portalsdk.core.service.AuditService;
import org.openecomp.portalsdk.core.service.AuditServiceImpl;
public class PortalAdminControllerTest extends MockitoTestSuite{
@InjectMocks
PortalAdminController portalAdminController = new PortalAdminController();
@Mock
AdminRolesService adminRolesService = new AdminRolesServiceImpl();
@Mock
PortalAdminService portalAdminService = new PortalAdminServiceImpl();
@Mock
AuditService auditService = new AuditServiceImpl();
@Mock
EcompPortalUtils ecompPortalUtils = new EcompPortalUtils();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
NullPointerException nullPointerException = new NullPointerException();
@Mock
EPUserUtils ePUserUtils = new EPUserUtils();
MockEPUser mockUser = new MockEPUser();
@Test
public void getPortalAdminsTest()
{
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
List<PortalAdmin> expectedPortalAdminsList = new ArrayList<PortalAdmin>();
PortalAdmin portalAdmin= new PortalAdmin();
portalAdmin.setUserId((long) 1);
portalAdmin.setLoginId("guestT");
portalAdmin.setFirstName("Test_FirstName");
portalAdmin.setLastName("Test_LastName");
expectedPortalAdminsList.add(portalAdmin);
Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true);
Mockito.when(portalAdminService.getPortalAdmins()).thenReturn(expectedPortalAdminsList);
List<PortalAdmin> actualPortalAdminsList = portalAdminController.getPortalAdmins(mockedRequest, mockedResponse);
assertEquals(actualPortalAdminsList,expectedPortalAdminsList);
}
// @Test
// public void getPortalAdminsIfUserIsNullTest()
// {
// EPUser user = null;
// Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
//
// assertNull(portalAdminController.getPortalAdmins(mockedRequest, mockedResponse));
//
// }
@Test
public void getPortalAdminsIfUserIsSuperAdminTest()
{
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
assertNull(portalAdminController.getPortalAdmins(mockedRequest, mockedResponse));
}
@Test
public void createPortalAdminTest()
{
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
FieldsValidator expectedFieldValidator = new FieldsValidator();
expectedFieldValidator.setHttpStatusCode((long) 200);
expectedFieldValidator.setFields(null);
expectedFieldValidator.setErrorCode(null);
FieldsValidator actualFieldValidator = new FieldsValidator();
String sbcid = "Test";
Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true);
Mockito.when(portalAdminService.createPortalAdmin(sbcid)).thenReturn(expectedFieldValidator);
actualFieldValidator = portalAdminController.createPortalAdmin(mockedRequest, sbcid, mockedResponse);
assertEquals(actualFieldValidator,expectedFieldValidator);
}
// @Test
// public void createPortalAdminIfUserIsNullTest()
// {
// //EPUser user = null;
// Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
// String sbcid = "null";
// assertNull(portalAdminController.createPortalAdmin(mockedRequest, sbcid, mockedResponse));
//
// }
@Test
public void createPortalAdminIfUserIsSuperAdminTest()
{
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
String sbcid = "Test";
assertNull(portalAdminController.createPortalAdmin(mockedRequest, sbcid, mockedResponse));
}
@Test
public void deletePortalAdminTest()
{
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
FieldsValidator expectedFieldValidator = new FieldsValidator();
expectedFieldValidator.setHttpStatusCode((long) 200);
expectedFieldValidator.setFields(null);
expectedFieldValidator.setErrorCode(null);
FieldsValidator actualFieldValidator = new FieldsValidator();
String userInfo = "1-test";
Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true);
Mockito.when(portalAdminService.deletePortalAdmin((long) 1)).thenReturn(expectedFieldValidator);
actualFieldValidator = portalAdminController.deletePortalAdmin(mockedRequest, userInfo, mockedResponse);
assertEquals(actualFieldValidator,expectedFieldValidator);
}
@Test
public void deletePortalAdminIfUserIsSuperAdminTest()
{
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
String userInfo = "1-test";
assertNull(portalAdminController.deletePortalAdmin(mockedRequest, userInfo, mockedResponse));
}
@Test
public void deletePortalAdminWithNoUserInfoTest()
{
EPUser user = mockUser.mockEPUser();
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
String userInfo = "";
assertNull(portalAdminController.deletePortalAdmin(mockedRequest, userInfo, mockedResponse));
}
@Test
public void getRolesByAppIfUserNullTest()
{
EPUser user = null;
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
Long appId = (long) 1;
assertNull(portalAdminController.getRolesByApp(mockedRequest, appId, mockedResponse));
}
@Test
public void getRolesByAppTest()
{
EPUser user = mockUser.mockEPUser();;
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
List<EPRole> expectedRoleList = new ArrayList<EPRole>();
EPRole ePRole = new EPRole();
expectedRoleList.add(ePRole);
Long appId = (long) 1;
Mockito.when(adminRolesService.getRolesByApp(user, appId)).thenReturn(expectedRoleList);
List<EPRole> actualRoleList = portalAdminController.getRolesByApp(mockedRequest, appId, mockedResponse);
assertEquals(actualRoleList,expectedRoleList);
}
@Test
public void getRolesByAppExceptionTest()
{
EPUser user = mockUser.mockEPUser();;
Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
List<EPRole> expectedRoleList = new ArrayList<EPRole>();
EPRole ePRole = new EPRole();
expectedRoleList.add(ePRole);
Long appId = (long) 1;
Mockito.when(adminRolesService.getRolesByApp(user, appId)).thenThrow(nullPointerException);
assertNull(portalAdminController.getRolesByApp(mockedRequest, appId, mockedResponse));
}
}
|