e3f1f7be8a39cb060fb98451461e56599e3833e2
[portal.git] / portal-BE / src / test / java / org / onap / portal / controller / UserControllerTest.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
39 package org.onap.portal.controller;
40
41 import static org.junit.jupiter.api.Assertions.assertEquals;
42 import static org.junit.jupiter.api.Assertions.assertNotEquals;
43
44 import org.junit.jupiter.api.Test;
45 import org.junit.runner.RunWith;
46 import org.onap.portal.domain.db.fn.FnUser;
47 import org.onap.portal.domain.dto.PortalRestResponse;
48 import org.onap.portal.domain.dto.PortalRestStatusEnum;
49 import org.onap.portal.domain.dto.ProfileDetail;
50 import org.onap.portal.service.fn.FnUserService;
51 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
52 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.boot.test.context.SpringBootTest;
55 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
56 import org.springframework.test.context.TestPropertySource;
57 import org.springframework.test.context.junit4.SpringRunner;
58
59 @RunWith(SpringRunner.class)
60 @SpringBootTest
61 @TestPropertySource(locations = "classpath:test.properties")
62 class UserControllerTest {
63
64        private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
65                "XZa6pS1vC0qKXWtn9wcZWdLx61L0=");
66
67        private final FnUserService userService;
68        private final UserController userController;
69
70        @Autowired
71        UserControllerTest(final FnUserService userService, final UserController userController) {
72               this.userService = userService;
73               this.userController = userController;
74        }
75
76        @Test
77        void getLoggedinUser() {
78               ProfileDetail expectedDetails = new ProfileDetail();
79               expectedDetails.setFirstName("Demo");
80               expectedDetails.setLastName("User");
81               expectedDetails.setEmail("demo@openecomp.org");
82               expectedDetails.setLoginId("demo");
83               expectedDetails.setLoginPassword("*****");
84               PortalRestResponse<ProfileDetail> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success",
85                       expectedDetails);
86
87               PortalRestResponse<ProfileDetail> actual = userController.getLoggedinUser(principal);
88
89               assertEquals(expected.getStatus(), actual.getStatus());
90               assertEquals(expected.getMessage(), actual.getMessage());
91               assertEquals(expected.getResponse(), actual.getResponse());
92        }
93
94        @Test
95        void modifyLoggedinUserBlanklastName() {
96               ProfileDetail expectedDetails = new ProfileDetail();
97               expectedDetails.setFirstName("Demo");
98               expectedDetails.setLastName("");
99               expectedDetails.setEmail("demo@openecomp.org");
100               expectedDetails.setLoginId("demo");
101               expectedDetails.setLoginPassword("*****");
102
103               PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
104               PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
105                       "lastName must not be blank", null);
106               assertEquals(expected, actual);
107        }
108
109        @Test
110        void modifyLoggedinUser() {
111               ProfileDetail expectedDetails = new ProfileDetail();
112               expectedDetails.setFirstName("Demo");
113               expectedDetails.setLastName("User");
114               expectedDetails.setEmail("demo@openecomp.org");
115               expectedDetails.setLoginId("demo");
116               expectedDetails.setLoginPassword("*****");
117
118               PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
119               PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
120               assertEquals(expected, actual);
121        }
122
123        @Test
124        void modifyLoggedinUserChangePassword() throws CipherUtilException {
125               ProfileDetail expectedDetails = new ProfileDetail();
126               expectedDetails.setFirstName("Demo");
127               expectedDetails.setLastName("User");
128               expectedDetails.setEmail("demo@openecomp.org");
129               expectedDetails.setLoginId("demo");
130               expectedDetails.setLoginPassword("123password");
131
132               FnUser user = userService.loadUserByUsername(principal.getName());
133               String oldPassword = user.getLoginPwd();
134
135               PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
136               PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
137
138               FnUser user2 = userService.loadUserByUsername(principal.getName());
139               String newPassword = user2.getLoginPwd();
140
141               assertEquals(expected, actual);
142               assertNotEquals(oldPassword, newPassword);
143        }
144 }