Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#noissue] Check existing users to handle duplicate user id error in insertUser() #10348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;

import com.navercorp.pinpoint.web.vo.User;
import com.navercorp.pinpoint.web.vo.exception.PinpointUserException;

/**
* @author minwoo.jung
Expand All @@ -26,7 +27,7 @@ public interface UserService {

void dropAndCreateUserTable();

void insertUser(User user);
void insertUser(User user) throws PinpointUserException;

void insertUserList(List<User> users);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.navercorp.pinpoint.web.util.UserInfoDecoder;
import com.navercorp.pinpoint.web.util.UserInfoEncoder;
import com.navercorp.pinpoint.web.vo.User;
import com.navercorp.pinpoint.web.vo.exception.PinpointUserException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -51,7 +52,10 @@ public UserServiceImpl(UserDao userDao, Optional<UserInfoDecoder> userInfoDecode
}

@Override
public void insertUser(User user) {
public void insertUser(User user) throws PinpointUserException {
if (isExistUserId(user.getUserId())) {
throw new PinpointUserException(String.format("There is already a user with %s ID.", user.getUserId()));
}
User encodedUser = userInfoEncoder.encodeUserInfo(user);
userDao.insertUser(encodedUser);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2023 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.web.vo.exception;

/**
* @author fatihsaracoglu
*/
public class PinpointUserException extends Exception {

public PinpointUserException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.navercorp.pinpoint.web.util.UserInfoDecoder;
import com.navercorp.pinpoint.web.util.UserInfoEncoder;
import com.navercorp.pinpoint.web.vo.User;
import com.navercorp.pinpoint.web.vo.exception.PinpointUserException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -18,9 +19,7 @@
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -47,7 +46,7 @@ public void before() throws Exception {
}

@Test
public void insertUserTest() {
public void insertUserTest() throws PinpointUserException {
UserDao userDao = new MemoryUserDao(mock(UserGroupDao.class));
UserService userService = new UserServiceImpl(userDao, Optional.of(userInfoDecoder), Optional.of(userInfoEncoder));

Expand All @@ -66,6 +65,19 @@ public void insertUserTest() {
assertEquals(decodedUser.getEmail(), ENCODED_EMAIL);
}

@Test
public void insertUserDuplicateUserIdErrorTest() throws PinpointUserException {
UserDao userDao = new MemoryUserDao(mock(UserGroupDao.class));
UserService userService = new UserServiceImpl(userDao, Optional.of(userInfoDecoder), Optional.of(userInfoEncoder));

String userId = "userId01";
User user1 = new User(userId, "name01", "departmentName", 82, "01012341234", "[email protected]");
userService.insertUser(user1);
User user2 = new User(userId, "name02", "departmentName", 82, "01012341235", "[email protected]");

assertThrows(PinpointUserException.class, () -> userService.insertUser(user2));
}

@Test
public void insertUserList() {
UserDao userDao = new MemoryUserDao(mock(UserGroupDao.class));
Expand Down Expand Up @@ -94,7 +106,7 @@ public void insertUserList() {
}

@Test
public void updateUserTest() {
public void updateUserTest() throws PinpointUserException {
UserDao userDao = new MemoryUserDao(mock(UserGroupDao.class));
UserService userService = new UserServiceImpl(userDao, Optional.of(userInfoDecoder), Optional.of(userInfoEncoder));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.navercorp.pinpoint.web.service.UserService;
import com.navercorp.pinpoint.web.util.ValueValidator;
import com.navercorp.pinpoint.web.vo.User;
import com.navercorp.pinpoint.web.vo.exception.PinpointUserException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -57,7 +58,7 @@ public UserController(UserService userService) {
}

@PostMapping
public Response insertUser(@RequestBody User user) {
public Response insertUser(@RequestBody User user) throws PinpointUserException {
if (!ValueValidator.validateUser(user)) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
Expand Down