ThingHelper { public static boolean equals(Thing a, Thing b) { if (!a.getUID().equals(b.getUID())) { return false; } if (!Objects.equals(a.getBridgeUID(), b.getBridgeUID())) { return false; } if (!Objects.equals(a.getConfiguration(), b.getConfiguration())) { return false; } if (!Objects.equals(a.getLabel(), b.getLabel())) { return false; } if (!Objects.equals(a.getLocation(), b.getLocation())) { return false; } List<Channel> channelsOfA = a.getChannels(); List<Channel> channelsOfB = b.getChannels(); if (channelsOfA.size() != channelsOfB.size()) { return false; } if (!toString(channelsOfA).equals(toString(channelsOfB))) { return false; } return true; }  static boolean equals(Thing a, Thing b); static void addChannelsToThing(Thing thing, Collection<Channel> channels); static void ensureUniqueChannels(final Channel[] channels); static void ensureUniqueChannels(final Collection<Channel> channels); static void ensureUniqueChannels(final Collection<Channel> channels, final Channel channel); static Thing merge(Thing thing, ThingDTO updatedContents);  }
@Test public void twoTechnicalEqualThingInstancesAreDetectedAsEqual() { Thing thingA = ThingBuilder.create(THING_TYPE_UID, THING_UID) .withChannels( ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel1"), "itemType").build(), ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel2"), "itemType").build()) .withConfiguration(new Configuration()).build(); thingA.getConfiguration().put("prop1", "value1"); thingA.getConfiguration().put("prop2", "value2"); assertTrue(ThingHelper.equals(thingA, thingA)); Thing thingB = ThingBuilder.create(THING_TYPE_UID, THING_UID) .withChannels( ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel2"), "itemType").build(), ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel1"), "itemType").build()) .withConfiguration(new Configuration()).build(); thingB.getConfiguration().put("prop2", "value2"); thingB.getConfiguration().put("prop1", "value1"); assertTrue(ThingHelper.equals(thingA, thingB)); }
@Test public void twoThingsAreDifferentAfterPropertiesWereModified() { Thing thingA = ThingBuilder.create(THING_TYPE_UID, THING_UID) .withChannels( ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel1"), "itemType").build(), ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel2"), "itemType").build()) .withConfiguration(new Configuration()).build(); thingA.getConfiguration().put("prop1", "value1"); Thing thingB = ThingBuilder.create(THING_TYPE_UID, THING_UID) .withChannels( ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel2"), "itemType").build(), ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel1"), "itemType").build()) .withConfiguration(new Configuration()).build(); thingB.getConfiguration().put("prop1", "value1"); assertTrue(ThingHelper.equals(thingA, thingB)); thingB.getConfiguration().put("prop3", "value3"); assertFalse(ThingHelper.equals(thingA, thingB)); }
@Test public void twoThingsAreDifferentAfterChannelsWereModified() { Thing thingA = ThingBuilder.create(THING_TYPE_UID, THING_UID).withConfiguration(new Configuration()).build(); Thing thingB = ThingBuilder.create(THING_TYPE_UID, THING_UID).withConfiguration(new Configuration()).build(); assertTrue(ThingHelper.equals(thingA, thingB)); ((ThingImpl) thingB).setChannels( List.of(ChannelBuilder.create(new ChannelUID("binding:type:thingId:channel3"), "itemType3").build())); assertFalse(ThingHelper.equals(thingA, thingB)); }
@Test public void twoThingsAreDifferentAfterLabelWasModified() { Thing thingA = ThingBuilder.create(THING_TYPE_UID, THING_UID).withConfiguration(new Configuration()) .withLabel("foo").build(); Thing thingB = ThingBuilder.create(THING_TYPE_UID, THING_UID).withConfiguration(new Configuration()) .withLabel("foo").build(); assertTrue(ThingHelper.equals(thingA, thingB)); thingB.setLabel("bar"); assertFalse(ThingHelper.equals(thingA, thingB)); }
@Test public void twoThingsAreDifferentAfterLocationWasModified() { Thing thingA = ThingBuilder.create(THING_TYPE_UID, THING_UID).withConfiguration(new Configuration()) .withLocation("foo").build(); Thing thingB = ThingBuilder.create(THING_TYPE_UID, THING_UID).withConfiguration(new Configuration()) .withLocation("foo").build(); assertTrue(ThingHelper.equals(thingA, thingB)); thingB.setLocation("bar"); assertFalse(ThingHelper.equals(thingA, thingB)); }