### Question:
UmaPolicyServiceImpl implements UmaPolicyService { @Override public Promise<UmaPolicy, ResourceException> createPolicy(final Context context, JsonValue policy) { final UmaPolicy umaPolicy; final ResourceSetDescription resourceSet; final String userId = contextHelper.getUserId(context); String realm = getRealm(context); try { String policyId = UmaPolicy.idOf(policy); resourceSet = getResourceSet(realm, policyId); umaPolicy = UmaPolicy.valueOf(resourceSet, resolveUsernameToUID(context, policy)); boolean canShare = canUserShareResourceSet(resourceSet.getResourceOwnerId(), userId, resourceSet.getClientId(), realm, resourceSet.getId(), umaPolicy.getScopes()); if (!canShare) { return new ForbiddenException().asPromise(); } validateScopes(resourceSet, umaPolicy.getScopes()); verifyPolicyDoesNotAlreadyExist(context, resourceSet); } catch (ResourceException e) { return e.asPromise(); } return beforeResourceShared(umaPolicy) .thenAsync(new AsyncFunction<UmaPolicy, List<ResourceResponse>, ResourceException>() { @Override public Promise<List<ResourceResponse>, ResourceException> apply(UmaPolicy umaPolicy) { return policyResourceDelegate.createPolicies(context, umaPolicy.asUnderlyingPolicies(userId)); } }) .thenAlways(afterResourceShared(umaPolicy)) .thenAsync(new UpdatePolicyGraphStatesFunction<List<ResourceResponse>>(resourceSet, context)) .thenAsync(new AuditAndProduceUmaPolicyFunction(resourceSet, context)); } @Inject  UmaPolicyServiceImpl(PolicyResourceDelegate policyResourceDelegate,
            ResourceSetStoreFactory resourceSetStoreFactory, Config<UmaAuditLogger> auditLogger,
            ContextHelper contextHelper, UmaPolicyEvaluatorFactory policyEvaluatorFactory,
            CoreServicesWrapper coreServicesWrapper, @Named("frRest") Debug debug,
            UmaSettingsFactory umaSettingsFactory, ExtensionFilterManager extensionFilterManager); @Override Promise<UmaPolicy, ResourceException> createPolicy(final Context context, JsonValue policy); @Override Promise<UmaPolicy, ResourceException> readPolicy(final Context context, final String resourceSetId); @Override Promise<UmaPolicy, ResourceException> updatePolicy(final Context context, final String resourceSetId, //TODO need to check if need to delete backend policies
            JsonValue policy); @Override Promise<Void, ResourceException> deletePolicy(final Context context, final String resourceSetId); @Override Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> queryPolicies(final Context context,
            final QueryRequest umaQueryRequest);  }### Answer:
@Test(expectedExceptions = BadRequestException.class) public void shouldNotCreateUmaPolicyIfContainsInvalidRequestedScope() throws Exception { Context context = createContext(); JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID", "SCOPE_D"); policyService.createPolicy(context, policy).getOrThrowUninterruptibly(); verifyZeroInteractions(policyResourceDelegate); }
@Test(expectedExceptions = BadRequestException.class) public void shouldNotCreateUmaPolicyIfResourceSetNotFound() throws Exception { Context context = createContext(); JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID").put("policyId", "OTHER_ID"); policyService.createPolicy(context, policy).getOrThrowUninterruptibly(); verifyZeroInteractions(policyResourceDelegate); }
@Test(expectedExceptions = InternalServerErrorException.class) public void shouldNotCreateUmaPolicyWhenFailToReadResourceSet() throws Exception { Context context = createContext(); JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID").put("policyId", "FAILING_ID"); policyService.createPolicy(context, policy).getOrThrowUninterruptibly(); verifyZeroInteractions(policyResourceDelegate); }