ServiceInstance extends DiscoveryRecord { @Override public boolean equals ( Object obj ) { if ( obj == null ) { return false; } if ( this.getClass() != obj.getClass() ) { return false; } final ServiceInstance that = (ServiceInstance) obj; return Objects.equals( this.serviceType, that.getServiceType() ) && Objects.equals( this.serviceRecord, that.getServiceRecord() ) && Objects.equals( this.textRecord, that.getTextRecord() ); } ServiceInstance( String serviceType, ServiceRecord serviceRecord, TextRecord textRecord ); ServiceRecord getServiceRecord(); TextRecord getTextRecord(); @Override int hashCode(); @Override boolean equals( Object obj ); @Override int compareTo( DiscoveryRecord other ); @Override String toString(); @Override String getServiceType(); @Override String getServiceZone( String dnsLabel ); @Override String getServiceName( String dnsLabel ); @Override String toDisplay();  }
@Test public void testEquals () throws TextParseException { ServiceRecord svc1 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 0, 10, 3600 ) ); ServiceRecord svc2 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 0, 10, 3600 ) ); TextRecord txt1 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); TextRecord txt2 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); ServiceInstance svcInst1 = new ServiceInstance( serviceTypeCoap, svc1, txt1 ); ServiceInstance svcInst2 = new ServiceInstance( serviceTypeCoap, svc2, txt2 ); Assert.assertTrue( "Both service instances should be equal", svcInst1.equals( svcInst2 ) ); }
@Test public void testNotEquals () throws TextParseException { ServiceRecord svc1 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 0, 10, 3600 ) ); ServiceRecord svc2 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 0, 10, 3600 ) ); TextRecord txt1 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); TextRecord txt2 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); ServiceInstance svcInst1 = new ServiceInstance( serviceTypeCoap, svc1, txt1 ); ServiceInstance svcInst2 = new ServiceInstance( serviceTypeMqtt, svc2, txt2 ); Assert.assertTrue( "Both service instances should not be equal", !svcInst1.equals( svcInst2 ) ); }
@Test public void testCompareSamePriorityWeightAndDifferentHost () throws TextParseException { ServiceRecord svc1 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host1.com.", 1800, 0, 10, 3600 ) ); ServiceRecord svc2 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host2.com.", 1800, 0, 10, 3600 ) ); ServiceRecord svc3 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host3.com.", 1800, 0, 10, 3600 ) ); TextRecord txt1 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); TextRecord txt2 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); TextRecord txt3 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); ServiceInstance svcInst1 = new ServiceInstance( serviceTypeCoap, svc1, txt1 ); ServiceInstance svcInst2 = new ServiceInstance( serviceTypeCoap, svc2, txt2 ); ServiceInstance svcInst3 = new ServiceInstance( serviceTypeCoap, svc3, txt3 ); List<ServiceInstance> list = new ArrayList<ServiceInstance>( 3 ); list.add( svcInst2 ); list.add( svcInst3 ); list.add( svcInst1 ); Collections.sort( list ); Assert.assertTrue( "First element in the list should be svcInst1", list.get( 0 ).equals( svcInst1 ) ); Assert.assertTrue( "Second element in the list should be svcInst2", list.get( 1 ).equals( svcInst2 ) ); Assert.assertTrue( "Third element in the list should be svcInst3", list.get( 2 ).equals( svcInst3 ) ); }
@Test public void testCompareSamePriorityWeightHostAndDifferentPort () throws TextParseException { ServiceRecord svc1 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1800, 0, 10, 3600 ) ); ServiceRecord svc2 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1801, 0, 10, 3600 ) ); ServiceRecord svc3 = ServiceRecord.build( DiscoveryRecordUtil.createSrvRecord( "host.com.", 1802, 0, 10, 3600 ) ); TextRecord txt1 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); TextRecord txt2 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); TextRecord txt3 = TextRecord.build( DiscoveryRecordUtil.createTxtRecord( "txt1", 3600 ) ); ServiceInstance svcInst1 = new ServiceInstance( serviceTypeCoap, svc1, txt1 ); ServiceInstance svcInst2 = new ServiceInstance( serviceTypeCoap, svc2, txt2 ); ServiceInstance svcInst3 = new ServiceInstance( serviceTypeCoap, svc3, txt3 ); List<ServiceInstance> list = new ArrayList<ServiceInstance>( 3 ); list.add( svcInst2 ); list.add( svcInst3 ); list.add( svcInst1 ); Collections.sort( list ); Assert.assertTrue( "First element in the list should be svcInst1", list.get( 0 ).equals( svcInst1 ) ); Assert.assertTrue( "Second element in the list should be svcInst2", list.get( 1 ).equals( svcInst2 ) ); Assert.assertTrue( "Third element in the list should be svcInst3", list.get( 2 ).equals( svcInst3 ) ); }