@Test public void registerWebPushVariantShouldReturn400WhenVariantModelIsNotValid() { final WebPushVariant variant = new WebPushVariant(); final ResteasyUriInfo uriInfo = new ResteasyUriInfo("http: final PushApplication pushApp = new PushApplication(); when(searchService.findByPushApplicationIDForDeveloper("push-app-id")).thenReturn(pushApp); when(validator.validate(variant)).thenReturn(Sets.newHashSet(sampleViolation)); final Response response = this.endpoint.registerWebPushVariant(variant, "push-app-id", uriInfo); assertEquals(response.getStatus(), 400); verify(variantService, never()).addVariant(variant); verify(pushAppService, never()).addVariant(pushApp, variant); }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response registerWebPushVariant( WebPushVariant webPushVariant, @PathParam("pushAppID") String pushApplicationID, @Context UriInfo uriInfo) { PushApplication pushApp = getSearch().findByPushApplicationIDForDeveloper(pushApplicationID); if (pushApp == null) { return Response.status(Response.Status.NOT_FOUND).entity(ErrorBuilder.forPushApplications().notFound().build()).build(); } if (webPushVariant.getPrivateKey() == null && webPushVariant.getPublicKey() == null) { try { KeyPair kp = KeyUtils.generateKeyPair(); webPushVariant.setPrivateKey(Base64Encoder.encodeUrl(Utils.encode((ECPrivateKey) kp.getPrivate()))); webPushVariant.setPublicKey(Base64Encoder.encodeUrl(Utils.encode((ECPublicKey) kp.getPublic()))); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException exc) { logger.error("Unable to create WebPush variant: error creating vapid keys", exc); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ErrorBuilder.forVariants().withDetail("vapid-keys", "Unable to generate vapid keys").build()).build(); } } try { validateModelClass(webPushVariant); } catch (ConstraintViolationException cve) { logger.trace("Unable to create WebPush variant"); Response.ResponseBuilder builder = createBadRequestResponse(cve.getConstraintViolations()); return builder.build(); } logger.trace("Register WebPush variant with Push Application '{}'", pushApplicationID); variantService.addVariant(webPushVariant); pushAppService.addVariant(pushApp, webPushVariant); return Response.created(uriInfo.getAbsolutePathBuilder().path(String.valueOf(webPushVariant.getVariantID())).build()).entity(webPushVariant).build(); }
WebPushVariantEndpoint extends AbstractVariantEndpoint<WebPushVariant> { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response registerWebPushVariant( WebPushVariant webPushVariant, @PathParam("pushAppID") String pushApplicationID, @Context UriInfo uriInfo) { PushApplication pushApp = getSearch().findByPushApplicationIDForDeveloper(pushApplicationID); if (pushApp == null) { return Response.status(Response.Status.NOT_FOUND).entity(ErrorBuilder.forPushApplications().notFound().build()).build(); } if (webPushVariant.getPrivateKey() == null && webPushVariant.getPublicKey() == null) { try { KeyPair kp = KeyUtils.generateKeyPair(); webPushVariant.setPrivateKey(Base64Encoder.encodeUrl(Utils.encode((ECPrivateKey) kp.getPrivate()))); webPushVariant.setPublicKey(Base64Encoder.encodeUrl(Utils.encode((ECPublicKey) kp.getPublic()))); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException exc) { logger.error("Unable to create WebPush variant: error creating vapid keys", exc); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ErrorBuilder.forVariants().withDetail("vapid-keys", "Unable to generate vapid keys").build()).build(); } } try { validateModelClass(webPushVariant); } catch (ConstraintViolationException cve) { logger.trace("Unable to create WebPush variant"); Response.ResponseBuilder builder = createBadRequestResponse(cve.getConstraintViolations()); return builder.build(); } logger.trace("Register WebPush variant with Push Application '{}'", pushApplicationID); variantService.addVariant(webPushVariant); pushAppService.addVariant(pushApp, webPushVariant); return Response.created(uriInfo.getAbsolutePathBuilder().path(String.valueOf(webPushVariant.getVariantID())).build()).entity(webPushVariant).build(); } }
WebPushVariantEndpoint extends AbstractVariantEndpoint<WebPushVariant> { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response registerWebPushVariant( WebPushVariant webPushVariant, @PathParam("pushAppID") String pushApplicationID, @Context UriInfo uriInfo) { PushApplication pushApp = getSearch().findByPushApplicationIDForDeveloper(pushApplicationID); if (pushApp == null) { return Response.status(Response.Status.NOT_FOUND).entity(ErrorBuilder.forPushApplications().notFound().build()).build(); } if (webPushVariant.getPrivateKey() == null && webPushVariant.getPublicKey() == null) { try { KeyPair kp = KeyUtils.generateKeyPair(); webPushVariant.setPrivateKey(Base64Encoder.encodeUrl(Utils.encode((ECPrivateKey) kp.getPrivate()))); webPushVariant.setPublicKey(Base64Encoder.encodeUrl(Utils.encode((ECPublicKey) kp.getPublic()))); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException exc) { logger.error("Unable to create WebPush variant: error creating vapid keys", exc); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ErrorBuilder.forVariants().withDetail("vapid-keys", "Unable to generate vapid keys").build()).build(); } } try { validateModelClass(webPushVariant); } catch (ConstraintViolationException cve) { logger.trace("Unable to create WebPush variant"); Response.ResponseBuilder builder = createBadRequestResponse(cve.getConstraintViolations()); return builder.build(); } logger.trace("Register WebPush variant with Push Application '{}'", pushApplicationID); variantService.addVariant(webPushVariant); pushAppService.addVariant(pushApp, webPushVariant); return Response.created(uriInfo.getAbsolutePathBuilder().path(String.valueOf(webPushVariant.getVariantID())).build()).entity(webPushVariant).build(); } WebPushVariantEndpoint();   WebPushVariantEndpoint(Validator validator, SearchManager searchManager); }
WebPushVariantEndpoint extends AbstractVariantEndpoint<WebPushVariant> { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response registerWebPushVariant( WebPushVariant webPushVariant, @PathParam("pushAppID") String pushApplicationID, @Context UriInfo uriInfo) { PushApplication pushApp = getSearch().findByPushApplicationIDForDeveloper(pushApplicationID); if (pushApp == null) { return Response.status(Response.Status.NOT_FOUND).entity(ErrorBuilder.forPushApplications().notFound().build()).build(); } if (webPushVariant.getPrivateKey() == null && webPushVariant.getPublicKey() == null) { try { KeyPair kp = KeyUtils.generateKeyPair(); webPushVariant.setPrivateKey(Base64Encoder.encodeUrl(Utils.encode((ECPrivateKey) kp.getPrivate()))); webPushVariant.setPublicKey(Base64Encoder.encodeUrl(Utils.encode((ECPublicKey) kp.getPublic()))); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException exc) { logger.error("Unable to create WebPush variant: error creating vapid keys", exc); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ErrorBuilder.forVariants().withDetail("vapid-keys", "Unable to generate vapid keys").build()).build(); } } try { validateModelClass(webPushVariant); } catch (ConstraintViolationException cve) { logger.trace("Unable to create WebPush variant"); Response.ResponseBuilder builder = createBadRequestResponse(cve.getConstraintViolations()); return builder.build(); } logger.trace("Register WebPush variant with Push Application '{}'", pushApplicationID); variantService.addVariant(webPushVariant); pushAppService.addVariant(pushApp, webPushVariant); return Response.created(uriInfo.getAbsolutePathBuilder().path(String.valueOf(webPushVariant.getVariantID())).build()).entity(webPushVariant).build(); } WebPushVariantEndpoint();   WebPushVariantEndpoint(Validator validator, SearchManager searchManager); @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response registerWebPushVariant(
            WebPushVariant webPushVariant,
            @PathParam("pushAppID") String pushApplicationID,
            @Context UriInfo uriInfo); @GET @Produces(MediaType.APPLICATION_JSON) Response listAllWebPushVariationsForPushApp(@PathParam("pushAppID") String pushApplicationID); @PUT @Path("/{webPushID}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response updateWebPushVariant(@PathParam("pushAppID") String pushApplicationID,
                                         @PathParam("webPushID") String webPushID, WebPushVariant updatedVariant); @PUT @Path("/{variantId}/renew") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response renewKeys(@PathParam("pushAppID") String pushApplicationID,
                                @PathParam("variantId") String variantId); }
