UserMgmtServerEao { public void checkServerChanged(UserMgmtServer userMgtServer) { UserMgmtServer existingUserMgmtServer = findByName(userMgtServer.getName(), false); ValidationErrors errors = new ValidationErrors(); if (existingUserMgmtServer == null) { ValidationError validationError = new ValidationError("NAME.NOT_FOUND", "given server is not found."); errors.addError("NAME", validationError); } if (existingUserMgmtServer !=null && existingUserMgmtServer.equals(userMgtServer)) { ValidationError validationError = new ValidationError("USERMGMTSERVER.NO_CHANGE", "The server info is not changed."); errors.addError("USERMGMTSERVER", validationError); } if (!errors.getErrors().isEmpty()) { throw new ValidationException(errors.getErrors()); } }  void persist(UserMgmtServer usrMgmtServer); @Transactional(propagation = Propagation.SUPPORTS) UserMgmtServer findByName(String name, boolean safely); void delete(String name); void checkServerChanged(UserMgmtServer userMgtServer); void modify(UserMgmtServer usrMgmtServer);  }
@Test(groups = {"TestUserMgmtServerEao"}) public void testCheckServerChanged() { userMgmtServerDao = new MockUp<IBaseDAO<UserMgmtServerEntity>>() { @Mock(invocations = 1) public UserMgmtServerEntity findById(Serializable id) { UserMgmtServerEntity userMgmtServerEntity = prepareUserMgmtServerEntity(decodePassword); return userMgmtServerEntity; } }.getMockInstance(); TestUtils.setPrivateField(userMgmtServerEao, "userMgmtServerDao", userMgmtServerDao); UserMgmtServer userMgmtServer = prepareUserMgmtServer(true); userMgmtServerEao.checkServerChanged(userMgmtServer); }
@Test(groups = {"TestUserMgmtServerEao"}, expectedExceptions = ValidationException.class) public void testCheckServerChangedWithNotFoundException() { userMgmtServerDao = new MockUp<IBaseDAO<UserMgmtServerEntity>>() { @Mock(invocations = 1) public UserMgmtServerEntity findById(Serializable id) { return null; } }.getMockInstance(); TestUtils.setPrivateField(userMgmtServerEao, "userMgmtServerDao", userMgmtServerDao); UserMgmtServer userMgmtServer = prepareUserMgmtServer(true); try { userMgmtServerEao.checkServerChanged(userMgmtServer); } catch (ValidationException e) { assertEquals(e.getErrors().get("NAME").getMessage(), NOT_FOUND_ERROR_MESSAGE); throw e; } }
@Test(groups = {"TestUserMgmtServerEao"}, expectedExceptions = ValidationException.class) public void testCheckServerChangedWithNoChangeException() { userMgmtServerDao = new MockUp<IBaseDAO<UserMgmtServerEntity>>() { @Mock(invocations = 1) public UserMgmtServerEntity findById(Serializable id) { UserMgmtServerEntity userMgmtServerEntity = prepareUserMgmtServerEntity(password); return userMgmtServerEntity; } }.getMockInstance(); TestUtils.setPrivateField(userMgmtServerEao, "userMgmtServerDao", userMgmtServerDao); UserMgmtServer userMgmtServer = prepareUserMgmtServer(false); try { userMgmtServerEao.checkServerChanged(userMgmtServer); } catch (ValidationException e) { assertEquals(e.getErrors().get("USERMGMTSERVER").getMessage(), NO_CHANGE_ERROR_MESSAGE); throw e; } }