Search in sources :

Example 71 with Consumer

use of org.candlepin.model.Consumer in project candlepin by candlepin.

the class ManifestManagerTest method verifyConsumerIsDistributorBeforeSchedulingManifestGeneration.

@Test
public void verifyConsumerIsDistributorBeforeSchedulingManifestGeneration() throws Exception {
    Owner owner = TestUtil.createOwner();
    Consumer consumer = this.createMockConsumer(owner, false);
    ConsumerType ctype = consumerTypeCurator.getConsumerType(consumer);
    Cdn cdn = new Cdn("test-cdn", "Test CDN", "");
    String webAppPrefix = "webapp-prefix";
    String apiUrl = "api-url";
    Map<String, String> extData = new HashMap<>();
    when(consumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
    when(cdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);
    try {
        manager.generateManifestAsync(consumer.getUuid(), owner.getKey(), cdn.getLabel(), webAppPrefix, apiUrl, extData);
        fail("Expected ForbiddenException not thrown");
    } catch (Exception e) {
        assertTrue(e instanceof ForbiddenException);
        String expectedMsg = String.format("Unit %s cannot be exported. A manifest cannot be made for " + "units of type \"%s\".", consumer.getUuid(), ctype.getLabel());
        assertEquals(e.getMessage(), expectedMsg);
    }
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) ConsumerType(org.candlepin.model.ConsumerType) Cdn(org.candlepin.model.Cdn) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Test(org.junit.Test)

Example 72 with Consumer

use of org.candlepin.model.Consumer in project candlepin by candlepin.

the class PoolManagerTest method testRevokeAllEntitlements.

@Test
public void testRevokeAllEntitlements() {
    Consumer c = TestUtil.createConsumer(owner);
    Entitlement e1 = new Entitlement(pool, c, owner, 1);
    Entitlement e2 = new Entitlement(pool, c, owner, 1);
    List<Entitlement> entitlementList = new ArrayList<>();
    entitlementList.add(e1);
    entitlementList.add(e2);
    when(entitlementCurator.listByConsumer(eq(c))).thenReturn(entitlementList);
    when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(pool);
    PreUnbindHelper preHelper = mock(PreUnbindHelper.class);
    ValidationResult result = new ValidationResult();
    when(preHelper.getResult()).thenReturn(result);
    int total = manager.revokeAllEntitlements(c);
    assertEquals(2, total);
    verify(entitlementCurator, times(1)).markDependentEntitlementsDirty(any(List.class));
// TODO assert batch revokes have been called
}
Also used : PreUnbindHelper(org.candlepin.policy.js.entitlement.PreUnbindHelper) Consumer(org.candlepin.model.Consumer) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) Entitlement(org.candlepin.model.Entitlement) ValidationResult(org.candlepin.policy.ValidationResult) Test(org.junit.Test)

Example 73 with Consumer

use of org.candlepin.model.Consumer in project candlepin by candlepin.

the class PoolManagerTest method testRefreshPoolsSortsStackDerivedPools.

@SuppressWarnings("rawtypes")
@Test
public void testRefreshPoolsSortsStackDerivedPools() {
    List<Subscription> subscriptions = new ArrayList<>();
    List<Pool> pools = new ArrayList<>();
    // Pool has no subscription ID:
    Product product = TestUtil.createProduct();
    Pool p = TestUtil.createPool(product);
    p.setSourceStack(new SourceStack(new Consumer(), "a"));
    pools.add(p);
    mockSubsList(subscriptions);
    mockPoolsList(pools);
    Owner owner = getOwner();
    when(mockOwnerCurator.lookupByKey(owner.getKey())).thenReturn(owner);
    this.mockProductImport(owner, product);
    this.mockContentImport(owner, new Content[] {});
    CandlepinQuery<Pool> cqmock = mock(CandlepinQuery.class);
    when(cqmock.list()).thenReturn(pools);
    when(cqmock.iterator()).thenReturn(pools.iterator());
    when(mockPoolCurator.listByOwnerAndType(eq(owner), any(PoolType.class))).thenReturn(cqmock);
    this.manager.getRefresher(mockSubAdapter, mockOwnerAdapter).add(owner).run();
    ArgumentCaptor<List> poolCaptor = ArgumentCaptor.forClass(List.class);
    verify(this.poolRulesMock).updatePools(poolCaptor.capture(), any(Map.class));
    assertEquals(1, poolCaptor.getValue().size());
    assertEquals(p, poolCaptor.getValue().get(0));
}
Also used : Owner(org.candlepin.model.Owner) PoolType(org.candlepin.model.Pool.PoolType) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Consumer(org.candlepin.model.Consumer) SourceStack(org.candlepin.model.SourceStack) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 74 with Consumer

