├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── settings.gradle └── src ├── main ├── java │ └── org │ │ └── paasta │ │ └── caas │ │ └── common │ │ └── api │ │ ├── PaasTaCaasCommonApiApplication.java │ │ ├── adminToken │ │ ├── AdminToken.java │ │ ├── AdminTokenController.java │ │ ├── AdminTokenRepository.java │ │ └── AdminTokenService.java │ │ ├── common │ │ ├── AspectService.java │ │ ├── CommonService.java │ │ ├── Constants.java │ │ ├── PropertyService.java │ │ └── RestTemplateService.java │ │ ├── config │ │ ├── RestTemplateConfig.java │ │ └── SecurityConfig.java │ │ ├── roles │ │ ├── RoleSets.java │ │ ├── Roles.java │ │ ├── RolesController.java │ │ ├── RolesList.java │ │ ├── RolesRepository.java │ │ └── RolesService.java │ │ └── users │ │ ├── Users.java │ │ ├── UsersController.java │ │ ├── UsersList.java │ │ ├── UsersRepository.java │ │ └── UsersService.java └── resources │ ├── application.yml │ └── logback-spring.xml └── test ├── java └── org │ └── paasta │ └── caas │ └── common │ └── api │ ├── PaasTaCaasCommonApiApplicationTests.java │ ├── adminToken │ └── AdminTokenServiceTest.java │ ├── roles │ └── RolesServiceTest.java │ └── users │ └── UsersServiceTest.java └── resources └── application.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # ignore output and build directories 14 | build/** 15 | bin/** 16 | out/** 17 | 18 | # Package Files # 19 | *.jar 20 | *.war 21 | *.ear 22 | *.zip 23 | *.tar.gz 24 | *.rar 25 | 26 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 27 | hs_err_pid* 28 | 29 | # generated gradle directories and files 30 | .gradle 31 | .settings 32 | 33 | # eclise settings 34 | .classpath 35 | .project 36 | 37 | # idea settings 38 | *.iml 39 | *.idea 40 | .idea/* 41 | 42 | # remove spring bean configuration 43 | .spring* 44 | 45 | /logs/* 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PAAS-TA-CAAS-COMMON-API 2 | PaaS-TA 에서 제공하는 Container 서비스의 DB API 를 제공하는 서비스이다. 3 | 4 | ## 개발 환경 5 | - JDK 8 6 | - Gradle 4.4.1 7 | - Spring Io Platform Cairo-SR3 8 | - Spring Boot 2.0.4 9 | - Mysql Java Connector 8.0.11 10 | - Lombok 1.18.2 11 | - Jacoco 0.8.1 -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | dependencies { 6 | classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE' 7 | classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.4.RELEASE") 8 | } 9 | } 10 | 11 | apply plugin: 'java' 12 | apply plugin: 'idea' 13 | apply plugin: 'jacoco' 14 | apply plugin: 'org.springframework.boot' 15 | apply plugin: 'io.spring.dependency-management' 16 | 17 | repositories { 18 | mavenCentral() 19 | } 20 | 21 | ext { 22 | appGroup = 'org.paasta.caas.common.api' 23 | springIoPlatformVersion = 'Cairo-SR3' 24 | mysqlConnectorJavaVersion = '8.0.11' 25 | lombokVersion = '1.18.2' 26 | jacocoVersion = '0.8.1' 27 | } 28 | 29 | dependencyManagement { 30 | imports { 31 | mavenBom "io.spring.platform:platform-bom:${springIoPlatformVersion}" 32 | } 33 | } 34 | 35 | sourceCompatibility = 1.8 36 | group = "${appGroup}" 37 | 38 | jar { 39 | archivesBaseName = 'paas-ta-caas-common-api' 40 | } 41 | 42 | dependencies { 43 | //Server Security 44 | compile ('org.springframework.boot:spring-boot-starter-security') 45 | compile "org.springframework.boot:spring-boot-starter-web" 46 | compile "org.springframework.boot:spring-boot-starter-aop" 47 | compile "org.springframework.boot:spring-boot-starter-data-jpa" 48 | compile "org.springframework.boot:spring-boot-starter-jdbc" 49 | compileOnly "org.projectlombok:lombok:${lombokVersion}" 50 | runtime "mysql:mysql-connector-java:${mysqlConnectorJavaVersion}" 51 | testCompile "org.springframework.boot:spring-boot-starter-test" 52 | } 53 | 54 | jacoco { 55 | toolVersion = "${jacocoVersion}" 56 | } 57 | 58 | test { 59 | ignoreFailures=true 60 | jacoco { 61 | destinationFile = file("$buildDir/jacoco/jacoco-overall.exec") 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'PAAS-TA-CAAS-COMMON-API' 2 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/PaasTaCaasCommonApiApplication.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class PaasTaCaasCommonApiApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(PaasTaCaasCommonApiApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/adminToken/AdminToken.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.adminToken; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import lombok.Data; 5 | 6 | import javax.persistence.*; 7 | import javax.validation.constraints.NotEmpty; 8 | import javax.validation.constraints.NotNull; 9 | 10 | /** 11 | * kuber와 관련 api를 호출 할 때 필요한 admin token을 저장하기 위한 model 12 | * token_name은 "caas_admin"으로 고정, property로 빼는 것이 좋은 것은 알지만 13 | * 릴리즈 수정을 줄이기 위함. 14 | * @author Hyerin 15 | * @since 2018.08.22 16 | * @version 20180822 17 | */ 18 | @Entity 19 | @Table(name = "admin_token") 20 | @Data 21 | @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 22 | public class AdminToken { 23 | 24 | @Id 25 | @Column(name = "token_name") 26 | private String tokenName; 27 | 28 | @Column(name = "token_value") 29 | @NotNull(message = "TOKEN VALUE cannot be null") 30 | @NotEmpty(message = "TOKEN VALUE is mandatory") 31 | private String tokenValue; 32 | 33 | @Transient 34 | private String resultCode; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/adminToken/AdminTokenController.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.adminToken; 2 | 3 | import org.paasta.caas.common.api.common.CommonService; 4 | import org.paasta.caas.common.api.common.Constants; 5 | import org.springframework.web.bind.annotation.*; 6 | 7 | /** 8 | * AdminToken Controller 클래스 9 | * 10 | * @author dojjang 11 | * @version 1.0 12 | * @since 2018.08.27 13 | */ 14 | @RestController 15 | public class AdminTokenController { 16 | 17 | private final AdminTokenService adminTokenService; 18 | private final CommonService commonService; 19 | 20 | /** 21 | * Instantiates a new Admin token controller. 22 | * 23 | * @param adminTokenService the admin token service 24 | * @param commonService 25 | */ 26 | public AdminTokenController(AdminTokenService adminTokenService, CommonService commonService) { 27 | this.adminTokenService = adminTokenService; 28 | this.commonService = commonService; 29 | } 30 | 31 | 32 | /** 33 | * Admin token 상세 정보를 조회한다. 34 | * 35 | * @param tokenName the tokenName 36 | * @return the AdminToken 37 | */ 38 | @GetMapping(value = Constants.URI_API_ADMIN_TOKEN_DETAIL) 39 | AdminToken getTokenValue(@PathVariable("tokenName") String tokenName) { 40 | return adminTokenService.getTokenValue(tokenName); 41 | } 42 | 43 | 44 | /** 45 | * Admin token 정보를 등록한다. 46 | * 47 | * @param adminToken the admin token 48 | * @return the admin token 49 | */ 50 | @PostMapping(value = Constants.URI_API_ADMIN_TOKEN) 51 | AdminToken createAdminToken(@RequestBody AdminToken adminToken) { 52 | return adminTokenService.createAdminToken(adminToken); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/adminToken/AdminTokenRepository.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.adminToken; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | 7 | /** 8 | * Service Instance JPA Repository 클래스 9 | * @author Hyerin 10 | * @since 2018.07.24 11 | * @version 20180725 12 | */ 13 | @Repository 14 | public interface AdminTokenRepository extends CrudRepository { 15 | 16 | AdminToken findByTokenName(String tokenName); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/adminToken/AdminTokenService.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.adminToken; 2 | 3 | import org.paasta.caas.common.api.common.CommonService; 4 | import org.paasta.caas.common.api.common.Constants; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | /** 9 | * AdminToken Service 클래스 10 | * 11 | * @author dojjang 12 | * @version 1.0 13 | * @since 2018.08.27 14 | */ 15 | @Service 16 | public class AdminTokenService { 17 | 18 | private final CommonService commonService; 19 | private final AdminTokenRepository adminTokenRepository; 20 | 21 | /** 22 | * Instantiates a new User service. 23 | * 24 | * @param commonService the common service 25 | * @param adminTokenRepository the adminToken repository 26 | */ 27 | @Autowired 28 | public AdminTokenService(CommonService commonService, AdminTokenRepository adminTokenRepository) { 29 | this.commonService = commonService; 30 | this.adminTokenRepository = adminTokenRepository; 31 | } 32 | 33 | 34 | /** 35 | * Admin token 상세 정보를 조회한다. 36 | * 37 | * @param tokenName the tokenName 38 | * @return the AdminToken 39 | */ 40 | AdminToken getTokenValue(String tokenName) { 41 | AdminToken token = adminTokenRepository.findByTokenName(tokenName); 42 | AdminToken nullObject = new AdminToken(); 43 | 44 | if(token == null) { 45 | return (AdminToken) commonService.setResultModel(nullObject, Constants.RESULT_STATUS_FAIL); 46 | } 47 | 48 | return (AdminToken) commonService.setResultModel(token, Constants.RESULT_STATUS_SUCCESS); 49 | } 50 | 51 | 52 | /** 53 | * Admin token 정보를 등록한다. 54 | * 55 | * @param adminToken the admin token 56 | * @return the admin token 57 | */ 58 | AdminToken createAdminToken(AdminToken adminToken) { 59 | String result = commonService.procValidator(adminToken); 60 | 61 | if (result.equals(Constants.RESULT_STATUS_SUCCESS)) { 62 | return adminTokenRepository.save(adminToken); 63 | } else { 64 | return (AdminToken) commonService.setResultModel(AdminToken.class, Constants.RESULT_STATUS_FAIL); 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/common/AspectService.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.common; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.annotation.Aspect; 5 | import org.aspectj.lang.annotation.Before; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.web.context.request.RequestContextHolder; 10 | import org.springframework.web.context.request.ServletRequestAttributes; 11 | 12 | import javax.servlet.http.HttpServletRequest; 13 | import java.util.Arrays; 14 | import java.util.Enumeration; 15 | import java.util.Objects; 16 | 17 | /** 18 | * Aspect Service 클래스 19 | * 20 | * @author REX 21 | * @version 1.0 22 | * @since 2018.08.06 23 | */ 24 | @Aspect 25 | @Service 26 | public class AspectService { 27 | 28 | private static final Logger LOGGER = LoggerFactory.getLogger(AspectService.class); 29 | 30 | /** 31 | * On before log service access. 32 | * 33 | * @param joinPoint the join point 34 | */ 35 | @Before("execution(* org.paasta.caas..*Service.*(..))") 36 | public void onBeforeLogServiceAccess(JoinPoint joinPoint) { 37 | LOGGER.warn("######## ON BEFORE SERVICE ACCESS :: {}", joinPoint); 38 | } 39 | 40 | 41 | /** 42 | * On before log controller access. 43 | * 44 | * @param joinPoint the join point 45 | */ 46 | @Before("execution(* org.paasta.caas..*Controller.*(..))") 47 | public void onBeforeLogControllerAccess(JoinPoint joinPoint) { 48 | LOGGER.warn("#### COMMON API :: ON BEFORE CONTROLLER ACCESS :: {}", joinPoint); 49 | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); 50 | 51 | LOGGER.warn("## Entering in Method: {}", joinPoint.getSignature().getName()); 52 | LOGGER.warn("## Class Name: {}", joinPoint.getSignature().getDeclaringTypeName()); 53 | LOGGER.warn("## Arguments: {}", Arrays.toString(joinPoint.getArgs())); 54 | LOGGER.warn("## Target class: {}", joinPoint.getTarget().getClass().getName()); 55 | 56 | if (null != request) { 57 | LOGGER.warn("## Request Path info: {}", request.getServletPath()); 58 | LOGGER.warn("## Method Type: {}", request.getMethod()); 59 | LOGGER.warn("================================================================================"); 60 | LOGGER.warn("Start Header Section of request"); 61 | Enumeration headerNames = request.getHeaderNames(); 62 | while (headerNames.hasMoreElements()) { 63 | String headerName = (String) headerNames.nextElement(); 64 | String headerValue = request.getHeader(headerName); 65 | LOGGER.warn(" Header Name: {} || Header Value: {}", headerName, headerValue); 66 | } 67 | LOGGER.warn("End Header Section of request"); 68 | LOGGER.warn("================================================================================"); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/common/CommonService.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.common; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Service; 6 | 7 | import javax.validation.ConstraintViolation; 8 | import javax.validation.Validation; 9 | import javax.validation.Validator; 10 | import java.lang.reflect.InvocationTargetException; 11 | import java.lang.reflect.Method; 12 | import java.util.Set; 13 | 14 | /** 15 | * Common Service 클래스 16 | * 17 | * @author REX 18 | * @version 1.0 19 | * @since 2018.08.07 20 | */ 21 | @Service 22 | public class CommonService { 23 | 24 | private static final Logger LOGGER = LoggerFactory.getLogger(CommonService.class); 25 | 26 | /** 27 | * Proc validator string. 28 | * 29 | * @param reqObject the req object 30 | * @return the string 31 | */ 32 | public String procValidator(Object reqObject) { 33 | String result = Constants.RESULT_STATUS_SUCCESS; 34 | 35 | Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); 36 | Set> violations = validator.validate(reqObject); 37 | 38 | for (ConstraintViolation violation : violations) { 39 | result = violation.getMessage(); 40 | } 41 | 42 | return result; 43 | } 44 | 45 | /** 46 | * Sets result model. 47 | * 48 | * @param reqObject the req object 49 | * @param resultCode the result status code 50 | * @return the result model 51 | */ 52 | public Object setResultModel(Object reqObject, String resultCode) { 53 | try { 54 | Class aClass = reqObject.getClass(); 55 | 56 | Method methodSetResultCode = aClass.getMethod("setResultCode", String.class); 57 | methodSetResultCode.invoke(reqObject, resultCode); 58 | 59 | } catch (NoSuchMethodException e) { 60 | LOGGER.error("NoSuchMethodException :: {}", e); 61 | } catch (IllegalAccessException e1) { 62 | LOGGER.error("IllegalAccessException :: {}", e1); 63 | } catch (InvocationTargetException e2) { 64 | LOGGER.error("InvocationTargetException :: {}", e2); 65 | } 66 | 67 | return reqObject; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/common/Constants.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.common; 2 | 3 | /** 4 | * Constants 클래스 5 | * 6 | * @author REX 7 | * @version 1.0 8 | * @since 2018.08.02 9 | */ 10 | public class Constants { 11 | 12 | public static final String RESULT_STATUS_SUCCESS = "SUCCESS"; 13 | public static final String RESULT_STATUS_FAIL = "FAIL"; 14 | public static final String STRING_DATE_TYPE = "yyyy-MM-dd HH:mm:ss"; 15 | public static final String STRING_TIME_ZONE_ID = "Asia/Seoul"; 16 | public static final String TARGET_CAAS_API = "caasApi"; 17 | 18 | // API URI 19 | public static final String URI_API_ADMIN_TOKEN = "/adminToken"; 20 | public static final String URI_API_ADMIN_TOKEN_DETAIL = "/adminToken/{tokenName:.+}"; 21 | 22 | public static final String URI_API_ROLES = "/roles"; 23 | public static final String URI_API_ROLES_ID_LIST = "/roles/{id:.+}"; 24 | public static final String URI_API_ROLES_ID_DETAIL = "/roles/{id:.+}/detail"; 25 | 26 | public static final String URI_API_USERS = "/users"; 27 | public static final String URI_API_USERS_DETAIL = "/users/{id:.+}"; 28 | public static final String URI_API_USERS_BY_SERVICE_INSTANCE_ID = "/users/serviceInstanceId/{serviceInstanceId:.+}"; 29 | public static final String URI_API_USERS_BY_SUID_AND_ORG_GUID = "/users/serviceInstanceId/{serviceInstanceId:.+}/organizationGuid/{organizationGuid:.+}"; 30 | public static final String URI_API_USERS_BY_SUID_AND_ORG_GUID_AND_USER_ID = "/users/serviceInstanceId/{serviceInstanceId:.+}/organizationGuid/{organizationGuid:.+}/userId/{userId:.+}/"; 31 | 32 | public static final String URI_API_USERS_VALID_EXIST_NAMESPACE = "/users/userId/{userId}/namespace/{namespace}"; 33 | 34 | private Constants() { 35 | throw new IllegalStateException(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/common/PropertyService.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.common; 2 | 3 | import lombok.Data; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Service; 6 | 7 | /** 8 | * Property Service 클래스 9 | * 10 | * @author REX 11 | * @version 1.0 12 | * @since 2018.08.07 13 | */ 14 | @Service 15 | @Data 16 | public class PropertyService { 17 | 18 | @Value("${caasApi.url}") 19 | private String caasApiUrl; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/common/RestTemplateService.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.common; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.http.*; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.util.Base64Utils; 10 | import org.springframework.web.client.RestTemplate; 11 | 12 | import java.nio.charset.StandardCharsets; 13 | 14 | /** 15 | * RestTemplate Service 클래스 16 | * 17 | * @author REX 18 | * @version 1.0 19 | * @since 2018.08.07 20 | */ 21 | @Service 22 | public class RestTemplateService { 23 | 24 | private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateService.class); 25 | private static final String AUTHORIZATION_HEADER_KEY = "Authorization"; 26 | private static final String CONTENT_TYPE = "Content-Type"; 27 | private final String caasApiBase64Authorization; 28 | private final RestTemplate restTemplate; 29 | private String base64Authorization; 30 | private String baseUrl; 31 | 32 | private final PropertyService propertyService; 33 | 34 | @Autowired 35 | public RestTemplateService(RestTemplate restTemplate, 36 | @Value("${caasApi.authorization.id}") String caasApiAuthorizationId, 37 | @Value("${caasApi.authorization.password}") String caasApiAuthorizationPassword, 38 | PropertyService propertyService) { 39 | this.restTemplate = restTemplate; 40 | this.propertyService = propertyService; 41 | 42 | this.caasApiBase64Authorization = "Basic " 43 | + Base64Utils.encodeToString( 44 | (caasApiAuthorizationId + ":" + caasApiAuthorizationPassword).getBytes(StandardCharsets.UTF_8)); 45 | } 46 | 47 | 48 | public T send(String reqApi, String reqUrl, HttpMethod httpMethod, Object bodyObject, Class responseType) { 49 | 50 | setApiUrlAuthorization(reqApi); 51 | 52 | HttpHeaders reqHeaders = new HttpHeaders(); 53 | reqHeaders.add(AUTHORIZATION_HEADER_KEY, base64Authorization); 54 | reqHeaders.add(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); 55 | 56 | HttpEntity reqEntity = new HttpEntity<>(bodyObject, reqHeaders); 57 | 58 | LOGGER.info(" T SEND :: REQUEST: {} BASE-URL: {}, CONTENT-TYPE: {}", httpMethod, reqUrl, reqHeaders.get(CONTENT_TYPE)); 59 | ResponseEntity resEntity = restTemplate.exchange(baseUrl + reqUrl, httpMethod, reqEntity, responseType); 60 | 61 | if (resEntity.getBody() != null) { 62 | LOGGER.info("RESPONSE-TYPE: {}", resEntity.getBody().getClass()); 63 | } else { 64 | LOGGER.error("RESPONSE-TYPE: RESPONSE BODY IS NULL"); 65 | } 66 | 67 | return resEntity.getBody(); 68 | } 69 | 70 | 71 | private void setApiUrlAuthorization(String reqApi) { 72 | 73 | String apiUrl = ""; 74 | String authorization = ""; 75 | 76 | // CAAS API 77 | if (Constants.TARGET_CAAS_API.equals(reqApi)) { 78 | apiUrl = propertyService.getCaasApiUrl(); 79 | authorization = caasApiBase64Authorization; 80 | } 81 | 82 | this.base64Authorization = authorization; 83 | this.baseUrl = apiUrl; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/config/RestTemplateConfig.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.client.RestTemplate; 6 | 7 | /** 8 | * RestTemplate Config 클래스 9 | * 10 | * @author REX 11 | * @version 1.0 12 | * @since 2018.08.07 13 | */ 14 | @Configuration 15 | public class RestTemplateConfig { 16 | 17 | @Bean 18 | public RestTemplate restTemplate() { 19 | return new RestTemplate(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.security.authentication.AuthenticationManager; 8 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 9 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 10 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 11 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 12 | import org.springframework.security.config.http.SessionCreationPolicy; 13 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 14 | import org.springframework.web.cors.CorsConfiguration; 15 | import org.springframework.web.cors.CorsConfigurationSource; 16 | 17 | import javax.servlet.http.HttpServletRequest; 18 | import java.util.Collections; 19 | 20 | @Configuration 21 | @EnableWebSecurity 22 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 23 | 24 | @Value("${spring.security.username}") 25 | String username; 26 | 27 | @Value("${spring.security.password}") 28 | String password; 29 | 30 | /** 31 | * Spring Security WebSecurityConfigurer implements 32 | * 33 | * @author ciss 34 | * @version 1.0 35 | * @since 2018.11.14 36 | */ 37 | 38 | @Bean 39 | public BCryptPasswordEncoder passwordEncoder() { 40 | return new BCryptPasswordEncoder(); 41 | } 42 | 43 | @Autowired 44 | protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 45 | auth.inMemoryAuthentication() 46 | .withUser(username) 47 | .password(passwordEncoder().encode(password)) 48 | .roles("USER"); 49 | } 50 | 51 | @Bean 52 | @Override 53 | public AuthenticationManager authenticationManagerBean() throws Exception { 54 | return super.authenticationManagerBean(); 55 | } 56 | 57 | @Override 58 | protected void configure(HttpSecurity http) throws Exception { 59 | http.csrf().disable() 60 | .sessionManagement() 61 | .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 62 | .and() 63 | .authorizeRequests() 64 | .antMatchers("/**").hasRole("USER") 65 | .anyRequest().authenticated() 66 | .and() 67 | .httpBasic() 68 | .and() 69 | .cors().configurationSource(corsConfiguration()); 70 | 71 | } 72 | 73 | private CorsConfigurationSource corsConfiguration(){ 74 | return new CorsConfigurationSource() { 75 | @Override 76 | public CorsConfiguration getCorsConfiguration(HttpServletRequest request) { 77 | CorsConfiguration config = new CorsConfiguration(); 78 | config.setAllowedHeaders(Collections.singletonList("*")); 79 | config.setAllowedMethods(Collections.singletonList("*")); 80 | config.addAllowedOrigin("*"); 81 | config.setAllowCredentials(true); 82 | return config; 83 | } 84 | }; 85 | } 86 | } -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/roles/RoleSets.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.roles; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * Roles PK 설정을 위한 클래스 9 | * 10 | * @author hrjin 11 | * @version 1.0 12 | * @since 2018-08-27 13 | */ 14 | @Data 15 | public class RoleSets implements Serializable{ 16 | private String roleSetCode; 17 | private String resourceCode; 18 | private String verbCode; 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/roles/Roles.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.roles; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import lombok.Data; 5 | import org.paasta.caas.common.api.common.Constants; 6 | 7 | import javax.persistence.*; 8 | import javax.validation.constraints.NotEmpty; 9 | import javax.validation.constraints.NotNull; 10 | import java.time.LocalDateTime; 11 | import java.time.ZoneId; 12 | import java.time.format.DateTimeFormatter; 13 | 14 | /** 15 | * Roles Model 클래스 16 | * 17 | * @author hrjin 18 | * @version 1.0 19 | * @since 2018-08-27 20 | */ 21 | @Entity 22 | @Table(name = "caas_user_role_set") 23 | @IdClass(RoleSets.class) 24 | @Data 25 | @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 26 | public class Roles { 27 | @Id 28 | @Column(name = "role_set_code", nullable = false) 29 | @NotNull(message = "role_set_code cannot be null") 30 | @NotEmpty(message = "role_set_code is mandatory") 31 | private String roleSetCode; 32 | 33 | @Id 34 | @Column(name = "resource_code", nullable = false) 35 | @NotNull(message = "resource_code cannot be null") 36 | @NotEmpty(message = "resource_code is mandatory") 37 | private String resourceCode; 38 | 39 | @Id 40 | @Column(name = "verb_code", nullable = false) 41 | @NotNull(message = "verb_code cannot be null") 42 | @NotEmpty(message = "verb_code is mandatory") 43 | private String verbCode; 44 | 45 | @Column(name = "description") 46 | private String description; 47 | 48 | @Column(name = "created", nullable = false, updatable = false) 49 | private String created; 50 | 51 | @Transient 52 | private String resultCode; 53 | 54 | @PrePersist 55 | void preInsert() { 56 | if (this.created == null) { 57 | this.created = LocalDateTime.now(ZoneId.of(Constants.STRING_TIME_ZONE_ID)).format(DateTimeFormatter.ofPattern(Constants.STRING_DATE_TYPE)); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/roles/RolesController.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.roles; 2 | 3 | import org.paasta.caas.common.api.common.Constants; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.*; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * Roles Controller 클래스 11 | * 12 | * @author hrjin 13 | * @version 1.0 14 | * @since 2018-08-27 15 | */ 16 | @RestController 17 | public class RolesController { 18 | 19 | private final RolesService rolesService; 20 | 21 | /** 22 | * Instantiates a new Roles controller 23 | * 24 | * @param rolesService the roles service 25 | */ 26 | @Autowired 27 | public RolesController(RolesService rolesService) { 28 | this.rolesService = rolesService; 29 | } 30 | 31 | /** 32 | * Roles 의 목록을 조회한다. 33 | * 34 | * @param id the id 35 | * @return the Roles List 36 | */ 37 | @GetMapping(value = Constants.URI_API_ROLES_ID_LIST) 38 | public RolesList getRoleList(@PathVariable("id") String id){ 39 | return rolesService.getRoleList(id); 40 | } 41 | 42 | 43 | /** 44 | * Roles 의 상세 정보를 조회한다. 45 | * 46 | * @param id the id 47 | * @param resourceCode the resourceCode 48 | * @param verbCode the verbCode 49 | * @return the Roles 50 | */ 51 | @GetMapping(value = Constants.URI_API_ROLES_ID_DETAIL) 52 | public Roles getRole(@PathVariable("id") String id, @RequestParam("resourceCode") String resourceCode, @RequestParam("verbCode") String verbCode){ 53 | return rolesService.getRole(id, resourceCode, verbCode); 54 | } 55 | 56 | /** 57 | * Role 을 생성한다. 58 | * 59 | * @param roles the roles 60 | * @return the Roles 61 | */ 62 | @PostMapping(value = Constants.URI_API_ROLES) 63 | public Roles createRole(@RequestBody Roles roles){ 64 | return rolesService.createRole(roles); 65 | } 66 | 67 | /** 68 | * Role 을 삭제한다. 69 | * 70 | * @param roles the roles 71 | * @return the Roles 72 | */ 73 | @DeleteMapping(value = Constants.URI_API_ROLES) 74 | public Roles deleteRole(@RequestBody Roles roles){ 75 | return rolesService.deleteRole(roles); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/roles/RolesList.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.roles; 2 | 3 | import lombok.Data; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.ElementCollection; 7 | import java.util.List; 8 | 9 | /** 10 | * Role List Model 클래스 11 | * 12 | * @author hrjin 13 | * @version 1.0 14 | * @since 2018-10-01 15 | */ 16 | @Data 17 | public class RolesList { 18 | private String resultCode; 19 | 20 | @Column(name = "items") 21 | @ElementCollection(targetClass = String.class) 22 | private List items; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/roles/RolesRepository.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.roles; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import javax.validation.constraints.NotEmpty; 7 | import javax.validation.constraints.NotNull; 8 | import java.util.List; 9 | 10 | /** 11 | * Roles JPA Repository 클래스 12 | * 13 | * @author hrjin 14 | * @version 1.0 15 | * @since 2018-08-27 16 | */ 17 | @Repository 18 | public interface RolesRepository extends JpaRepository { 19 | 20 | /** 21 | * Role 의 상세 정보를 조회한다. 22 | * 23 | * @param id the id 24 | * @param resourceCode the resource code 25 | * @param verbCode the verb code 26 | * @return Roles 27 | */ 28 | Roles findByRoleSetCodeAndResourceCodeAndVerbCode(String id, String resourceCode, String verbCode); 29 | 30 | /** 31 | * Role 의 목록을 조회한다. 32 | * 33 | * @param id the id 34 | * @return the Roles List 35 | */ 36 | List findByRoleSetCode(String id); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/roles/RolesService.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.roles; 2 | 3 | import org.paasta.caas.common.api.common.CommonService; 4 | import org.paasta.caas.common.api.common.Constants; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * Roles Service 클래스 12 | * 13 | * @author hrjin 14 | * @version 1.0 15 | * @since 2018-08-27 16 | */ 17 | @Service 18 | public class RolesService { 19 | 20 | private final CommonService commonService; 21 | private final RolesRepository rolesRepository; 22 | 23 | /** 24 | * Instantiates a new Roles service 25 | * 26 | * @param commonService the common service 27 | * @param rolesRepository the role repository 28 | */ 29 | @Autowired 30 | public RolesService(CommonService commonService, RolesRepository rolesRepository) { 31 | this.commonService = commonService; 32 | this.rolesRepository = rolesRepository; 33 | } 34 | 35 | /** 36 | * Role 목록을 조회한다. 37 | * 38 | * @param id the id 39 | * @return the Roles List 40 | */ 41 | RolesList getRoleList(String id) { 42 | List roles = rolesRepository.findByRoleSetCode(id); 43 | 44 | RolesList rolesList = new RolesList(); 45 | rolesList.setItems(roles); 46 | rolesList.setResultCode(Constants.RESULT_STATUS_SUCCESS); 47 | 48 | return rolesList; 49 | } 50 | 51 | 52 | /** 53 | * Role 상세 정보를 조회한다. 54 | * 55 | * @param id the id 56 | * @param resourceCode the resourceCode 57 | * @param verbCode the verbCode 58 | * @return the Roles 59 | */ 60 | Roles getRole(String id, String resourceCode, String verbCode) { 61 | return rolesRepository.findByRoleSetCodeAndResourceCodeAndVerbCode(id, resourceCode, verbCode); 62 | } 63 | 64 | 65 | /** 66 | * Role 을 생성한다. 67 | * 68 | * @param role the role 69 | * @return the Roles 70 | */ 71 | Roles createRole(Roles role) { 72 | return rolesRepository.save(role); 73 | } 74 | 75 | 76 | /** 77 | * Role 을 삭제한다. 78 | * 79 | * @param role the role 80 | * @return the Roles 81 | */ 82 | Roles deleteRole(Roles role) { 83 | rolesRepository.delete(role); 84 | return (Roles) commonService.setResultModel(Roles.class, Constants.RESULT_STATUS_SUCCESS); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/users/Users.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.users; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import lombok.Data; 5 | import org.paasta.caas.common.api.common.Constants; 6 | 7 | import javax.persistence.*; 8 | import javax.validation.constraints.NotEmpty; 9 | import javax.validation.constraints.NotNull; 10 | import java.time.LocalDateTime; 11 | import java.time.ZoneId; 12 | import java.time.format.DateTimeFormatter; 13 | 14 | /** 15 | * User Model 클래스 16 | * 17 | * @author REX 18 | * @version 1.0 19 | * @since 2018.08.02 20 | */ 21 | @Entity 22 | @Table(name = "user") 23 | @Data 24 | @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 25 | public class Users { 26 | 27 | @Id 28 | @GeneratedValue(strategy = GenerationType.IDENTITY) 29 | @Column(name = "id") 30 | private long id; 31 | 32 | @Column(name = "user_id", nullable = false) 33 | @NotNull(message = "USER ID cannot be null") 34 | @NotEmpty(message = "USER ID is mandatory") 35 | private String userId; 36 | 37 | @Column(name = "service_instance_id", nullable = false) 38 | @NotNull(message = "SERVICE INSTANCE ID cannot be null") 39 | @NotEmpty(message = "SERVICE INSTANCE ID is mandatory") 40 | private String serviceInstanceId; 41 | 42 | @Column(name = "caas_namespace", nullable = false) 43 | @NotNull(message = "CAAS NAMESPACE cannot be null") 44 | @NotEmpty(message = "CAAS NAMESPACE is mandatory") 45 | private String caasNamespace; 46 | 47 | @Column(name = "caas_account_token_name") 48 | @NotNull(message = "CAAS ACCOUNT TOKEN NAME cannot be null") 49 | @NotEmpty(message = "CAAS ACCOUNT TOKEN NAME is mandatory") 50 | private String caasAccountTokenName; 51 | 52 | // kubernetes 에서 service account 생성 name 53 | @Column(name = "caas_account_name") 54 | @NotNull(message = "CAAS ACCOUNT NAME cannot be null") 55 | @NotEmpty(message = " CAAS ACCOUNT NAME is mandatory") 56 | private String caasAccountName; 57 | 58 | @Column(name = "organization_guid") 59 | @NotNull(message = "ORGANIZATION GUID cannot be null") 60 | @NotEmpty(message = "ORGANIZATION GUID is mandatory") 61 | private String organizationGuid; 62 | 63 | @Column(name = "space_guid") 64 | @NotNull(message = "SPACE GUID cannot be null") 65 | @NotEmpty(message = "SPACE GUID is mandatory") 66 | private String spaceGuid; 67 | 68 | @Column(name = "role_set_code", nullable = false) 69 | @NotNull(message = "ROLE CODE cannot be null") 70 | @NotEmpty(message = "ROLE CODE is mandatory") 71 | private String roleSetCode; 72 | 73 | @Column(name = "description") 74 | private String description; 75 | 76 | @Column(name = "plan_name") 77 | private String planName; 78 | 79 | @Column(name = "plan_description") 80 | private String planDescription; 81 | 82 | @Column(name = "created", nullable = false, updatable = false) 83 | private String created; 84 | 85 | @Column(name = "last_modified", nullable = false) 86 | private String lastModified; 87 | 88 | @Transient 89 | private String resultCode; 90 | 91 | @PrePersist 92 | void preInsert() { 93 | if (this.created == null) { 94 | this.created = LocalDateTime.now(ZoneId.of(Constants.STRING_TIME_ZONE_ID)).format(DateTimeFormatter.ofPattern(Constants.STRING_DATE_TYPE)); 95 | } 96 | 97 | if (this.lastModified == null) { 98 | this.lastModified = LocalDateTime.now(ZoneId.of(Constants.STRING_TIME_ZONE_ID)).format(DateTimeFormatter.ofPattern(Constants.STRING_DATE_TYPE)); 99 | } 100 | } 101 | 102 | @PreUpdate 103 | void preUpdate() { 104 | if (this.lastModified != null) { 105 | this.lastModified = LocalDateTime.now(ZoneId.of(Constants.STRING_TIME_ZONE_ID)).format(DateTimeFormatter.ofPattern(Constants.STRING_DATE_TYPE)); 106 | } 107 | } 108 | 109 | 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/users/UsersController.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.users; 2 | 3 | import org.paasta.caas.common.api.common.Constants; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.*; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * User Controller 클래스 11 | * 12 | * @author REX 13 | * @version 1.0 14 | * @since 2018.08.02 15 | */ 16 | @RestController 17 | public class UsersController { 18 | 19 | private final UsersService userService; 20 | 21 | /** 22 | * Instantiates a new User controller. 23 | * 24 | * @param userService the user service 25 | */ 26 | @Autowired 27 | public UsersController(UsersService userService) {this.userService = userService;} 28 | 29 | /** 30 | * User 목록을 조회한다. 31 | * 32 | * @return the user list 33 | */ 34 | @GetMapping(value = Constants.URI_API_USERS) 35 | List getUserList() { 36 | return userService.getUserList(); 37 | } 38 | 39 | /** 40 | * User 상세 정보를 조회한다. 41 | * 42 | * @param id the id 43 | * @return the user 44 | */ 45 | @GetMapping(value = Constants.URI_API_USERS_DETAIL) 46 | Users getUser(@PathVariable("id") int id) { 47 | return userService.getUser(id); 48 | } 49 | 50 | /** 51 | * Service Instance Id 와 Organization guid 로 User 목록을 조회한다. 52 | * 53 | * @param serviceInstanceId the serviceInstanceId 54 | * @param organizationGuid the organizationGuid 55 | * @return the user 56 | */ 57 | @GetMapping(value = Constants.URI_API_USERS_BY_SUID_AND_ORG_GUID) 58 | UsersList getUsersByServiceInstanceIdAndOrganizationGuid(@PathVariable("serviceInstanceId") String serviceInstanceId, @PathVariable("organizationGuid") String organizationGuid) { 59 | return userService.getUsersByServiceInstanceIdAndOrganizationGuid(serviceInstanceId, organizationGuid); 60 | } 61 | 62 | 63 | /** 64 | * Service Instance Id 와 Organization guid, userId 로 User 상세 정보를 조회한다. 65 | * 66 | * @param serviceInstanceId the serviceInstanceId 67 | * @param organizationGuid the organizationGuid 68 | * @param userId the userId 69 | * @return the user 70 | */ 71 | @GetMapping(value = Constants.URI_API_USERS_BY_SUID_AND_ORG_GUID_AND_USER_ID) 72 | Users getUserByServiceInstanceIdAndOrganizationGuid(@PathVariable("serviceInstanceId") String serviceInstanceId, 73 | @PathVariable("organizationGuid") String organizationGuid, 74 | @PathVariable("userId") String userId){ 75 | return userService.getUserByServiceInstanceIdAndOrganizationGuid(serviceInstanceId, organizationGuid, userId); 76 | } 77 | 78 | /** 79 | * Service Instance Id 와 Organization guid, userId 로 User 권한을 업데이트한다. 80 | * 81 | * @param serviceInstanceId the serviceInstanceId 82 | * @param organizationGuid the organizationGuid 83 | * @param user the user 84 | * @return the user 85 | */ 86 | @PostMapping(value = Constants.URI_API_USERS_BY_SUID_AND_ORG_GUID_AND_USER_ID) 87 | Users updateUserRoleByServiceInstanceIdAndOrganizationGuid(@PathVariable("serviceInstanceId") String serviceInstanceId, 88 | @PathVariable("organizationGuid") String organizationGuid, 89 | @RequestBody Users user){ 90 | return userService.updateUserRoleByServiceInstanceIdAndOrganizationGuid(user); 91 | } 92 | 93 | /** 94 | * User 를 생성한다. 95 | * 96 | * @param user the user 97 | * @return the user 98 | */ 99 | @PostMapping(value = Constants.URI_API_USERS) 100 | Users createUser(@RequestBody Users user) { 101 | return userService.createUser(user); 102 | } 103 | 104 | /** 105 | * User 를 수정한다. 106 | * 107 | * @param user the user 108 | * @return the user 109 | */ 110 | @PutMapping(value = Constants.URI_API_USERS) 111 | Users updateUser(@RequestBody Users user) { 112 | return userService.updateUser(user); 113 | } 114 | 115 | 116 | /** 117 | * service instance id 를 통해 User 를 삭제한다. 118 | * 119 | * @param serviceInstanceId the serviceInstanceId 120 | */ 121 | @DeleteMapping(value = Constants.URI_API_USERS_BY_SERVICE_INSTANCE_ID) 122 | void deleteByServiceInstanceId(@PathVariable("serviceInstanceId") String serviceInstanceId) { 123 | userService.deleteByServiceInstanceId(serviceInstanceId); 124 | } 125 | 126 | 127 | /** 128 | * User 를 삭제한다. 129 | * 130 | * @param serviceInstanceId the serviceInstanceId 131 | * @param organizationGuid the organizationGuid 132 | * @param userId the userId 133 | * @return the user 134 | */ 135 | @DeleteMapping(value = Constants.URI_API_USERS_BY_SUID_AND_ORG_GUID_AND_USER_ID) 136 | Users deleteUserByServiceInstanceIdAndOrganizationGuid(@PathVariable("serviceInstanceId") String serviceInstanceId, 137 | @PathVariable("organizationGuid") String organizationGuid, 138 | @PathVariable("userId") String userId){ 139 | return userService.deleteByServiceInstanceIdAndOrganizationGuid(serviceInstanceId, organizationGuid, userId); 140 | } 141 | 142 | /** 143 | * service instance id 를 통해 User Update한다. 144 | * 145 | * @param serviceInstanceId the serviceInstanceId 146 | */ 147 | @PutMapping(value = Constants.URI_API_USERS_BY_SERVICE_INSTANCE_ID) 148 | void updateByServiceInstanceId(@PathVariable("serviceInstanceId") String serviceInstanceId, @RequestBody Users user) { 149 | userService.updateByServiceInstanceId(serviceInstanceId, user); 150 | } 151 | 152 | /** 153 | * Caas namespace 를 통해 유저 정보를 가져온다. 154 | * 155 | * @param namespace the namespace 156 | */ 157 | @GetMapping(value = Constants.URI_API_USERS_VALID_EXIST_NAMESPACE) 158 | boolean isUserCountByCaasNamespace(@PathVariable("userId") String userId, @PathVariable("namespace") String namespace) { 159 | return userService.isUserByCaasNamespace(userId, namespace); 160 | } 161 | 162 | } 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/users/UsersList.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.users; 2 | 3 | import lombok.Data; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.ElementCollection; 7 | import java.util.List; 8 | 9 | /** 10 | * User List Model 클래스 11 | * 12 | * @author hrjin 13 | * @version 1.0 14 | * @since 2018-10-01 15 | */ 16 | @Data 17 | public class UsersList { 18 | private String resultCode; 19 | 20 | @Column(name = "items") 21 | @ElementCollection(targetClass = String.class) 22 | private List items; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/users/UsersRepository.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.users; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.Modifying; 5 | import org.springframework.data.jpa.repository.Query; 6 | import org.springframework.stereotype.Repository; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * User Repository 인터페이스 13 | * 14 | * @author REX 15 | * @version 1.0 16 | * @since 2018.08.02 17 | */ 18 | @Repository 19 | @Transactional 20 | public interface UsersRepository extends JpaRepository { 21 | 22 | /** 23 | * User 의 목록을 조회한다. 24 | * 25 | * @param serviceInstanceId the service instance id 26 | * @param organizationGuid the organization id 27 | * @return the User List 28 | */ 29 | List findByServiceInstanceIdAndOrganizationGuid(String serviceInstanceId, String organizationGuid); 30 | 31 | /** 32 | * User 의 상세 정보를 조회한다. 33 | * 34 | * @param serviceInstanceId the service instance id 35 | * @param organizationGuid the organization id 36 | * @param userId the user id 37 | * @return the user 38 | */ 39 | Users findByServiceInstanceIdAndOrganizationGuidAndUserId(String serviceInstanceId, String organizationGuid, String userId); 40 | 41 | /** 42 | * User 를 삭제한다. 43 | * 44 | * @param serviceInstanceId the service instance id 45 | * @return Integer 46 | */ 47 | @Modifying 48 | @Query(value = "delete from user where service_instance_id = ?1", nativeQuery = true) 49 | Integer deleteByServiceInstanceId(String serviceInstanceId); 50 | 51 | /** 52 | * 해당 Namespace의 User 의 상세 정보를 조회한다.(By namespace) 53 | * 54 | * @param namespace the namespace 55 | * @return the user 56 | */ 57 | Users findByUserIdAndCaasNamespace(String userId, String namespace); 58 | 59 | /** 60 | * User 의 상세 정보를 service Instance ID로 조회한다. 61 | * 62 | * @param serviceInstanceId serviceInstance ID 63 | * @return List 64 | */ 65 | List findByServiceInstanceId(String serviceInstanceId); 66 | 67 | 68 | /** 69 | * serviceInstanceId 와 organizationGuid 와 userId 로 User 를 삭제한다. 70 | * 71 | * @param serviceInstanceId the serviceInstanceId 72 | * @param organizationGuid the organizationGuid 73 | * @param userId the userId 74 | * @return int 75 | */ 76 | Integer deleteByServiceInstanceIdAndOrganizationGuidAndUserId(String serviceInstanceId, String organizationGuid, String userId); 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/paasta/caas/common/api/users/UsersService.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.users; 2 | 3 | import org.paasta.caas.common.api.common.CommonService; 4 | import org.paasta.caas.common.api.common.Constants; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * User Service 클래스 14 | * 15 | * @author REX 16 | * @version 1.0 17 | * @since 2018.08.02 18 | */ 19 | @Service 20 | public class UsersService { 21 | private static final Logger LOGGER = LoggerFactory.getLogger(UsersService.class); 22 | private final CommonService commonService; 23 | private final UsersRepository userRepository; 24 | 25 | /** 26 | * Instantiates a new User service. 27 | * 28 | * @param commonService the common service 29 | * @param userRepository the user repository 30 | */ 31 | @Autowired 32 | public UsersService(CommonService commonService, UsersRepository userRepository) { 33 | this.commonService = commonService; 34 | this.userRepository = userRepository;} 35 | 36 | /** 37 | * user 목록을 조회한다. 38 | * 39 | * @return the user list 40 | */ 41 | List getUserList() { 42 | return userRepository.findAll(); 43 | } 44 | 45 | /** 46 | * Gets user. 47 | * 48 | * @param id the id 49 | * @return the user 50 | */ 51 | Users getUser(int id) { 52 | return userRepository.getOne((long) id); 53 | } 54 | 55 | /** 56 | * ServiceInstanceId, OrganizationGuid 로 user 목록을 조회한다. 57 | * 58 | * @param serviceInstanceId the serviceInstanceId 59 | * @param organizationGuid the organizationGuid 60 | * @return the user 61 | */ 62 | UsersList getUsersByServiceInstanceIdAndOrganizationGuid(String serviceInstanceId, String organizationGuid) { 63 | List users = userRepository.findByServiceInstanceIdAndOrganizationGuid(serviceInstanceId, organizationGuid); 64 | 65 | UsersList usersList = new UsersList(); 66 | usersList.setItems(users); 67 | usersList.setResultCode(Constants.RESULT_STATUS_SUCCESS); 68 | 69 | return usersList; 70 | } 71 | 72 | 73 | /** 74 | * ServiceInstanceId, OrganizationGuid, userId 로 user 를 조회한다. 75 | * 76 | * @param serviceInstanceId the serviceInstanceId 77 | * @param organizationGuid the organizationGuid 78 | * @param userId the userId 79 | * @return the user 80 | */ 81 | Users getUserByServiceInstanceIdAndOrganizationGuid(String serviceInstanceId, String organizationGuid, String userId) { 82 | return (Users) commonService.setResultModel(userRepository.findByServiceInstanceIdAndOrganizationGuidAndUserId(serviceInstanceId, organizationGuid, userId), Constants.RESULT_STATUS_SUCCESS); 83 | } 84 | 85 | 86 | /** 87 | * ServiceInstanceId 와 OrganizationGuid 으로 user 의 권한을 수정한다. 88 | * 89 | * @param user the user 90 | * @return the user 91 | */ 92 | Users updateUserRoleByServiceInstanceIdAndOrganizationGuid(Users user) { 93 | return (Users) commonService.setResultModel(userRepository.save(user), Constants.RESULT_STATUS_SUCCESS); 94 | } 95 | 96 | /** 97 | * user 를 생성한다. 98 | * 99 | * @param user the user 100 | * @return the user 101 | */ 102 | Users createUser(Users user) { 103 | String result = commonService.procValidator(user); 104 | 105 | if (result.equals(Constants.RESULT_STATUS_SUCCESS)) { 106 | return userRepository.save(user); 107 | } else { 108 | return (Users) commonService.setResultModel(Users.class, Constants.RESULT_STATUS_FAIL); 109 | } 110 | } 111 | 112 | /** 113 | * user 를 수정한다. 114 | * 115 | * @param user the user 116 | * @return the user 117 | */ 118 | Users updateUser(Users user) { 119 | String result = commonService.procValidator(user); 120 | 121 | if (result.equals(Constants.RESULT_STATUS_SUCCESS)) { 122 | return userRepository.save(user); 123 | } else { 124 | return (Users) commonService.setResultModel(Users.class, Constants.RESULT_STATUS_FAIL); 125 | } 126 | } 127 | 128 | 129 | /** 130 | * ServiceInstanceId 로 user 를 삭제한다. 131 | * 132 | * @param serviceInstanceId the serviceInstanceId 133 | */ 134 | void deleteByServiceInstanceId(String serviceInstanceId) { 135 | userRepository.deleteByServiceInstanceId(serviceInstanceId); 136 | } 137 | 138 | /** 139 | * ServiceInstanceId 로 user 를 수정한다. 140 | * 141 | * @param serviceInstanceId the serviceInstanceId 142 | */ 143 | void updateByServiceInstanceId(String serviceInstanceId, Users userInfo) { 144 | List userList = userRepository.findByServiceInstanceId(serviceInstanceId); 145 | for(Users user : userList) { 146 | user.setPlanName(userInfo.getPlanName()); 147 | user.setPlanDescription(userInfo.getPlanDescription()); 148 | userRepository.save(user); 149 | } 150 | } 151 | 152 | /** 153 | * namespace 로 user name 을 조회한다. 154 | * 155 | * @param namespace the namespace 156 | */ 157 | boolean isUserByCaasNamespace(String userId, String namespace) { 158 | boolean isExistUser = true; 159 | 160 | Users users = userRepository.findByUserIdAndCaasNamespace(userId, namespace); 161 | 162 | LOGGER.info("users is "+users); 163 | 164 | if(users == null ){ 165 | LOGGER.info("findByCaasNamespace is NULL"); 166 | isExistUser = false; 167 | } 168 | return isExistUser; 169 | } 170 | 171 | 172 | /** 173 | * ServiceInstanceId, OrganizationGuid, userId 로 User 를 삭제한다. 174 | * 175 | * @param serviceInstanceId the serviceInstanceId 176 | * @param organizationGuid the organizationGuid 177 | * @param userId the userId 178 | * @return the user 179 | */ 180 | Users deleteByServiceInstanceIdAndOrganizationGuid(String serviceInstanceId, String organizationGuid, String userId) { 181 | int resultValue = userRepository.deleteByServiceInstanceIdAndOrganizationGuidAndUserId(serviceInstanceId, organizationGuid, userId); 182 | Users users = new Users(); 183 | String resultCode; 184 | if(resultValue == 1){ 185 | resultCode = Constants.RESULT_STATUS_SUCCESS; 186 | }else { 187 | resultCode = Constants.RESULT_STATUS_FAIL; 188 | } 189 | 190 | return (Users) commonService.setResultModel(users, resultCode); 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # Spring properties 2 | spring: 3 | application: 4 | name: PortalCommonApi # Service registers under this name 5 | security: 6 | username: 7 | password: 8 | 9 | # HTTP Server 10 | server: 11 | port: ${PORT:3334} # HTTP (Tomcat) port 12 | 13 | management: 14 | security: 15 | enabled: false 16 | 17 | 18 | --- 19 | spring: 20 | profiles: local 21 | datasource: 22 | caas: 23 | driver-class-name: com.mysql.jdbc.Driver 24 | url: jdbc:mysql:// 25 | username: 26 | password: 27 | jpa: 28 | database: postgresql 29 | show-sql: true 30 | hibernate: 31 | ddl-auto: none 32 | naming: 33 | strategy: org.hibernate.dialect.MySQL5Dialect 34 | generate-ddl: false 35 | 36 | --- 37 | spring: 38 | profiles: dev 39 | 40 | datasource: 41 | caas: 42 | driver-class-name: com.mysql.jdbc.Driver 43 | url: jdbc:mysql:// 44 | username: 45 | password: 46 | jpa: 47 | database: postgresql 48 | show-sql: true 49 | hibernate: 50 | ddl-auto: none 51 | naming: 52 | strategy: org.hibernate.dialect.MySQL5Dialect 53 | generate-ddl: false 54 | -------------------------------------------------------------------------------- /src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | true 7 | 8 | 9 | application.%d{yyyy-MM-dd}.log 10 | 7 11 | 12 | 13 | INFO 14 | 15 | 16 | 17 | %d{yyyy:MM:dd HH:mm:ss.SSS} %-5level --- [%thread] %logger{35} : %msg %n 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/test/java/org/paasta/caas/common/api/PaasTaCaasCommonApiApplicationTests.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class PaasTaCaasCommonApiApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/org/paasta/caas/common/api/adminToken/AdminTokenServiceTest.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.adminToken; 2 | 3 | import org.assertj.core.api.Assertions; 4 | import org.junit.After; 5 | import org.junit.Before; 6 | import org.junit.FixMethodOrder; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.MethodSorters; 10 | import org.mockito.InjectMocks; 11 | import org.mockito.Mock; 12 | import org.paasta.caas.common.api.common.CommonService; 13 | import org.paasta.caas.common.api.common.Constants; 14 | import org.springframework.boot.test.context.SpringBootTest; 15 | import org.springframework.test.context.TestPropertySource; 16 | import org.springframework.test.context.junit4.SpringRunner; 17 | 18 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 19 | import static org.junit.Assert.assertEquals; 20 | import static org.mockito.Mockito.when; 21 | 22 | /** 23 | * The type Admin token service test. 24 | */ 25 | @RunWith(SpringRunner.class) 26 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 27 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) 28 | @TestPropertySource("classpath:application.yml") 29 | public class AdminTokenServiceTest { 30 | 31 | private final String TOKEN_NAME = "test-token-name"; 32 | private final String TOKEN_VALUE = "test-token-value"; 33 | private final String RESULT_CODE_SUCCESS = Constants.RESULT_STATUS_SUCCESS; 34 | 35 | private AdminToken gTestModel = null; 36 | private AdminToken gTestErrorModel = null; 37 | private AdminToken gTestResultModel = null; 38 | private AdminToken gTestResultErrorModel = null; 39 | 40 | @Mock 41 | private AdminTokenRepository adminTokenRepository; 42 | 43 | @Mock 44 | private CommonService commonService; 45 | 46 | @InjectMocks 47 | private AdminTokenService adminTokenService; 48 | 49 | 50 | /** 51 | * Sets up. 52 | */ 53 | @Before 54 | public void setUp() { 55 | 56 | gTestModel = new AdminToken(); 57 | gTestErrorModel = new AdminToken(); 58 | gTestResultModel = new AdminToken(); 59 | gTestResultErrorModel = new AdminToken(); 60 | 61 | gTestModel.setTokenName(TOKEN_NAME); 62 | gTestModel.setTokenValue(TOKEN_VALUE); 63 | 64 | gTestResultModel.setResultCode(RESULT_CODE_SUCCESS); 65 | gTestResultModel.setTokenName(TOKEN_NAME); 66 | gTestResultModel.setTokenValue(TOKEN_VALUE); 67 | 68 | gTestResultErrorModel.setResultCode(Constants.RESULT_STATUS_FAIL); 69 | } 70 | 71 | /** 72 | * Tear down. 73 | */ 74 | @After 75 | public void tearDown() { 76 | } 77 | 78 | 79 | //////////////////////////////////////////////////////////////////////////////////////////////////// 80 | //////////////////// MethodName_StateUnderTest_ExpectedBehavior 81 | //////////////////////////////////////////////////////////////////////////////////////////////////// 82 | 83 | 84 | /** 85 | * Gets token value valid return model. 86 | */ 87 | @Test 88 | public void getTokenValue_Valid_ReturnModel() { 89 | // CONDITION 90 | when(adminTokenRepository.findByTokenName(TOKEN_NAME)).thenReturn(gTestModel); 91 | when(commonService.setResultModel(gTestModel, Constants.RESULT_STATUS_SUCCESS)).thenReturn(gTestResultModel); 92 | 93 | // TEST 94 | AdminToken resultModel = adminTokenService.getTokenValue(TOKEN_NAME); 95 | 96 | // VERIFY 97 | assertThat(resultModel).isNotNull(); 98 | assertEquals(gTestResultModel, resultModel); 99 | assertEquals(TOKEN_NAME, resultModel.getTokenName()); 100 | assertEquals(TOKEN_VALUE, resultModel.getTokenValue()); 101 | assertEquals(RESULT_CODE_SUCCESS, resultModel.getResultCode()); 102 | } 103 | 104 | @Test 105 | public void getTokenValue_Invalid_ReturnModel(){ 106 | // CONDITION 107 | when(adminTokenRepository.findByTokenName(TOKEN_NAME)).thenReturn(null); 108 | when(commonService.setResultModel(gTestErrorModel, Constants.RESULT_STATUS_FAIL)).thenReturn(gTestResultErrorModel); 109 | 110 | // TEST 111 | AdminToken resultModel = adminTokenService.getTokenValue(TOKEN_NAME); 112 | 113 | // VERIFY 114 | assertThat(resultModel).isNotNull(); 115 | } 116 | 117 | /** 118 | * Create admin token valid return model. 119 | */ 120 | @Test 121 | public void createAdminToken_Valid_ReturnModel() { 122 | // CONDITION 123 | when(commonService.procValidator(gTestModel)).thenReturn(Constants.RESULT_STATUS_SUCCESS); 124 | when(adminTokenRepository.save(gTestModel)).thenReturn(gTestResultModel); 125 | 126 | // TEST 127 | AdminToken resultModel = adminTokenService.createAdminToken(gTestModel); 128 | 129 | // VERIFY 130 | assertThat(resultModel).isNotNull(); 131 | assertEquals(gTestResultModel, resultModel); 132 | assertEquals(TOKEN_NAME, resultModel.getTokenName()); 133 | assertEquals(TOKEN_VALUE, resultModel.getTokenValue()); 134 | assertEquals(RESULT_CODE_SUCCESS, resultModel.getResultCode()); 135 | } 136 | 137 | 138 | /** 139 | * Create admin token invalid model return error model. 140 | */ 141 | @Test 142 | public void createAdminToken_InvalidModel_ReturnErrorModel() { 143 | // CONDITION 144 | when(commonService.procValidator(gTestModel)).thenReturn(Constants.RESULT_STATUS_FAIL); 145 | when(commonService.setResultModel(AdminToken.class, Constants.RESULT_STATUS_FAIL)).thenReturn(gTestResultErrorModel); 146 | 147 | // TEST 148 | AdminToken resultModel = adminTokenService.createAdminToken(gTestModel); 149 | 150 | // VERIFY 151 | Assertions.assertThat(resultModel).isNotNull(); 152 | assertEquals(Constants.RESULT_STATUS_FAIL, resultModel.getResultCode()); 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /src/test/java/org/paasta/caas/common/api/roles/RolesServiceTest.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.roles; 2 | 3 | import org.assertj.core.api.Assertions; 4 | import org.junit.After; 5 | import org.junit.Before; 6 | import org.junit.FixMethodOrder; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.MethodSorters; 10 | import org.mockito.InjectMocks; 11 | import org.mockito.Mock; 12 | import org.paasta.caas.common.api.common.CommonService; 13 | import org.paasta.caas.common.api.common.Constants; 14 | import org.springframework.boot.test.context.SpringBootTest; 15 | import org.springframework.test.context.TestPropertySource; 16 | import org.springframework.test.context.junit4.SpringRunner; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import static org.assertj.core.api.Assertions.assertThat; 22 | import static org.junit.Assert.assertEquals; 23 | import static org.mockito.Mockito.doNothing; 24 | import static org.mockito.Mockito.when; 25 | 26 | /** 27 | * The type Roles service test. 28 | */ 29 | @RunWith(SpringRunner.class) 30 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 31 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) 32 | @TestPropertySource("classpath:application.yml") 33 | public class RolesServiceTest { 34 | 35 | private final String ROLE_SET_CODE = "test-role-set-code"; 36 | private final String RESOURCE_CODE = "test-resource-code"; 37 | private final String VERB_CODE = "test-verb-code"; 38 | private final String DESCRIPTION = "test-description"; 39 | private final String CREATED = "test-created"; 40 | 41 | private Roles gTestModel = null; 42 | private Roles gTestResultModel = null; 43 | private List gTestResultList = null; 44 | 45 | @Mock 46 | private RolesRepository rolesRepository; 47 | 48 | @Mock 49 | private CommonService commonService; 50 | 51 | @InjectMocks 52 | private RolesService rolesService; 53 | 54 | 55 | /** 56 | * Sets up. 57 | */ 58 | @Before 59 | public void setUp() { 60 | 61 | gTestResultList = new ArrayList<>(); 62 | gTestModel = new Roles(); 63 | gTestResultModel = new Roles(); 64 | 65 | gTestModel.setResourceCode(RESOURCE_CODE); 66 | gTestModel.setVerbCode(VERB_CODE); 67 | gTestModel.setDescription(DESCRIPTION); 68 | 69 | gTestResultModel.setRoleSetCode(ROLE_SET_CODE); 70 | gTestResultModel.setResourceCode(RESOURCE_CODE); 71 | gTestResultModel.setVerbCode(VERB_CODE); 72 | gTestResultModel.setDescription(DESCRIPTION); 73 | gTestResultModel.setCreated(CREATED); 74 | gTestResultModel.setResultCode(Constants.RESULT_STATUS_SUCCESS); 75 | 76 | gTestResultList.add(gTestResultModel); 77 | } 78 | 79 | /** 80 | * Tear down. 81 | */ 82 | @After 83 | public void tearDown() { 84 | } 85 | 86 | 87 | //////////////////////////////////////////////////////////////////////////////////////////////////// 88 | //////////////////// MethodName_StateUnderTest_ExpectedBehavior 89 | //////////////////////////////////////////////////////////////////////////////////////////////////// 90 | 91 | 92 | /** 93 | * Gets role list valid return model. 94 | */ 95 | @Test 96 | public void getRoleList_Valid_ReturnModel() { 97 | // CONDITION 98 | when(rolesRepository.findByRoleSetCode(ROLE_SET_CODE)).thenReturn(gTestResultList); 99 | 100 | // TEST 101 | RolesList resultList = rolesService.getRoleList(ROLE_SET_CODE); 102 | 103 | // VERIFY 104 | Assertions.assertThat(resultList).isNotNull(); 105 | assertEquals(gTestResultList, resultList.getItems()); 106 | assertEquals(ROLE_SET_CODE, resultList.getItems().get(0).getRoleSetCode()); 107 | assertEquals(RESOURCE_CODE, resultList.getItems().get(0).getResourceCode()); 108 | assertEquals(VERB_CODE, resultList.getItems().get(0).getVerbCode()); 109 | assertEquals(DESCRIPTION, resultList.getItems().get(0).getDescription()); 110 | assertEquals(CREATED, resultList.getItems().get(0).getCreated()); 111 | } 112 | 113 | 114 | /** 115 | * Gets role valid return model. 116 | */ 117 | @Test 118 | public void getRole_Valid_ReturnModel() { 119 | // CONDITION 120 | when(rolesRepository.findByRoleSetCodeAndResourceCodeAndVerbCode(ROLE_SET_CODE, RESOURCE_CODE, VERB_CODE)).thenReturn(gTestResultModel); 121 | 122 | // TEST 123 | Roles resultModel = rolesService.getRole(ROLE_SET_CODE, RESOURCE_CODE, VERB_CODE); 124 | 125 | // VERIFY 126 | assertThat(resultModel).isNotNull(); 127 | assertEquals(gTestResultModel, resultModel); 128 | assertEquals(ROLE_SET_CODE, resultModel.getRoleSetCode()); 129 | } 130 | 131 | 132 | /** 133 | * Create role valid return model. 134 | */ 135 | @Test 136 | public void createRole_Valid_ReturnModel() { 137 | // CONDITION 138 | when(rolesRepository.save(gTestModel)).thenReturn(gTestResultModel); 139 | 140 | // TEST 141 | Roles resultModel = rolesService.createRole(gTestModel); 142 | 143 | // VERIFY 144 | assertThat(resultModel).isNotNull(); 145 | assertEquals(gTestResultModel, resultModel); 146 | assertEquals(ROLE_SET_CODE, resultModel.getRoleSetCode()); 147 | } 148 | 149 | 150 | /** 151 | * Delete role valid return model. 152 | */ 153 | @Test 154 | public void deleteRole_Valid_ReturnModel() { 155 | // SET 156 | gTestModel.setRoleSetCode(ROLE_SET_CODE); 157 | 158 | // CONDITION 159 | doNothing().when(rolesRepository).delete(gTestModel); 160 | when(commonService.setResultModel(Roles.class, Constants.RESULT_STATUS_SUCCESS)).thenReturn(gTestResultModel); 161 | 162 | // TEST 163 | Roles resultModel = rolesService.deleteRole(gTestModel); 164 | 165 | // VERIFY 166 | assertThat(resultModel).isNotNull(); 167 | assertEquals(gTestResultModel, resultModel); 168 | assertEquals(ROLE_SET_CODE, resultModel.getRoleSetCode()); 169 | } 170 | 171 | } 172 | -------------------------------------------------------------------------------- /src/test/java/org/paasta/caas/common/api/users/UsersServiceTest.java: -------------------------------------------------------------------------------- 1 | package org.paasta.caas.common.api.users; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.junit.Assert.assertEquals; 5 | import static org.mockito.Mockito.doNothing; 6 | import static org.mockito.Mockito.when; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import org.junit.After; 12 | import org.junit.Before; 13 | import org.junit.FixMethodOrder; 14 | import org.junit.Test; 15 | import org.junit.runner.RunWith; 16 | import org.junit.runners.MethodSorters; 17 | import org.mockito.InjectMocks; 18 | import org.mockito.Mock; 19 | import org.mockito.junit.MockitoJUnitRunner; 20 | import org.paasta.caas.common.api.common.CommonService; 21 | import org.paasta.caas.common.api.common.Constants; 22 | import org.springframework.boot.test.context.SpringBootTest; 23 | import org.springframework.test.context.TestPropertySource; 24 | import org.springframework.test.context.junit4.SpringRunner; 25 | 26 | /** 27 | * The type Users service test. 28 | */ 29 | @RunWith(SpringRunner.class) 30 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 31 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) 32 | @TestPropertySource("classpath:application.yml") 33 | public class UsersServiceTest { 34 | 35 | private final int PID = 1; 36 | private final String USER_ID = "test-id"; 37 | private final String SERVICE_INSTANCE_ID = "test-service-instance-id"; 38 | private final String CAAS_ACCOUNT_ACCESS_TOKEN = "test-account-access-token"; 39 | private final String CAAS_ACCOUNT_NAME = "test-account-name"; 40 | private final String ORGANIZATION_GUID = "test-caas-org"; 41 | private final String SPACE_GUID = "test-caas-space"; 42 | 43 | private final String ROLE_SET_CODE = "test-role"; 44 | private final String NAMESPACE = "test-kube-namespace"; 45 | private final String DESCRIPTION = "test-description"; 46 | private final String CREATED = "test-created"; 47 | private final String LAST_MODIFIED = "test-last-modified"; 48 | private final String RESULT_CODE_SUCCESS = Constants.RESULT_STATUS_SUCCESS; 49 | private final String RESULT_CODE_FAIL = Constants.RESULT_STATUS_FAIL; 50 | 51 | private Users gTestModel = null; 52 | private Users gTestResultModel = null; 53 | private Users gTestFinalModel = null; 54 | private Users gTestResultErrorModel = null; 55 | private Users gTestEmptyModel = null; 56 | private List gTestResultList = null; 57 | 58 | @Mock 59 | private UsersRepository userRepository; 60 | 61 | @Mock 62 | private CommonService commonService; 63 | 64 | @InjectMocks 65 | private UsersService userService; 66 | 67 | 68 | /** 69 | * Sets up. 70 | */ 71 | @Before 72 | public void setUp() { 73 | 74 | gTestResultList = new ArrayList<>(); 75 | gTestModel = new Users(); 76 | gTestResultModel = new Users(); 77 | gTestFinalModel = new Users(); 78 | gTestResultErrorModel = new Users(); 79 | gTestEmptyModel = new Users(); 80 | 81 | gTestModel.setUserId(USER_ID); 82 | gTestModel.setServiceInstanceId(SERVICE_INSTANCE_ID); 83 | gTestModel.setCaasAccountTokenName(CAAS_ACCOUNT_ACCESS_TOKEN); 84 | gTestModel.setCaasAccountName(CAAS_ACCOUNT_NAME); 85 | gTestModel.setOrganizationGuid(ORGANIZATION_GUID); 86 | gTestModel.setSpaceGuid(SPACE_GUID); 87 | gTestModel.setRoleSetCode(ROLE_SET_CODE); 88 | gTestModel.setCaasNamespace(NAMESPACE); 89 | gTestModel.setDescription(DESCRIPTION); 90 | 91 | gTestResultModel.setResultCode(RESULT_CODE_SUCCESS); 92 | gTestResultModel.setId(PID); 93 | gTestResultModel.setUserId(USER_ID); 94 | gTestResultModel.setServiceInstanceId(SERVICE_INSTANCE_ID); 95 | gTestResultModel.setCaasAccountTokenName(CAAS_ACCOUNT_ACCESS_TOKEN); 96 | gTestResultModel.setCaasAccountName(CAAS_ACCOUNT_NAME); 97 | gTestResultModel.setOrganizationGuid(ORGANIZATION_GUID); 98 | gTestResultModel.setSpaceGuid(SPACE_GUID); 99 | gTestResultModel.setRoleSetCode(ROLE_SET_CODE); 100 | gTestResultModel.setCaasNamespace(NAMESPACE); 101 | gTestResultModel.setDescription(DESCRIPTION); 102 | gTestResultModel.setCreated(CREATED); 103 | gTestResultModel.setLastModified(LAST_MODIFIED); 104 | 105 | gTestResultList.add(gTestResultModel); 106 | 107 | gTestFinalModel.setRoleSetCode(RESULT_CODE_SUCCESS); 108 | gTestFinalModel.setId(PID); 109 | gTestFinalModel.setUserId(USER_ID); 110 | gTestFinalModel.setServiceInstanceId(SERVICE_INSTANCE_ID); 111 | gTestFinalModel.setCaasAccountTokenName(CAAS_ACCOUNT_ACCESS_TOKEN); 112 | gTestFinalModel.setCaasAccountName(CAAS_ACCOUNT_NAME); 113 | gTestFinalModel.setOrganizationGuid(ORGANIZATION_GUID); 114 | gTestFinalModel.setSpaceGuid(SPACE_GUID); 115 | gTestFinalModel.setRoleSetCode(ROLE_SET_CODE); 116 | gTestFinalModel.setCaasNamespace(NAMESPACE); 117 | gTestFinalModel.setDescription(DESCRIPTION); 118 | gTestFinalModel.setCreated(CREATED); 119 | gTestFinalModel.setLastModified(LAST_MODIFIED); 120 | gTestFinalModel.setResultCode(RESULT_CODE_SUCCESS); 121 | 122 | gTestResultErrorModel.setResultCode(Constants.RESULT_STATUS_FAIL); 123 | } 124 | 125 | /** 126 | * Tear down. 127 | */ 128 | @After 129 | public void tearDown() { 130 | } 131 | 132 | 133 | //////////////////////////////////////////////////////////////////////////////////////////////////// 134 | //////////////////// MethodName_StateUnderTest_ExpectedBehavior 135 | //////////////////////////////////////////////////////////////////////////////////////////////////// 136 | 137 | 138 | /** 139 | * Gets user list valid return list. 140 | */ 141 | @Test 142 | public void getUserList_Valid_ReturnList() { 143 | // CONDITION 144 | when(userRepository.findAll()).thenReturn(gTestResultList); 145 | 146 | // TEST 147 | List resultList = userService.getUserList(); 148 | 149 | // VERIFY 150 | assertThat(resultList).isNotNull(); 151 | assertEquals(gTestResultList, resultList); 152 | assertEquals(PID, resultList.get(0).getId()); 153 | assertEquals(USER_ID, resultList.get(0).getUserId()); 154 | assertEquals(SERVICE_INSTANCE_ID, resultList.get(0).getServiceInstanceId()); 155 | assertEquals(CAAS_ACCOUNT_ACCESS_TOKEN, resultList.get(0).getCaasAccountTokenName()); 156 | assertEquals(CAAS_ACCOUNT_NAME, resultList.get(0).getCaasAccountName()); 157 | assertEquals(ORGANIZATION_GUID, resultList.get(0).getOrganizationGuid()); 158 | assertEquals(SPACE_GUID, resultList.get(0).getSpaceGuid()); 159 | assertEquals(ROLE_SET_CODE, resultList.get(0).getRoleSetCode()); 160 | assertEquals(NAMESPACE, resultList.get(0).getCaasNamespace()); 161 | assertEquals(DESCRIPTION, resultList.get(0).getDescription()); 162 | assertEquals(CREATED, resultList.get(0).getCreated()); 163 | assertEquals(LAST_MODIFIED, resultList.get(0).getLastModified()); 164 | assertEquals(RESULT_CODE_SUCCESS, resultList.get(0).getResultCode()); 165 | } 166 | 167 | @Test 168 | public void getUsersByServiceInstanceIdAndOrganizationGuid_Valid_ReturnModel(){ 169 | // CONDITION 170 | when(userRepository.findByServiceInstanceIdAndOrganizationGuid(SERVICE_INSTANCE_ID, ORGANIZATION_GUID)).thenReturn(gTestResultList); 171 | 172 | // TEST 173 | UsersList resultList = userService.getUsersByServiceInstanceIdAndOrganizationGuid(SERVICE_INSTANCE_ID, ORGANIZATION_GUID); 174 | 175 | // VERIFY 176 | assertThat(resultList.getItems()).isNotNull(); 177 | assertEquals(gTestResultList, resultList.getItems()); 178 | assertEquals(PID, resultList.getItems().get(0).getId()); 179 | assertEquals(USER_ID, resultList.getItems().get(0).getUserId()); 180 | assertEquals(SERVICE_INSTANCE_ID, resultList.getItems().get(0).getServiceInstanceId()); 181 | assertEquals(CAAS_ACCOUNT_ACCESS_TOKEN, resultList.getItems().get(0).getCaasAccountTokenName()); 182 | assertEquals(CAAS_ACCOUNT_NAME, resultList.getItems().get(0).getCaasAccountName()); 183 | assertEquals(ORGANIZATION_GUID, resultList.getItems().get(0).getOrganizationGuid()); 184 | assertEquals(SPACE_GUID, resultList.getItems().get(0).getSpaceGuid()); 185 | assertEquals(ROLE_SET_CODE, resultList.getItems().get(0).getRoleSetCode()); 186 | assertEquals(NAMESPACE, resultList.getItems().get(0).getCaasNamespace()); 187 | assertEquals(DESCRIPTION, resultList.getItems().get(0).getDescription()); 188 | assertEquals(CREATED, resultList.getItems().get(0).getCreated()); 189 | assertEquals(LAST_MODIFIED, resultList.getItems().get(0).getLastModified()); 190 | assertEquals(RESULT_CODE_SUCCESS, resultList.getItems().get(0).getResultCode()); 191 | } 192 | 193 | /** 194 | * Gets user valid return model. 195 | */ 196 | @Test 197 | public void getUser_Valid_ReturnModel() { 198 | // CONDITION 199 | when(userRepository.getOne((long) PID)).thenReturn(gTestResultModel); 200 | 201 | // TEST 202 | Users resultModel = userService.getUser(PID); 203 | 204 | // VERIFY 205 | assertThat(resultModel).isNotNull(); 206 | assertEquals(gTestResultModel, resultModel); 207 | assertEquals(PID, resultModel.getId()); 208 | assertEquals(USER_ID, resultModel.getUserId()); 209 | assertEquals(SERVICE_INSTANCE_ID, resultModel.getServiceInstanceId()); 210 | assertEquals(CAAS_ACCOUNT_ACCESS_TOKEN, resultModel.getCaasAccountTokenName()); 211 | assertEquals(CAAS_ACCOUNT_NAME, resultModel.getCaasAccountName()); 212 | assertEquals(ORGANIZATION_GUID, resultModel.getOrganizationGuid()); 213 | assertEquals(SPACE_GUID, resultModel.getSpaceGuid()); 214 | assertEquals(ROLE_SET_CODE, resultModel.getRoleSetCode()); 215 | assertEquals(NAMESPACE, resultModel.getCaasNamespace()); 216 | assertEquals(DESCRIPTION, resultModel.getDescription()); 217 | assertEquals(CREATED, resultModel.getCreated()); 218 | assertEquals(LAST_MODIFIED, resultModel.getLastModified()); 219 | assertEquals(RESULT_CODE_SUCCESS, resultModel.getResultCode()); 220 | } 221 | 222 | @Test 223 | public void getUserByServiceInstanceIdAndOrganizationGuid_Valid_ReturnModel(){ 224 | // CONDITION 225 | when(userRepository.findByServiceInstanceIdAndOrganizationGuidAndUserId(SERVICE_INSTANCE_ID, ORGANIZATION_GUID, USER_ID)).thenReturn(gTestResultModel); 226 | when(commonService.setResultModel(gTestResultModel, Constants.RESULT_STATUS_SUCCESS)).thenReturn(gTestFinalModel); 227 | 228 | // TEST 229 | Users resultModel = userService.getUserByServiceInstanceIdAndOrganizationGuid(SERVICE_INSTANCE_ID, ORGANIZATION_GUID, USER_ID); 230 | 231 | // VERIFY 232 | assertThat(resultModel).isNotNull(); 233 | assertEquals(gTestResultModel, resultModel); 234 | assertEquals(PID, resultModel.getId()); 235 | assertEquals(USER_ID, resultModel.getUserId()); 236 | assertEquals(SERVICE_INSTANCE_ID, resultModel.getServiceInstanceId()); 237 | assertEquals(CAAS_ACCOUNT_ACCESS_TOKEN, resultModel.getCaasAccountTokenName()); 238 | assertEquals(CAAS_ACCOUNT_NAME, resultModel.getCaasAccountName()); 239 | assertEquals(ORGANIZATION_GUID, resultModel.getOrganizationGuid()); 240 | assertEquals(SPACE_GUID, resultModel.getSpaceGuid()); 241 | assertEquals(ROLE_SET_CODE, resultModel.getRoleSetCode()); 242 | assertEquals(NAMESPACE, resultModel.getCaasNamespace()); 243 | assertEquals(DESCRIPTION, resultModel.getDescription()); 244 | assertEquals(CREATED, resultModel.getCreated()); 245 | assertEquals(LAST_MODIFIED, resultModel.getLastModified()); 246 | assertEquals(RESULT_CODE_SUCCESS, resultModel.getResultCode()); 247 | } 248 | 249 | /** 250 | * Create user valid return model. 251 | */ 252 | @Test 253 | public void createUser_Valid_ReturnModel() { 254 | // CONDITION 255 | when(commonService.procValidator(gTestModel)).thenReturn(Constants.RESULT_STATUS_SUCCESS); 256 | when(userRepository.save(gTestModel)).thenReturn(gTestResultModel); 257 | 258 | // TEST 259 | Users resultModel = userService.createUser(gTestModel); 260 | 261 | // VERIFY 262 | assertThat(resultModel).isNotNull(); 263 | assertEquals(gTestResultModel, resultModel); 264 | assertEquals(PID, resultModel.getId()); 265 | assertEquals(USER_ID, resultModel.getUserId()); 266 | assertEquals(SERVICE_INSTANCE_ID, resultModel.getServiceInstanceId()); 267 | assertEquals(CAAS_ACCOUNT_ACCESS_TOKEN, resultModel.getCaasAccountTokenName()); 268 | assertEquals(CAAS_ACCOUNT_NAME, resultModel.getCaasAccountName()); 269 | assertEquals(ORGANIZATION_GUID, resultModel.getOrganizationGuid()); 270 | assertEquals(SPACE_GUID, resultModel.getSpaceGuid()); 271 | assertEquals(ROLE_SET_CODE, resultModel.getRoleSetCode()); 272 | assertEquals(NAMESPACE, resultModel.getCaasNamespace()); 273 | assertEquals(DESCRIPTION, resultModel.getDescription()); 274 | assertEquals(CREATED, resultModel.getCreated()); 275 | assertEquals(LAST_MODIFIED, resultModel.getLastModified()); 276 | assertEquals(RESULT_CODE_SUCCESS, resultModel.getResultCode()); 277 | } 278 | 279 | 280 | /** 281 | * Create user invalid model return error model. 282 | */ 283 | @Test 284 | public void createUser_InvalidModel_ReturnErrorModel() { 285 | // CONDITION 286 | when(commonService.procValidator(gTestModel)).thenReturn(Constants.RESULT_STATUS_FAIL); 287 | when(commonService.setResultModel(Users.class, Constants.RESULT_STATUS_FAIL)).thenReturn(gTestResultErrorModel); 288 | 289 | // TEST 290 | Users resultModel = userService.createUser(gTestModel); 291 | 292 | // VERIFY 293 | assertThat(resultModel).isNotNull(); 294 | assertEquals(Constants.RESULT_STATUS_FAIL, resultModel.getResultCode()); 295 | } 296 | 297 | 298 | /** 299 | * Update user valid return model. 300 | */ 301 | @Test 302 | public void updateUser_Valid_ReturnModel() { 303 | // SET 304 | gTestModel.setId(PID); 305 | 306 | // CONDITION 307 | when(commonService.procValidator(gTestModel)).thenReturn(Constants.RESULT_STATUS_SUCCESS); 308 | when(userRepository.save(gTestModel)).thenReturn(gTestResultModel); 309 | 310 | // TEST 311 | Users resultModel = userService.updateUser(gTestModel); 312 | 313 | // VERIFY 314 | assertThat(resultModel).isNotNull(); 315 | assertEquals(gTestResultModel, resultModel); 316 | assertEquals(PID, resultModel.getId()); 317 | assertEquals(USER_ID, resultModel.getUserId()); 318 | assertEquals(SERVICE_INSTANCE_ID, resultModel.getServiceInstanceId()); 319 | assertEquals(CAAS_ACCOUNT_ACCESS_TOKEN, resultModel.getCaasAccountTokenName()); 320 | assertEquals(CAAS_ACCOUNT_NAME, resultModel.getCaasAccountName()); 321 | assertEquals(ORGANIZATION_GUID, resultModel.getOrganizationGuid()); 322 | assertEquals(SPACE_GUID, resultModel.getSpaceGuid()); 323 | assertEquals(ROLE_SET_CODE, resultModel.getRoleSetCode()); 324 | assertEquals(NAMESPACE, resultModel.getCaasNamespace()); 325 | assertEquals(DESCRIPTION, resultModel.getDescription()); 326 | assertEquals(CREATED, resultModel.getCreated()); 327 | assertEquals(LAST_MODIFIED, resultModel.getLastModified()); 328 | assertEquals(RESULT_CODE_SUCCESS, resultModel.getResultCode()); 329 | } 330 | 331 | @Test 332 | public void updateUserRoleByServiceInstanceIdAndOrganizationGuid_Valid_Return_Model(){ 333 | // SET 334 | gTestModel.setUserId(USER_ID); 335 | gTestModel.setRoleSetCode(ROLE_SET_CODE); 336 | 337 | // CONDITION 338 | when(userRepository.save(gTestModel)).thenReturn(gTestResultModel); 339 | when(commonService.setResultModel(gTestResultModel, RESULT_CODE_SUCCESS)).thenReturn(gTestFinalModel); 340 | 341 | // TEST 342 | Users resultModel = userService.updateUserRoleByServiceInstanceIdAndOrganizationGuid(gTestModel); 343 | 344 | // VERIFY 345 | assertThat(resultModel).isNotNull(); 346 | assertEquals(gTestResultModel, resultModel); 347 | assertEquals(PID, resultModel.getId()); 348 | assertEquals(USER_ID, resultModel.getUserId()); 349 | assertEquals(SERVICE_INSTANCE_ID, resultModel.getServiceInstanceId()); 350 | assertEquals(CAAS_ACCOUNT_ACCESS_TOKEN, resultModel.getCaasAccountTokenName()); 351 | assertEquals(CAAS_ACCOUNT_NAME, resultModel.getCaasAccountName()); 352 | assertEquals(ORGANIZATION_GUID, resultModel.getOrganizationGuid()); 353 | assertEquals(SPACE_GUID, resultModel.getSpaceGuid()); 354 | assertEquals(ROLE_SET_CODE, resultModel.getRoleSetCode()); 355 | assertEquals(NAMESPACE, resultModel.getCaasNamespace()); 356 | assertEquals(DESCRIPTION, resultModel.getDescription()); 357 | assertEquals(CREATED, resultModel.getCreated()); 358 | assertEquals(LAST_MODIFIED, resultModel.getLastModified()); 359 | assertEquals(RESULT_CODE_SUCCESS, resultModel.getResultCode()); 360 | } 361 | 362 | /** 363 | * Update user invalid model return error model. 364 | */ 365 | @Test 366 | public void updateUser_InvalidModel_ReturnErrorModel() { 367 | // SET 368 | gTestModel.setId(PID); 369 | 370 | // CONDITION 371 | when(commonService.procValidator(gTestModel)).thenReturn(Constants.RESULT_STATUS_FAIL); 372 | when(commonService.setResultModel(Users.class, Constants.RESULT_STATUS_FAIL)).thenReturn(gTestResultErrorModel); 373 | 374 | // TEST 375 | Users resultModel = userService.updateUser(gTestModel); 376 | 377 | // VERIFY 378 | assertThat(resultModel).isNotNull(); 379 | assertEquals(Constants.RESULT_STATUS_FAIL, resultModel.getResultCode()); 380 | } 381 | 382 | 383 | /** 384 | * Delete user valid return model. 385 | */ 386 | @Test 387 | public void deleteUser_Valid_ReturnModel() { 388 | // SET 389 | gTestModel.setId(1); 390 | 391 | // CONDITION 392 | when(userRepository.deleteByServiceInstanceIdAndOrganizationGuidAndUserId(gTestModel.getServiceInstanceId(), gTestModel.getOrganizationGuid(), gTestModel.getUserId())).thenReturn(1); 393 | when(commonService.setResultModel(gTestEmptyModel, Constants.RESULT_STATUS_SUCCESS)).thenReturn(gTestResultModel); 394 | 395 | // TEST 396 | Users resultModel = userService.deleteByServiceInstanceIdAndOrganizationGuid(gTestModel.getServiceInstanceId(), gTestModel.getOrganizationGuid(), gTestModel.getUserId()); 397 | 398 | // VERIFY 399 | assertThat(resultModel).isNotNull(); 400 | assertEquals(RESULT_CODE_SUCCESS, resultModel.getResultCode()); 401 | } 402 | 403 | @Test 404 | public void deleteUser_Invalid_ReturnModel(){ 405 | // SET 406 | gTestModel.setId(1); 407 | 408 | // CONDITION 409 | when(userRepository.deleteByServiceInstanceIdAndOrganizationGuidAndUserId(gTestModel.getServiceInstanceId(), gTestModel.getOrganizationGuid(), gTestModel.getUserId())).thenReturn(0); 410 | when(commonService.setResultModel(gTestEmptyModel, Constants.RESULT_STATUS_FAIL)).thenReturn(gTestResultErrorModel); 411 | 412 | // TEST 413 | Users resultModel = userService.deleteByServiceInstanceIdAndOrganizationGuid(gTestModel.getServiceInstanceId(), gTestModel.getOrganizationGuid(), gTestModel.getUserId()); 414 | 415 | // VERIFY 416 | assertThat(resultModel).isNotNull(); 417 | assertEquals(RESULT_CODE_FAIL, resultModel.getResultCode()); 418 | } 419 | 420 | @Test 421 | public void deleteByServiceInstanceId_Valid_ReturnModel(){ 422 | // SET 423 | gTestModel.setServiceInstanceId(SERVICE_INSTANCE_ID); 424 | 425 | // CONDITION 426 | when(userRepository.deleteByServiceInstanceId(gTestModel.getServiceInstanceId())).thenReturn(1); 427 | when(commonService.setResultModel(Users.class, Constants.RESULT_STATUS_SUCCESS)).thenReturn(gTestResultModel); 428 | 429 | // TEST 430 | userService.deleteByServiceInstanceId(gTestModel.getServiceInstanceId()); 431 | } 432 | 433 | @Test 434 | public void updateByServiceInstanceId_Valid() { 435 | //set gTestResultList 436 | gTestModel.setServiceInstanceId(SERVICE_INSTANCE_ID); 437 | 438 | when(userRepository.findByServiceInstanceId(SERVICE_INSTANCE_ID)).thenReturn(gTestResultList); 439 | // when(userRepository.save(gTestModel)).thenReturn(gTestResultModel); 440 | 441 | //call method 442 | userService.updateByServiceInstanceId(SERVICE_INSTANCE_ID, gTestModel); 443 | } 444 | 445 | @Test 446 | public void isUserByCaasNamespace_Valid_ReturnTrue(){ 447 | // SET 448 | gTestModel.setUserId(USER_ID); 449 | gTestModel.setCaasNamespace(NAMESPACE); 450 | 451 | // CONDITION 452 | when(userRepository.findByUserIdAndCaasNamespace(gTestModel.getUserId(), gTestModel.getCaasNamespace())).thenReturn(gTestResultModel); 453 | 454 | // TEST 455 | boolean result = userService.isUserByCaasNamespace(gTestModel.getUserId(), gTestModel.getCaasNamespace() ); 456 | 457 | // VERIFY 458 | assertThat(result).isNotNull(); 459 | assertEquals(true, result); 460 | 461 | } 462 | 463 | @Test 464 | public void isUserByCaasNamespace_Invalid_ReturnFalse(){ 465 | // SET 466 | gTestModel.setUserId(USER_ID); 467 | gTestModel.setCaasNamespace(NAMESPACE); 468 | 469 | // CONDITION 470 | when(userRepository.findByUserIdAndCaasNamespace(gTestModel.getUserId(), gTestModel.getCaasNamespace())).thenReturn(null); 471 | 472 | // TEST 473 | boolean result = userService.isUserByCaasNamespace(gTestModel.getUserId(), gTestModel.getCaasNamespace() ); 474 | 475 | // VERIFY 476 | assertThat(result).isNotNull(); 477 | assertEquals(false, result); 478 | } 479 | } 480 | -------------------------------------------------------------------------------- /src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: caas-common-api 4 | security: 5 | username: admin 6 | password: PaaS-TA 7 | 8 | --- 9 | spring: 10 | profiles: 11 | active: local 12 | datasource: 13 | url: jdbc:mysql://DATASOURCE-URL 14 | driver-class-name: com.mysql.cj.jdbc.Driver 15 | validationQuery: SELECT 1 16 | username: DATASOURCE-USER-NAME 17 | password: DATASOURCE-PASSWORD 18 | jpa: 19 | database: mysql 20 | hibernate: 21 | ddl-auto: none 22 | naming: 23 | strategy: org.hibernate.cfg.EJB3NamingStrategy 24 | generate-ddl: false 25 | properties: 26 | hibernate: 27 | dialect: org.hibernate.dialect.MySQLInnoDBDialect 28 | show_sql: true 29 | format_sql: true 30 | use_sql_comments: true 31 | 32 | logging: 33 | level: 34 | ROOT: INFO 35 | path: classpath:logback-spring.xml 36 | file: logs/application.log 37 | 38 | server: 39 | port: 3334 40 | 41 | caasApi: 42 | url: http://localhost:3333 43 | authorization: 44 | id: AUTHORIZATION-ID 45 | password: AUTHORIZATION-PASSWORD 46 | --------------------------------------------------------------------------------