Search in sources :

Example 76 with Consumer

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

the class PoolManagerTest method init.

@Before
public void init() throws Exception {
    MockitoAnnotations.initMocks(this);
    i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.FALLBACK);
    owner = TestUtil.createOwner("key", "displayname");
    product = TestUtil.createProduct();
    pool = TestUtil.createPool(owner, product);
    when(mockOwnerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    when(mockConfig.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    when(eventFactory.getEventBuilder(any(Target.class), any(Type.class))).thenReturn(eventBuilder);
    when(eventBuilder.setEventData(any(Eventful.class))).thenReturn(eventBuilder);
    this.principal = TestUtil.createOwnerPrincipal(owner);
    this.manager = spy(new CandlepinPoolManager(mockPoolCurator, mockEventSink, eventFactory, mockConfig, enforcerMock, poolRulesMock, entitlementCurator, consumerCuratorMock, consumerTypeCuratorMock, certCuratorMock, mockECGenerator, complianceRules, autobindRules, activationKeyRules, mockProductCurator, mockProductManager, mockContentManager, mockOwnerContentCurator, mockOwnerCurator, mockOwnerProductCurator, mockOwnerManager, pinsetterKernel, i18n, mockBindChainFactory));
    setupBindChain();
    Map<String, EntitlementCertificate> entCerts = new HashMap<>();
    entCerts.put(pool.getId(), new EntitlementCertificate());
    when(mockECGenerator.generateEntitlementCertificates(any(Consumer.class), any(Map.class), any(Map.class), any(Map.class), eq(false))).thenReturn(entCerts);
    dummyComplianceStatus = new ComplianceStatus(new Date());
    when(complianceRules.getStatus(any(Consumer.class), any(Date.class))).thenReturn(dummyComplianceStatus);
    when(consumerCuratorMock.lockAndLoad(any(Consumer.class))).thenAnswer(new Answer<Consumer>() {

        @Override
        public Consumer answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            return (Consumer) args[0];
        }
    });
}
Also used : EntitlementCertificate(org.candlepin.model.EntitlementCertificate) HashMap(java.util.HashMap) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) Matchers.anyString(org.mockito.Matchers.anyString) Eventful(org.candlepin.model.Eventful) Date(java.util.Date) Target(org.candlepin.audit.Event.Target) Type(org.candlepin.audit.Event.Type) CallerType(org.candlepin.policy.js.entitlement.Enforcer.CallerType) ConsumerType(org.candlepin.model.ConsumerType) PoolType(org.candlepin.model.Pool.PoolType) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) Before(org.junit.Before)

Example 77 with Consumer

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

the class PoolManagerTest method testEntitleWithADate.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testEntitleWithADate() throws Exception {
    Product product = TestUtil.createProduct();
    List<Pool> pools = new ArrayList<>();
    Pool pool1 = TestUtil.createPool(product);
    pools.add(pool1);
    Pool pool2 = TestUtil.createPool(product);
    pools.add(pool2);
    Date now = new Date();
    ValidationResult result = mock(ValidationResult.class);
    Page page = mock(Page.class);
    when(page.getPageData()).thenReturn(pools);
    when(mockPoolCurator.listAvailableEntitlementPools(any(Consumer.class), any(String.class), any(String.class), any(String.class), eq(now), any(PoolFilterBuilder.class), any(PageRequest.class), anyBoolean(), anyBoolean(), anyBoolean(), any(Date.class))).thenReturn(page);
    CandlepinQuery mockQuery = mock(CandlepinQuery.class);
    when(mockPoolCurator.listAllByIds(any(List.class))).thenReturn(mockQuery);
    when(mockQuery.iterator()).thenReturn(Arrays.asList(pool1).listIterator());
    when(enforcerMock.preEntitlement(any(Consumer.class), any(Pool.class), anyInt(), any(CallerType.class))).thenReturn(result);
    when(result.isSuccessful()).thenReturn(true);
    List<PoolQuantity> bestPools = new ArrayList<>();
    bestPools.add(new PoolQuantity(pool1, 1));
    when(autobindRules.selectBestPools(any(Consumer.class), any(String[].class), any(List.class), any(ComplianceStatus.class), any(String.class), any(Set.class), eq(false))).thenReturn(bestPools);
    ConsumerType ctype = this.mockConsumerType(TestUtil.createConsumerType());
    Consumer consumer = TestUtil.createConsumer(ctype, owner);
    AutobindData data = AutobindData.create(consumer, owner).forProducts(new String[] { product.getUuid() }).on(now);
    doNothing().when(mockPoolCurator).flush();
    doNothing().when(mockPoolCurator).clear();
    List<Entitlement> e = manager.entitleByProducts(data);
    assertNotNull(e);
    assertEquals(e.size(), 1);
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Set(java.util.Set) HashSet(java.util.HashSet) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Page(org.candlepin.common.paging.Page) CandlepinQuery(org.candlepin.model.CandlepinQuery) Matchers.anyString(org.mockito.Matchers.anyString) CallerType(org.candlepin.policy.js.entitlement.Enforcer.CallerType) ValidationResult(org.candlepin.policy.ValidationResult) Date(java.util.Date) PageRequest(org.candlepin.common.paging.PageRequest) Consumer(org.candlepin.model.Consumer) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) AutobindData(org.candlepin.resource.dto.AutobindData) ConsumerType(org.candlepin.model.ConsumerType) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 78 with Consumer

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

