Skip to content

Commit

Permalink
ISSUE-2428 refactor: update null checking
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Zhou <[email protected]>
  • Loading branch information
luke-zhou committed Jul 10, 2024
2 parents d5924c1 + 697950c commit fe540b3
Show file tree
Hide file tree
Showing 15 changed files with 1,133 additions and 105 deletions.
3 changes: 1 addition & 2 deletions coral/docs/development-with-remote-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
2. run `pnpm install`
3. run `pnpm add-precommit` the first time you install the repository to set the custom directory for our pre commit hooks.
4. Run development:
4.1. If you have not set up a remote API mode, please follow [First setup](../docs/development-with-remote-api.
md#first-setup)
4.1. If you have not set up a remote API mode, please follow [First setup](../docs/development-with-remote-api.md#first-setup)
4.2. If you already have a setup, run `pnpm dev`
5. [Create new Vite mode for remote API development](#create-new-vite-mode-for-remote-api-development)
6. When your API runs on `https`: [Set up self-signed certificate](#set-up-self-signed-certificate-required-when-api-runs-on-https)
Expand Down
28 changes: 14 additions & 14 deletions coral/types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ export type components = {
errCode?: string;
message: string;
debugMessage?: string;
data?: Record<string, never>;
data?: unknown;
/** Format: date-time */
timestamp?: string;
};
Expand Down Expand Up @@ -629,7 +629,7 @@ export type components = {
sourceEnv?: string;
selectedTeam?: string;
typeOfSync?: string;
topicDetails?: Record<string, never>[];
topicDetails?: unknown[];
topicSearchFilter?: string;
};
SyncConnectorUpdates: {
Expand Down Expand Up @@ -2230,25 +2230,25 @@ export type operations = {
/** @description OK */
200: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Multi Status */
207: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Bad Request */
405: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Internal Server Error */
500: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
};
Expand All @@ -2267,25 +2267,25 @@ export type operations = {
/** @description OK */
200: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Multi Status */
207: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Bad Request */
405: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Internal Server Error */
500: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
};
Expand All @@ -2304,25 +2304,25 @@ export type operations = {
/** @description OK */
200: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Multi Status */
207: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Bad Request */
405: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
/** @description Internal Server Error */
500: {
content: {
"application/json": components["schemas"]["ApiResponse"][];
"application/json": unknown;
};
};
};
Expand Down
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<apachecommons.version>2.16.1</apachecommons.version>
<apachepoi.version>5.2.4</apachepoi.version>
<commons-text.version>1.11.0</commons-text.version>
<exec-maven-plugin.version>3.2.0</exec-maven-plugin.version>
<clean-maven-plugin.version>3.3.2</clean-maven-plugin.version>
<exec-maven-plugin.version>3.3.0</exec-maven-plugin.version>
<clean-maven-plugin.version>3.4.0</clean-maven-plugin.version>
<front-end-maven-plugin.version>1.12.1</front-end-maven-plugin.version>
<h2.version>2.1.214</h2.version>
<jasyptencrypt.version>3.0.5</jasyptencrypt.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.aiven.klaw.helpers.db.rdbms;

import static org.springframework.beans.BeanUtils.copyProperties;

import io.aiven.klaw.dao.*;
import io.aiven.klaw.model.enums.ApiResultStatus;
import io.aiven.klaw.model.enums.EntityType;
import io.aiven.klaw.model.enums.NewUserStatus;
import io.aiven.klaw.model.enums.RequestStatus;
import io.aiven.klaw.repository.*;
import java.sql.Timestamp;
Expand Down Expand Up @@ -380,13 +381,21 @@ public String insertIntoRegisterUsers(RegisterUserInfo userInfo) {
Optional<UserInfo> userNameExists = userInfoRepo.findById(userInfo.getUsername());
if (userNameExists.isPresent()) return "Failure. User already exists";

// STAGING status comes from AD users
RegisterUserInfo userRegistration =
registerInfoRepo.findFirstByUsernameAndStatusIn(
userInfo.getUsername(),
List.of(NewUserStatus.PENDING.value, NewUserStatus.STAGING.value));
if (userRegistration != null) {
return "Failure. Registration already exists";
Optional<RegisterUserInfo> optionalUserRegistration =
registerInfoRepo.findByUsername(userInfo.getUsername());
if (optionalUserRegistration.isPresent()) {
if ("APPROVED".equals(optionalUserRegistration.get().getStatus())) {
// do nothing -- user is deleted
} else if (!"STAGING".equals(optionalUserRegistration.get().getStatus())
&& !"PENDING".equals(optionalUserRegistration.get().getStatus())) {
return "Failure. Registration already exists";
} else {
int id = optionalUserRegistration.get().getId();
copyProperties(userInfo, optionalUserRegistration.get());
optionalUserRegistration.get().setId(id);
registerInfoRepo.save(optionalUserRegistration.get());
return ApiResultStatus.SUCCESS.value;
}
}

registerInfoRepo.save(userInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,10 @@ public void updateNewUserRequest(String username, String approver, boolean isApp
if (isApprove) {
status = NewUserStatus.APPROVED.value;
} else {
status = NewUserStatus.DECLINED.value;
// In case if user registration is declined, delete the record from db, so user can try to
// register again with any new data.
registerInfoRepo.deleteById("" + registerUser.getId());
return;
}
if (registerUser != null) {
if (NewUserStatus.PENDING.value.equals(registerUser.getStatus())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public List<KwClustersModelResponse> getClustersPaginated(
model.getClusterName(),
model.getBootstrapServers(),
model.getProtocol().getName())
.filter(s -> s != null)
.filter(Objects::nonNull)
.map(String::toLowerCase)
.anyMatch(s -> s.contains(searchClusterParam.toLowerCase())))
.sorted(Comparator.comparingInt(KwClustersModelResponse::getClusterId))
Expand Down Expand Up @@ -322,7 +322,7 @@ public List<EnvModelResponse> getEnvsPaginated(
env.getClusterName(),
env.getOtherParams(),
env.getTenantName())
.filter(s -> s != null)
.filter(Objects::nonNull)
.map(String::toLowerCase)
.anyMatch(s -> s.contains(searchEnvParam.toLowerCase())))
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -620,7 +621,8 @@ public ApiResponse addNewUser(UserInfoModel newUser, boolean isExternal) throws

if (isExternal) {

if ("".equals(newUser.getUserPassword())) {
if ("".equals(newUser.getUserPassword())
|| ACTIVE_DIRECTORY.value.equals(authenticationType)) {
mailService.sendMail(
newUser.getUsername(),
newUser.getUserPassword(),
Expand All @@ -645,7 +647,7 @@ public ApiResponse addNewUser(UserInfoModel newUser, boolean isExternal) throws
} catch (Exception e1) {
log.error("Try deleting user");
}
if (e.getMessage().contains("should not exist")) {
if (StringUtils.isNotBlank(e.getMessage()) && e.getMessage().contains("should not exist")) {
return ApiResponse.notOk(TEAMS_ERR_110);
} else {
log.error("Error ", e);
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/io/aiven/klaw/MockMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.aiven.klaw.model.requests.EnvModel;
import io.aiven.klaw.model.requests.KwClustersModel;
import io.aiven.klaw.model.requests.KwRolesPermissionsModel;
import io.aiven.klaw.model.requests.RegisterUserInfoModel;
import io.aiven.klaw.model.requests.TeamModel;
import io.aiven.klaw.model.requests.UserInfoModel;
import io.aiven.klaw.model.response.EnvParams;
Expand Down Expand Up @@ -53,6 +54,18 @@ public UserInfoModel getUserInfoModel(String username, String role, String team)
return userInfoModel;
}

public RegisterUserInfoModel getRegisterUserInfoModel(String username, String role) {
RegisterUserInfoModel userInfoModel = new RegisterUserInfoModel();
userInfoModel.setUsername(username);
userInfoModel.setPwd("testpwd");
userInfoModel.setRole(role);
userInfoModel.setTeamId(1001);
userInfoModel.setFullname("New User");
userInfoModel.setMailid("[email protected]");

return userInfoModel;
}

public UserInfoModel getUserInfoModelSwitchTeams(
String username, String role, int teamId, int switchTeamSize) {
UserInfoModel userInfoModel = new UserInfoModel();
Expand Down
Loading

0 comments on commit fe540b3

Please sign in to comment.