Merge "Tests Coverage up"
diff --git a/portal-BE/src/main/java/org/onap/portal/controller/LanguageController.java b/portal-BE/src/main/java/org/onap/portal/controller/LanguageController.java
index 8f2e50e..90ea068 100644
--- a/portal-BE/src/main/java/org/onap/portal/controller/LanguageController.java
+++ b/portal-BE/src/main/java/org/onap/portal/controller/LanguageController.java
@@ -107,7 +107,7 @@
response.setStatus(PortalRestStatusEnum.OK);
} else {
response.setMessage("FAILURE");
- response.setResponse("User for id:" + userId + " do not exist");
+ response.setResponse("User for id: " + userId + " do not exist");
response.setStatus(PortalRestStatusEnum.ERROR);
}
} catch (Exception e) {
@@ -120,11 +120,11 @@
}
@GetMapping(value = "/languageSetting/user/{loginId}", produces = MediaType.APPLICATION_JSON_VALUE)
- public FnLanguage getUserLanguage(@PathVariable("loginId") final Long loginId) {
- if (fnUserService.getUser(loginId).isPresent()) {
- return Optional.of(fnUserService.getUser(loginId).get().getLanguageId()).orElse(new FnLanguage());
+ public FnLanguageDto getUserLanguage(final Principal principal, @PathVariable("loginId") final Long loginId) {
+ if (fnUserService.existById(loginId)) {
+ return fnLanguageMapper.fnLanguageToDto(Optional.of(fnUserService.getUser(loginId).get().getLanguageId()).orElse(new FnLanguage()));
}
- return new FnLanguage();
+ return new FnLanguageDto();
}
@PostMapping(value = "/language", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
diff --git a/portal-BE/src/main/java/org/onap/portal/controller/UserController.java b/portal-BE/src/main/java/org/onap/portal/controller/UserController.java
index d514dfa..d339e45 100644
--- a/portal-BE/src/main/java/org/onap/portal/controller/UserController.java
+++ b/portal-BE/src/main/java/org/onap/portal/controller/UserController.java
@@ -111,7 +111,7 @@
user.setMiddleName(profileDetail.getMiddleName());
user.setLoginId(profileDetail.getLoginId());
if (!HIDDEN_DEFAULT_PASSWORD.equals(profileDetail.getLoginPassword())) {
- user.setLoginPwd(CipherUtil.encryptPKC(profileDetail.getLoginPassword()));
+ user.setLoginPwd(CipherUtil.encryptPKC(profileDetail.getLoginPassword(), "AGLDdG4D04BKm2IxIWEr8o==!"));
}
userService.saveFnUser(principal, user);
// Update user info in the session
diff --git a/portal-BE/src/main/java/org/onap/portal/dao/fn/FnUserDao.java b/portal-BE/src/main/java/org/onap/portal/dao/fn/FnUserDao.java
index 458cc66..8dbfb70 100644
--- a/portal-BE/src/main/java/org/onap/portal/dao/fn/FnUserDao.java
+++ b/portal-BE/src/main/java/org/onap/portal/dao/fn/FnUserDao.java
@@ -58,5 +58,12 @@
FnUser getOne(Long integer);
@Query
- Optional<List<FnUser>> getUsersByOrgUserId(@Param("orgIds") String orgIds);
+ Optional<List<FnUser>> getUserWithOrgUserId(final @Param("orgId") String orgId);
+
+ @Query
+ Optional<List<FnUser>> getUsersByOrgIds(final @Param("orgIds") List<String> orgIds);
+
+ @Query
+ Optional<List<FnUser>> getActiveUsers();
+
}
diff --git a/portal-BE/src/main/java/org/onap/portal/domain/db/fn/FnUser.java b/portal-BE/src/main/java/org/onap/portal/domain/db/fn/FnUser.java
index cbca44c..1399dd5 100644
--- a/portal-BE/src/main/java/org/onap/portal/domain/db/fn/FnUser.java
+++ b/portal-BE/src/main/java/org/onap/portal/domain/db/fn/FnUser.java
@@ -43,7 +43,6 @@
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Set;
-import java.util.UUID;
import java.util.stream.Collectors;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@@ -58,6 +57,8 @@
import javax.persistence.ManyToOne;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@@ -72,7 +73,6 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicUpdate;
-import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.SafeHtml;
import org.onap.portal.domain.db.cr.CrReportFileHistory;
import org.onap.portal.domain.db.ep.EpPersUserWidgetPlacement;
@@ -148,13 +148,18 @@
)
*/
-@NamedNativeQueries({
- @NamedNativeQuery(
- name = "getUsersByOrgUserId",
- query = "SELECT * FROM FnUser WHERE where org_user_id in :orgIds"
+@NamedQueries({
+ @NamedQuery(
+ name = "FnUser.getUserWithOrgUserId",
+ query = "FROM FnUser WHERE orgUserId = :orgId"),
+ @NamedQuery(
+ name = "FnUser.getActiveUsers",
+ query = "FROM FnUser WHERE activeYn = 'Y'"),
+ @NamedQuery(
+ name = "FnUser.getUsersByOrgIds",
+ query = "FROM FnUser WHERE orgUserId IN :orgIds"
)
})
-
@Table(name = "fn_user", indexes = {
@Index(name = "fn_user_address_id", columnList = "address_id"),
@Index(name = "fn_user_alert_method_cd", columnList = "alert_method_cd"),
@@ -350,7 +355,7 @@
private boolean guest;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "fnUserList")
private Set<CrReportFileHistory> crReportFileHistorie;
- @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
+ @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<FnRole> fnRoles;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<FnMenuFunctional> fnRoleList;
diff --git a/portal-BE/src/main/java/org/onap/portal/service/fn/FnUserService.java b/portal-BE/src/main/java/org/onap/portal/service/fn/FnUserService.java
index aedac49..8c5806e 100644
--- a/portal-BE/src/main/java/org/onap/portal/service/fn/FnUserService.java
+++ b/portal-BE/src/main/java/org/onap/portal/service/fn/FnUserService.java
@@ -59,7 +59,7 @@
@Transactional
public class FnUserService implements UserDetailsService {
- private final FnUserDao fnUserDao;
+ private FnUserDao fnUserDao;
@Autowired
public FnUserService(FnUserDao fnUserDao) {
@@ -71,7 +71,7 @@
}
@Override
- public FnUser loadUserByUsername(String username) throws UsernameNotFoundException {
+ public FnUser loadUserByUsername(final String username) throws UsernameNotFoundException {
Optional<FnUser> fnUser = fnUserDao.findByLoginId(username);
if (fnUser.isPresent()) {
return fnUser.get();
@@ -80,36 +80,29 @@
}
}
- public Optional<FnUser> getUser(Long id) {
+ public Optional<FnUser> getUser(final Long id) {
return Optional.of(fnUserDao.getOne(id));
}
List<FnUser> getUserWithOrgUserId(final String orgUserIdValue){
- Optional<List<FnUser>> fnUsers = Optional.of(fnUserDao.findAll()
- .stream()
- .filter(fnUser -> orgUserIdValue.equals(fnUser.getOrgUserId()))
- .collect(Collectors.toList()));
- return fnUsers.orElse(new ArrayList<>());
+ return fnUserDao.getUserWithOrgUserId(orgUserIdValue).orElse(new ArrayList<>());
}
- List<FnUser> getUsersByOrgIdsNotificationsResult(List<String> orgIds){
+ List<FnUser> getUsersByOrgIds(final List<String> orgIds){
String ids = "(" + orgIds.stream().map(s -> "'" + s + "'").collect(Collectors.joining()) + ")";
- return fnUserDao.getUsersByOrgUserId(ids).orElse(new ArrayList<>());
+ return fnUserDao.getUsersByOrgIds(orgIds).orElse(new ArrayList<>());
}
- List<FnUser> getUsersByOrgIdsNotificationsResult(String orgIds){
- return fnUserDao.getUsersByOrgUserId(orgIds).orElse(new ArrayList<>());
- }
List<FnUser> getActiveUsers(){
- return fnUserDao.findAll().stream().filter(fnUser -> "Y".equals(fnUser.getActiveYn())).collect(Collectors.toList());
+ return fnUserDao.getActiveUsers().orElse(new ArrayList<>());
}
- public void deleteUser(FnUser fnUser){
+ public void deleteUser(final FnUser fnUser){
fnUserDao.delete(fnUser);
}
- public boolean existById(Long userId) {
+ public boolean existById(final Long userId) {
return fnUserDao.existsById(userId);
}
}
diff --git a/portal-BE/src/test/java/org/onap/portal/controller/LanguageControllerTest.java b/portal-BE/src/test/java/org/onap/portal/controller/LanguageControllerTest.java
index 0f9f09a..0977f28 100644
--- a/portal-BE/src/test/java/org/onap/portal/controller/LanguageControllerTest.java
+++ b/portal-BE/src/test/java/org/onap/portal/controller/LanguageControllerTest.java
@@ -41,6 +41,7 @@
package org.onap.portal.controller;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
@@ -49,6 +50,7 @@
import org.onap.portal.domain.db.fn.FnUser;
import org.onap.portal.domain.dto.PortalRestResponse;
import org.onap.portal.domain.dto.PortalRestStatusEnum;
+import org.onap.portal.domain.dto.fn.FnLanguageDto;
import org.onap.portal.service.fn.FnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@@ -139,6 +141,50 @@
fnLanguageDao.delete(fnLanguage);
}
+ @Test
+ void setUpUserLanguageWrongUserId(){
+ //Given
+ FnLanguage fnLanguage = new FnLanguage();
+ fnLanguage.setLanguageName("Polish");
+ fnLanguage.setLanguageAlias("PL");
+
+ PortalRestResponse<String> expected = new PortalRestResponse<>();
+ expected.setMessage("FAILURE");
+ expected.setStatus(PortalRestStatusEnum.ERROR);
+ expected.setResponse("User for id: 345 do not exist");
+
+ languageController.saveLanguage(principal, fnLanguage);
+ PortalRestResponse<String> actual = languageController.setUpUserLanguage(principal, fnLanguage, 345L);
+
+ assertEquals(expected.getMessage(), actual.getMessage());
+ assertEquals(expected.getStatus(), actual.getStatus());
+ assertEquals(expected.getResponse(), actual.getResponse());
+ //Clean up
+ fnLanguageDao.delete(fnLanguage);
+ }
+
+
+ @Test
+ void getUserLanguage() {
+ FnLanguageDto expected = new FnLanguageDto();
+ expected.setLanguageAlias("EN");
+ expected.setLanguageName("English");
+
+ FnLanguageDto actual = languageController.getUserLanguage(principal, 1L);
+
+ assertEquals(expected.getLanguageAlias(), actual.getLanguageAlias());
+ assertEquals(expected.getLanguageName(), actual.getLanguageName());
+ }
+
+ @Test
+ void getUserLanguageNotExistingUser() {
+ FnLanguageDto expected = new FnLanguageDto();
+
+ FnLanguageDto actual = languageController.getUserLanguage(principal, 456L);
+
+ assertNull(actual.getLanguageAlias());
+ assertNull(actual.getLanguageName());
+ }
}
\ No newline at end of file
diff --git a/portal-BE/src/test/java/org/onap/portal/controller/UserControllerTest.java b/portal-BE/src/test/java/org/onap/portal/controller/UserControllerTest.java
index b216799..e3f1f7b 100644
--- a/portal-BE/src/test/java/org/onap/portal/controller/UserControllerTest.java
+++ b/portal-BE/src/test/java/org/onap/portal/controller/UserControllerTest.java
@@ -39,13 +39,17 @@
package org.onap.portal.controller;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
-import lombok.AllArgsConstructor;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
+import org.onap.portal.domain.db.fn.FnUser;
import org.onap.portal.domain.dto.PortalRestResponse;
import org.onap.portal.domain.dto.PortalRestStatusEnum;
import org.onap.portal.domain.dto.ProfileDetail;
+import org.onap.portal.service.fn.FnUserService;
+import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
+import org.onap.portalsdk.core.onboarding.util.CipherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -54,14 +58,18 @@
@RunWith(SpringRunner.class)
@SpringBootTest
-@TestPropertySource(locations="classpath:test.properties")
+@TestPropertySource(locations = "classpath:test.properties")
class UserControllerTest {
- private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo", "XZa6pS1vC0qKXWtn9wcZWdLx61L0=");
+ private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
+ "XZa6pS1vC0qKXWtn9wcZWdLx61L0=");
+
+ private final FnUserService userService;
private final UserController userController;
@Autowired
- UserControllerTest(UserController userController) {
+ UserControllerTest(final FnUserService userService, final UserController userController) {
+ this.userService = userService;
this.userController = userController;
}
@@ -73,7 +81,8 @@
expectedDetails.setEmail("demo@openecomp.org");
expectedDetails.setLoginId("demo");
expectedDetails.setLoginPassword("*****");
- PortalRestResponse<ProfileDetail> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", expectedDetails);
+ PortalRestResponse<ProfileDetail> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success",
+ expectedDetails);
PortalRestResponse<ProfileDetail> actual = userController.getLoggedinUser(principal);
@@ -92,7 +101,8 @@
expectedDetails.setLoginPassword("*****");
PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
- PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "lastName must not be blank", null);
+ PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
+ "lastName must not be blank", null);
assertEquals(expected, actual);
}
@@ -109,4 +119,26 @@
PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
assertEquals(expected, actual);
}
+
+ @Test
+ void modifyLoggedinUserChangePassword() throws CipherUtilException {
+ ProfileDetail expectedDetails = new ProfileDetail();
+ expectedDetails.setFirstName("Demo");
+ expectedDetails.setLastName("User");
+ expectedDetails.setEmail("demo@openecomp.org");
+ expectedDetails.setLoginId("demo");
+ expectedDetails.setLoginPassword("123password");
+
+ FnUser user = userService.loadUserByUsername(principal.getName());
+ String oldPassword = user.getLoginPwd();
+
+ PortalRestResponse<String> actual = userController.modifyLoggedinUser(principal, expectedDetails);
+ PortalRestResponse<String> expected = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
+
+ FnUser user2 = userService.loadUserByUsername(principal.getName());
+ String newPassword = user2.getLoginPwd();
+
+ assertEquals(expected, actual);
+ assertNotEquals(oldPassword, newPassword);
+ }
}
\ No newline at end of file
diff --git a/portal-BE/src/test/java/org/onap/portal/service/fn/FnUserServiceTest.java b/portal-BE/src/test/java/org/onap/portal/service/fn/FnUserServiceTest.java
index 3f242fb..26731ea 100644
--- a/portal-BE/src/test/java/org/onap/portal/service/fn/FnUserServiceTest.java
+++ b/portal-BE/src/test/java/org/onap/portal/service/fn/FnUserServiceTest.java
@@ -43,6 +43,10 @@
import static org.junit.jupiter.api.Assertions.*;
import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.onap.portal.domain.db.fn.FnLanguage;
@@ -167,4 +171,21 @@
assertEquals(expected.getSiloStatus(), actual.getSiloStatus());
assertEquals(expected.getLanguageId(), actual.getLanguageId());
}
+
+ @Test
+ void getActiveUsers() {
+ assertEquals(12, fnUserService.getActiveUsers().size());
+ }
+
+ @Test
+ void getUserWithOrgUserId() {
+ assertEquals(1, fnUserService.getUserWithOrgUserId("cs0008").size());
+ }
+
+ @Test
+ void getUserWithOrgUserIds() {
+ List<String> ids = new ArrayList<>(Arrays.asList("cs0008", "op0001"));
+ assertEquals(2, fnUserService.getUsersByOrgIds(ids).size());
+ }
+
}
\ No newline at end of file