use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.
the class ConsumerBindUtil method handleActivationKeyPools.
private boolean handleActivationKeyPools(Consumer consumer, ActivationKey key) {
if (key.getPools().size() == 0) {
return true;
}
boolean onePassed = false;
List<ActivationKeyPool> toBind = new LinkedList<>();
for (ActivationKeyPool akp : key.getPools()) {
if (akp.getPool().getId() != null) {
toBind.add(akp);
}
}
// Sort pools before binding to avoid deadlocks
Collections.sort(toBind);
for (ActivationKeyPool akp : toBind) {
int quantity = (akp.getQuantity() == null) ? getQuantityToBind(akp.getPool(), consumer) : akp.getQuantity().intValue();
try {
entitler.sendEvents(entitler.bindByPoolQuantity(consumer, akp.getPool().getId(), quantity));
onePassed = true;
} catch (ForbiddenException e) {
log.warn(i18n.tr("Cannot bind to pool \"{0}\" in activation key \"{1}\": {2}", akp.getPool().getId(), akp.getKey().getName(), e.getMessage()));
}
}
return onePassed;
}
use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.
the class ActivationKeyTranslator method populate.
/**
* {@inheritDoc}
*/
@Override
public ActivationKeyDTO populate(ModelTranslator modelTranslator, ActivationKey source, ActivationKeyDTO dest) {
dest = super.populate(modelTranslator, source, dest);
dest.setId(source.getId()).setName(source.getName()).setDescription(source.getDescription()).setServiceLevel(source.getServiceLevel()).setAutoAttach(source.isAutoAttach());
// Process nested objects if we have a model translator to use to the translation...
if (modelTranslator != null) {
Owner owner = source.getOwner();
dest.setOwner(owner != null ? modelTranslator.translate(owner, OwnerDTO.class) : null);
Set<ActivationKeyPool> pools = source.getPools();
if (pools != null && !pools.isEmpty()) {
for (ActivationKeyPool poolEntry : pools) {
if (poolEntry != null) {
dest.addPool(new ActivationKeyDTO.ActivationKeyPoolDTO(poolEntry.getPool().getId(), poolEntry.getQuantity()));
}
}
} else {
dest.setPools(Collections.<ActivationKeyDTO.ActivationKeyPoolDTO>emptySet());
}
Set<Product> products = source.getProducts();
if (products != null && !products.isEmpty()) {
for (Product prod : products) {
if (prod != null) {
dest.addProductId(prod.getId());
}
}
} else {
dest.setProductIds(Collections.<String>emptySet());
}
Set<ActivationKeyContentOverride> overrides = source.getContentOverrides();
if (overrides != null && !overrides.isEmpty()) {
for (ActivationKeyContentOverride override : overrides) {
if (override != null) {
dest.addContentOverride(new ActivationKeyDTO.ActivationKeyContentOverrideDTO(override.getContentLabel(), override.getName(), override.getValue()));
}
}
} else {
dest.setContentOverrides(Collections.<ActivationKeyDTO.ActivationKeyContentOverrideDTO>emptySet());
}
Release release = source.getReleaseVer();
if (release != null) {
dest.setReleaseVersion(release.getReleaseVer());
}
}
return dest;
}
use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.
the class ActivationKeyTranslatorTest method verifyOutput.
@Override
protected void verifyOutput(ActivationKey source, ActivationKeyDTO dest, boolean childrenGenerated) {
if (source != null) {
assertEquals(source.getId(), dest.getId());
assertEquals(source.getName(), dest.getName());
assertEquals(source.getDescription(), dest.getDescription());
assertEquals(source.getServiceLevel(), dest.getServiceLevel());
assertEquals(source.isAutoAttach(), dest.isAutoAttach());
if (childrenGenerated) {
this.ownerTranslatorTest.verifyOutput(source.getOwner(), dest.getOwner(), true);
for (Product prod : source.getProducts()) {
for (String prodDto : dest.getProductIds()) {
assertNotNull(prodDto);
if (prodDto.equals(prod.getId())) {
// Nothing else to assert on, since prodDto only holds the product id.
}
}
}
for (ActivationKeyPool akPool : source.getPools()) {
for (ActivationKeyDTO.ActivationKeyPoolDTO akPoolDto : dest.getPools()) {
assertNotNull(akPoolDto);
if (akPoolDto.getPoolId().equals(akPool.getId())) {
assertEquals(akPool.getQuantity(), akPoolDto.getQuantity());
}
}
}
for (ActivationKeyContentOverride akOverride : source.getContentOverrides()) {
for (ActivationKeyDTO.ActivationKeyContentOverrideDTO akOverrideDto : dest.getContentOverrides()) {
assertNotNull(akOverrideDto);
if (akOverrideDto.getName().equals(akOverride.getName())) {
assertEquals(akOverrideDto.getContentLabel(), akOverride.getContentLabel());
assertEquals(akOverrideDto.getValue(), akOverride.getValue());
}
}
}
Release releaseSource = source.getReleaseVer();
String releaseDestination = dest.getReleaseVersion();
if (releaseSource != null) {
assertEquals(releaseSource.getReleaseVer(), releaseDestination);
} else {
assertNull(releaseDestination);
}
} else {
assertNull(dest.getOwner());
assertNull(dest.getProductIds());
assertNull(dest.getPools());
assertNull(dest.getContentOverrides());
assertNull(dest.getReleaseVersion());
}
} else {
assertNull(dest);
}
}
use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.
the class ActivationKeyResourceTest method testActivationKeyWithSameHostReqPools.
@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.verifyAndLookupKey(eq("testKey"))).thenReturn(ak);
when(poolManager.find(eq("testPool1"))).thenReturn(p1);
when(poolManager.find(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());
}
use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.
the class TestUtil method createActivationKey.
public static ActivationKey createActivationKey(Owner owner, List<Pool> pools) {
ActivationKey key = new ActivationKey();
key.setOwner(owner);
key.setName("A Test Key");
key.setServiceLevel("TestLevel");
key.setDescription("A test description for the test key.");
if (pools != null) {
Set<ActivationKeyPool> akPools = new HashSet<>();
for (Pool p : pools) {
akPools.add(new ActivationKeyPool(key, p, (long) 1));
}
key.setPools(akPools);
}
return key;
}
Aggregations