Transport DTO up
[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.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;
57
58 @RunWith(SpringRunner.class)
59 @SpringBootTest
60 @TestPropertySource(locations = "classpath:test.properties")
61 class UserControllerTest {
62
63        private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
64                "XZa6pS1vC0qKXWtn9wcZWdLx61L0=");
65
66        private final FnUserService userService;
67        private final UserController userController;
68
69        @Autowired
70        UserControllerTest(final FnUserService userService, final UserController userController) {
71               this.userService = userService;
72               this.userController = userController;
73        }
74
75        @Test
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",
84                       expectedDetails);
85
86               PortalRestResponse<ProfileDetail> actual = userController.getLoggedinUser(principal);
87
88               assertEquals(expected.getStatus(), actual.getStatus());
89               assertEquals(expected.getMessage(), actual.getMessage());
90               assertEquals(expected.getResponse(), actual.getResponse());
91        }
92
93        @Test
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("*****");
101
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);
106        }
107
108        @Test
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("*****");
116
117               PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
118               PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
119               assertEquals(expected, actual);
120        }
121
122        @Test
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");
130
131               FnUser user = userService.loadUserByUsername(principal.getName());
132               String oldPassword = user.getLoginPwd();
133
134               PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
135               PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
136
137               FnUser user2 = userService.loadUserByUsername(principal.getName());
138               String newPassword = user2.getLoginPwd();
139
140               assertEquals(expected, actual);
141               assertNotEquals(oldPassword, newPassword);
142        }
143 }