2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
39 package org.onap.portal.controller;
41 import static org.junit.jupiter.api.Assertions.assertEquals;
42 import static org.junit.jupiter.api.Assertions.assertNotEquals;
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.transport.ProfileDetail;
50 import org.onap.portal.service.fn.FnUserService;
51 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.boot.test.context.SpringBootTest;
54 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
55 import org.springframework.test.context.TestPropertySource;
56 import org.springframework.test.context.junit4.SpringRunner;
58 @RunWith(SpringRunner.class)
60 @TestPropertySource(locations = "classpath:test.properties")
61 class UserControllerTest {
63 private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
64 "XZa6pS1vC0qKXWtn9wcZWdLx61L0=");
66 private final FnUserService userService;
67 private final UserController userController;
70 UserControllerTest(final FnUserService userService, final UserController userController) {
71 this.userService = userService;
72 this.userController = userController;
76 void getLoggedinUser() {
77 ProfileDetail expectedDetails = new ProfileDetail();
78 expectedDetails.setFirstName("Demo");
79 expectedDetails.setLastName("User");
80 expectedDetails.setEmail("demo@openecomp.org");
81 expectedDetails.setLoginId("demo");
82 expectedDetails.setLoginPassword("*****");
83 PortalRestResponse<ProfileDetail> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success",
86 PortalRestResponse<ProfileDetail> actual = userController.getLoggedinUser(principal);
88 assertEquals(expected.getStatus(), actual.getStatus());
89 assertEquals(expected.getMessage(), actual.getMessage());
90 assertEquals(expected.getResponse(), actual.getResponse());
94 void modifyLoggedinUserBlanklastName() {
95 ProfileDetail expectedDetails = new ProfileDetail();
96 expectedDetails.setFirstName("Demo");
97 expectedDetails.setLastName("");
98 expectedDetails.setEmail("demo@openecomp.org");
99 expectedDetails.setLoginId("demo");
100 expectedDetails.setLoginPassword("*****");
102 PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
103 PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
104 "lastName must not be blank", null);
105 assertEquals(expected, actual);
109 void modifyLoggedinUser() {
110 ProfileDetail expectedDetails = new ProfileDetail();
111 expectedDetails.setFirstName("Demo");
112 expectedDetails.setLastName("User");
113 expectedDetails.setEmail("demo@openecomp.org");
114 expectedDetails.setLoginId("demo");
115 expectedDetails.setLoginPassword("*****");
117 PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
118 PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
119 assertEquals(expected, actual);
123 void modifyLoggedinUserChangePassword() throws CipherUtilException {
124 ProfileDetail expectedDetails = new ProfileDetail();
125 expectedDetails.setFirstName("Demo");
126 expectedDetails.setLastName("User");
127 expectedDetails.setEmail("demo@openecomp.org");
128 expectedDetails.setLoginId("demo");
129 expectedDetails.setLoginPassword("123password");
131 FnUser user = userService.loadUserByUsername(principal.getName());
132 String oldPassword = user.getLoginPwd();
134 PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
135 PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
137 FnUser user2 = userService.loadUserByUsername(principal.getName());
138 String newPassword = user2.getLoginPwd();
140 assertEquals(expected, actual);
141 assertNotEquals(oldPassword, newPassword);