### Question:
ResourceSetLabelRegistration { void updateLabelsForExistingResourceSet(ResourceSetDescription resourceSet) { JsonValue newLabels = resourceSet.getDescription().get(OAuth2Constants.ResourceSets.LABELS); if (newLabels.isNull()) { newLabels = json(array()); } Collection<String> addedLabels = new HashSet<>(newLabels.asSet(String.class)); try { Set<ResourceSetLabel> labels = labelsStore.forResourceSet(resourceSet.getRealm(), resourceSet.getResourceOwnerId(), resourceSet.getId(), true); Collection<String> removedLabels = new HashSet<>(); for (ResourceSetLabel label : labels) { String labelName = label.getName().substring(label.getName().lastIndexOf("/") + 1); if (!addedLabels.remove(labelName)) { removedLabels.add(labelName); } } updateLabels(resourceSet, addedLabels, removedLabels); } catch (ResourceException e) { logger.error("Failed to find current labels on resource set: {}", resourceSet.getId(), e); } } @Inject  ResourceSetLabelRegistration(UmaLabelsStore labelsStore);   }### Answer:
@Test public void shouldUpdateLabelsForExistingResourceSet() throws Exception { givenLabelsForResourceSet("LABEL_ONE", "LABEL_TWO"); ResourceSetDescription resourceSet = newResourceSet("LABEL_ONE", "LABEL_THREE", "LABEL_FOUR"); givenLabelsExist("LABEL_ONE", "LABEL_TWO", "LABEL_THREE"); givenLabelsDoesNotExist("LABEL_FOUR"); labelRegistration.updateLabelsForExistingResourceSet(resourceSet); ArgumentCaptor<ResourceSetLabel> labelCaptor = ArgumentCaptor.forClass(ResourceSetLabel.class); verify(labelsStore, times(2)).update(eq("REALM"), eq("RESOURCE_OWNER_ID"), labelCaptor.capture()); verify(labelsStore).create(eq("REALM"), eq("RESOURCE_OWNER_ID"), labelCaptor.capture()); List<ResourceSetLabel> labels = labelCaptor.getAllValues(); for (ResourceSetLabel label : labels) { if (label.getId().contains("LABEL_TWO")) { assertThat(label.getResourceSetIds()).isEmpty(); } else if (label.getId().contains("LABEL_THREE")) { assertThat(label.getResourceSetIds()).containsOnly("RESOURCE_SET_ID"); } else if (label.getId().contains("LABEL_FOUR")) { assertThat(label.getResourceSetIds()).containsOnly("RESOURCE_SET_ID"); } } }
@Test public void shouldUpdateLabelsForExistingResourceSetWithAllLabelsRemoved() throws Exception { givenLabelsForResourceSet("LABEL_ONE", "LABEL_TWO"); ResourceSetDescription resourceSet = newResourceSet(); givenLabelsExist("LABEL_ONE", "LABEL_TWO"); labelRegistration.updateLabelsForExistingResourceSet(resourceSet); ArgumentCaptor<ResourceSetLabel> labelCaptor = ArgumentCaptor.forClass(ResourceSetLabel.class); verify(labelsStore, times(2)).update(eq("REALM"), eq("RESOURCE_OWNER_ID"), labelCaptor.capture()); List<ResourceSetLabel> labels = labelCaptor.getAllValues(); for (ResourceSetLabel label : labels) { assertThat(label.getResourceSetIds()).isEmpty(); } }