fixes for supporting non-gui application access provisioning
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / PersUserAppServiceImplTest.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 java.util.ArrayList;
41 import java.util.List;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.InjectMocks;
49 import org.mockito.Mock;
50 import org.mockito.Mockito;
51 import org.mockito.MockitoAnnotations;
52 import org.onap.portalapp.portal.core.MockEPUser;
53 import org.onap.portalapp.portal.domain.EPApp;
54 import org.onap.portalapp.portal.domain.EPUser;
55 import org.onap.portalapp.portal.domain.EPUserApp;
56 import org.onap.portalapp.portal.domain.EpAppType;
57 import org.onap.portalapp.portal.domain.PersUserAppSelection;
58 import org.onap.portalapp.portal.framework.MockitoTestSuite;
59 import org.onap.portalapp.portal.service.AdminRolesService;
60 import org.onap.portalapp.portal.service.PersUserAppServiceImpl;
61 import org.onap.portalapp.portal.service.UserRolesService;
62 import org.onap.portalsdk.core.service.DataAccessService;
63
64 public class PersUserAppServiceImplTest {
65
66         @Mock
67         DataAccessService dataAccessService;
68         @Mock
69         AdminRolesService adminRolesService;
70         @Mock
71         UserRolesService userRolesService;
72
73         @Before
74         public void setup() {
75                 MockitoAnnotations.initMocks(this);
76         }
77
78         @InjectMocks
79         PersUserAppServiceImpl persUserAppServiceImpl = new PersUserAppServiceImpl();
80
81         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
82
83         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
84         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
85         NullPointerException nullPointerException = new NullPointerException();
86         MockEPUser mockUser = new MockEPUser();
87
88         public EPApp getApp() {
89                 EPApp app = new EPApp();
90                 app.setName("Test");
91                 app.setImageUrl("test");
92                 app.setAppDescription("test");
93                 app.setAppNotes("test");
94                 app.setLandingPage("test");
95                 app.setId((long) 1);
96                 app.setAppRestEndpoint("test");
97                 app.setAlternateLandingPage("test");
98                 app.setName("test");
99                 app.setMlAppName("test");
100                 app.setMlAppAdminId("test");
101                 app.setAppBasicAuthUsername("test");
102                 app.setAppBasicAuthPassword("test");
103                 app.setOpen(true);
104                 app.setEnabled(false);
105                 app.setUebKey("test");
106                 app.setUebSecret("test");
107                 app.setUebTopicName("test");
108                 app.setAppType(EpAppType.GUI);
109                 return app;
110         }
111
112         @Test(expected = IllegalArgumentException.class)
113         public void setPersUserAppValueIfUserNull() {
114                 persUserAppServiceImpl.setPersUserAppValue(null, null, false, false);
115         }
116
117         @Test
118         public void setPersUserAppValueTest() {
119                 EPApp app = getApp();
120                 EPUser user = mockUser.mockEPUser();
121                 List<PersUserAppSelection> persUserAppSelectionList = new ArrayList<>();
122                 PersUserAppSelection persUserAppSelection = new PersUserAppSelection();
123                 persUserAppSelection.setId((long) 1);
124                 persUserAppSelectionList.add(persUserAppSelection);
125                 Mockito.when(dataAccessService.getList(PersUserAppSelection.class, "test", null, null))
126                                 .thenReturn(persUserAppSelectionList);
127                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(persUserAppSelection, null);
128                 persUserAppServiceImpl.setPersUserAppValue(user, app, false, true);
129         }
130
131         @Test
132         public void setPersUserAppValueIfSelectTest() {
133                 EPApp app = getApp();
134                 EPUser user = mockUser.mockEPUser();
135                 List<PersUserAppSelection> persUserAppSelectionList = new ArrayList<>();
136                 PersUserAppSelection persUserAppSelection = new PersUserAppSelection();
137                 persUserAppSelection.setId((long) 1);
138                 persUserAppSelectionList.add(persUserAppSelection);
139                 Mockito.when(dataAccessService.getList(PersUserAppSelection.class, "test", null, null))
140                                 .thenReturn(persUserAppSelectionList);
141                 Mockito.doNothing().when(dataAccessService).saveDomainObject(persUserAppSelection, null);
142                 persUserAppServiceImpl.setPersUserAppValue(user, app, true, true);
143         }
144
145         @Test
146         public void setPersUserAppValueIfOpenTest() {
147                 EPApp app = getApp();
148                 app.setOpen(false);
149                 EPUser user = mockUser.mockEPUser();
150                 List<PersUserAppSelection> persUserAppSelectionList = new ArrayList<>();
151                 PersUserAppSelection persUserAppSelection = new PersUserAppSelection();
152                 persUserAppSelection.setId((long) 1);
153                 persUserAppSelectionList.add(persUserAppSelection);
154                 Mockito.when(dataAccessService.getList(PersUserAppSelection.class, "test", null, null))
155                                 .thenReturn(persUserAppSelectionList);
156                 Mockito.doNothing().when(dataAccessService).saveDomainObject(persUserAppSelection, null);
157                 persUserAppServiceImpl.setPersUserAppValue(user, app, true, true);
158         }
159
160         @Test
161         public void setPersUserAppValueIfAppNotOpenTest() {
162                 EPApp app = getApp();
163                 app.setOpen(false);
164                 EPUser user = mockUser.mockEPUser();
165                 List<PersUserAppSelection> persUserAppSelectionList = new ArrayList<>();
166                 PersUserAppSelection persUserAppSelection = new PersUserAppSelection();
167                 persUserAppSelection.setId((long) 1);
168                 persUserAppSelectionList.add(persUserAppSelection);
169                 Mockito.when(dataAccessService.getList(PersUserAppSelection.class, "test", null, null))
170                                 .thenReturn(persUserAppSelectionList);
171                 Mockito.doNothing().when(dataAccessService).saveDomainObject(persUserAppSelection, null);
172                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true);
173                 List<EPUserApp> roles = new ArrayList<>();
174                 EPUserApp epUserApp = new EPUserApp();
175                 roles.add(epUserApp);
176                 Mockito.when(userRolesService.getCachedAppRolesForUser(app.getId(), user.getId())).thenReturn(roles);
177                 persUserAppServiceImpl.setPersUserAppValue(user, app, true, false);
178         }
179
180         @Test
181         public void setPersUserAppValueIfNotPortalAdminTest() {
182                 EPApp app = getApp();
183                 app.setOpen(false);
184                 EPUser user = mockUser.mockEPUser();
185                 List<PersUserAppSelection> persUserAppSelectionList = new ArrayList<>();
186                 PersUserAppSelection persUserAppSelection = new PersUserAppSelection();
187                 persUserAppSelection.setId((long) 1);
188                 persUserAppSelectionList.add(persUserAppSelection);
189                 Mockito.when(dataAccessService.getList(PersUserAppSelection.class, "test", null, null))
190                                 .thenReturn(persUserAppSelectionList);
191                 Mockito.doNothing().when(dataAccessService).deleteDomainObject(persUserAppSelection, null);
192                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
193                 List<EPUserApp> roles = new ArrayList<>();
194                 EPUserApp epUserApp = new EPUserApp();
195                 roles.add(epUserApp);
196                 Mockito.when(userRolesService.getCachedAppRolesForUser(app.getId(), user.getId())).thenReturn(roles);
197                 persUserAppServiceImpl.setPersUserAppValue(user, app, true, false);
198         }
199
200         @Test
201         public void setPersUserAppValueNewTest() {
202                 EPApp app = getApp();
203                 app.setOpen(false);
204                 EPUser user = mockUser.mockEPUser();
205                 List<PersUserAppSelection> persUserAppSelectionList = new ArrayList<>();
206                 PersUserAppSelection persUserAppSelection = new PersUserAppSelection();
207                 persUserAppSelection.setId((long) 1);
208                 persUserAppSelectionList.add(persUserAppSelection);
209                 Mockito.when(dataAccessService.getList(PersUserAppSelection.class, "test", null, null))
210                                 .thenReturn(persUserAppSelectionList);
211                 Mockito.doNothing().when(dataAccessService).saveDomainObject(persUserAppSelection, null);
212                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true);
213                 List<EPUserApp> roles = new ArrayList<>();
214                 EPUserApp epUserApp = new EPUserApp();
215                 roles.add(epUserApp);
216                 Mockito.when(userRolesService.getCachedAppRolesForUser(app.getId(), user.getId())).thenReturn(roles);
217                 persUserAppServiceImpl.setPersUserAppValue(user, app, false, false);
218         }
219 }