UpgradeCatalog240 extends AbstractUpgradeCatalog { protected void updateRequestScheduleEntityUserIds() throws SQLException { List<RequestScheduleEntity> requestScheduleEntities = requestScheduleDAO.findAll(); for (RequestScheduleEntity requestScheduleEntity : requestScheduleEntities) { String createdUserName = requestScheduleEntity.getCreateUser(); if (createdUserName != null) { User user = users.getUserIfUnique(createdUserName); if (user != null && StringUtils.equals(user.getUserName(), createdUserName)) { requestScheduleEntity.setAuthenticatedUserId(user.getUserId()); requestScheduleDAO.merge(requestScheduleEntity); } } } } @Inject  UpgradeCatalog240(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion(); static final String DESIRED_VERSION_COLUMN_NAME; static final String BLUEPRINT_SETTING_TABLE; static final String BLUEPRINT_NAME_COL; static final String SETTING_NAME_COL; static final String SETTING_DATA_COL; static final String ID; static final String BLUEPRINT_TABLE; static final String VIEWINSTANCE_TABLE; static final String SHORT_URL_COLUMN; static final String CLUSTER_HANDLE_COLUMN; static final String REQUESTSCHEDULE_TABLE; static final String AUTHENTICATED_USER_ID_COLUMN; static final String TIMELINE_METRICS_SINK_COLLECTION_PERIOD; static final String ONE_DIR_PER_PARITION_PROPERTY; static final String VIEWURL_TABLE; static final String URL_ID_COLUMN; static final String PRECISION_TABLE_TTL_PROPERTY; static final String CLUSTER_SECOND_TABLE_TTL_PROPERTY; static final String CLUSTER_TYPE_COLUMN; static final String REMOTE_AMBARI_CLUSTER_TABLE; static final String REMOTE_AMBARI_CLUSTER_SERVICE_TABLE; static final String CLUSTER_ID; static final String SERVICE_NAME; static final String CLUSTER_NAME; }
@Test public void testUpdateRequestScheduleEntityUserIds() throws Exception{ final RequestScheduleDAO requestScheduleDAO = createMock(RequestScheduleDAO.class); final Users users = createMock(Users.class); RequestScheduleEntity requestScheduleEntity = new RequestScheduleEntity(); requestScheduleEntity.setCreateUser("createdUser"); requestScheduleEntity.setClusterId(1L); expect(requestScheduleDAO.findAll()).andReturn(Collections.singletonList(requestScheduleEntity)).once(); UserEntity userEntity = new UserEntity(); userEntity.setUserName("createdUser"); userEntity.setUserId(1); userEntity.setPrincipal(new PrincipalEntity()); User user = new User(userEntity); expect(users.getUserIfUnique("createdUser")).andReturn(user).once(); expect(requestScheduleDAO.merge(requestScheduleEntity)).andReturn(requestScheduleEntity).once(); final Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(RequestScheduleDAO.class).toInstance(requestScheduleDAO); bind(Users.class).toInstance(users); bind(PasswordEncoder.class).toInstance(createMock(PasswordEncoder.class)); bind(DBAccessor.class).toInstance(createMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); bind(EntityManager.class).toInstance(entityManager); bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class)); bind(HookService.class).toInstance(createMock(HookService.class)); } }); UpgradeCatalog240 upgradeCatalog240 = new UpgradeCatalog240(injector); replay(requestScheduleDAO, users); upgradeCatalog240.updateRequestScheduleEntityUserIds(); verify(requestScheduleDAO, users); assertEquals(Integer.valueOf(1), requestScheduleEntity.getAuthenticatedUserId()); }
@Test public void testUpdateRequestScheduleEntityWithUnuniqueUser() throws Exception{ final RequestScheduleDAO requestScheduleDAO = createMock(RequestScheduleDAO.class); final Users users = createMock(Users.class); RequestScheduleEntity requestScheduleEntity = new RequestScheduleEntity(); requestScheduleEntity.setCreateUser("createdUser"); requestScheduleEntity.setClusterId(1L); expect(requestScheduleDAO.findAll()).andReturn(Collections.singletonList(requestScheduleEntity)).once(); expect(users.getUserIfUnique("createdUser")).andReturn(null).once(); final Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(RequestScheduleDAO.class).toInstance(requestScheduleDAO); bind(Users.class).toInstance(users); bind(PasswordEncoder.class).toInstance(createMock(PasswordEncoder.class)); bind(DBAccessor.class).toInstance(createMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); bind(EntityManager.class).toInstance(entityManager); bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class)); bind(HookService.class).toInstance(createMock(HookService.class)); } }); UpgradeCatalog240 upgradeCatalog240 = new UpgradeCatalog240(injector); replay(requestScheduleDAO, users); upgradeCatalog240.updateRequestScheduleEntityUserIds(); verify(requestScheduleDAO, users); assertEquals(null, requestScheduleEntity.getAuthenticatedUserId()); }