ImmutablePolicy implements Policy { @Override public Policy setResourcesFor(final CharSequence label, final Resources resources) { final Label lbl = Label.of(label); checkNotNull(resources, "resources to set to the Policy entry"); final Map<Label, PolicyEntry> entriesCopy = copyEntries(); final PolicyEntry policyEntry = entriesCopy.get(lbl); final PolicyEntry modifiedEntry; if (null == policyEntry) { modifiedEntry = newPolicyEntry(lbl, PoliciesModelFactory.emptySubjects(), resources); } else { modifiedEntry = newPolicyEntry(lbl, policyEntry.getSubjects(), resources); } entriesCopy.put(lbl, modifiedEntry); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified, created); } private  ImmutablePolicy(@Nullable final PolicyId policyId,
            final Map<Label, PolicyEntry> theEntries,
            @Nullable final PolicyLifecycle lifecycle,
            @Nullable final PolicyRevision revision,
            @Nullable final Instant modified,
            @Nullable final Instant created); @Deprecated static Policy of(@Nullable final String policyId,
            @Nullable final PolicyLifecycle lifecycle,
            @Nullable final PolicyRevision revision,
            @Nullable final Instant modified,
            @Nullable final Instant created,
            final Iterable<PolicyEntry> entries); static Policy of(@Nullable final PolicyId policyId,
            @Nullable final PolicyLifecycle lifecycle,
            @Nullable final PolicyRevision revision,
            @Nullable final Instant modified,
            @Nullable final Instant created,
            final Iterable<PolicyEntry> entries); static Policy fromJson(final JsonObject jsonObject); @Override Optional<PolicyId> getEntityId(); @Override Optional<String> getNamespace(); @Override Optional<PolicyLifecycle> getLifecycle(); @Override Optional<PolicyRevision> getRevision(); @Override Optional<Instant> getModified(); @Override Optional<Instant> getCreated(); @Override Optional<Metadata> getMetadata(); @Override boolean isDeleted(); @Override Policy setEntry(final PolicyEntry policyEntry); @Override Set<Label> getLabels(); @Override boolean contains(final CharSequence label); @Override Optional<PolicyEntry> getEntryFor(final CharSequence label); @Override Policy removeEntry(final CharSequence label); @Override Policy removeEntry(final PolicyEntry entry); @Override Policy setSubjectsFor(final CharSequence label, final Subjects subjects); @Override Policy setSubjectFor(final CharSequence label, final Subject subject); @Override Policy removeSubjectFor(final CharSequence label, final SubjectId subjectId); @Override Policy setResourcesFor(final CharSequence label, final Resources resources); @Override Policy setResourceFor(final CharSequence label, final Resource resource); @Override Policy removeResourceFor(final CharSequence label, final ResourceKey resourceKey); @Override Optional<EffectedPermissions> getEffectedPermissionsFor(final CharSequence label, final SubjectId subjectId,
            final ResourceKey resourceKey); @Override boolean isEmpty(); @Override int getSize(); @Override Set<PolicyEntry> getEntriesSet(); @Override Stream<PolicyEntry> stream(); @Override Iterator<PolicyEntry> iterator(); @Override JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void setResourcesForWorks() { final Policy policy = createPolicy(); final EffectedPermissions DUMMY_PERMISSIONS = END_USER_EFFECTED_PERMISSIONS_1; final Resource NEW_RESOURCE_1 = Resource.newInstance(TestConstants.Policy.RESOURCE_TYPE, "/attributes", DUMMY_PERMISSIONS); final Resource NEW_RESOURCE_2 = Resource.newInstance(TestConstants.Policy.RESOURCE_TYPE, "/features", DUMMY_PERMISSIONS); final Resources NEW_RESOURCES = Resources.newInstance(NEW_RESOURCE_1, NEW_RESOURCE_2); final Policy policyModified = policy.setResourcesFor(END_USER_LABEL, NEW_RESOURCES); assertThat(policy).hasLabel(END_USER_LABEL); assertThat(policy).hasSubjectFor(END_USER_LABEL, END_USER_SUBJECT_ID_1); assertThat(policy).hasSubjectTypeFor(END_USER_LABEL, END_USER_SUBJECT_ID_1, END_USER_SUBJECT_TYPE_1); assertThat(policy).hasResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, END_USER_RESOURCE_1); assertThat(policy).doesNotHaveResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, NEW_RESOURCE_1.getPath()); assertThat(policy).doesNotHaveResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, NEW_RESOURCE_2.getPath()); assertThat(policyModified).hasLabel(END_USER_LABEL); assertThat(policyModified).hasSubjectFor(END_USER_LABEL, END_USER_SUBJECT_ID_1); assertThat(policyModified).hasSubjectTypeFor(END_USER_LABEL, END_USER_SUBJECT_ID_1, END_USER_SUBJECT_TYPE_1); assertThat(policyModified).doesNotHaveResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, END_USER_RESOURCE_1); assertThat(policyModified).hasResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, NEW_RESOURCE_1.getPath()); assertThat(policyModified).hasResourceFor(END_USER_LABEL, TestConstants.Policy.RESOURCE_TYPE, NEW_RESOURCE_2.getPath()); }