@Test
  public void testIdCertGetsRegenerated() throws Exception {
    // using lconsumer simply to avoid hiding consumer. This should
    // get renamed once we refactor this test suite.
    IdentityCertServiceAdapter mockedIdSvc = Mockito.mock(IdentityCertServiceAdapter.class);

    EventSink sink = Mockito.mock(EventSinkImpl.class);

    SubscriptionServiceAdapter ssa = Mockito.mock(SubscriptionServiceAdapter.class);
    ComplianceRules rules = Mockito.mock(ComplianceRules.class);

    Consumer consumer = createConsumer();
    ComplianceStatus status = new ComplianceStatus();
    when(rules.getStatus(any(Consumer.class), any(Date.class), anyBoolean())).thenReturn(status);
    // cert expires today which will trigger regen
    consumer.setIdCert(createIdCert());
    BigInteger origserial = consumer.getIdCert().getSerial().getSerial();

    when(mockedConsumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);
    when(mockedIdSvc.regenerateIdentityCert(consumer)).thenReturn(createIdCert());

    ConsumerResource cr =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            ssa,
            null,
            mockedIdSvc,
            null,
            null,
            sink,
            eventFactory,
            null,
            null,
            null,
            null,
            null,
            mockedOwnerCurator,
            null,
            null,
            rules,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);
    Consumer c = cr.getConsumer(consumer.getUuid());

    assertFalse(origserial.equals(c.getIdCert().getSerial().getSerial()));
  }
  @Test
  public void testIdCertDoesNotRegenerate() throws Exception {
    SubscriptionServiceAdapter ssa = Mockito.mock(SubscriptionServiceAdapter.class);
    ComplianceRules rules = Mockito.mock(ComplianceRules.class);

    Consumer consumer = createConsumer();
    ComplianceStatus status = new ComplianceStatus();
    when(rules.getStatus(any(Consumer.class), any(Date.class), anyBoolean())).thenReturn(status);
    consumer.setIdCert(createIdCert(TestUtil.createDate(2025, 6, 9)));
    BigInteger origserial = consumer.getIdCert().getSerial().getSerial();

    when(mockedConsumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);

    ConsumerResource cr =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            ssa,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            mockedOwnerCurator,
            null,
            null,
            rules,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);

    Consumer c = cr.getConsumer(consumer.getUuid());

    assertEquals(origserial, c.getIdCert().getSerial().getSerial());
  }
  @Test
  public void testGetComplianceStatusList() {
    Consumer c = mock(Consumer.class);
    Consumer c2 = mock(Consumer.class);
    when(c.getUuid()).thenReturn("1");
    when(c2.getUuid()).thenReturn("2");

    List<Consumer> consumers = new ArrayList<Consumer>();
    consumers.add(c);
    consumers.add(c2);

    List<String> uuids = new ArrayList<String>();
    uuids.add("1");
    uuids.add("2");
    when(mockedConsumerCurator.findByUuids(eq(uuids))).thenReturn(consumers);

    ComplianceStatus status = new ComplianceStatus();
    when(mockedComplianceRules.getStatus(any(Consumer.class), any(Date.class))).thenReturn(status);

    ConsumerResource cr =
        new ConsumerResource(
            mockedConsumerCurator,
            null,
            null,
            null,
            null,
            null,
            null,
            i18n,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            mockedComplianceRules,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);

    Map<String, ComplianceStatus> results = cr.getComplianceStatusList(uuids);
    assertEquals(2, results.size());
    assertTrue(results.containsKey("1"));
    assertTrue(results.containsKey("2"));
  }
  @Test
  public void testEntitleByProductsEmptyArray() throws Exception {
    Product product = TestUtil.createProduct();
    List<Pool> pools = Util.newList();
    Pool pool1 = TestUtil.createPool(product);
    pools.add(pool1);
    Date now = new Date();

    ValidationResult result = mock(ValidationResult.class);

    // Setup an installed product for the consumer, we'll make the bind request
    // with no products specified, so this should get used instead:
    String[] installedPids = new String[] {product.getId()};
    ComplianceStatus mockCompliance = new ComplianceStatus(now);
    mockCompliance.addNonCompliantProduct(installedPids[0]);
    when(complianceRules.getStatus(any(Consumer.class), any(Date.class)))
        .thenReturn(mockCompliance);

    when(mockPoolCurator.listAvailableEntitlementPools(
            any(Consumer.class),
            any(Owner.class),
            anyString(),
            eq(now),
            anyBoolean(),
            anyBoolean()))
        .thenReturn(pools);

    when(mockPoolCurator.lockAndLoad(any(Pool.class))).thenReturn(pool1);
    when(enforcerMock.preEntitlement(any(Consumer.class), any(Pool.class), anyInt()))
        .thenReturn(result);

    when(result.isSuccessful()).thenReturn(true);

    List<PoolQuantity> bestPools = new ArrayList<PoolQuantity>();
    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)))
        .thenReturn(bestPools);

    // Make the call but provide a null array of product IDs (simulates healing):
    manager.entitleByProducts(TestUtil.createConsumer(o), null, now);

    verify(autobindRules)
        .selectBestPools(
            any(Consumer.class),
            eq(installedPids),
            any(List.class),
            eq(mockCompliance),
            any(String.class),
            any(Set.class));
  }
  @Before
  public void setupTest() {
    this.i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.FALLBACK);
    this.hypervisorType = new ConsumerType(ConsumerTypeEnum.HYPERVISOR);
    this.consumerResource =
        new ConsumerResource(
            this.consumerCurator,
            this.consumerTypeCurator,
            null,
            this.subscriptionService,
            null,
            this.idCertService,
            null,
            this.i18n,
            this.sink,
            this.eventFactory,
            null,
            null,
            this.userService,
            null,
            null,
            null,
            null,
            this.ownerCurator,
            this.activationKeyCurator,
            null,
            this.complianceRules,
            this.deletedConsumerCurator,
            null,
            new CandlepinCommonTestConfig());
    hypervisorResource =
        new HypervisorResource(
            consumerResource, poolManager, consumerCurator, this.deletedConsumerCurator, i18n);

    // Ensure that we get the consumer that was passed in back from the create call.
    when(consumerCurator.create(any(Consumer.class)))
        .thenAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {
                return invocation.getArguments()[0];
              }
            });
    when(complianceRules.getStatus(any(Consumer.class), any(Date.class)))
        .thenReturn(new ComplianceStatus(new Date()));
  }
  @Before
  public void init() throws Exception {
    product = TestUtil.createProduct();
    o = new Owner("key", "displayname");
    pool = TestUtil.createPool(o, product);

    when(mockConfig.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    this.productCache = new ProductCache(mockConfig, mockProductAdapter);

    this.principal = TestUtil.createOwnerPrincipal();
    this.manager =
        spy(
            new CandlepinPoolManager(
                mockPoolCurator,
                mockSubAdapter,
                productCache,
                entCertAdapterMock,
                mockEventSink,
                eventFactory,
                mockConfig,
                enforcerMock,
                poolRulesMock,
                entitlementCurator,
                consumerCuratorMock,
                certCuratorMock,
                complianceRules,
                envCurator,
                autobindRules));

    when(entCertAdapterMock.generateEntitlementCert(
            any(Entitlement.class), any(Subscription.class), any(Product.class)))
        .thenReturn(new EntitlementCertificate());

    dummyComplianceStatus = new ComplianceStatus(new Date());
    when(complianceRules.getStatus(any(Consumer.class), any(Date.class)))
        .thenReturn(dummyComplianceStatus);
  }