7b099824e086b666ebcf7145f78298efd7bb42ca
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / AppCatalogControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
42 import static org.junit.Assert.assertTrue;
43
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50 import javax.servlet.http.HttpSession;
51
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.mockito.InjectMocks;
55 import org.mockito.Mock;
56 import org.mockito.Mockito;
57 import org.mockito.MockitoAnnotations;
58 import org.onap.portalapp.portal.controller.AppCatalogController;
59 import org.onap.portalapp.portal.core.MockEPUser;
60 import org.onap.portalapp.portal.domain.EPApp;
61 import org.onap.portalapp.portal.domain.EPUser;
62 import org.onap.portalapp.portal.ecomp.model.AppCatalogItem;
63 import org.onap.portalapp.portal.framework.MockitoTestSuite;
64 import org.onap.portalapp.portal.service.AdminRolesService;
65 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
66 import org.onap.portalapp.portal.service.EPAppCommonServiceImpl;
67 import org.onap.portalapp.portal.service.EPAppService;
68 import org.onap.portalapp.portal.service.PersUserAppService;
69 import org.onap.portalapp.portal.service.PersUserAppServiceImpl;
70 import org.onap.portalapp.portal.transport.AppCatalogPersonalization;
71 import org.onap.portalapp.portal.transport.FieldsValidator;
72 import org.onap.portalapp.portal.transport.FieldsValidator.FieldName;
73 import org.onap.portalapp.util.EPUserUtils;
74 import org.onap.portalsdk.core.util.SystemProperties;
75
76 public class AppCatalogControllerTest extends MockitoTestSuite {
77
78         @Mock
79         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
80
81         @Mock
82         EPAppService appService = new EPAppCommonServiceImpl();
83
84         @InjectMocks
85         AppCatalogController appCatalogController = new AppCatalogController();
86
87         PersUserAppService persUserAppService = Mockito.spy(new PersUserAppServiceImpl());
88
89         @Before
90         public void setup() {
91                 MockitoAnnotations.initMocks(this);
92         }
93
94         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
95
96         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
97         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
98
99         @Mock
100         EPUserUtils ePUserUtils = new EPUserUtils();
101
102         @Mock
103         EPUser epuser;
104
105         NullPointerException nullPointerException = new NullPointerException();
106
107         MockEPUser mockUser = new MockEPUser();
108
109         public AppCatalogItem mockAppCatalogItem() {
110                 AppCatalogItem appCatalogItem = new AppCatalogItem();
111                 appCatalogItem.setId((long) 1);
112                 appCatalogItem.setName("Ecomp Portal");
113                 appCatalogItem.setImageUrl("Test_URL");
114                 appCatalogItem.setDescription("Testing");
115                 appCatalogItem.setNotes("Test");
116                 appCatalogItem.setUrl("test");
117                 appCatalogItem.setAlternateUrl("test");
118                 appCatalogItem.setRestricted(false);
119                 appCatalogItem.setOpen(false);
120                 appCatalogItem.setAccess(true);
121                 appCatalogItem.setSelect(true);
122                 appCatalogItem.setPending(false);
123
124                 return appCatalogItem;
125         }
126
127         @Test
128         public void getAppCatalogTestIfUserNotAdmin() throws IOException {
129                 EPUser user = mockUser.mockEPUser();
130                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
131                 List<AppCatalogItem> actualAppCatalogList = null;
132
133                 List<AppCatalogItem> expectedAppCatalog = new ArrayList<>();
134
135                 AppCatalogItem appCatalogItem = mockAppCatalogItem();
136                 expectedAppCatalog.add(appCatalogItem);
137                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
138                 Mockito.when(appService.getUserAppCatalog(user)).thenReturn(expectedAppCatalog);
139                 actualAppCatalogList = appCatalogController.getAppCatalog(mockedRequest, mockedResponse);
140
141                 assertTrue(actualAppCatalogList.contains(appCatalogItem));
142
143         }
144
145         @Test
146         public void getAppCatalogTestIfUserIsAdmin() throws IOException {
147                 EPUser user = mockUser.mockEPUser();
148                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
149                 List<AppCatalogItem> actualAppCatalogList = null;
150
151                 List<AppCatalogItem> expectedAppCatalog = new ArrayList<>();
152
153                 AppCatalogItem appCatalogItem = mockAppCatalogItem();
154
155                 expectedAppCatalog.add(appCatalogItem);
156                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true);
157                 Mockito.when(appService.getAdminAppCatalog(user)).thenReturn(expectedAppCatalog);
158                 actualAppCatalogList = appCatalogController.getAppCatalog(mockedRequest, mockedResponse);
159
160                 assertTrue(actualAppCatalogList.contains(appCatalogItem));
161
162         }
163
164         @Test
165         public void getAppCatalogTestIfUserisNull() throws IOException {
166                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
167                 List<AppCatalogItem> actualAppCatalogList = new ArrayList<>();
168                 ;
169                 actualAppCatalogList = appCatalogController.getAppCatalog(mockedRequest, mockedResponse);
170                 assertNull(actualAppCatalogList);
171
172         }
173
174         @Test
175         public void getAppCatalogTestIfUserThrowsExceptionTest() throws IOException {
176                 EPUser user = new EPUser();
177                 user.setFirstName("test");
178                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
179                 List<AppCatalogItem> actualAppCatalogList = new ArrayList<>();
180                 ;
181
182                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
183
184                 Mockito.when(appCatalogController.getAppCatalog(mockedRequest, mockedResponse)).thenThrow(nullPointerException);
185
186                 actualAppCatalogList = appCatalogController.getAppCatalog(mockedRequest, mockedResponse);
187                 assertNull(actualAppCatalogList);
188
189         }
190
191         @Test
192         public void putAppCatalogSelectionTestWhenAppIsNull() throws IOException {
193
194                 AppCatalogPersonalization persRequest = new AppCatalogPersonalization();
195                 persRequest.setAppId((long) 1);
196                 persRequest.setPending(false);
197                 persRequest.setSelect(false);
198
199                 EPUser user = mockUser.mockEPUser();
200
201                 FieldsValidator expectedFieldValidator = new FieldsValidator();
202
203                 FieldsValidator actualFieldValidator = new FieldsValidator();
204                 List<FieldName> fields = new ArrayList<>();
205                 ;
206
207                 expectedFieldValidator.setHttpStatusCode((long) 200);
208                 expectedFieldValidator.setFields(fields);
209                 expectedFieldValidator.setErrorCode(null);
210
211                 EPApp app = null;
212
213                 Mockito.when(appService.getApp(persRequest.getAppId())).thenReturn(app);
214
215                 HttpSession session = mockedRequest.getSession();
216                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
217
218                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
219                 actualFieldValidator = appCatalogController.putAppCatalogSelection(mockedRequest, persRequest, mockedResponse);
220                 assertEquals(expectedFieldValidator, actualFieldValidator);
221
222         }
223
224         @Test
225         public void putAppCatalogSelectionTest() throws IOException {
226
227                 AppCatalogPersonalization persRequest = new AppCatalogPersonalization();
228                 persRequest.setAppId((long) 1);
229                 persRequest.setPending(false);
230                 persRequest.setSelect(false);
231
232                 EPUser user = mockUser.mockEPUser();
233
234                 FieldsValidator expectedFieldValidator = new FieldsValidator();
235
236                 FieldsValidator actualFieldValidator = new FieldsValidator();
237                 List<FieldName> fields = new ArrayList<>();
238                 ;
239
240                 expectedFieldValidator.setHttpStatusCode((long) 200);
241                 expectedFieldValidator.setFields(fields);
242                 expectedFieldValidator.setErrorCode(null);
243
244                 EPApp app = new EPApp();
245
246                 app.setName("Test");
247                 app.setImageUrl("test");
248                 app.setDescription("test");
249                 app.setNotes("test");
250                 app.setUrl("test");
251                 app.setId((long) 1);
252                 app.setAppRestEndpoint("test");
253                 app.setAlternateUrl("test");
254                 app.setName("test");
255                 app.setMlAppName("test");
256                 app.setMlAppAdminId("test");
257                 app.setUsername("test");
258                 app.setAppPassword("test");
259                 app.setOpen(false);
260                 app.setEnabled(false);
261                 app.setUebKey("test");
262                 app.setUebSecret("test");
263                 app.setUebTopicName("test");
264                 app.setAppType(1);
265
266                 Mockito.when(appService.getApp(persRequest.getAppId())).thenReturn(app);
267
268                 HttpSession session = mockedRequest.getSession();
269                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
270
271                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
272                 Mockito.doNothing().when(persUserAppService).setPersUserAppValue(user, app, persRequest.getSelect(),
273                                 persRequest.getPending());
274
275                 actualFieldValidator = appCatalogController.putAppCatalogSelection(mockedRequest, persRequest, mockedResponse);
276
277                 assertEquals(expectedFieldValidator, actualFieldValidator);
278
279         }
280
281 }