EntitlementResource { @ApiOperation(notes = "Retrieves a Subscription Certificate. We can't return CdnInfo " + "at this time, but when the time comes this is the implementation we want to start" + " from. It will require changes to thumbslug. will also" + " @Produces(MediaType.APPLICATION_JSON)", value = "getUpstreamCert") @ApiResponses({ @ApiResponse(code = 404, message = "") }) @GET @Produces(MediaType.TEXT_PLAIN) @Path("{dbid}/upstream_cert") public String getUpstreamCert( @PathParam("dbid") String entitlementId) { Entitlement ent = entitlementCurator.get(entitlementId); if (ent == null) { throw new NotFoundException(i18n.tr( "Entitlement with ID \"{0}\" could not be found.", entitlementId)); } Pool entPool = ent.getPool(); if (!StringUtils.isBlank(entPool.getSourceStackId())) { log.debug("Entitlement is from a stack derived pool, searching for oldest " + "active entitlements in source stack."); ent = entitlementCurator.findUpstreamEntitlementForStack( entPool.getSourceStack().getSourceConsumer(), entPool.getSourceStackId()); } if (ent == null || ent.getPool() == null || ent.getPool().getCertificate() == null) { throw new NotFoundException( i18n.tr("Unable to find upstream certificate for entitlement: {0}", entitlementId) ); } SubscriptionsCertificate cert = ent.getPool().getCertificate(); return cert.getCert() + cert.getKey(); } @Inject  EntitlementResource(EntitlementCurator entitlementCurator,
        ConsumerCurator consumerCurator,
        ConsumerTypeCurator consumerTypeCurator,
        PoolManager poolManager,
        I18n i18n,
        Entitler entitler,
        Enforcer enforcer,
        EntitlementRulesTranslator messageTranslator,
        JobManager jobManager,
        ModelTranslator translator); @ApiOperation(notes = "Checks Consumer for Product Entitlement", value = "hasEntitlement") @ApiResponses({ @ApiResponse(code = 404, message = "") }) @GET @Produces(MediaType.APPLICATION_JSON) @Path("consumer/{consumer_uuid}/product/{product_id}") EntitlementDTO hasEntitlement(@PathParam("consumer_uuid") String consumerUuid,
        @PathParam("product_id") String productId); @ApiOperation(notes = "Retrieves list of Entitlements", value = "listAllForConsumer") @ApiResponses({ @ApiResponse(code = 400, message = "") }) @GET @Produces(MediaType.APPLICATION_JSON) List<EntitlementDTO> listAllForConsumer(
        @QueryParam("consumer") String consumerUuid,
        @QueryParam("matches") String matches,
        @QueryParam("attribute") List<KeyValueParameter> attrFilters,
        @Context PageRequest pageRequest); @ApiOperation(notes = "Retrieves a single Entitlement", value = "getEntitlement") @ApiResponses({ @ApiResponse(code = 404, message = "") }) @GET @Produces(MediaType.APPLICATION_JSON) @Path("{entitlement_id}") EntitlementDTO getEntitlement(
        @PathParam("entitlement_id") @Verify(Entitlement.class) String entitlementId); @ApiOperation(notes = "Updates an Entitlement. This only works for the quantity.", value = "updateEntitlement") @ApiResponses({ @ApiResponse(code = 404, message = "") }) @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("{entitlement_id}") void updateEntitlement(
        @PathParam("entitlement_id") @Verify(Entitlement.class) String id,
        @ApiParam(name = "update", required = true) EntitlementDTO update); @ApiOperation(notes = "Retrieves a Subscription Certificate. We can't return CdnInfo " + "at this time, but when the time comes this is the implementation we want to start" + " from. It will require changes to thumbslug. will also" + " @Produces(MediaType.APPLICATION_JSON)", value = "getUpstreamCert") @ApiResponses({ @ApiResponse(code = 404, message = "") }) @GET @Produces(MediaType.TEXT_PLAIN) @Path("{dbid}/upstream_cert") String getUpstreamCert(
        @PathParam("dbid") String entitlementId); @ApiOperation(notes = "Deletes an Entitlement", value = "unbind") @ApiResponses({ @ApiResponse(code = 403, message = ""), @ApiResponse(code = 404, message = "") }) @DELETE @Produces(MediaType.WILDCARD) @Path("/{dbid}") void unbind(@PathParam("dbid") String dbid); @ApiOperation(notes = "Regenerates the Entitlement Certificates for a Product", value = "regenerateEntitlementCertificatesForProduct") @ApiResponses({ @ApiResponse(code = 202, message = "") }) @PUT @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.WILDCARD) @Path("product/{product_id}") AsyncJobStatusDTO regenerateEntitlementCertificatesForProduct(
        @PathParam("product_id") String productId,
        @QueryParam("lazy_regen") @DefaultValue("true") boolean lazyRegen); @ApiOperation(notes = "Migrate entitlements from one distributor consumer to another." + " Can specify full or partial quantity. No specified quantity " + "will lead to full migration of the entitlement.", value = "migrateEntitlement") @ApiResponses({ @ApiResponse(code = 404, message = "") }) @PUT @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.WILDCARD) @Path("{entitlement_id}/migrate") @Transactional Response migrateEntitlement(
        @PathParam("entitlement_id") @Verify(Entitlement.class) String id,
        @QueryParam("to_consumer") @Verify(Consumer.class) String uuid,
        @QueryParam("quantity") Integer quantity);  }
@Test public void getUpstreamCertSimple() { Entitlement e = TestUtil.createEntitlement(); e.setId("entitlementID"); SubscriptionsCertificate subcert = new SubscriptionsCertificate(); subcert.setCert("HELLO"); subcert.setKey("CERT"); e.getPool().setCertificate(subcert); when(entitlementCurator.get(eq(e.getId()))).thenReturn(e); String expected = "HELLOCERT"; String result = entResource.getUpstreamCert(e.getId()); assertEquals(expected, result); }
@Test public void getUpstreamCertSimpleNothingFound() { Entitlement e = TestUtil.createEntitlement(); e.setId("entitlementID"); e.getPool().setSourceSubscription(null); when(entitlementCurator.get(eq(e.getId()))).thenReturn(e); assertThrows(NotFoundException.class, () -> entResource.getUpstreamCert(e.getId())); }
@Test public void getUpstreamCertStackSubPool() { Entitlement parentEnt = TestUtil.createEntitlement(); parentEnt.setId("parentEnt"); SubscriptionsCertificate subcert = new SubscriptionsCertificate(); subcert.setCert("HELLO"); subcert.setKey("CERT"); parentEnt.getPool().setCertificate(subcert); when(entitlementCurator.findUpstreamEntitlementForStack(consumer, "mystack")) .thenReturn(parentEnt); String expected = "HELLOCERT"; Entitlement e = TestUtil.createEntitlement(); e.setId("entitlementID"); e.getPool().setSourceStack(new SourceStack(consumer, "mystack")); when(entitlementCurator.get(eq(e.getId()))).thenReturn(e); String result = entResource.getUpstreamCert(e.getId()); assertEquals(expected, result); }
@Test public void getUpstreamCertStackSubPoolNothingFound() { when(entitlementCurator.findUpstreamEntitlementForStack(consumer, "mystack")) .thenReturn(null); Entitlement e = TestUtil.createEntitlement(); e.setId("entitlementID"); e.getPool().setSourceStack(new SourceStack(consumer, "mystack")); when(entitlementCurator.get(eq(e.getId()))).thenReturn(e); assertThrows(NotFoundException.class, () -> entResource.getUpstreamCert(e.getId())); }