DcMetaComparator extends AbstractMetaComparator<ClusterMeta, DcChange> { public void compare(){ Triple<Set<String>, Set<String>, Set<String>> result = getDiff(current.getClusters().keySet(), future.getClusters().keySet()); Set<String> addedClusterIds = result.getFirst(); Set<String> intersectionClusterIds = result.getMiddle(); Set<String> deletedClusterIds = result.getLast(); for(String clusterId : addedClusterIds){ added.add(future.findCluster(clusterId)); } for(String clusterId : deletedClusterIds){ removed.add(current.findCluster(clusterId)); } for(String clusterId : intersectionClusterIds){ ClusterMeta currentMeta = current.findCluster(clusterId); ClusterMeta futureMeta = future.findCluster(clusterId); if(!reflectionEquals(currentMeta, futureMeta)){ ClusterMetaComparator clusterMetaComparator = new ClusterMetaComparator(currentMeta, futureMeta); clusterMetaComparator.compare(); modified.add(clusterMetaComparator); } } } private  DcMetaComparator();  DcMetaComparator(DcMeta current, DcMeta future); static DcMetaComparator buildComparator(DcMeta current, DcMeta future); static DcMetaComparator buildClusterRemoved(ClusterMeta clusterMeta); static DcMetaComparator buildClusterChanged(ClusterMeta current, ClusterMeta future); void compare(); @Override String idDesc();  }
@Test public void testEquals(){ DcMetaComparator dcMetaComparator = new DcMetaComparator(current, future); dcMetaComparator.compare(); Assert.assertEquals(0, dcMetaComparator.getRemoved().size()); Assert.assertEquals(0, dcMetaComparator.getAdded().size()); Assert.assertEquals(0, dcMetaComparator.getMofified().size()); }
@Test public void testAdded(){ ClusterMeta cluster = differentCluster(current); future.addCluster(cluster); DcMetaComparator dcMetaComparator = new DcMetaComparator(current, future); dcMetaComparator.compare(); Assert.assertEquals(0, dcMetaComparator.getRemoved().size()); Assert.assertEquals(1, dcMetaComparator.getAdded().size()); Assert.assertEquals(cluster, dcMetaComparator.getAdded().toArray()[0]); Assert.assertEquals(0, dcMetaComparator.getMofified().size()); }
@Test public void testDeleted(){ ClusterMeta cluster = differentCluster(current); current.addCluster(cluster); DcMetaComparator dcMetaComparator = new DcMetaComparator(current, future); dcMetaComparator.compare(); Assert.assertEquals(1, dcMetaComparator.getRemoved().size()); Assert.assertEquals(cluster, dcMetaComparator.getRemoved().toArray()[0]); Assert.assertEquals(0, dcMetaComparator.getAdded().size()); Assert.assertEquals(0, dcMetaComparator.getMofified().size()); }
@Test public void testModified(){ ClusterMeta clusterMeta = (ClusterMeta) future.getClusters().values().toArray()[0]; clusterMeta.addShard(differentShard(clusterMeta)); DcMetaComparator dcMetaComparator = new DcMetaComparator(current, future); dcMetaComparator.compare(); Assert.assertEquals(0, dcMetaComparator.getRemoved().size()); Assert.assertEquals(0, dcMetaComparator.getAdded().size()); Assert.assertEquals(1, dcMetaComparator.getMofified().size()); ClusterMetaComparator comparator = (ClusterMetaComparator) dcMetaComparator.getMofified().toArray()[0]; Assert.assertEquals(clusterMeta.getId(), comparator.getCurrent().getId()); Assert.assertEquals(1, comparator.getAdded().size()); }
@Test public void testModifyRedisConfig(){ ClusterMeta clusterMeta = (ClusterMeta) future.getClusters().values().toArray()[0]; ShardMeta shardMeta = (ShardMeta) clusterMeta.getShards().values().toArray()[0]; RedisMeta redisMeta = shardMeta.getRedises().get(0); redisMeta.setPort(redisMeta.getPort() + 10000); DcMetaComparator dcMetaComparator = new DcMetaComparator(current, future); dcMetaComparator.compare(); Assert.assertEquals(0, dcMetaComparator.getRemoved().size()); Assert.assertEquals(0, dcMetaComparator.getAdded().size()); Assert.assertEquals(1, dcMetaComparator.getMofified().size()); ClusterMetaComparator comparator = (ClusterMetaComparator) dcMetaComparator.getMofified().toArray()[0]; Assert.assertEquals(clusterMeta.getId(), comparator.getCurrent().getId()); Assert.assertEquals(0, comparator.getAdded().size()); Assert.assertEquals(0, comparator.getRemoved().size()); Assert.assertEquals(1, comparator.getMofified().size()); ShardMetaComparator shardMetaComparator = (ShardMetaComparator) comparator.getMofified().toArray()[0]; Assert.assertEquals(1, shardMetaComparator.getAdded().size()); Assert.assertEquals(1, shardMetaComparator.getRemoved().size()); Assert.assertEquals(0, shardMetaComparator.getMofified().size()); logger.debug("{}", dcMetaComparator); }