WebPushVariantEndpoint extends AbstractVariantEndpoint<WebPushVariant> { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response registerWebPushVariant( WebPushVariant webPushVariant, @PathParam("pushAppID") String pushApplicationID, @Context UriInfo uriInfo) { PushApplication pushApp = getSearch().findByPushApplicationIDForDeveloper(pushApplicationID); if (pushApp == null) { return Response.status(Response.Status.NOT_FOUND).entity(ErrorBuilder.forPushApplications().notFound().build()).build(); } if (webPushVariant.getPrivateKey() == null && webPushVariant.getPublicKey() == null) { try { KeyPair kp = KeyUtils.generateKeyPair(); webPushVariant.setPrivateKey(Base64Encoder.encodeUrl(Utils.encode((ECPrivateKey) kp.getPrivate()))); webPushVariant.setPublicKey(Base64Encoder.encodeUrl(Utils.encode((ECPublicKey) kp.getPublic()))); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException exc) { logger.error("Unable to create WebPush variant: error creating vapid keys", exc); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ErrorBuilder.forVariants().withDetail("vapid-keys", "Unable to generate vapid keys").build()).build(); } } try { validateModelClass(webPushVariant); } catch (ConstraintViolationException cve) { logger.trace("Unable to create WebPush variant"); Response.ResponseBuilder builder = createBadRequestResponse(cve.getConstraintViolations()); return builder.build(); } logger.trace("Register WebPush variant with Push Application '{}'", pushApplicationID); variantService.addVariant(webPushVariant); pushAppService.addVariant(pushApp, webPushVariant); return Response.created(uriInfo.getAbsolutePathBuilder().path(String.valueOf(webPushVariant.getVariantID())).build()).entity(webPushVariant).build(); } WebPushVariantEndpoint();   WebPushVariantEndpoint(Validator validator, SearchManager searchManager); @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response registerWebPushVariant(
            WebPushVariant webPushVariant,
            @PathParam("pushAppID") String pushApplicationID,
            @Context UriInfo uriInfo); @GET @Produces(MediaType.APPLICATION_JSON) Response listAllWebPushVariationsForPushApp(@PathParam("pushAppID") String pushApplicationID); @PUT @Path("/{webPushID}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response updateWebPushVariant(@PathParam("pushAppID") String pushApplicationID,
                                         @PathParam("webPushID") String webPushID, WebPushVariant updatedVariant); @PUT @Path("/{variantId}/renew") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response renewKeys(@PathParam("pushAppID") String pushApplicationID,
                                @PathParam("variantId") String variantId);  }