2  * ============LICENSE_START==========================================
 
   4  * ===================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ===================================================================
 
   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
 
  13  *             http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  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
 
  26  *             https://creativecommons.org/licenses/by/4.0/
 
  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.
 
  34  * ============LICENSE_END============================================
 
  38 package org.onap.portalapp.portal.controller;
 
  40 import static org.junit.Assert.assertEquals;
 
  41 import static org.junit.Assert.assertNull;
 
  42 import static org.junit.Assert.assertTrue;
 
  44 import java.io.IOException;
 
  45 import java.util.ArrayList;
 
  46 import java.util.List;
 
  48 import javax.servlet.http.HttpServletRequest;
 
  49 import javax.servlet.http.HttpServletResponse;
 
  50 import javax.servlet.http.HttpSession;
 
  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;
 
  76 public class AppCatalogControllerTest extends MockitoTestSuite {
 
  79         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
 
  82         EPAppService appService = new EPAppCommonServiceImpl();
 
  85         AppCatalogController appCatalogController = new AppCatalogController();
 
  87         PersUserAppService persUserAppService = Mockito.spy(new PersUserAppServiceImpl());
 
  91                 MockitoAnnotations.initMocks(this);
 
  94         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
 
  96         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
 
  97         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
 
 100         EPUserUtils ePUserUtils = new EPUserUtils();
 
 105         NullPointerException nullPointerException = new NullPointerException();
 
 107         MockEPUser mockUser = new MockEPUser();
 
 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);
 
 124                 return appCatalogItem;
 
 128         public void getAppCatalogTestIfUserNotAdmin() throws IOException {
 
 129                 EPUser user = mockUser.mockEPUser();
 
 130                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
 
 131                 List<AppCatalogItem> actualAppCatalogList = null;
 
 133                 List<AppCatalogItem> expectedAppCatalog = new ArrayList<>();
 
 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);
 
 141                 assertTrue(actualAppCatalogList.contains(appCatalogItem));
 
 146         public void getAppCatalogTestIfUserIsAdmin() throws IOException {
 
 147                 EPUser user = mockUser.mockEPUser();
 
 148                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
 
 149                 List<AppCatalogItem> actualAppCatalogList = null;
 
 151                 List<AppCatalogItem> expectedAppCatalog = new ArrayList<>();
 
 153                 AppCatalogItem appCatalogItem = mockAppCatalogItem();
 
 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);
 
 160                 assertTrue(actualAppCatalogList.contains(appCatalogItem));
 
 165         public void getAppCatalogTestIfUserisNull() throws IOException {
 
 166                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
 
 167                 List<AppCatalogItem> actualAppCatalogList = new ArrayList<>();
 
 169                 actualAppCatalogList = appCatalogController.getAppCatalog(mockedRequest, mockedResponse);
 
 170                 assertNull(actualAppCatalogList);
 
 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<>();
 
 182                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
 
 184                 Mockito.when(appCatalogController.getAppCatalog(mockedRequest, mockedResponse)).thenThrow(nullPointerException);
 
 186                 actualAppCatalogList = appCatalogController.getAppCatalog(mockedRequest, mockedResponse);
 
 187                 assertNull(actualAppCatalogList);
 
 192         public void putAppCatalogSelectionTestWhenAppIsNull() throws IOException {
 
 194                 AppCatalogPersonalization persRequest = new AppCatalogPersonalization();
 
 195                 persRequest.setAppId((long) 1);
 
 196                 persRequest.setPending(false);
 
 197                 persRequest.setSelect(false);
 
 199                 EPUser user = mockUser.mockEPUser();
 
 201                 FieldsValidator expectedFieldValidator = new FieldsValidator();
 
 203                 FieldsValidator actualFieldValidator = new FieldsValidator();
 
 204                 List<FieldName> fields = new ArrayList<>();
 
 207                 expectedFieldValidator.setHttpStatusCode((long) 200);
 
 208                 expectedFieldValidator.setFields(fields);
 
 209                 expectedFieldValidator.setErrorCode(null);
 
 213                 Mockito.when(appService.getApp(persRequest.getAppId())).thenReturn(app);
 
 215                 HttpSession session = mockedRequest.getSession();
 
 216                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
 
 218                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
 
 219                 actualFieldValidator = appCatalogController.putAppCatalogSelection(mockedRequest, persRequest, mockedResponse);
 
 220                 assertEquals(expectedFieldValidator, actualFieldValidator);
 
 225         public void putAppCatalogSelectionTest() throws IOException {
 
 227                 AppCatalogPersonalization persRequest = new AppCatalogPersonalization();
 
 228                 persRequest.setAppId((long) 1);
 
 229                 persRequest.setPending(false);
 
 230                 persRequest.setSelect(false);
 
 232                 EPUser user = mockUser.mockEPUser();
 
 234                 FieldsValidator expectedFieldValidator = new FieldsValidator();
 
 236                 FieldsValidator actualFieldValidator = new FieldsValidator();
 
 237                 List<FieldName> fields = new ArrayList<>();
 
 240                 expectedFieldValidator.setHttpStatusCode((long) 200);
 
 241                 expectedFieldValidator.setFields(fields);
 
 242                 expectedFieldValidator.setErrorCode(null);
 
 244                 EPApp app = new EPApp();
 
 247                 app.setImageUrl("test");
 
 248                 app.setDescription("test");
 
 249                 app.setNotes("test");
 
 252                 app.setAppRestEndpoint("test");
 
 253                 app.setAlternateUrl("test");
 
 255                 app.setMlAppName("test");
 
 256                 app.setMlAppAdminId("test");
 
 257                 app.setUsername("test");
 
 258                 app.setAppPassword("test");
 
 260                 app.setEnabled(false);
 
 261                 app.setUebKey("test");
 
 262                 app.setUebSecret("test");
 
 263                 app.setUebTopicName("test");
 
 266                 Mockito.when(appService.getApp(persRequest.getAppId())).thenReturn(app);
 
 268                 HttpSession session = mockedRequest.getSession();
 
 269                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
 
 271                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
 
 272                 Mockito.doNothing().when(persUserAppService).setPersUserAppValue(user, app, persRequest.getSelect(),
 
 273                                 persRequest.getPending());
 
 275                 actualFieldValidator = appCatalogController.putAppCatalogSelection(mockedRequest, persRequest, mockedResponse);
 
 277                 assertEquals(expectedFieldValidator, actualFieldValidator);