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