the class PoolManagerTest method testDeleteExcessEntitlements.

@Test
public void testDeleteExcessEntitlements() 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"));
    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");
    Set<Entitlement> ents = new HashSet<>();
    ents.add(derivedEnt);
    derivedPool.setEntitlements(ents);
    // before
    assertEquals(3, derivedPool.getConsumed().intValue());
    assertEquals(1, derivedPool.getEntitlements().size());
    Collection<Pool> overPools = Collections.singletonList(derivedPool);
    when(mockPoolCurator.lockAndLoad(any(Collection.class))).thenReturn(overPools);
    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(Collections.singletonList(derivedPool));
    when(mockPoolCurator.retrieveOrderedEntitlementsOf(anyListOf(Pool.class))).thenReturn(Collections.singletonList(derivedEnt));
    when(mockPoolCurator.lockAndLoad(eq(derivedPool))).thenReturn(derivedPool);
    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, hasItem(derivedEnt));
    assertEquals(2, derivedPool.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 79 with Consumer

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

the class EventFactoryTest method testComplianceCreatedSetsEventData.

@Test
public void testComplianceCreatedSetsEventData() {
    Consumer consumer = mock(Consumer.class);
    ComplianceStatus status = mock(ComplianceStatus.class);
    when(consumer.getName()).thenReturn("consumer-name");
    when(consumer.getOwnerId()).thenReturn("owner-id");
    when(consumer.getUuid()).thenReturn("48b09f4e-f18c-4765-9c41-9aed6f122739");
    when(status.getStatus()).thenReturn("invalid");
    ComplianceReason reason1 = new ComplianceReason();
    reason1.setKey(ComplianceReason.ReasonKeys.SOCKETS);
    reason1.setMessage("Only supports 2 of 12 sockets.");
    reason1.setAttributes(ImmutableMap.of(ComplianceReason.Attributes.MARKETING_NAME, "Awesome OS"));
    ComplianceReason reason2 = new ComplianceReason();
    reason2.setKey(ComplianceReason.ReasonKeys.ARCHITECTURE);
    reason2.setMessage("Supports architecture ppc64 but the system is x86_64.");
    reason2.setAttributes(ImmutableMap.of(ComplianceReason.Attributes.MARKETING_NAME, "Awesome Middleware"));
    when(status.getReasons()).thenReturn(ImmutableSet.of(reason1, reason2));
    String expectedEventData = "{\"reasons\":[" + "{\"productName\":\"Awesome OS\"," + "\"message\":\"Only supports 2 of 12 sockets.\"}," + "{\"productName\":\"Awesome Middleware\"," + "\"message\":\"Supports architecture ppc64 but the system is x86_64.\"}]," + "\"status\":\"invalid\"}";
    Event event = eventFactory.complianceCreated(consumer, status);
    assertEquals(expectedEventData, event.getEventData());
}
Also used : Consumer(org.candlepin.model.Consumer) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ComplianceReason(org.candlepin.policy.js.compliance.ComplianceReason) Test(org.junit.Test)

Example 80 with Consumer

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

the class EventSinkImplTest method consumerCreatedShouldEmitSuccessfully.

@Test
public void consumerCreatedShouldEmitSuccessfully() throws Exception {
    Consumer consumer = TestUtil.createConsumer();
    eventSinkImpl.emitConsumerCreated(consumer);
    eventSinkImpl.sendEvents();
    verify(mockClientProducer).send(any(ClientMessage.class));
}
Also used : Consumer(org.candlepin.model.Consumer) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) 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