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