use of org.candlepin.model.Consumer in project candlepin by candlepin.

the class PoolManagerTest method testDeleteExcessEntitlementsBatch.

@Test
public void testDeleteExcessEntitlementsBatch() throws EntitlementRefusedException {
    ConsumerType ctype = this.mockConsumerType(TestUtil.createConsumerType());
    Consumer consumer = TestUtil.createConsumer(ctype, owner);
    Subscription sub = TestUtil.createSubscription(owner, product);
    sub.setId("testing-subid");
    pool.setSourceSubscription(new SourceSubscription(sub.getId(), "master"));
    final Pool derivedPool = TestUtil.createPool(owner, product, 1);
    derivedPool.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
    derivedPool.setSourceSubscription(new SourceSubscription(sub.getId(), "der"));
    derivedPool.setConsumed(3L);
    derivedPool.setId("derivedPool");
    Entitlement masterEnt = new Entitlement(pool, consumer, owner, 5);
    Entitlement derivedEnt = new Entitlement(derivedPool, consumer, owner, 1);
    derivedEnt.setId("1");
    Entitlement derivedEnt2 = new Entitlement(derivedPool, consumer, owner, 1);
    derivedEnt2.setId("2");
    final Pool derivedPool2 = TestUtil.createPool(owner, product, 1);
    derivedPool2.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
    derivedPool2.setSourceSubscription(new SourceSubscription(sub.getId(), "der"));
    derivedPool2.setConsumed(2L);
    derivedPool2.setId("derivedPool2");
    Entitlement derivedEnt3 = new Entitlement(derivedPool2, consumer, owner, 1);
    derivedEnt3.setId("3");
    Set<Entitlement> ents = new HashSet<>();
    ents.add(derivedEnt);
    ents.add(derivedEnt2);
    derivedPool.setEntitlements(ents);
    Set<Entitlement> ents2 = new HashSet<>();
    ents2.add(derivedEnt3);
    derivedPool2.setEntitlements(ents2);
    Pool derivedPool3 = TestUtil.createPool(owner, product, 1);
    derivedPool3.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
    derivedPool3.setSourceSubscription(new SourceSubscription(sub.getId(), "der"));
    derivedPool3.setConsumed(2L);
    derivedPool3.setId("derivedPool3");
    // before
    assertEquals(3, derivedPool.getConsumed().intValue());
    assertEquals(2, derivedPool2.getConsumed().intValue());
    assertEquals(2, derivedPool3.getConsumed().intValue());
    when(mockPoolCurator.lockAndLoad(pool)).thenReturn(pool);
    when(enforcerMock.update(any(Consumer.class), any(Entitlement.class), any(Integer.class))).thenReturn(new ValidationResult());
    when(mockPoolCurator.lookupOversubscribedBySubscriptionIds(any(String.class), anyMap())).thenReturn(Arrays.asList(derivedPool, derivedPool2, derivedPool3));
    when(mockPoolCurator.retrieveOrderedEntitlementsOf(eq(Arrays.asList(derivedPool)))).thenReturn(Arrays.asList(derivedEnt, derivedEnt2));
    when(mockPoolCurator.retrieveOrderedEntitlementsOf(eq(Arrays.asList(derivedPool2)))).thenReturn(Arrays.asList(derivedEnt3));
    when(mockPoolCurator.retrieveOrderedEntitlementsOf(eq(Arrays.asList(derivedPool3)))).thenReturn(new ArrayList<>());
    Collection<Pool> overPools = new ArrayList<Pool>() {

        {
            add(derivedPool);
            add(derivedPool2);
        }
    };
    when(mockPoolCurator.lockAndLoad(any(Collection.class))).thenReturn(overPools);
    when(mockPoolCurator.lockAndLoad(eq(derivedPool))).thenReturn(derivedPool);
    when(mockPoolCurator.lockAndLoad(eq(derivedPool2))).thenReturn(derivedPool2);
    when(mockPoolCurator.lockAndLoad(eq(derivedPool3))).thenReturn(derivedPool3);
    pool.setId("masterpool");
    manager.adjustEntitlementQuantity(consumer, masterEnt, 3);
    Class<List<Entitlement>> listClass = (Class<List<Entitlement>>) (Class) ArrayList.class;
    ArgumentCaptor<List<Entitlement>> arg = ArgumentCaptor.forClass(listClass);
    verify(entitlementCurator).batchDelete(arg.capture());
    List<Entitlement> entsDeleted = arg.getValue();
    assertThat(entsDeleted, hasItems(derivedEnt, derivedEnt2, derivedEnt3));
    assertEquals(1, derivedPool.getConsumed().intValue());
    assertEquals(1, derivedPool2.getConsumed().intValue());
    assertEquals(2, derivedPool3.getConsumed().intValue());
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) ValidationResult(org.candlepin.policy.ValidationResult) SourceSubscription(org.candlepin.model.SourceSubscription) Consumer(org.candlepin.model.Consumer) Matchers.anyCollection(org.mockito.Matchers.anyCollection) Collection(java.util.Collection) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) ConsumerType(org.candlepin.model.ConsumerType) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 75 with Consumer

