ActivationKeyResource { @ApiOperation(notes = "Adds a Pool to an Activation Key", value = "Add Pool to Key") @ApiResponses({ @ApiResponse(code = 400, message = "")}) @POST @Path("{activation_key_id}/pools/{pool_id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.WILDCARD) public ActivationKeyDTO addPoolToKey( @PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId, @PathParam("pool_id") @Verify(Pool.class) String poolId, @QueryParam("quantity") Long quantity) { ActivationKey key = this.fetchActivationKey(activationKeyId); Pool pool = findPool(poolId); String message = activationKeyRules.validatePoolForActivationKey(key, pool, quantity); if (message != null) { throw new BadRequestException(message); } if (key.hasPool(pool)) { throw new BadRequestException( i18n.tr("Pool ID \"{0}\" has already been registered with this activation key", poolId) ); } key.addPool(pool, quantity); activationKeyCurator.update(key); return this.translator.translate(key, ActivationKeyDTO.class); } @Inject  ActivationKeyResource(ActivationKeyCurator activationKeyCurator, I18n i18n,
        PoolManager poolManager, ServiceLevelValidator serviceLevelValidator,
        ActivationKeyRules activationKeyRules, OwnerProductCurator ownerProductCurator,
        ProductCachedSerializationModule productCachedModule, ModelTranslator translator); @ApiOperation(notes = "Retrieves a single Activation Key", value = "Get Activation Key") @ApiResponses({ @ApiResponse(code = 400, message = "") }) @GET @Path("{activation_key_id}") @Produces(MediaType.APPLICATION_JSON) ActivationKeyDTO getActivationKey(
        @PathParam("activation_key_id")
        @Verify(ActivationKey.class) String activationKeyId); @ApiOperation(notes = "Retrieves a list of Pools based on the Activation Key", value = "Get Activation Key Pools", response = PoolDTO.class, responseContainer = "list") @ApiResponses({ @ApiResponse(code = 400, message = "")}) @GET @Path("{activation_key_id}/pools") @Produces(MediaType.APPLICATION_JSON) Iterator<PoolDTO> getActivationKeyPools(
        @PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId); @ApiOperation(notes = "Updates an Activation Key", value = "Update Activation Key") @ApiResponses({ @ApiResponse(code = 400, message = "") }) @PUT @Path("{activation_key_id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) ActivationKeyDTO updateActivationKey(
        @PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId,
        @ApiParam(name = "update", required = true) ActivationKeyDTO update); @ApiOperation(notes = "Adds a Pool to an Activation Key", value = "Add Pool to Key") @ApiResponses({ @ApiResponse(code = 400, message = "")}) @POST @Path("{activation_key_id}/pools/{pool_id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.WILDCARD) ActivationKeyDTO addPoolToKey(
        @PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId,
        @PathParam("pool_id") @Verify(Pool.class) String poolId,
        @QueryParam("quantity") Long quantity); @ApiOperation(notes = "Removes a Pool from an Activation Key", value = "Remove Pool From Key") @ApiResponses({ @ApiResponse(code = 400, message = "")}) @DELETE @Path("{activation_key_id}/pools/{pool_id}") @Produces(MediaType.APPLICATION_JSON) ActivationKeyDTO removePoolFromKey(
        @PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId,
        @PathParam("pool_id")
        @Verify(Pool.class) String poolId); @ApiOperation(notes = "Adds an Product ID to an Activation Key", value = "Add Product ID to key") @ApiResponses({ @ApiResponse(code = 400, message = "")}) @POST @Path("{activation_key_id}/product/{product_id}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.WILDCARD) ActivationKeyDTO addProductIdToKey(
        @PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId,
        @PathParam("product_id") String productId); @ApiOperation(notes = "Removes a Product ID from an Activation Key", value = "Remove Product Id from Key") @ApiResponses({ @ApiResponse(code = 400, message = "") }) @DELETE @Path("{activation_key_id}/product/{product_id}") @Produces(MediaType.APPLICATION_JSON) ActivationKeyDTO removeProductIdFromKey(
        @PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId,
        @PathParam("product_id") String productId); @ApiOperation(notes = "Retrieves a list of Activation Keys", value = "findActivationKey", response = ActivationKeyDTO.class, responseContainer = "list") @GET @Produces(MediaType.APPLICATION_JSON) CandlepinQuery<ActivationKeyDTO> findActivationKey(); @ApiOperation(notes = "Removes an Activation Key", value = "deleteActivationKey") @ApiResponses({ @ApiResponse(code = 400, message = "") }) @DELETE @Path("{activation_key_id}") @Produces(MediaType.APPLICATION_JSON) void deleteActivationKey(
        @PathParam("activation_key_id")
        @Verify(ActivationKey.class) String activationKeyId);  }
