MigrationServiceImpl extends AbstractConsoleService<MigrationEventTblDao> implements MigrationService { protected DcTbl findToDc(String fromIdc, String toIdc, List<DcTbl> clusterRelatedDc) throws ToIdcNotFoundException { DcTbl fromIdcInfo = null; for(DcTbl dcTbl : clusterRelatedDc) { if(dcTbl.getDcName().equals(fromIdc)) { fromIdcInfo = dcTbl; break; } } if(StringUtil.isEmpty(toIdc)){ for (DcTbl dcTbl : clusterRelatedDc) { if (!dcTbl.getDcName().equalsIgnoreCase(fromIdc) && isSameZone(fromIdcInfo, dcTbl)) { return dcTbl; } } throw new ToIdcNotFoundException(String.format("fromIdc:%s, toIdc empty, can not find target dc %s", fromIdc, clusterRelatedDcToString(clusterRelatedDc))); }else { if(toIdc.equalsIgnoreCase(fromIdc)){ throw new ToIdcNotFoundException(String.format("fromIdc:%s equals with toIdc %s", fromIdc, toIdc)); } for (DcTbl dcTbl : clusterRelatedDc) { if (dcTbl.getDcName().equalsIgnoreCase(toIdc)) { if(!isSameZone(fromIdcInfo, dcTbl)) { throw new ToIdcNotFoundException("To Idc should be in same zone with from Idc"); } return dcTbl; } } throw new ToIdcNotFoundException(String.format("toIdc : %s, can not find it in all related dcs:%s", toIdc, clusterRelatedDcToString(clusterRelatedDc))); } }  @Override MigrationEventTbl find(final long id); @Override long countAll(); @Override long countAllByCluster(long clusterId); @Override List<MigrationModel> find(long size, long offset); @Override List<MigrationModel> findByCluster(long clusterId, long size, long offset); @Override List<MigrationEventTbl> findAll(); @Override MigrationClusterTbl findMigrationCluster(final long eventId, final long clusterId); @Override void updateMigrationClusterStartTime(long migrationClusterId, Date startTime); @Override void updateStatusAndEndTimeById(long migrationClusterId, MigrationStatus status, Date endTime); @Override void updatePublishInfoById(long migrationClusterId, String publishInfo); @Override MigrationClusterTbl findLatestUnfinishedMigrationCluster(final long clusterId); @Override List<MigrationShardTbl> findMigrationShards(final long migrationClusterId); @Override List<MigrationClusterModel> getMigrationClusterModel(long eventId); @Override Long createMigrationEvent(MigrationRequest request); @Override void updateMigrationShardLogById(final long id, final String log); @Override void continueMigrationCluster(final long eventId, final long clusterId); @Override void continueMigrationEvent(long id); @Override MigrationEvent getMigrationEvent(long eventId); @Override MigrationCluster rollbackMigrationCluster(long eventId, long clusterId); @Override MigrationCluster rollbackMigrationCluster(long eventId, String clusterName); @Override void cancelMigrationCluster(long eventId, long clusterId); @Override void forcePublishMigrationCluster(long eventId, long clusterId); @Override void forceEndMigrationClsuter(long eventId, long clusterId); @Override MigrationSystemAvailableChecker.MigrationSystemAvailability getMigrationSystemAvailability(); @Override TryMigrateResult tryMigrate(String clusterName, String fromIdc, String toIdc); @Override RetMessage getMigrationSystemHealth(); @VisibleForTesting MigrationServiceImpl setChecker(MigrationSystemAvailableChecker checker); @VisibleForTesting MigrationServiceImpl setMigrationEventManager(MigrationEventManager migrationEventManager); @VisibleForTesting MigrationServiceImpl setClusterService(ClusterService clusterService); @VisibleForTesting MigrationServiceImpl setDcClusterService(DcClusterService dcClusterService); @VisibleForTesting MigrationServiceImpl setDcService(DcService dcService); @VisibleForTesting MigrationServiceImpl setMigrationClusterDao(MigrationClusterDao migrationClusterDao); @VisibleForTesting MigrationServiceImpl setConfigService(ConfigService configService); @VisibleForTesting MigrationServiceImpl setMigrationShardTblDao(MigrationShardTblDao migrationShardTblDao);  }
@Test public void testFindToDc() throws ToIdcNotFoundException { List<DcTbl> relatedDcs = new LinkedList<>(); try{ Assert.assertEquals("dc2", migrationService.findToDc("dc1", null, relatedDcs).getDcName()); Assert.fail(); }catch (ToIdcNotFoundException e){ } try{ Assert.assertEquals("dc2", migrationService.findToDc("dc1", "dc2", relatedDcs).getDcName()); Assert.fail(); }catch (ToIdcNotFoundException e){ } relatedDcs.add(new DcTbl().setDcName("dc1")); relatedDcs.add(new DcTbl().setDcName("dc2")); relatedDcs.add(new DcTbl().setDcName("dc3")); Assert.assertEquals("dc2", migrationService.findToDc("dc1", null, relatedDcs).getDcName()); Assert.assertEquals("dc2", migrationService.findToDc("dc1", "dc2", relatedDcs).getDcName()); Assert.assertEquals("dc3", migrationService.findToDc("dc1", "dc3", relatedDcs).getDcName()); try { Assert.assertEquals("dc3", migrationService.findToDc("dc1", "dc1", relatedDcs).getDcName()); Assert.fail(); }catch (ToIdcNotFoundException e){ } try{ Assert.assertEquals("dc2", migrationService.findToDc("dc1", "dc4", relatedDcs).getDcName()); Assert.fail(); }catch (ToIdcNotFoundException e){ } }
@Test(expected = ToIdcNotFoundException.class) public void testFindToDcWithNotSameZone() throws ToIdcNotFoundException { List<DcTbl> relatedDcs = new LinkedList<>(); relatedDcs.add(new DcTbl().setDcName("dc1").setZoneId(1L)); relatedDcs.add(new DcTbl().setDcName("dc2").setZoneId(1L)); relatedDcs.add(new DcTbl().setDcName("dc3").setZoneId(2L)); migrationService.findToDc("dc1", "dc3", relatedDcs); }