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============================================
 
  36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  38 package org.onap.portalapp.portal.controller;
 
  40 import static org.junit.Assert.assertEquals;
 
  41 import static org.junit.Assert.assertTrue;
 
  43 import java.util.ArrayList;
 
  44 import java.util.List;
 
  47 import javax.servlet.http.HttpServletRequest;
 
  48 import javax.servlet.http.HttpServletResponse;
 
  50 import org.junit.Before;
 
  51 import org.junit.Test;
 
  52 import org.junit.runner.RunWith;
 
  53 import org.mockito.InjectMocks;
 
  54 import org.mockito.Mock;
 
  55 import org.mockito.Mockito;
 
  56 import org.mockito.MockitoAnnotations;
 
  57 import org.onap.portalapp.portal.controller.AppContactUsController;
 
  58 import org.onap.portalapp.portal.ecomp.model.AppCategoryFunctionsItem;
 
  59 import org.onap.portalapp.portal.ecomp.model.AppContactUsItem;
 
  60 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
 
  61 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
 
  62 import org.onap.portalapp.portal.framework.MockitoTestSuite;
 
  63 import org.onap.portalapp.portal.service.AppContactUsService;
 
  64 import org.onap.portalapp.portal.service.AppContactUsServiceImpl;
 
  65 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
 
  66 import org.onap.portalapp.util.EPUserUtils;
 
  67 import org.onap.portalsdk.core.domain.support.CollaborateList;
 
  68 import org.onap.portalsdk.core.util.SystemProperties;
 
  69 import org.powermock.api.mockito.PowerMockito;
 
  70 import org.powermock.core.classloader.annotations.PrepareForTest;
 
  71 import org.powermock.modules.junit4.PowerMockRunner;
 
  73 @RunWith(PowerMockRunner.class)
 
  74 @PrepareForTest({SystemProperties.class, EPCommonSystemProperties.class})
 
  75 public class AppContactUsControllerTest extends MockitoTestSuite{
 
  78         AppContactUsService contactUsService = new AppContactUsServiceImpl();
 
  81         AppContactUsController appContactUsController = new AppContactUsController();
 
  85                 MockitoAnnotations.initMocks(this);
 
  88         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
 
  90         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
 
  91         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
 
  92         NullPointerException nullPointerException = new NullPointerException();
 
  95         EPUserUtils ePUserUtils = new EPUserUtils();
 
  97         public List<AppContactUsItem> mockResponse() {
 
  98                 List<AppContactUsItem> appContactUsItemList = new ArrayList<AppContactUsItem>();
 
  99                 AppContactUsItem appContactUsItem = new AppContactUsItem();
 
 100                 appContactUsItem.setAppId((long) 1);
 
 101                 appContactUsItem.setAppName("ECOMP Portal");
 
 102                 appContactUsItem.setDescription("Test");
 
 103                 appContactUsItem.setContactName("Test");
 
 104                 appContactUsItem.setContactEmail("person@onap.org");
 
 105                 appContactUsItem.setUrl("Test_URL");
 
 106                 appContactUsItem.setActiveYN("Y");
 
 107                 appContactUsItemList.add(appContactUsItem);
 
 109                 return appContactUsItemList;
 
 113         public PortalRestResponse<List<AppContactUsItem>> successPortalRestResponse() {
 
 114                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
 
 115                 List<AppContactUsItem> appContactUsItemList = mockResponse();
 
 116                 expectedportalRestResponse.setMessage("success");
 
 117                 expectedportalRestResponse.setResponse(appContactUsItemList);
 
 118                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
 
 119                 return expectedportalRestResponse;
 
 123         public PortalRestResponse<List<AppContactUsItem>> exceptionPortalRestResponse() {
 
 124                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
 
 125                 expectedportalRestResponse.setMessage(null);
 
 126                 expectedportalRestResponse.setResponse(null);
 
 127                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
 
 128                 return expectedportalRestResponse;
 
 133         public void getAppContactUsList() throws Exception {
 
 134                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = successPortalRestResponse();
 
 135                 List<AppContactUsItem> appContactUsItemList = mockResponse();
 
 136                 PortalRestResponse<List<AppContactUsItem>> actualPortalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
 
 137                 Mockito.when(contactUsService.getAppContactUs()).thenReturn(appContactUsItemList);
 
 138                 actualPortalRestResponse = appContactUsController.getAppContactUsList(mockedRequest);
 
 139                 assertEquals(actualPortalRestResponse, expectedportalRestResponse);
 
 143         public void getAppContactUsListCatchesExeptionTest() throws Exception {
 
 145                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = exceptionPortalRestResponse();
 
 146                 PortalRestResponse<List<AppContactUsItem>> actualPortalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
 
 147                 Mockito.when(contactUsService.getAppContactUs()).thenThrow(nullPointerException);
 
 148                 actualPortalRestResponse = appContactUsController.getAppContactUsList(mockedRequest);
 
 149                 assertEquals(actualPortalRestResponse, expectedportalRestResponse);
 
 153         public void getAppsAndContactsTest() throws Exception {
 
 154                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = successPortalRestResponse();
 
 155                 List<AppContactUsItem> appContactUsItemList = mockResponse();
 
 156                 PortalRestResponse<List<AppContactUsItem>> actualPortalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
 
 157                 Mockito.when(contactUsService.getAppsAndContacts()).thenReturn(appContactUsItemList);
 
 158                 actualPortalRestResponse = appContactUsController.getAppsAndContacts(mockedRequest);
 
 159                 assertEquals(actualPortalRestResponse, expectedportalRestResponse);
 
 164         public void getAppsAndContactsCatchesExceptionTest() throws Exception {
 
 165                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = exceptionPortalRestResponse();
 
 166                 PortalRestResponse<List<AppContactUsItem>> actualPortalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
 
 167                 Mockito.when(contactUsService.getAppsAndContacts()).thenThrow(nullPointerException);
 
 168                 actualPortalRestResponse = appContactUsController.getAppsAndContacts(mockedRequest);
 
 169                 assertEquals(actualPortalRestResponse, expectedportalRestResponse);
 
 174         public void getAppCategoryFunctionsTest() throws Exception {
 
 175                 PortalRestResponse<List<AppCategoryFunctionsItem>> actualportalRestResponse = null;
 
 177                 List<AppCategoryFunctionsItem> contents = new ArrayList<AppCategoryFunctionsItem>();
 
 179                 AppCategoryFunctionsItem appCategoryFunctionsItem = new AppCategoryFunctionsItem();
 
 180                 AppCategoryFunctionsItem appCategoryFunctionsItem1 = new AppCategoryFunctionsItem();
 
 182                 appCategoryFunctionsItem.setRowId("1");
 
 183                 appCategoryFunctionsItem.setAppId("1");
 
 184                 appCategoryFunctionsItem.setApplication("Ecomp-portal");
 
 185                 appCategoryFunctionsItem.setCategory("test");
 
 186                 appCategoryFunctionsItem.setFunctions("test");
 
 188                 appCategoryFunctionsItem1.setRowId("2");
 
 189                 appCategoryFunctionsItem1.setAppId("2");
 
 190                 appCategoryFunctionsItem1.setApplication("Ecomp-portal-test");
 
 191                 appCategoryFunctionsItem1.setCategory("test");
 
 192                 appCategoryFunctionsItem1.setFunctions("test");
 
 193                 contents.add(appCategoryFunctionsItem);
 
 194                 contents.add(appCategoryFunctionsItem1);
 
 196                 PortalRestResponse<List<AppCategoryFunctionsItem>> expectedportalRestResponse = new PortalRestResponse<List<AppCategoryFunctionsItem>>();
 
 197                 expectedportalRestResponse.setMessage("success");
 
 198                 expectedportalRestResponse.setResponse(contents);
 
 199                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
 
 201                 Mockito.when(contactUsService.getAppCategoryFunctions()).thenReturn(contents);
 
 202                 actualportalRestResponse = appContactUsController.getAppCategoryFunctions(mockedRequest);
 
 203                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
 
 208         public void getAppCategoryFunctionsCatchesExceptionTest() throws Exception {
 
 209                 PortalRestResponse<List<AppCategoryFunctionsItem>> actualportalRestResponse = null;
 
 210                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = exceptionPortalRestResponse();
 
 211                 Mockito.when(contactUsService.getAppCategoryFunctions()).thenThrow(nullPointerException);
 
 212                 actualportalRestResponse = appContactUsController.getAppCategoryFunctions(mockedRequest);
 
 213                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
 
 218         public void saveTest() throws Exception {
 
 219                 PortalRestResponse<String> actualSaveAppContactUS = null;
 
 221                 AppContactUsItem contactUs = new AppContactUsItem();
 
 222                 contactUs.setAppId((long) 1);
 
 223                 contactUs.setAppName("Ecomp Portal");
 
 224                 contactUs.setDescription("Test");
 
 225                 contactUs.setContactName("Test");
 
 226                 contactUs.setContactEmail("person@onap.org");
 
 227                 contactUs.setUrl("Test_URL");
 
 228                 contactUs.setActiveYN("Y");
 
 230                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("SUCCESS");
 
 231                 actualSaveAppContactUS = appContactUsController.save(contactUs);
 
 232                 assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS");
 
 236         public void saveExceptionTest() throws Exception {
 
 237                 PortalRestResponse<String> actualSaveAppContactUS = null;
 
 239                 AppContactUsItem contactUs = new AppContactUsItem();
 
 240                 contactUs.setAppId((long) 1);
 
 241                 contactUs.setAppName("Ecomp Portal");
 
 242                 contactUs.setDescription("Test");
 
 243                 contactUs.setContactName("Test");
 
 244                 contactUs.setContactEmail("person@onap.org");
 
 245                 contactUs.setUrl("Test_URL");
 
 246                 contactUs.setActiveYN("Y");
 
 248                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenThrow(new Exception());
 
 249                 actualSaveAppContactUS = appContactUsController.save(contactUs);
 
 250                 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
 
 254         public void saveWhenAppContactUsItemNullTest() throws Exception {
 
 255                 PortalRestResponse<String> actualSaveAppContactUS = null;
 
 256                 AppContactUsItem contactUs = null;
 
 257                 actualSaveAppContactUS = appContactUsController.save(contactUs);
 
 258                 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
 
 263         public void saveAllTest() throws Exception {
 
 265                 List<AppContactUsItem> contactUs = mockResponse();
 
 266                 PortalRestResponse<String> actualSaveAppContactUS = null;
 
 267                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("SUCCESS");
 
 268                 actualSaveAppContactUS = appContactUsController.save(contactUs);
 
 269                 assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS");
 
 273         public void saveAllExceptionTest() throws Exception {
 
 275                 List<AppContactUsItem> contactUs = mockResponse();
 
 276                 PortalRestResponse<String> actualSaveAppContactUS = null;
 
 277                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenThrow(new Exception());
 
 278                 actualSaveAppContactUS = appContactUsController.save(contactUs);
 
 279                 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
 
 283         public void deleteTest() throws Exception {
 
 285                 PortalRestResponse<String> actualSaveAppContactUS = null;
 
 287                 String saveAppContactUs = "SUCCESS";
 
 288                 Mockito.when(contactUsService.deleteContactUs(id)).thenReturn(saveAppContactUs);
 
 289                 actualSaveAppContactUS = appContactUsController.delete(id);
 
 290                 assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS");
 
 294         public void deleteExceptionTest() throws Exception {
 
 296                 PortalRestResponse<String> actualSaveAppContactUS = null;
 
 298                 Mockito.when(contactUsService.deleteContactUs(id)).thenThrow(new Exception());
 
 299                 actualSaveAppContactUS = appContactUsController.delete(id);
 
 300                 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
 
 304         public void getPortalDetailsTest(){
 
 305                 PortalRestResponse<String> actualResponse = new PortalRestResponse<String>();
 
 306                 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
 
 307                 expectedResponse.setStatus(PortalRestStatusEnum.OK);
 
 308                 expectedResponse.setMessage("success");
 
 309                 expectedResponse.setResponse("\"ush_ticket_url\":\"http://todo_enter_ush_ticket_url\",\"portal_info_url\":\"https://todo_enter_portal_info_url\",\"feedback_email_address\":\"portal@lists.onap.org\"");
 
 310                 PowerMockito.mockStatic(SystemProperties.class);
 
 311                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 313                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL)).thenReturn("http://todo_enter_ush_ticket_url"); 
 
 314                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL)).thenReturn("https://todo_enter_portal_info_url"); 
 
 315                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS)).thenReturn("portal@lists.onap.org"); 
 
 317                 actualResponse = appContactUsController.getPortalDetails(mockedRequest);
 
 318                 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.OK) == 0);
 
 322         public void getPortalDetailsExceptionTest(){
 
 323                 PortalRestResponse<String> actualResponse = new PortalRestResponse<String>();
 
 324                 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
 
 325                 expectedResponse.setStatus(PortalRestStatusEnum.ERROR);
 
 326                 expectedResponse.setMessage("failure");
 
 327                 expectedResponse.setResponse(null);
 
 328                 PowerMockito.mockStatic(SystemProperties.class);
 
 329                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
 
 331                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL)).thenThrow(nullPointerException); 
 
 332                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL)).thenReturn("https://todo_enter_portal_info_url"); 
 
 333                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS)).thenReturn("portal@lists.onap.org"); 
 
 335                 actualResponse = appContactUsController.getPortalDetails(mockedRequest);
 
 336                 assertEquals(actualResponse, expectedResponse);