@Test public void testReaddingPools() { ActivationKey key = new ActivationKey(); Product product = this.createProduct(owner); Pool pool = createPool(owner, product, 10L, new Date(), new Date()); key.setOwner(owner); key.setName("dd"); key = activationKeyCurator.create(key); assertNotNull(key.getId()); activationKeyResource.addPoolToKey(key.getId(), pool.getId(), 1L); assertTrue(key.getPools().size() == 1); ActivationKey finalKey = key; assertThrows(BadRequestException.class, () -> activationKeyResource.addPoolToKey(finalKey.getId(), pool.getId(), 1L) ); }
@Test public void testActivationKeyWithNonMultiPool() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); Pool p = genPool(); p.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "no"); PoolManager poolManager = mock(PoolManager.class); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool"))).thenReturn(p); ActivationKeyResource akr = new ActivationKeyResource( akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); assertThrows(BadRequestException.class, () -> akr.addPoolToKey("testKey", "testPool", 2L) ); }
@Test public void testActivationKeyWithNegPoolQuantity() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); Pool p = genPool(); p.setQuantity(10L); PoolManager poolManager = mock(PoolManager.class); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool"))).thenReturn(p); ActivationKeyResource akr = new ActivationKeyResource( akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); assertThrows(BadRequestException.class, () -> akr.addPoolToKey("testKey", "testPool", -3L) ); }
@Test public void testActivationKeyWithLargePoolQuantity() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); Pool p = genPool(); p.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); p.setQuantity(10L); PoolManager poolManager = mock(PoolManager.class); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool"))).thenReturn(p); ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); akr.addPoolToKey("testKey", "testPool", 15L); }
@Test public void testActivationKeyWithUnlimitedPool() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); Pool p = genPool(); p.getProduct().setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes"); p.setQuantity(-1L); PoolManager poolManager = mock(PoolManager.class); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool"))).thenReturn(p); ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); akr.addPoolToKey("testKey", "testPool", 15L); }
@Test public void testActivationKeyWithPersonConsumerType() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); Pool p = genPool(); p.getProduct().setAttribute(Pool.Attributes.REQUIRES_CONSUMER_TYPE, "person"); p.setQuantity(1L); PoolManager poolManager = mock(PoolManager.class); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool"))).thenReturn(p); ActivationKeyResource akr = new ActivationKeyResource( akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); assertThrows(BadRequestException.class, () -> akr.addPoolToKey("testKey", "testPool", 1L) ); }
@Test public void testActivationKeyWithNonPersonConsumerType() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); Pool p = genPool(); p.getProduct().setAttribute(Pool.Attributes.REQUIRES_CONSUMER_TYPE, "candlepin"); PoolManager poolManager = mock(PoolManager.class); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool"))).thenReturn(p); ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); assertNotNull(akr.addPoolToKey("testKey", "testPool", 1L)); }
@Test public void testActivationKeyWithSameHostReqPools() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); PoolManager poolManager = mock(PoolManager.class); Pool p1 = genPool(); p1.setAttribute(Pool.Attributes.REQUIRES_HOST, "host1"); Pool p2 = genPool(); p2.setAttribute(Pool.Attributes.REQUIRES_HOST, "host1"); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool1"))).thenReturn(p1); when(poolManager.get(eq("testPool2"))).thenReturn(p2); ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); akr.addPoolToKey("testKey", "testPool1", 1L); assertEquals(1, ak.getPools().size()); Set<ActivationKeyPool> akPools = new HashSet<>(); akPools.add(new ActivationKeyPool(ak, p1, 1L)); ak.setPools(akPools); akr.addPoolToKey("testKey", "testPool2", 1L); assertEquals(2, ak.getPools().size()); }
@Test public void testActivationKeyWithDiffHostReqPools() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); PoolManager poolManager = mock(PoolManager.class); Pool p1 = genPool(); p1.setAttribute(Pool.Attributes.REQUIRES_HOST, "host1"); Pool p2 = genPool(); p2.setAttribute(Pool.Attributes.REQUIRES_HOST, "host2"); ak.addPool(p2, 1L); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool1"))).thenReturn(p1); ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); akr.addPoolToKey("testKey", "testPool1", 1L); }
@Test public void testActivationKeyHostReqPoolThenNonHostReq() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); PoolManager poolManager = mock(PoolManager.class); Pool p1 = genPool(); p1.setAttribute(Pool.Attributes.REQUIRES_HOST, "host1"); Pool p2 = genPool(); p2.setAttribute(Pool.Attributes.REQUIRES_HOST, ""); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool1"))).thenReturn(p1); when(poolManager.get(eq("testPool2"))).thenReturn(p2); ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); akr.addPoolToKey("testKey", "testPool1", 1L); assertEquals(1, ak.getPools().size()); ak.addPool(p1, 1L); akr.addPoolToKey("testKey", "testPool2", 1L); assertEquals(3, ak.getPools().size()); }
@Test public void testActivationKeyWithNullQuantity() { ActivationKey ak = genActivationKey(); ActivationKeyCurator akc = mock(ActivationKeyCurator.class); Pool p = genPool(); PoolManager poolManager = mock(PoolManager.class); when(akc.secureGet(eq("testKey"))).thenReturn(ak); when(poolManager.get(eq("testPool"))).thenReturn(p); ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator); akr.addPoolToKey("testKey", "testPool", null); }