use of org.candlepin.model.Consumer in project candlepin by candlepin.

the class PoolManagerTest method expiredEntitlementEvent.

@Test
public void expiredEntitlementEvent() {
    Date now = new Date();
    Product p = TestUtil.createProduct();
    p.setAttribute(Product.Attributes.HOST_LIMITED, "true");
    p.setAttribute(Product.Attributes.VIRT_LIMIT, "unlimited");
    Consumer guest = TestUtil.createConsumer(owner);
    guest.setFact("virt.is_guest", "true");
    guest.addInstalledProduct(new ConsumerInstalledProduct(p));
    Pool pool = TestUtil.createPool(owner, p);
    pool.setAttribute(Pool.Attributes.UNMAPPED_GUESTS_ONLY, "true");
    pool.setAttribute(Pool.Attributes.VIRT_ONLY, "true");
    pool.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
    pool.setAttribute(Pool.Attributes.PHYSICAL_ONLY, "false");
    pool.setAttribute(Product.Attributes.VIRT_LIMIT, "0");
    pool.setStartDate(new Date(now.getTime() - (1000 * 60 * 60 * 24 * 2)));
    Entitlement ent = TestUtil.createEntitlement(owner, guest, pool, null);
    ent.setEndDateOverride(new Date(now.getTime() - (1000 * 60 * 60 * 24 * 1)));
    ent.setId("test-ent-id");
    ent.setQuantity(1);
    Set<Entitlement> entitlements = new HashSet<>();
    entitlements.add(ent);
    pool.setEntitlements(entitlements);
    Event event = new Event();
    event.setConsumerUuid(guest.getUuid());
    event.setOwnerId(owner.getId());
    event.setTarget(Target.ENTITLEMENT);
    event.setType(Type.EXPIRED);
    when(eventFactory.entitlementExpired(eq(ent))).thenReturn(event);
    when(mockPoolCurator.lockAndLoad(eq(pool))).thenReturn(pool);
    manager.revokeEntitlement(ent);
    String message = event.getMessageText();
    assertNotNull(message);
    message = message.split(": ")[1];
    assertEquals(message, i18n.tr("Unmapped guest entitlement expired without establishing a host/guest mapping."));
}
Also used : Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Event(org.candlepin.audit.Event) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Entitlement(org.candlepin.model.Entitlement) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Consumer (org.candlepin.model.Consumer)470 Test (org.junit.Test)345 Entitlement (org.candlepin.model.Entitlement)162 Owner (org.candlepin.model.Owner)126 Pool (org.candlepin.model.Pool)114 Product (org.candlepin.model.Product)112 Date (java.util.Date)102 ConsumerType (org.candlepin.model.ConsumerType)94 LinkedList (java.util.LinkedList)73 ArrayList (java.util.ArrayList)71 HashSet (java.util.HashSet)69 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)69 HashMap (java.util.HashMap)48 ApiOperation (io.swagger.annotations.ApiOperation)43 Produces (javax.ws.rs.Produces)43 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)40 ApiResponses (io.swagger.annotations.ApiResponses)38 GuestId (org.candlepin.model.GuestId)37 Path (javax.ws.rs.Path)36 List (java.util.List)35