use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.
the class ConsumerResourceTest method testBindByPoolBadConsumerUuid.
@Test(expected = NotFoundException.class)
public void testBindByPoolBadConsumerUuid() throws Exception {
when(mockConsumerCurator.verifyAndLookupConsumerWithEntitlements(any(String.class))).thenThrow(new NotFoundException(""));
ConsumerResource consumerResource = new ConsumerResource(mockConsumerCurator, mockConsumerTypeCurator, null, null, null, null, null, null, i18n, null, null, null, null, null, null, null, null, null, null, null, null, null, null, this.config, null, null, null, consumerBindUtil, null, null, this.factValidator, null, consumerEnricher, migrationProvider, translator);
Consumer c = createConsumer(createOwner());
when(mockConsumerCurator.verifyAndLookupConsumerWithEntitlements(eq(c.getUuid()))).thenReturn(c);
consumerResource.bind(c.getUuid(), "fake pool uuid", null, null, null, null, false, null, null);
}
use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.
the class OwnerContentResourceTest method updateContentThatDoesntExist.
@Test(expected = NotFoundException.class)
public void updateContentThatDoesntExist() {
Owner owner = this.createOwner("test_owner");
ContentDTO cdto = TestUtil.createContentDTO("test_content", "updated_name");
assertNull(this.ownerContentCurator.getContentById(owner, cdto.getId()));
try {
this.ownerContentResource.updateContent(owner.getKey(), cdto.getId(), cdto);
} catch (NotFoundException e) {
assertNull(this.ownerContentCurator.getContentById(owner, cdto.getId()));
throw e;
}
}
use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.
the class OwnerResource method updatePool.
/**
* Updates a pool for an Owner.
* assumes this is a normal pool, and errors out otherwise cause we cannot
* create master pools from bonus pools
*
* @httpcode 404
* @httpcode 200
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/pools")
@ApiOperation(notes = "Updates a pool for an Owner. assumes this is a normal pool, and " + "errors out otherwise cause we cannot create master pools from bonus pools ", value = "Update Pool")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public void updatePool(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @ApiParam(name = "pool", required = true) PoolDTO newPoolDTO) {
Pool currentPool = this.poolManager.find(newPoolDTO.getId());
if (currentPool == null) {
throw new NotFoundException(i18n.tr("Unable to find a pool with the ID \"{0}\"", newPoolDTO.getId()));
}
// potential security concerns.
if (currentPool.getOwner() == null || !ownerKey.equals(currentPool.getOwner().getKey())) {
throw new NotFoundException(i18n.tr("Pool \"{0}\" does not belong to the specified owner \"{1}\"", currentPool.getId(), ownerKey));
}
// Verify the pool type is one that allows modifications
if (currentPool.getType() != PoolType.NORMAL) {
throw new BadRequestException(i18n.tr("Cannot update bonus pools, as they are auto generated"));
}
if (currentPool.isCreatedByShare()) {
throw new BadRequestException(i18n.tr("Cannot update shared pools, This should be triggered " + "by updating the share entitlement instead"));
}
Pool newPool = new Pool();
this.populateEntity(newPool, newPoolDTO);
// Owner & id have already been validated/resolved.
newPool.setOwner(currentPool.getOwner());
newPool.setId(newPoolDTO.getId());
/* These are @JsonIgnored. If a client creates a pool and subsequently
* wants to update it , we need to ensure Products are set appropriately.
*/
newPool.setProduct(currentPool.getProduct());
newPool.setDerivedProduct(currentPool.getDerivedProduct());
// Forcefully set fields we don't allow the client to change.
newPool.setCreatedByShare(currentPool.isCreatedByShare());
newPool.setHasSharedAncestor(currentPool.hasSharedAncestor());
newPool.setSourceSubscription(currentPool.getSourceSubscription());
newPool.setContractNumber(currentPool.getContractNumber());
newPool.setAccountNumber(currentPool.getAccountNumber());
newPool.setOrderNumber(currentPool.getOrderNumber());
newPool.setUpstreamPoolId(currentPool.getUpstreamPoolId());
newPool.setUpstreamEntitlementId(currentPool.getUpstreamEntitlementId());
newPool.setUpstreamConsumerId(currentPool.getUpstreamConsumerId());
newPool.setCertificate(currentPool.getCertificate());
newPool.setCdn(currentPool.getCdn());
// Update fields the client has changed, or otherwise set the current value.
if (newPoolDTO.getStartDate() == null) {
newPool.setStartDate(currentPool.getStartDate());
}
if (newPoolDTO.getEndDate() == null) {
newPool.setEndDate(currentPool.getEndDate());
}
if (newPoolDTO.getQuantity() == null) {
newPool.setQuantity(currentPool.getQuantity());
}
if (newPoolDTO.getAttributes() == null) {
newPool.setAttributes(currentPool.getAttributes());
}
if (newPoolDTO.getProvidedProducts() == null) {
newPool.setProvidedProducts(currentPool.getProvidedProducts());
}
if (newPoolDTO.getDerivedProvidedProducts() == null) {
newPool.setDerivedProvidedProducts(currentPool.getDerivedProvidedProducts());
}
if (newPoolDTO.getBranding() == null) {
newPool.setBranding(currentPool.getBranding());
}
// Apply changes to the pool and its derived pools
this.poolManager.updateMasterPool(newPool);
}
use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.
the class OwnerResource method getUeberCertificate.
/**
* Retrieves the Ueber Entitlement Certificate
*
* @return an EntitlementCertificate object
* @httpcode 404
* @httpcode 200
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/uebercert")
@ApiOperation(notes = "Retrieves the Ueber Entitlement Certificate", value = "Get Ueber Entitlement Certificate")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public UeberCertificate getUeberCertificate(@Context Principal principal, @Verify(Owner.class) @PathParam("owner_key") String ownerKey) {
Owner owner = this.findOwnerByKey(ownerKey);
UeberCertificate ueberCert = ueberCertCurator.findForOwner(owner);
if (ueberCert == null) {
throw new NotFoundException(i18n.tr("uber certificate for owner {0} was not found. Please generate one.", owner.getKey()));
}
return ueberCert;
}
use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.
the class OwnerResource method listPools.
/**
* Retrieves a list of Pools for an Owner
*
* @param ownerKey id of the owner whose entitlement pools are sought.
* @param matches Find pools matching the given pattern in a variety of fields.
* * and ? wildcards are supported.
* @return a list of Pool objects
* @httpcode 400
* @httpcode 404
* @httpcode 200
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/pools")
@SuppressWarnings("checkstyle:indentation")
@ApiOperation(notes = "Retrieves a list of Pools for an Owner", value = "List Pools")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found"), @ApiResponse(code = 400, message = "Invalid request") })
public List<PoolDTO> listPools(@PathParam("owner_key") @Verify(value = Owner.class, subResource = SubResource.POOLS) String ownerKey, @QueryParam("consumer") String consumerUuid, @QueryParam("activation_key") String activationKeyName, @QueryParam("product") String productId, @QueryParam("subscription") String subscriptionId, @ApiParam("Include pools that are not suited to the unit's facts.") @QueryParam("listall") @DefaultValue("false") boolean listAll, @ApiParam("Date to use as current time for lookup criteria. Defaults" + " to current date if not specified.") @QueryParam("activeon") @DefaultValue(DateFormat.NOW) @DateFormat Date activeOn, @ApiParam("Find pools matching the given pattern in a variety of fields;" + " * and ? wildcards are supported; may be specified multiple times") @QueryParam("matches") List<String> matches, @ApiParam("The attributes to return based on the specified types.") @QueryParam("attribute") @CandlepinParam(type = KeyValueParameter.class) List<KeyValueParameter> attrFilters, @ApiParam("When set to true, it will add future dated pools to the result, " + "based on the activeon date.") @QueryParam("add_future") @DefaultValue("false") boolean addFuture, @ApiParam("When set to true, it will return only future dated pools to the result, " + "based on the activeon date.") @QueryParam("only_future") @DefaultValue("false") boolean onlyFuture, @ApiParam("Will only return pools with a start date after the supplied date. " + "Overrides the activeOn date.") @QueryParam("after") @DateFormat Date after, @ApiParam("One or more pool IDs to use to filter the output; only pools with IDs matching " + "those provided will be returned; may be specified multiple times") @QueryParam("poolid") List<String> poolIds, @Context Principal principal, @Context PageRequest pageRequest) {
Owner owner = findOwnerByKey(ownerKey);
Consumer c = null;
if (consumerUuid != null) {
c = consumerCurator.findByUuid(consumerUuid);
if (c == null) {
throw new NotFoundException(i18n.tr("Unit: {0} not found", consumerUuid));
}
if (!c.getOwnerId().equals(owner.getId())) {
throw new BadRequestException("Consumer specified does not belong to owner on path");
}
if (!principal.canAccess(c, SubResource.NONE, Access.READ_ONLY)) {
throw new ForbiddenException(i18n.tr("User {0} cannot access consumer {1}", principal.getPrincipalName(), c.getUuid()));
}
}
ActivationKey key = null;
if (activationKeyName != null) {
key = activationKeyCurator.lookupForOwner(activationKeyName, owner);
if (key == null) {
throw new BadRequestException(i18n.tr("ActivationKey with id {0} could not be found.", activationKeyName));
}
}
if (addFuture && onlyFuture) {
throw new BadRequestException(i18n.tr("The flags add_future and only_future cannot be used at the same time."));
}
if (after != null && (addFuture || onlyFuture)) {
throw new BadRequestException(i18n.tr("The flags add_future and only_future cannot be used with the parameter after."));
}
if (after != null) {
activeOn = null;
}
// Process the filters passed for the attributes
PoolFilterBuilder poolFilters = new PoolFilterBuilder();
for (KeyValueParameter filterParam : attrFilters) {
poolFilters.addAttributeFilter(filterParam.key(), filterParam.value());
}
if (matches != null) {
matches.stream().filter(elem -> elem != null && !elem.isEmpty()).forEach(elem -> poolFilters.addMatchesFilter(elem));
}
if (poolIds != null && !poolIds.isEmpty()) {
poolFilters.addIdFilters(poolIds);
}
Page<List<Pool>> page = poolManager.listAvailableEntitlementPools(c, key, owner.getId(), productId, subscriptionId, activeOn, listAll, poolFilters, pageRequest, addFuture, onlyFuture, after);
List<Pool> poolList = page.getPageData();
calculatedAttributesUtil.setCalculatedAttributes(poolList, activeOn);
calculatedAttributesUtil.setQuantityAttributes(poolList, c, activeOn);
// Store the page for the LinkHeaderResponseFilter
ResteasyProviderFactory.pushContext(Page.class, page);
List<PoolDTO> poolDTOs = new ArrayList<>();
for (Pool pool : poolList) {
poolDTOs.add(translator.translate(pool, PoolDTO.class));
}
return poolDTOs;
}
Aggregations