diff options
author | Dominik Mizyn <d.mizyn@samsung.com> | 2019-08-22 17:33:03 +0200 |
---|---|---|
committer | Dominik Mizyn <d.mizyn@samsung.com> | 2019-08-22 17:33:11 +0200 |
commit | 8c6ccfe62953f28f704891c76258d595dc9d0b03 (patch) | |
tree | 2d626040c4e4f3a15e206bedf312f436ca5c4a7c /portal-BE/src/main/java | |
parent | 88db33c6a8e2a03e72a0c676f51937418ee84912 (diff) |
LanguageController tests up
LanguageController tests up
Issue-ID: PORTAL-710
Change-Id: I1236e3ee62aca3d07cc6d0f6c61a410a8847f281
Signed-off-by: Dominik Mizyn <d.mizyn@samsung.com>
Diffstat (limited to 'portal-BE/src/main/java')
10 files changed, 235 insertions, 37 deletions
diff --git a/portal-BE/src/main/java/org/onap/portal/aop/service/FnLanguageServiceAOP.java b/portal-BE/src/main/java/org/onap/portal/aop/service/FnLanguageServiceAOP.java index 250a6e2d..db8e0699 100644 --- a/portal-BE/src/main/java/org/onap/portal/aop/service/FnLanguageServiceAOP.java +++ b/portal-BE/src/main/java/org/onap/portal/aop/service/FnLanguageServiceAOP.java @@ -63,15 +63,22 @@ public class FnLanguageServiceAOP { @Before("execution(* org.onap.portal.service.fn.FnLanguageService.save(..)) && args(principal, fnLanguage)") public void save(final Principal principal, final FnLanguage fnLanguage) { if (fnLanguage == null) { - LOGGER.error("User " + principal.getName() + " try to save NULL fnLanguage"); + LOGGER.info("User " + principal.getName() + " try to save NULL fnLanguage"); throw new NullPointerException("FnLanguage cannot be null or empty"); } if (!dataValidator.isValid(fnLanguage)) { String violations = dataValidator.getConstraintViolations(fnLanguage).stream() .map(ConstraintViolation::getMessage) .collect(Collectors.joining(", ")); - LOGGER.error("User " + principal.getName() + " try to save not valid fnLanguage: " + violations); + LOGGER.info("User " + principal.getName() + " try to save not valid fnLanguage: " + violations); throw new IllegalArgumentException("FnLanguage is not valid, " + violations); } } + + @Before("execution(* org.onap.portal.service.fn.FnLanguageService.getLanguageList(..)) && args(principal)") + public void getLanguageList(final Principal principal) { + LOGGER.info("User " + principal.getName() + " try requested for all language list"); + } + + } diff --git a/portal-BE/src/main/java/org/onap/portal/aop/service/FnUserServiceAOP.java b/portal-BE/src/main/java/org/onap/portal/aop/service/FnUserServiceAOP.java new file mode 100644 index 00000000..0cb17cc6 --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/aop/service/FnUserServiceAOP.java @@ -0,0 +1,37 @@ +package org.onap.portal.aop.service; + +import java.security.Principal; +import java.util.stream.Collectors; +import javax.validation.ConstraintViolation; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.onap.portal.domain.db.fn.FnUser; +import org.onap.portal.validation.DataValidator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class FnUserServiceAOP { + private static final Logger LOGGER = LoggerFactory.getLogger(FnLanguageServiceAOP.class); + + @Autowired + private DataValidator dataValidator; + + @Before("execution(* org.onap.portal.service.fn.FnUserService.saveFnUser(..)) && args(principal, fnUser)") + public void save(final Principal principal, final FnUser fnUser) { + if (fnUser == null) { + LOGGER.error("User " + principal.getName() + " try to save NULL fnUser"); + throw new NullPointerException("FnUser cannot be null or empty"); + } + if (!dataValidator.isValid(fnUser)) { + String violations = dataValidator.getConstraintViolations(fnUser).stream() + .map(ConstraintViolation::getMessage) + .collect(Collectors.joining(", ")); + LOGGER.error("User " + principal.getName() + " try to save not valid fnUser: " + violations); + throw new IllegalArgumentException("FnUser is not valid, " + violations); + } + } +} 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 97fe03da..adee349b 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 @@ -42,9 +42,7 @@ package org.onap.portal.controller; import java.security.Principal; import java.util.List; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.onap.portal.aop.service.FnLanguageServiceAOP; +import java.util.Optional; import org.onap.portal.domain.db.fn.FnLanguage; import org.onap.portal.domain.db.fn.FnUser; import org.onap.portal.domain.dto.PortalRestResponse; @@ -60,12 +58,12 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/auxapi") public class LanguageController { + private static final Logger LOGGER = LoggerFactory.getLogger(LanguageController.class); private final FnLanguageService languageService; @@ -78,39 +76,49 @@ public class LanguageController { this.fnUserService = fnUserService; } - @GetMapping(value = "/language", produces = "application/json;charset=UTF-8") - public List<FnLanguage> getLanguageList() { - return languageService.getLanguages(); + @GetMapping(value = "/language", produces = MediaType.APPLICATION_JSON_VALUE) + public List<FnLanguage> getLanguageList(final Principal principal) { + return languageService.getLanguages(principal); } @PostMapping(value = "/languageSetting/user/{loginId}") - public void setUpUserLanguage(@RequestBody FnLanguage fnLanguage, - @PathVariable("loginId") Long loginId) { - if (fnUserService.getUser(loginId).isPresent()){ - FnUser user = fnUserService.getUser(loginId).get(); - user.setLanguage_id(fnLanguage.getLanguageId()); - fnUserService.saveFnUser(user); + public PortalRestResponse<String> setUpUserLanguage(Principal principal, @RequestBody FnLanguage fnLanguage, + @PathVariable("loginId") Long userId) { + PortalRestResponse<String> response = new PortalRestResponse<>(); + try { + if (fnUserService.getUser(userId).isPresent()) { + FnUser user = fnUserService.getUser(userId).get(); + user.setLanguageId(fnLanguage); + fnUserService.saveFnUser(principal, user); + } + response.setMessage("SUCCESS"); + response.setStatus(PortalRestStatusEnum.OK); + } catch (Exception e) { + response.setMessage("FAILURE"); + response.setResponse(e.getMessage()); + response.setStatus(PortalRestStatusEnum.ERROR); + return response; } + return response; } - @GetMapping(value = "/languageSetting/user/{loginId}") - public FnLanguage getUserLanguage(HttpServletRequest request, HttpServletResponse response, - @PathVariable("loginId") Long loginId) { - if (fnUserService.getUser(loginId).isPresent()){ - Long languageId = fnUserService.getUser(loginId).get().getLanguage_id(); - return languageService.findById(languageId).orElse(new FnLanguage()); + @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()); } return new FnLanguage(); } @PostMapping(value = "/language", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public PortalRestResponse<String> saveLanguage(final Principal principal, @RequestBody final FnLanguage fnLanguage){ + public PortalRestResponse<String> saveLanguage(final Principal principal, + @RequestBody final FnLanguage fnLanguage) { PortalRestResponse<String> response = new PortalRestResponse<>(); try { response.setMessage("SUCCESS"); response.setResponse(languageService.save(principal, fnLanguage).toString()); response.setStatus(PortalRestStatusEnum.OK); - } catch (Exception e){ + } catch (Exception e) { response.setMessage("FAILURE"); response.setResponse(e.getMessage()); response.setStatus(PortalRestStatusEnum.ERROR); diff --git a/portal-BE/src/main/java/org/onap/portal/dao/fn/FnLuTimezoneDao.java b/portal-BE/src/main/java/org/onap/portal/dao/fn/FnLuTimezoneDao.java new file mode 100644 index 00000000..eca61ddd --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/dao/fn/FnLuTimezoneDao.java @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Modifications Copyright (c) 2019 Samsung + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ + +package org.onap.portal.dao.fn; + +import org.onap.portal.domain.db.fn.FnLuTimezone; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +@Transactional +public interface FnLuTimezoneDao extends JpaRepository<FnLuTimezone, Integer> { + +} diff --git a/portal-BE/src/main/java/org/onap/portal/domain/db/fn/FnLanguage.java b/portal-BE/src/main/java/org/onap/portal/domain/db/fn/FnLanguage.java index 996929e7..d9aff94c 100644 --- a/portal-BE/src/main/java/org/onap/portal/domain/db/fn/FnLanguage.java +++ b/portal-BE/src/main/java/org/onap/portal/domain/db/fn/FnLanguage.java @@ -41,11 +41,16 @@ package org.onap.portal.domain.db.fn; import com.fasterxml.jackson.annotation.JsonInclude; +import java.util.ArrayList; +import java.util.List; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.validation.constraints.Digits; @@ -74,7 +79,7 @@ CREATE TABLE `fn_language` ( @Setter @Entity @JsonInclude() -@SequenceGenerator(name="seq", initialValue=3, allocationSize=100) +@SequenceGenerator(name="seq", initialValue=1000, allocationSize=100000) public class FnLanguage { @Id @@ -92,5 +97,12 @@ public class FnLanguage { @NotNull(message = "languageAlias must not be null") @SafeHtml private String languageAlias; + @OneToMany( + targetEntity = FnUser.class, + mappedBy = "languageId", + cascade = CascadeType.ALL, + fetch = FetchType.EAGER + ) + private List<FnUser> fnUsers = new ArrayList<>(); } 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 8a778bfa..4747cdf8 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 @@ -59,6 +59,8 @@ import javax.persistence.ManyToOne; import javax.persistence.NamedNativeQueries; import javax.persistence.NamedNativeQuery; import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.UniqueConstraint; import javax.validation.Valid; @@ -68,9 +70,11 @@ import javax.validation.constraints.NotNull; import javax.validation.constraints.PastOrPresent; import javax.validation.constraints.Size; import lombok.AllArgsConstructor; +import lombok.Builder.Default; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.ToString; import org.hibernate.validator.constraints.SafeHtml; import org.onap.portal.domain.db.cr.CrReportFileHistory; import org.onap.portal.domain.db.ep.EpPersUserWidgetPlacement; @@ -172,10 +176,11 @@ CREATE TABLE `fn_user` ( @Getter @Setter @Entity -public class FnUser extends DomainVo implements UserDetails { +@SequenceGenerator(name="seq", initialValue=1000, allocationSize=100000) +public class FnUser implements UserDetails { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq") @Column(name = "user_id", length = 11, nullable = false) @Digits(integer = 11, fraction = 0) private Long userId; @@ -248,7 +253,7 @@ public class FnUser extends DomainVo implements UserDetails { @Column(name = "active_yn", length = 1, columnDefinition = "character varying(1) default 'y'", nullable = false) @Size(max = 1) @SafeHtml - @NotNull + //@NotNull(message = "activeYn must not be null") private String activeYn; @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "created_id") @@ -267,7 +272,7 @@ public class FnUser extends DomainVo implements UserDetails { @Column(name = "is_internal_yn", length = 1, columnDefinition = "character varying(1) default 'n'", nullable = false) @Size(max = 1) @SafeHtml - @NotNull + //@NotNull(message = "isInternalYn must not be null") private String isInternalYn; @Column(name = "address_line_1", length = 100, columnDefinition = "varchar(100) DEFAULT NULL") @Size(max = 100) @@ -341,12 +346,13 @@ public class FnUser extends DomainVo implements UserDetails { @Size(max = 10) @SafeHtml private String siloStatus; - @Column(name = "language_id", length = 2, columnDefinition = "int(2) default 1", nullable = false) - @Digits(integer = 2, fraction = 0) - @NotNull - private Long language_id; + @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @JoinColumn(name = "language_id", nullable = false, columnDefinition = "int(11) DEFAULT 1") + @Valid + //@NotNull(message = "languageId must not be null") + private FnLanguage languageId; @Column(name = "is_guest", columnDefinition = "boolean default 0", nullable = false) - @NotNull + @NotNull(message = "guest must not be null") private boolean guest; @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "fnUserList") private List<CrReportFileHistory> crReportFileHistorie = new ArrayList<>(); diff --git a/portal-BE/src/main/java/org/onap/portal/domain/dto/DomainVo.java b/portal-BE/src/main/java/org/onap/portal/domain/dto/DomainVo.java index 6006a1b2..305af8e6 100644 --- a/portal-BE/src/main/java/org/onap/portal/domain/dto/DomainVo.java +++ b/portal-BE/src/main/java/org/onap/portal/domain/dto/DomainVo.java @@ -19,7 +19,7 @@ import org.onap.portalsdk.core.domain.FusionVo; @Getter @Setter -@EqualsAndHashCode(callSuper = true) +@EqualsAndHashCode @NoArgsConstructor @AllArgsConstructor @Inheritance(strategy = InheritanceType.JOINED) diff --git a/portal-BE/src/main/java/org/onap/portal/service/fn/FnLanguageService.java b/portal-BE/src/main/java/org/onap/portal/service/fn/FnLanguageService.java index da9c0482..c58bf098 100644 --- a/portal-BE/src/main/java/org/onap/portal/service/fn/FnLanguageService.java +++ b/portal-BE/src/main/java/org/onap/portal/service/fn/FnLanguageService.java @@ -46,9 +46,13 @@ import java.util.Optional; import org.onap.portal.dao.fn.FnLanguageDao; import org.onap.portal.domain.db.fn.FnLanguage; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service +@EnableAspectJAutoProxy +@Transactional public class FnLanguageService { private final FnLanguageDao fnLanguageDao; @@ -60,7 +64,7 @@ public class FnLanguageService { public Optional<FnLanguage> findById(final Long id){ return fnLanguageDao.findById(id); } - public List<FnLanguage> getLanguages(){ + public List<FnLanguage> getLanguages(Principal principal){ return fnLanguageDao.findAll(); } public FnLanguage save(final Principal principal, final FnLanguage fnLanguage){ diff --git a/portal-BE/src/main/java/org/onap/portal/service/fn/FnLuTimezoneService.java b/portal-BE/src/main/java/org/onap/portal/service/fn/FnLuTimezoneService.java new file mode 100644 index 00000000..ea3dd386 --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/service/fn/FnLuTimezoneService.java @@ -0,0 +1,63 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Modifications Copyright (c) 2019 Samsung + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ + +package org.onap.portal.service.fn; + +import java.util.Optional; +import org.onap.portal.dao.fn.FnLuTimezoneDao; +import org.onap.portal.domain.db.fn.FnLuTimezone; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class FnLuTimezoneService { + private final FnLuTimezoneDao fnLuTimezoneDao; + + @Autowired + public FnLuTimezoneService(final FnLuTimezoneDao fnLuTimezoneDao) { + this.fnLuTimezoneDao = fnLuTimezoneDao; + } + + public Optional<FnLuTimezone> getById(Integer id){ + return fnLuTimezoneDao.findById(id); + } +} 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 91873139..b06abfb5 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 @@ -40,6 +40,7 @@ package org.onap.portal.service.fn; +import java.security.Principal; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -47,21 +48,25 @@ import java.util.stream.Collectors; import org.onap.portal.dao.fn.FnUserDao; import org.onap.portal.domain.db.fn.FnUser; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service +@EnableAspectJAutoProxy +@Transactional public class FnUserService implements UserDetailsService { - private FnUserDao fnUserDao; + private final FnUserDao fnUserDao; @Autowired public FnUserService(FnUserDao fnUserDao) { this.fnUserDao = fnUserDao; } - public FnUser saveFnUser(FnUser fnUser) { + public FnUser saveFnUser(final Principal principal, final FnUser fnUser) { return fnUserDao.save(fnUser); } @@ -99,4 +104,8 @@ public class FnUserService implements UserDetailsService { List<FnUser> etActiveUsers(){ return fnUserDao.findAll().stream().filter(fnUser -> "Y".equals(fnUser.getActiveYn())).collect(Collectors.toList()); } + + public void deleteUser(FnUser fnUser){ + fnUserDao.delete(fnUser); + } } |