@Test public void testValidCreate() throws URISyntaxException { setRealms(REALM_ID); EntityBody body = getValidRoleDoc(); mockGetRealmId(); Mockito.when(service.create(body)).thenReturn("new-role-id"); Response res = resource.createCustomRole(body, getMockUriInfo()); Assert.assertEquals(201, res.getStatus()); }
@POST @RightsAllowed({Right.CRUD_ROLE }) public Response createCustomRole(EntityBody newCustomRole, @Context final UriInfo uriInfo) { String realmId = (String) newCustomRole.get("realmId"); Response res = validateRights(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role rights validation failed.",realmId); return res; } res = validateUniqueRoles(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role unique roles check failed.",realmId); return res; } res = validateValidRealm(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role invalid realm specified.",realmId); return res; } NeutralQuery existingCustomRoleQuery = new NeutralQuery(); existingCustomRoleQuery.addCriteria(new NeutralCriteria("realmId", NeutralCriteria.OPERATOR_EQUAL, realmId)); Entity existingRoleDoc = repo.findOne(RESOURCE_NAME, existingCustomRoleQuery); if (existingRoleDoc != null) { auditSecEvent(uriInfo, "Failed to create custom role Already exists.",realmId); return buildBadRequest(ERROR_MULTIPLE_DOCS + ": Realm '" + realmId + "'"); } String id = service.create(newCustomRole); if (id != null) { String uri = uriToString(uriInfo) + "/" + id; auditSecEvent(uriInfo, "Created custom role with id: " + id,realmId); this.sessions.clear(); return Response.status(Status.CREATED).header("Location", uri).build(); } return Response.status(Status.BAD_REQUEST).build(); }
CustomRoleResource { @POST @RightsAllowed({Right.CRUD_ROLE }) public Response createCustomRole(EntityBody newCustomRole, @Context final UriInfo uriInfo) { String realmId = (String) newCustomRole.get("realmId"); Response res = validateRights(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role rights validation failed.",realmId); return res; } res = validateUniqueRoles(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role unique roles check failed.",realmId); return res; } res = validateValidRealm(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role invalid realm specified.",realmId); return res; } NeutralQuery existingCustomRoleQuery = new NeutralQuery(); existingCustomRoleQuery.addCriteria(new NeutralCriteria("realmId", NeutralCriteria.OPERATOR_EQUAL, realmId)); Entity existingRoleDoc = repo.findOne(RESOURCE_NAME, existingCustomRoleQuery); if (existingRoleDoc != null) { auditSecEvent(uriInfo, "Failed to create custom role Already exists.",realmId); return buildBadRequest(ERROR_MULTIPLE_DOCS + ": Realm '" + realmId + "'"); } String id = service.create(newCustomRole); if (id != null) { String uri = uriToString(uriInfo) + "/" + id; auditSecEvent(uriInfo, "Created custom role with id: " + id,realmId); this.sessions.clear(); return Response.status(Status.CREATED).header("Location", uri).build(); } return Response.status(Status.BAD_REQUEST).build(); } }
CustomRoleResource { @POST @RightsAllowed({Right.CRUD_ROLE }) public Response createCustomRole(EntityBody newCustomRole, @Context final UriInfo uriInfo) { String realmId = (String) newCustomRole.get("realmId"); Response res = validateRights(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role rights validation failed.",realmId); return res; } res = validateUniqueRoles(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role unique roles check failed.",realmId); return res; } res = validateValidRealm(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role invalid realm specified.",realmId); return res; } NeutralQuery existingCustomRoleQuery = new NeutralQuery(); existingCustomRoleQuery.addCriteria(new NeutralCriteria("realmId", NeutralCriteria.OPERATOR_EQUAL, realmId)); Entity existingRoleDoc = repo.findOne(RESOURCE_NAME, existingCustomRoleQuery); if (existingRoleDoc != null) { auditSecEvent(uriInfo, "Failed to create custom role Already exists.",realmId); return buildBadRequest(ERROR_MULTIPLE_DOCS + ": Realm '" + realmId + "'"); } String id = service.create(newCustomRole); if (id != null) { String uri = uriToString(uriInfo) + "/" + id; auditSecEvent(uriInfo, "Created custom role with id: " + id,realmId); this.sessions.clear(); return Response.status(Status.CREATED).header("Location", uri).build(); } return Response.status(Status.BAD_REQUEST).build(); }  }
CustomRoleResource { @POST @RightsAllowed({Right.CRUD_ROLE }) public Response createCustomRole(EntityBody newCustomRole, @Context final UriInfo uriInfo) { String realmId = (String) newCustomRole.get("realmId"); Response res = validateRights(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role rights validation failed.",realmId); return res; } res = validateUniqueRoles(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role unique roles check failed.",realmId); return res; } res = validateValidRealm(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role invalid realm specified.",realmId); return res; } NeutralQuery existingCustomRoleQuery = new NeutralQuery(); existingCustomRoleQuery.addCriteria(new NeutralCriteria("realmId", NeutralCriteria.OPERATOR_EQUAL, realmId)); Entity existingRoleDoc = repo.findOne(RESOURCE_NAME, existingCustomRoleQuery); if (existingRoleDoc != null) { auditSecEvent(uriInfo, "Failed to create custom role Already exists.",realmId); return buildBadRequest(ERROR_MULTIPLE_DOCS + ": Realm '" + realmId + "'"); } String id = service.create(newCustomRole); if (id != null) { String uri = uriToString(uriInfo) + "/" + id; auditSecEvent(uriInfo, "Created custom role with id: " + id,realmId); this.sessions.clear(); return Response.status(Status.CREATED).header("Location", uri).build(); } return Response.status(Status.BAD_REQUEST).build(); }  @PostConstruct void init(); @GET @RightsAllowed({Right.CRUD_ROLE }) Response readAll(@Context final UriInfo uriInfo,
            @DefaultValue("") @QueryParam("realmId") String realmId); @GET @Path("{id}") @RightsAllowed({Right.CRUD_ROLE }) Response read(@PathParam("id") String id, @Context final UriInfo uriInfo); @POST @RightsAllowed({Right.CRUD_ROLE }) Response createCustomRole(EntityBody newCustomRole, @Context final UriInfo uriInfo); @PUT @Path("{id}") @RightsAllowed({Right.CRUD_ROLE }) Response updateCustomRole(@PathParam("id") String id, EntityBody updated, @Context final UriInfo uriInfo); @DELETE @Path("{id}") @RightsAllowed({Right.CRUD_ROLE }) Response deleteCustomRole(@PathParam("id") String id, @Context final UriInfo uriInfo); }
CustomRoleResource { @POST @RightsAllowed({Right.CRUD_ROLE }) public Response createCustomRole(EntityBody newCustomRole, @Context final UriInfo uriInfo) { String realmId = (String) newCustomRole.get("realmId"); Response res = validateRights(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role rights validation failed.",realmId); return res; } res = validateUniqueRoles(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role unique roles check failed.",realmId); return res; } res = validateValidRealm(newCustomRole); if (res != null) { auditSecEvent(uriInfo, "Failed to create custom role invalid realm specified.",realmId); return res; } NeutralQuery existingCustomRoleQuery = new NeutralQuery(); existingCustomRoleQuery.addCriteria(new NeutralCriteria("realmId", NeutralCriteria.OPERATOR_EQUAL, realmId)); Entity existingRoleDoc = repo.findOne(RESOURCE_NAME, existingCustomRoleQuery); if (existingRoleDoc != null) { auditSecEvent(uriInfo, "Failed to create custom role Already exists.",realmId); return buildBadRequest(ERROR_MULTIPLE_DOCS + ": Realm '" + realmId + "'"); } String id = service.create(newCustomRole); if (id != null) { String uri = uriToString(uriInfo) + "/" + id; auditSecEvent(uriInfo, "Created custom role with id: " + id,realmId); this.sessions.clear(); return Response.status(Status.CREATED).header("Location", uri).build(); } return Response.status(Status.BAD_REQUEST).build(); }  @PostConstruct void init(); @GET @RightsAllowed({Right.CRUD_ROLE }) Response readAll(@Context final UriInfo uriInfo,
            @DefaultValue("") @QueryParam("realmId") String realmId); @GET @Path("{id}") @RightsAllowed({Right.CRUD_ROLE }) Response read(@PathParam("id") String id, @Context final UriInfo uriInfo); @POST @RightsAllowed({Right.CRUD_ROLE }) Response createCustomRole(EntityBody newCustomRole, @Context final UriInfo uriInfo); @PUT @Path("{id}") @RightsAllowed({Right.CRUD_ROLE }) Response updateCustomRole(@PathParam("id") String id, EntityBody updated, @Context final UriInfo uriInfo); @DELETE @Path("{id}") @RightsAllowed({Right.CRUD_ROLE }) Response deleteCustomRole(@PathParam("id") String id, @Context final UriInfo uriInfo); static final String RESOURCE_NAME; }