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.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;
59 @RunWith(SpringRunner.class)
61 @TestPropertySource(locations = "classpath:test.properties")
62 class UserControllerTest {
64 private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
65 "XZa6pS1vC0qKXWtn9wcZWdLx61L0=");
67 private final FnUserService userService;
68 private final UserController userController;
71 UserControllerTest(final FnUserService userService, final UserController userController) {
72 this.userService = userService;
73 this.userController = userController;
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",
87 PortalRestResponse<ProfileDetail> actual = userController.getLoggedinUser(principal);
89 assertEquals(expected.getStatus(), actual.getStatus());
90 assertEquals(expected.getMessage(), actual.getMessage());
91 assertEquals(expected.getResponse(), actual.getResponse());
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("*****");
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);
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("*****");
118 PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
119 PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
120 assertEquals(expected, actual);
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");
132 FnUser user = userService.loadUserByUsername(principal.getName());
133 String oldPassword = user.getLoginPwd();
135 PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
136 PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
138 FnUser user2 = userService.loadUserByUsername(principal.getName());
139 String newPassword = user2.getLoginPwd();
141 assertEquals(expected, actual);
142 assertNotEquals(oldPassword, newPassword);