Merge "Added the link to the architecture documentation"
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / service / UserServiceImplTest.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.io.ByteArrayInputStream;
43 import java.net.HttpURLConnection;
44 import java.util.ArrayList;
45 import java.util.List;
46
47 import org.hibernate.criterion.Criterion;
48 import org.hibernate.criterion.Restrictions;
49 import org.json.simple.JSONObject;
50 import org.junit.Before;
51 import org.junit.Ignore;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
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.core.MockEPUser;
59 import org.onap.portalapp.portal.domain.EPApp;
60 import org.onap.portalapp.portal.domain.EPUser;
61 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
62 import org.onap.portalapp.portal.utils.EcompPortalUtils;
63 import org.onap.portalapp.portal.utils.PortalConstants;
64 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
65 import org.onap.portalsdk.core.service.DataAccessService;
66 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
67 import org.onap.portalsdk.core.util.SystemProperties;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.core.classloader.annotations.PrepareForTest;
70 import org.powermock.modules.junit4.PowerMockRunner;
71
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, PortalConstants.class,
74                 EPCommonSystemProperties.class, Criterion.class, CipherUtil.class, Restrictions.class })
75 @Ignore
76 public class UserServiceImplTest {
77
78         @Mock
79         DataAccessService dataAccessService = new DataAccessServiceImpl();
80         
81         @InjectMocks
82         UserServiceImpl userServiceImpl= new UserServiceImpl();
83         
84         @Before
85         public void setup() {
86                 MockitoAnnotations.initMocks(this);
87         }
88         
89         public EPApp mockApp() {
90                 EPApp app = new EPApp();
91                 app.setName("Test");
92                 app.setImageUrl("test");
93                 app.setNameSpace("com.test.app");
94                 app.setCentralAuth(true);
95                 app.setDescription("test");
96                 app.setNotes("test");
97                 app.setUrl("test");
98                 app.setId((long) 10);
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(false);
107                 app.setEnabled(true);
108                 app.setUebKey("test");
109                 app.setUebSecret("test");
110                 app.setUebTopicName("test");
111                 app.setAppType(1);
112                 return app;
113         }
114         
115         MockEPUser mockUser = new MockEPUser();
116         
117         @SuppressWarnings("unchecked")
118         @Test
119         public void getUserByUserIdExceptionTest() throws Exception {
120                 PowerMockito.mockStatic(SystemProperties.class);
121                 EPUser user = mockUser.mockEPUser();
122                 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
123                 Mockito.when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUTH_USER_SERVER)).thenReturn("http://www.test.com");
124                 HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
125                 JSONObject response = new JSONObject();
126                 JSONObject userJson = new JSONObject();
127                 userJson.put("id", 1);
128                 userJson.put("givenName", "Guest");
129                 userJson.put("familyName", "Test");
130                 userJson.put("email", "test@123.com");
131                 List<JSONObject> userListJson =  new ArrayList<>();
132                 userListJson.add(userJson);
133                 response.put("response", userListJson);
134                 ByteArrayInputStream getBody = new ByteArrayInputStream(response.toString().getBytes("UTF-8"));
135                 PowerMockito.when(connection.getInputStream()).thenReturn(getBody);
136                 userServiceImpl.getUserByUserId(user.getOrgUserId());
137         }
138         
139         @SuppressWarnings("unchecked")
140         @Test
141         public void saveNewUserTest() throws Exception {
142                 PowerMockito.mockStatic(Restrictions.class);
143                 PowerMockito.mockStatic(Criterion.class);
144                 PowerMockito.mockStatic(CipherUtil.class);
145                 EPUser user = mockUser.mockEPUser();
146                 List<EPUser> users = new ArrayList<>();
147                 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
148                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
149                 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",user.getLoginId());
150                 restrictionsList.add(orgUserIdCriterion);
151                 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(users);
152                 String actual = userServiceImpl.saveNewUser(user, "No");
153                 assertEquals("success", actual);
154         }
155         
156         @SuppressWarnings("unchecked")
157         @Test
158         public void saveExistingUserTest() throws Exception {
159                 PowerMockito.mockStatic(Restrictions.class);
160                 PowerMockito.mockStatic(Criterion.class);
161                 PowerMockito.mockStatic(CipherUtil.class);
162                 EPUser user = mockUser.mockEPUser();
163                 user.setLoginPwd("xyz");
164                 List<EPUser> users = new ArrayList<>();
165                 users.add(user);
166                 EPUser oldUser = mockUser.mockEPUser();
167                 oldUser.setLoginPwd("abc");
168                 List<EPUser> oldUsers = new ArrayList<>();
169                 oldUsers.add(oldUser);
170                 Mockito.when(CipherUtil.encryptPKC(user.getLoginPwd())).thenReturn("xyz");
171                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
172                 Criterion orgUserIdCriterion = Restrictions.eq("orgUserId",user.getLoginId());
173                 restrictionsList.add(orgUserIdCriterion);
174                 Mockito.when((List<EPUser>) dataAccessService.getList(EPUser.class, null, restrictionsList, null)).thenReturn(oldUsers);
175                 String actual = userServiceImpl.saveNewUser(user, "No");
176                 assertEquals("success", actual);
177         }
178
179         
180 }