acc25ac8692b833f02c6682ca71315236c21a7b0
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / AppContactUsServiceImplTest.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  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import static org.junit.Assert.assertEquals;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.InjectMocks;
52 import org.mockito.Matchers;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.portal.domain.AppContactUs;
57 import org.onap.portalapp.portal.domain.EPApp;
58 import org.onap.portalapp.portal.ecomp.model.AppCategoryFunctionsItem;
59 import org.onap.portalapp.portal.ecomp.model.AppContactUsItem;
60 import org.onap.portalapp.portal.framework.MockitoTestSuite;
61 import org.onap.portalapp.portal.service.AppContactUsService;
62 import org.onap.portalapp.portal.service.AppContactUsServiceImpl;
63 import org.onap.portalsdk.core.service.DataAccessService;
64 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
65
66 public class AppContactUsServiceImplTest {
67
68         
69      @Mock
70          DataAccessService dataAccessService ;
71     
72      @Mock
73      AppContactUsService AppContactUsService ;
74      @InjectMocks
75      AppContactUsServiceImpl appContactUsServiceImpl = new AppContactUsServiceImpl();
76
77         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
78
79         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
80         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
81      
82      @Before
83         public void setup() {
84                 MockitoAnnotations.initMocks(this);
85         }
86         
87    
88         NullPointerException nullPointerException = new NullPointerException();
89         
90         
91         public EPApp getApp() {
92                 EPApp app = new EPApp();
93                 app.setName("Test");
94                 app.setImageUrl("test");
95                 app.setDescription("test");
96                 app.setNotes("test");
97                 app.setUrl("test");
98                 app.setId((long) 1);
99                 app.setAppRestEndpoint("test");
100                 app.setAlternateUrl("test");
101                 app.setName("test");
102                 app.setMlAppName("test");
103                 app.setMlAppAdminId("test");
104                 app.setUsername("test");
105                 app.setAppPassword("test");
106                 app.setOpen(true);
107                 app.setEnabled(false);
108                 app.setUebKey("test");
109                 app.setUebSecret("test");
110                 app.setUebTopicName("test");
111                 app.setAppType(1);
112                 return app;
113         }
114         @Test
115         public void getAppContactUsTest() throws Exception
116         {
117                 
118                 List<AppContactUsItem> contactUsItemList  = new ArrayList<>();
119                 AppContactUsItem appContactUsItem= new AppContactUsItem();
120                 appContactUsItem.setAppName("testNew");
121                 contactUsItemList.add(appContactUsItem);
122                 AppContactUsItem appContactUsItem1= new AppContactUsItem();
123                 appContactUsItem1.setAppName("test");
124                 contactUsItemList.add(appContactUsItem1);
125                 Mockito.when(dataAccessService.executeNamedQuery("getAppContactUsItems", null, null)).thenReturn(contactUsItemList);
126                 List<AppContactUsItem> expectedcontactUsItemList = appContactUsServiceImpl.getAppContactUs();
127                 assertEquals(expectedcontactUsItemList, contactUsItemList);             
128         }
129         
130         @Test
131         public void getAppsAndContactsTest() throws Exception
132         {
133                 List<AppContactUsItem> contactUsItemList  = new ArrayList<>();
134                 AppContactUsItem appContactUsItem= new AppContactUsItem();
135                 appContactUsItem.setAppName("testNew");
136                 contactUsItemList.add(appContactUsItem);
137                 AppContactUsItem appContactUsItem1= new AppContactUsItem();
138                 appContactUsItem1.setAppName("test");
139                 contactUsItemList.add(appContactUsItem1);
140                 Mockito.when(dataAccessService.executeNamedQuery("getAppsAndContacts", null, null)).thenReturn(contactUsItemList);
141                 List<AppContactUsItem> expectedcontactUsItemList = appContactUsServiceImpl.getAppsAndContacts();
142                 assertEquals(expectedcontactUsItemList, contactUsItemList);             
143         }
144         
145         
146         @Test
147         public void getAppCategoryFunctionsTest() throws Exception
148         {
149                 List<AppCategoryFunctionsItem> list  = new ArrayList<>();
150                 Mockito.when(dataAccessService.executeNamedQuery("getAppCategoryFunctions", null, null)).thenReturn(list);
151                 List<AppCategoryFunctionsItem> expectedlist = appContactUsServiceImpl.getAppCategoryFunctions();
152                 assertEquals(list, expectedlist); 
153         }
154         
155         @Test(expected = java.lang.Exception.class)
156         public void saveAppContactUsTest() throws Exception
157         {
158                 HashMap<String, Object> map = new HashMap<String, Object>();
159                 List<AppContactUsItem> contactUsModelList = new ArrayList<>();
160                 AppContactUsItem appContactUsItem= new AppContactUsItem();
161                 appContactUsItem.setAppId((long) 1);
162                 contactUsModelList.add(appContactUsItem);
163                 AppContactUs appContact = new AppContactUs();
164                 Mockito.when(dataAccessService.getDomainObject(AppContactUs.class, 1, map)).thenReturn(appContact);
165                 EPApp app = getApp();
166                 Mockito.when(dataAccessService.getDomainObject(EPApp.class, 1, new HashMap<String, Object>())).thenReturn(app);
167                 AppContactUs contactUs  = new AppContactUs();
168                 contactUs.setApp(app);
169                 contactUs.setDescription(appContactUsItem.getDescription());
170                 contactUs.setContactName(appContactUsItem.getContactName());
171                 contactUs.setContactEmail(appContactUsItem.getContactEmail());
172                 contactUs.setActiveYN(appContactUsItem.getActiveYN());
173                 contactUs.setUrl(appContactUsItem.getUrl());
174                 Mockito.doNothing().when(dataAccessService).saveDomainObject(contactUs,map);
175                 appContactUsServiceImpl.saveAppContactUs(contactUsModelList);
176         }
177         
178         @Test
179         public void saveAppContacts()throws Exception {
180                 
181                 List<AppContactUsItem> contactUsModelList = new ArrayList<>();
182                 AppContactUsItem appContactUsItem= new AppContactUsItem();
183                 appContactUsItem.setAppId((long) 1);
184                 contactUsModelList.add(appContactUsItem);
185                 HashMap<String, Object> map = new HashMap<String, Object>();
186                 
187                 Mockito.when(dataAccessService.getDomainObject(AppContactUs.class,
188                                                 appContactUsItem.getAppId(), map)).thenReturn(appContactUsItem);
189                 
190                 Mockito.when(dataAccessService.getDomainObject(EPApp.class, appContactUsItem.getAppId(), map)).thenReturn(getApp());
191                 appContactUsServiceImpl.saveAppContactUs(contactUsModelList);
192         }
193         
194         @Test(expected = java.lang.NullPointerException.class)
195         public void deleteContactUs_error_Test() throws Exception
196         {
197                 HashMap<String, Object> map = new HashMap<String, Object>();
198                 AppContactUs contactUs = new AppContactUs();
199                 Mockito.when((AppContactUs) dataAccessService.getDomainObject(AppContactUs.class, 1, map)).thenReturn(contactUs);
200                 appContactUsServiceImpl.deleteContactUs((long) 1);
201         }
202         @Test(expected=Exception.class)
203         public void deleteContactUsTest()throws Exception {
204                 HashMap<String, Object> map = new HashMap<String, Object>();
205                 
206                 AppContactUs contactUs = new AppContactUs();
207                 contactUs.setId(1l);
208                 Mockito.when(dataAccessService.getDomainObject(AppContactUs.class,
209                                 contactUs.getId(), map)).thenReturn(contactUs);
210                 appContactUsServiceImpl.deleteContactUs(        contactUs.getId());
211         }
212         
213 }